Coverage for lib/lib_logrotate.py : 51%

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.logfiles_custom = hookenv.config('logfiles-custom')
"""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)
mod_contents = self.modify_header(mod_contents)
logrotate_file = open(file_path, 'w') logrotate_file.write(mod_contents) logrotate_file.close()
# Remove the juju-common file and re-add it with fresh configurations self.cleanup_custom_config() # Add the custom configs here self.write_custom_config()
def split_items(cls, content): """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 """
"""Edit the content of a logrotate file."""
# Work on each item - checking the rotation configuration and setting # the rotate option to the appropriate value # if rotate is missing, add it as last line in the item entry else: result = item.replace('}', rotate + '\n}')
"""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
def generate_custom_configurations(cls, custom_data): """Apply custom logrotate configurations.""" try: objects = json.loads(custom_data) except ValueError: hookenv.log('Custom JSON object could not be decoded.', level='ERROR') hookenv.status_set('error', 'Invalid syntax for custom JSON \ logfiles object.') return "" items = "" for obj in objects: items += obj['path'] + " {\n" for option in obj['options']: items += " " + option + "\n" items += "}\n"
return items
"""Cleanup juju-common logrotate file.""" if os.path.exists(JUJU_LOGROTATE_FILE): os.remove(JUJU_LOGROTATE_FILE)
"""Write the juju-common logrotate file.""" custom = self.generate_custom_configurations(self.logfiles_custom) custom = self.modify_header(custom) logrotate_file = open(JUJU_LOGROTATE_FILE, 'w') logrotate_file.write(custom) logrotate_file.close() |