public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: Thomas de Grenier de Latour <degrenier@easyconnect.fr>
To: gentoo-dev@lists.gentoo.org
Subject: Re: [gentoo-dev] New global USE flag: logrotate
Date: Thu, 28 Apr 2005 13:01:37 +0200	[thread overview]
Message-ID: <20050428130137.50f71bae@eusebe> (raw)
In-Reply-To: <20050428104444.GB13609@curie-int.orbis-terrarum.net>

[-- Attachment #1: Type: text/plain, Size: 455 bytes --]

On Thu, 28 Apr 2005 03:44:44 -0700
"Robin H. Johnson" <robbat2@gentoo.org> wrote:

> I meant an eclass to take care of all of it together.

I had started writing one too some time ago, but i've actually
never applied it to any ebuild and then forgot about it. Anyway,
here it is, with some freshly added comments which explain what it
was supposed to do, but still not tested. It may be a bit
over-complicated for little added-value actually...

-- 
TGL.

[-- Attachment #2: logrotate.eclass --]
[-- Type: application/octet-stream, Size: 6297 bytes --]

# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

ECLASS=logrotate
INHERITED="${INHERITED} ${ECLASS}"
IUSE="${IUSE} logrotate"
RDEPEND="${RDEPEND} logrotate? (app-admin/logrotate)"
#XXX DEPEND=sed?

# This eclass can:
# - create some /etc/logrotate.d files
# - install some /etc/logrotate.d files 
#   (freshly created or from your ${FILESDIR})
# - set a few user-defined preferences in this files

# Some typical usage patterns:
#
# 1) make it create a standard logrotate file:
# --------------------------------------------
# --- ebuild:
# inherit logrotate
# LOGROTATE_logs_path="/var/log/clam-update.log"
# LOGROTATE_missingok="1"
# ...
# src_install() {
#     install_logrotate_file
# ...
# --------------------------------------------
#
#
# 2) use an already created logrotate file:
# --------------------------------------------
# --- ${FILESDIR}/exim.logrotate:
# /var/log/exim/exim*.log {
#     <frequence>
#     rotate <rotations>
#     <compress>
#     missingok
#     notifempty
#     create 640 mail mail
#     sharedscripts
#     postrotate
#         /etc/init.d/exim restart > /dev/null
#     endscript
# }
# --------------------------------------------
# --- ebuild:
# inherit logrotate
# ...
# src_install() {
#     install_logrotate_file
# ...
# --------------------------------------------


# From the user point of view, some options can be controlled through the
# following environment variables:
# - LOGROTATE_FREQUENCE: daily, weekly, monthly, etc.
# - LOGROTATE_ROTATIONS: integer (number of rotations to keep)
# - LOGROTATE_COMPRESS: if non-null, will add "compress" to the rotations rules

# From the ebuild point of view, there are more config vars available:
# - LOGROTATE_frequence: defaults to $LOGROTATE_FREQUENCE or weekly if not set
# - LOGROTATE_rotations: defaults to $LOGROTATE_ROTATIONS or 6
# - LOGROTATE_compress: defaults to $LOGROTATE_COMPRESS or "true"
# - LOGROTATE_missingok: no default. Will add "missingok" if non-null
# - LOGROTATE_create_rights: no default. Will add "create ..." if non-null. It
#   should be something like "640 user group" if set.
# - LOGROTATE_logs_path: no default. It's the log files path. This one is
#   mandatory if you want the eclass to create the logrotate.d file.
# - LOGROTATE_FILE: defaults to "${FILESDIR}/${PN}.logrotate". If the file it
#   points to does not exist, then a standard file will be created.

# Other global variables:
# - MY_LOGROTATE_FILE is location of the writable copy of the logrotate file
# - LOGROTATE_CONFIG_VARS is a list of vars that may be used in logrotate file
LOGROTATE_CONFIG_VARS="\
	frequence
	rotations
	compress
	missingok
	create_rights
	logs_path"

# This function set default settings values in a logrotate.d file. It will
# replace "<config_var>" with "$LOGROTATE_config_var". Sure, do not apply this
# to your $LOGROTATE_FILE directly, but to a local writable copy (or do not use
# it at all btw, it should be automatic).
sed_logrotate_file() {
	local sed_cmd="sed -e '/^[ ^t]*$/d'"
	local var_name var_value
	# Build some sed expressions like this:
	#   -e 's:<frequence>:weekly:' -e ...
	for var_name in ${LOGROTATE_CONFIG_VARS} ; do
		eval var_value="\${LOGROTATE_${var_name}}"
		#XXX Any chance to get weird chars breaking things here?
		sed_cmd="${sed_cmd} -e 's:\\b<${var_name}>\\b:${var_value}:g'"
	done
	# Apply the sed command
	${sed_cmd} "${@}"
}

# This function creates a standard logrotate.d file
create_logrotate_file() {
	[ -z "${MY_LOGROTATE_FILE}" ] \
		&& eerror "MY_LOGROTATE_FILE: variable is not set"
		&& return 1
	[ -e "${MY_LOGROTATE_FILE}" ] \
		&& [ ! -f "${MY_LOGROTATE_FILE}" ] \
		&& eerror "${MY_LOGROTATE_FILE}: not a regular file"
		&& return 1
	touch "${MY_LOGROTATE_FILE}" \
		|| { eerror "${MY_LOGROTATE_FILE}: not a writable file"
		     return 1 ; }
	cat > ${MY_LOGROTATE_FILE} <<EOF
<logs_path> {
	<frequence>
	rotate <rotations>
	<compress>
	<missingok>
	create <create_rights>
}
EOF
	[ -z "${LOGROTATE_create_rights}" ] \
		&& sed -i '/create/d' "${MY_LOGROTATE_FILE}"
	return 0
}

# This is the only function that should be called from the ebuild. It will:
# - set config vars to their defaults or user-provided values
# - copy or create the logrotate.d file
# - apply sed tricks to set config vars values in it
# - install the file in /etc/logrotate.d
install_logrotate_file() {
	local INSDESTTREE_SAVE MY_LOGROTATE_FILENAME
	LOGROTATE_frequence=${LOGROTATE_FREQUENCE:-${LOGROTATE_frequence}}
	[ -z "${LOGROTATE_frequence}" ] \
		&& LOGROTATE_frequence=weekly
	LOGROTATE_rotations=${LOGROTATE_ROTATIONS:-${LOGROTATE_rotations}}
	[ -z "${LOGROTATE_rotations}" ] \
		&& LOGROTATE_rotations=6
	LOGROTATE_compress=${LOGROTATE_COMPRESS:-${LOGROTATE_compress}}
	[ -n "${LOGROTATE_compress}" ] \
		&& LOGROTATE_compress=compress
	[ -n "${LOGROTATE_missingok}" ] \
		&& LOGROTATE_missingok=missingok
	LOGROTATE_FILE="${LOGROTATE_FILE:-${FILESDIR}/${PN}.logrotate}"
	if [ -f "${LOGROTATE_FILE}" ] ; then
		MY_LOGROTATE_FILE="${WORKDIR}/${LOGROTATE_FILE##*/}"
		cp ${LOGROTATE_FILE} ${MY_LOGROTATE_FILE} \
			|| die "failed to copy the logrotate file"
	else
		MY_LOGROTATE_FILE="${WORKDIR}/${PN}.logrotate"	
		[ -z "${LOGROTATE_logs_path}" ] \
			&& eerror "LOGROTATE_logs_path: variable is not set"
			&& die "failed to create the logrotate file"
		create_logrotate_file || die "failed to create the logrotate file"
	fi
	sed_logrotate_file -i ${MY_LOGROTATE_FILE} \
		|| die "failed to sed the logrotate file"
	MY_LOGROTATE_FILENAME="${MY_LOGROTATE_FILE##*/}"
	MY_LOGROTATE_FILENAME="${MY_LOGROTATE_FILENAME%.*}"
	[ -n "${INSDESTTREE}" ] && INSDESTTREE_SAVE="${INSDESTTREE}"
	insinto /etc/logrotate.d
	newins ${MY_LOGROTATE_FILE} ${MY_LOGROTATE_FILENAME}
	[ -n "${INSDESTTREE_SAVE}" ] && insinto "${INSDESTTREE_SAVE}"
}


# FIXME/TODO/etc.:
# - "create"... should be "nocreate" if "missingok", or something like that?
# - "size=XXX" is a nice alternative for "frequence", maybe it should be
#   explicitly supported (LOGROTATE_FREQUENCE="size=5M" should work but sounds
#   hackish)
# - errr... is user config actualy useful? may be better to not set the vars
#   and leave it to the "inherit from /etc/logrotate.conf", no?
# - apply it to more ebuilds to validate it :)

  reply	other threads:[~2005-04-28 11:02 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-28  8:30 [gentoo-dev] New global USE flag: logrotate Henrik Brix Andersen
2005-04-28  8:51 ` Robin H. Johnson
2005-04-28 10:34   ` Henrik Brix Andersen
2005-04-28 10:44     ` Robin H. Johnson
2005-04-28 11:01       ` Thomas de Grenier de Latour [this message]
2005-04-28 11:01       ` Henrik Brix Andersen
2005-04-28 14:00         ` Mike Frysinger
2005-04-28 14:08           ` Thomas de Grenier de Latour
2005-04-28 12:05     ` Ciaran McCreesh
2005-04-28 12:56       ` Georgi Georgiev
2005-04-28 13:41 ` Cory Visi
2005-04-28 13:44   ` Lance Albertson
2005-04-28 13:47   ` Ciaran McCreesh
2005-04-28 15:23   ` Chris Gianelloni
2005-04-28 15:51     ` Thomas de Grenier de Latour
2005-04-28 19:48 ` Donnie Berkholz
2005-04-28 20:16   ` Ciaran McCreesh
2005-04-28 20:21     ` Donnie Berkholz
2005-04-28 22:18       ` Grant Goodyear
2005-04-28 22:28         ` Donnie Berkholz
2005-04-29  0:36           ` Jason Stubbs
2005-04-29  0:43             ` Donnie Berkholz
2005-04-29 12:26             ` Chris Gianelloni
2005-04-29 13:25               ` Dan Meltzer
2005-04-29 13:55                 ` Jason Stubbs
2005-04-29 15:52                   ` Chris Gianelloni
2005-04-29 16:17                     ` Jason Stubbs
2005-04-29 17:26                       ` Chris Gianelloni
2005-04-29 17:25                     ` Jason Stubbs
2005-04-29 17:46                       ` Chris Gianelloni
2005-04-29 18:03                         ` Jason Stubbs
2005-04-29 15:32                 ` Chris Gianelloni
2005-04-29 17:44             ` Robin H. Johnson
2005-04-29 18:06               ` Jason Stubbs
2005-04-29 18:59               ` Chris Gianelloni
2005-04-29 19:17               ` Donnie Berkholz
2005-04-29  9:32         ` Christian Birchinger
2005-04-28 22:32 ` Tom Wesley
2005-04-28 23:10 ` Spider
  -- strict thread matches above, loose matches on Subject: below --
2005-08-02  9:22 Tom Martin
2005-08-02 10:44 ` Anthony Gorecki
2005-08-02 12:17 ` Chris Gianelloni
2005-08-02 17:04   ` Tom Martin
2005-08-02 17:24     ` Chris Gianelloni
2005-08-02 12:34 ` tomk
2005-08-02 16:48 ` Donnie Berkholz
2005-08-02 17:01   ` Alec Warner
2005-08-02 17:33   ` Ciaran McCreesh
2005-08-02 17:48     ` Alec Joseph Warner
2005-08-02 17:59       ` Ciaran McCreesh
2005-08-02 17:52     ` Donnie Berkholz
2005-08-02 18:12       ` Ciaran McCreesh
2005-08-02 17:58   ` Alin Nastac
2005-08-03 23:50     ` Tom Martin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050428130137.50f71bae@eusebe \
    --to=degrenier@easyconnect.fr \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox