From: Frank Brehm Date: Wed, 29 Jun 2011 13:58:23 +0000 (+0000) Subject: _create_olddir scheinbar fertig X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=7d0b1c6a0e7648442b00e66424e10d0d708cfa49;p=my-stuff%2Fpy-logrotate.git _create_olddir scheinbar fertig git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@264 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- diff --git a/LogRotateHandler.py b/LogRotateHandler.py index 3a7030f..e5232fe 100755 --- a/LogRotateHandler.py +++ b/LogRotateHandler.py @@ -666,7 +666,7 @@ class LogrotateHandler(object): mode = o['mode'] if mode is None: - mode = int(0755, 8) + mode = int('0755', 8) owner = o['owner'] if not owner: owner = uid @@ -705,11 +705,107 @@ class LogrotateHandler(object): if not os.path.isabs(olddir): olddir = os.path.join(dirname, olddir) + olddir = os.path.normpath(olddir) if self.verbose > 1: msg = _("Olddir name is now '%s'") % (olddir) self.logger.debug(msg) + # Check for Existence and Consistence + if os.path.exists(olddir): + if os.path.isdir(olddir): + if os.access(olddir, (os.W_OK | os.X_OK)): + if self.verbose > 2: + msg = _("Olddir '%s' allready exists, not created.") % (olddir) + self.logger.debug(msg) + return True + else: + msg = _("No write and execute access to olddir '%s'.") % (olddir) + raise LogrotateHandlerError(msg) + return False + else: + msg = _("Olddir '%s' exists, but is not a valid directory.") % (olddir) + raise LogrotateHandlerError(msg) + return False + + dirs = [] + dir_head = olddir + while dir_head != os.sep: + (dir_head, dir_tail) = os.path.split(dir_head) + dirs.insert(0, dir_tail) + if self.verbose > 2: + msg = _("Directory chain to create: '%s'") % (str(dirs)) + self.logger.debug(msg) + + # Create olddir recursive, if necessary + msg = _("Creating olddir '%s' recursive ...") % (olddir) + self.logger.info(msg) + create_dir = None + parent_statinfo = os.stat(os.sep) + parent_mode = parent_statinfo.st_mode + parent_uid = parent_statinfo.st_uid + parent_gid = parent_statinfo.st_gid + while len(dirs): + dir_head = dirs.pop(0) + if create_dir: + create_dir = os.path.join(create_dir, dir_head) + else: + create_dir = os.sep + dir_head + if self.verbose > 3: + msg = _("Try to create directory '%s' ...") % (create_dir) + self.logger.debug(msg) + if os.path.exists(create_dir): + if os.path.isdir(create_dir): + if self.verbose > 3: + msg = _("Directory '%s' allready exists, not created.") % (create_dir) + self.logger.debug(msg) + parent_statinfo = os.stat(create_dir) + parent_mode = parent_statinfo.st_mode + parent_uid = parent_statinfo.st_uid + parent_gid = parent_statinfo.st_gid + continue + else: + msg = _("Directory '%s' exists, but is not a valid directory.") % (create_dir) + self.logger.error(msg) + return False + msg = _("Creating directory '%s' ...") % (create_dir) + self.logger.debug(msg) + create_mode = parent_mode + if o['mode'] is not None: + create_mode = o['mode'] + create_uid = parent_uid + if o['owner'] is not None: + create_uid = o['owner'] + create_gid = parent_gid + if o['group'] is not None: + create_gid = o['group'] + if self.verbose > 1: + msg = _("Create permissions: %(mode)4o, Owner-UID: %(uid)d, Group-GID: %(gid)d") \ + % {'mode': create_mode, 'uid': create_uid, 'gid': create_gid} + self.logger.debug(msg) + if not self.test: + if self.verbose > 2: + msg = "os.mkdir('%s', %4o)" % (create_dir, create_mode) + self.logger.debug(msg) + try: + os.mkdir(create_dir, create_mode) + except OSError, e: + msg = _("Error on creating directory '%(dir)s': %(err)s") \ + % {'dir': create_dir, 'err': e.strerror} + self.logger.error(msg) + return False + if (create_uid != uid) or (create_gid != gid): + if self.verbose > 2: + msg = "os.chown('%s', %d, %d)" % (create_dir, create_uid, create_gid) + self.logger.debug(msg) + try: + os.chown(create_dir, create_uid, create_gid) + except OSError, e: + msg = _("Error on chowning directory '%(dir)s': %(err)s") \ + % {'dir': create_dir, 'err': e.strerror} + self.logger.error(msg) + return False + return True #------------------------------------------------------------ diff --git a/test/apache2 b/test/apache2 index 2ae67d9..23043c0 100644 --- a/test/apache2 +++ b/test/apache2 @@ -15,7 +15,8 @@ statusfile /home/frank/Development/Python/PyLogrotate/logrotate.status size 5K maxage 0.5y mail test@uhu-banane.de - olddir /var/log/apache2/%Y-%m 0755 apache users + #olddir %Y-%m 0755 apache users + olddir %Y-%m postrotate echo "/etc/init.d/apache2 reload > /dev/null 2>&1 || true" endscript @@ -35,7 +36,8 @@ statusfile /home/frank/Development/Python/PyLogrotate/logrotate.status maxage 0.5y mail test@uhu-banane.de #olddir /var/log/apache2/%Y-%m - olddir /var/log/apache2/%Y-%m 0755 apache users + #olddir %Y-%m 0755 apache users + olddir %Y-%m postrotate echo "/etc/init.d/apache2 reload > /dev/null 2>&1 || true" endscript