def __init__(self, client, repository=None): utils.verify_client_object(client) if not repository: raise exceptions.MissingArgument('No value for "repository" provided') if not utils.repository_exists(client, repository): raise exceptions.FailedExecution( 'Unable to verify existence of repository ' '{0}'.format(repository) ) self.loggit = logging.getLogger('curator.snapshotlist') #: An Elasticsearch Client object. #: Also accessible as an instance variable. self.client = client #: An Elasticsearch repository. #: Also accessible as an instance variable. self.repository = repository #: Instance variable. #: Information extracted from snapshots, such as age, etc. #: Populated by internal method `__get_snapshots` at instance creation #: time. **Type:** ``dict()`` self.snapshot_info = {} #: Instance variable. #: The running list of snapshots which will be used by an Action class. #: Populated by internal methods `__get_snapshots` at instance creation #: time. **Type:** ``list()`` self.snapshots = [] #: Instance variable. #: Raw data dump of all snapshots in the repository at instance creation #: time. **Type:** ``list()`` of ``dict()`` data. self.__get_snapshots()
def _calculate_ages(self, source='creation_date', timestring=None): """ This method initiates snapshot age calculation based on the given parameters. Exceptions are raised when they are improperly configured. Set instance variable `age_keyfield` for use later, if needed. :arg source: Source of snapshot age. Can be 'name' or 'creation_date'. :arg timestring: An strftime string to match the datestamp in an snapshot name. Only used if ``source`` is ``name``. """ if source == 'name': self.age_keyfield = 'age_by_name' if not timestring: raise exceptions.MissingArgument( 'source "name" requires the "timestring" keyword argument' ) self._get_name_based_ages(timestring) elif source == 'creation_date': self.age_keyfield = 'start_time_in_millis' else: raise ValueError( 'Invalid source: {0}. ' 'Must be "name", or "creation_date".'.format(source) )
def filter_by_count( self, count=None, reverse=True, use_age=False, source='creation_date', timestring=None, exclude=True ): """ Remove snapshots from the actionable list beyond the number `count`, sorted reverse-alphabetically by default. If you set `reverse` to `False`, it will be sorted alphabetically. The default is usually what you will want. If only one kind of snapshot is provided--for example, snapshots matching ``curator-%Y%m%d%H%M%S``-- then reverse alphabetical sorting will mean the oldest will remain in the list, because lower numbers in the dates mean older snapshots. By setting `reverse` to `False`, then ``snapshot3`` will be acted on before ``snapshot2``, which will be acted on before ``snapshot1`` `use_age` allows ordering snapshots by age. Age is determined by the snapshot creation date (as identified by ``start_time_in_millis``) by default, but you can also specify a `source` of ``name``. The ``name`` `source` requires the timestring argument. :arg count: Filter snapshots beyond `count`. :arg reverse: The filtering direction. (default: `True`). :arg use_age: Sort snapshots by age. ``source`` is required in this case. :arg source: Source of snapshot age. Can be one of ``name``, or ``creation_date``. Default: ``creation_date`` :arg timestring: An strftime string to match the datestamp in a snapshot name. Only used if `source` ``name`` is selected. :arg exclude: If `exclude` is `True`, this filter will remove matching snapshots from `snapshots`. If `exclude` is `False`, then only matching snapshots will be kept in `snapshots`. Default is `True` """ self.loggit.debug('Filtering snapshots by count') if not count: raise exceptions.MissingArgument('No value for "count" provided') # Create a copy-by-value working list working_list = self.working_list() if use_age: self._calculate_ages(source=source, timestring=timestring) # Using default value of reverse=True in self._sort_by_age() sorted_snapshots = self._sort_by_age(working_list, reverse=reverse) else: # Default to sorting by snapshot name sorted_snapshots = sorted(working_list, reverse=reverse) idx = 1 for snap in sorted_snapshots: msg = ( '{0} is {1} of specified count of {2}.'.format( snap, idx, count ) ) condition = True if idx <= count else False self.__excludify(condition, exclude, snap, msg) idx += 1
def filter_by_age(self, source='creation_date', direction=None, timestring=None, unit=None, unit_count=None, epoch=None, exclude=False): """ Remove snapshots from `snapshots` by relative age calculations. :arg source: Source of snapshot age. Can be 'name', or 'creation_date'. :arg direction: Time to filter, either ``older`` or ``younger`` :arg timestring: An strftime string to match the datestamp in an snapshot name. Only used for snapshot filtering by ``name``. :arg unit: One of ``seconds``, ``minutes``, ``hours``, ``days``, ``weeks``, ``months``, or ``years``. :arg unit_count: The number of ``unit`` (s). ``unit_count`` * ``unit`` will be calculated out to the relative number of seconds. :arg epoch: An epoch timestamp used in conjunction with ``unit`` and ``unit_count`` to establish a point of reference for calculations. If not provided, the current time will be used. :arg exclude: If `exclude` is `True`, this filter will remove matching snapshots from `snapshots`. If `exclude` is `False`, then only matching snapshots will be kept in `snapshots`. Default is `False` """ self.loggit.debug('Starting filter_by_age') # Get timestamp point of reference, por por = utils.get_point_of_reference(unit, unit_count, epoch) self.loggit.debug('Point of Reference: {0}'.format(por)) if not direction: raise exceptions.MissingArgument( 'Must provide a value for "direction"') if direction not in ['older', 'younger']: raise ValueError( 'Invalid value for "direction": {0}'.format(direction)) self._calculate_ages(source=source, timestring=timestring) for snapshot in self.working_list(): if not self.snapshot_info[snapshot][self.age_keyfield]: self.loggit.debug('Removing snapshot {0} for having no age') self.snapshots.remove(snapshot) continue msg = ('Snapshot "{0}" age ({1}), direction: "{2}", point of ' 'reference, ({3})'.format( snapshot, utils.fix_epoch( self.snapshot_info[snapshot][self.age_keyfield]), direction, por)) # Because time adds to epoch, smaller numbers are actually older # timestamps. snapshot_age = utils.fix_epoch( self.snapshot_info[snapshot][self.age_keyfield]) if direction == 'older': agetest = snapshot_age < por else: # 'younger' agetest = snapshot_age > por self.__excludify(agetest, exclude, snapshot, msg)