Coverage for lib/lib_logrotate.py : 55%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""Logrotate module."""
"""Helper class for logrotate charm."""
"""Init function.""" self.retention = hookenv.config('logrotate-retention') self.override = hookenv.config('override') self.override_files = self.get_override_files() self.override_settings = self.get_override_settings()
"""Read changes from disk.
Config changed/install hooks dumps config out to disk, Here we read that config to update the cronjob """ config_file = open("/etc/logrotate_cronjob_config", "r") lines = config_file.read() lines = lines.split('\n')
self.retention = int(lines[2])
"""Modify the logrotate config files.""" for config_file in os.listdir(LOGROTATE_DIR): file_path = LOGROTATE_DIR + config_file
logrotate_file = open(file_path, 'r') content = logrotate_file.read() logrotate_file.close()
mod_contents = self.modify_content(content, file_path)
mod_contents = self.modify_header(mod_contents)
logrotate_file = open(file_path, 'w') logrotate_file.write(mod_contents) logrotate_file.close()
"""Return paths for files to be overrided.""" return [path['path'] for path in self.override if self.override.keys() == ['path', 'rotate', 'interval']]
"""Return paths for files to be overrided.""" for override_entry in self.override: if file_path == override_entry['path']: rotate = override_entry['rotate'] interval = override_entry['interval'] return {'rotate': rotate, 'interval': interval}
"""Edit the content of a logrotate file.""" # Split the contents in a logrotate file in separate entries (if # multiple are found in the file) and put in a list for further # processing
# Work on each item - checking the rotation configuration and setting # the rotate option to the appropriate value # Override rotate, if defined count = self.override_settings['rotate'] else: # if rotate is missing, add it as last line in the item entry else: result = item.replace('}', ' ' + rotate + '\n}')
# Override interval, if defined interval = self.override_settings['interval'] regex = re.compile("(daily|weekly|monthly|yearly)") results = regex.sub(interval, results)
"""Add Juju headers to the file.""" header = "# Configuration file maintained by Juju. Local changes may be overwritten"
split = content.split('\n') if split[0].startswith(header): result = content else: result = header + '\n' + content
return result
def calculate_count(cls, item, retention): """Calculate rotate based on rotation interval. Always round up.""" # Fallback to default lowest retention - days # better to keep the logs than lose them # Daily 1:1 to configuration retention period (in days) # Weekly rounding up, as weeks are 7 days # Monthly default 30 days and round up because of 28/31 days months # For every 360 days - add 1 year
|