Ejemplo n.º 1
0
    def regex_filter(self, filter, snapshots):

        filtered = []

        for i in snapshots:
            m = re.match(filter['pattern'], i['snapshotName'])
            if m:
                filtered.append(i)

        validate_empty_snapshots(filtered, 'Any matches by regex filter...')

        return filtered
Ejemplo n.º 2
0
    def run(self):

        if self.kwargs['parameters'].get('fail_on_error') is None:
            fail_on_error = f_on_e
        else:
            fail_on_error = self.kwargs['parameters'].get('fail_on_error')

        if self.kwargs['action'] == 'create':
            resource = self.create_snapshot()
            self.wait_snapshot(self.parameters['DBSnapshotIdentifier'],
                               self.parameters['region'])
            if self.parameters.get('copy_to_region') is not None:
                jobs = []
                for region in self.parameters.get('copy_to_region'):
                    self.copy_snapshot(resource, region)
                    p = Process(target=self.wait_snapshot,
                                args=(self.parameters['DBSnapshotIdentifier'],
                                      region))
                    jobs.append(p)
                    p.start()

        if self.kwargs['action'] == 'delete':

            snapshots = self.get_snapshots()
            validate_empty_snapshots(
                snapshots['DBSnapshots'],
                get_msg(self.kwargs['type']) +
                ' There are no snapshots in {} region...\n'.format(
                    self.parameters['region']), fail_on_error)

            if self.parameters['snapshot_type'] != 'all':
                snapshots_by_type = self.filter_snapshots_by_type(
                    snapshots, self.parameters['snapshot_type'])
            else:
                snapshots_by_type = [
                    snapshot for snapshot in snapshots['DBSnapshots']
                ]

            validate_empty_snapshots(
                snapshots_by_type,
                get_msg(self.kwargs['type']) +
                ' There are no {} snapshots in {} region...\n'.format(
                    self.parameters['snapshot_type'],
                    self.parameters['region']), fail_on_error)

            adapted = self.adapted_snapshots(snapshots_by_type)

            snapshots_filtered = f_main(self.parameters.get('filters'),
                                        adapted)

            self.delete_snapshot(snapshots_filtered)
Ejemplo n.º 3
0
    def age_filter(self, filter, snapshots):

        def check_age(s_date, unit, count):

            seconds = time_mapper[unit] * count
            today = datetime.utcnow().replace(tzinfo=pytz.utc)
            delta = today - s_date
            delta_time = delta.total_seconds()

            return delta_time > seconds

        filtered = [i for i in snapshots if check_age(i['creationTime'],
                                                      filter['unit'],
                                                      filter['count'])]

        validate_empty_snapshots(filtered, 'Any matches by age filter...')

        return filtered
Ejemplo n.º 4
0
    def run(self):

        if self.kwargs['action'] == 'create':
            resource = self.create_snapshot()
            self.wait_snapshot(self.parameters['snapshotId'], self.parameters['region'])
            if self.parameters.get('copyToRegion') is not None:
                jobs = []
                for region in self.parameters.get('copyToRegion'):
                    self.copy_snapshot(resource, region)
                    p = Process(target=self.wait_snapshot,
                                args=(self.parameters['snapshotId'], region))
                    jobs.append(p)
                    p.start()

        if self.kwargs['action'] == 'delete':
            snapshots = self.get_snapshots()
            validate_empty_snapshots(snapshots['DBSnapshots'])
            if self.parameters['snapshotType'] != 'all':
                snapshots_by_type=self.filter_snapshots_by_type(
                    snapshots, self.parameters['snapshotType'])
            snapshots_by_type=[snapshot for snapshot in snapshots['DBSnapshots']]
            validate_empty_snapshots(snapshots_by_type)
            print(get_msg(self.kwargs['type']) + 
                ' There are no {} snapshots in region...\n'.format(self.parameters['snapshotType']))
            adapted = self.adapted_snapshots(snapshots_by_type)
            snapshots_filtered = f_main(
                self.parameters.get('filters'), adapted)
            self.delete_snapshot(snapshots_filtered)

        if self.kwargs['action'] == 'restore':
            restore = self.restore_from_snapshot()
            print(get_msg(self.kwargs['type']) +
                  self.kwargs['action'] + ' is in progress...\n')
            i = 0
            while i != 'available':
                i = self.instance_is_available()
                sleep(60)

        print(get_msg(self.kwargs['type']) + self.kwargs['action'] +
              ' completed in {} region...\n'.format(self.parameters['region']))
Ejemplo n.º 5
0
    def run(self):

        if self.kwargs['action'] == 'create':

            create_r = self.create_snapshot()
            self.wait_snapshot(self.config()['a_region'],
                               self.config()['a_snap_id'])

            if self.config()['a_copy_to_region'] is not None:
                jobs = []
                for region in self.config()['a_copy_to_region']:
                    self.copy_snapshot(create_r, region)
                    p = Process(target=self.wait_snapshot,
                                args=(region, self.config()['a_snap_id']))
                    jobs.append(p)
                    p.start()

        if self.kwargs['action'] == 'delete':
            snapshots = self.get_snapshots(self.config()['a_region'])

            validate_empty_snapshots(
                snapshots['DBSnapshots'],
                get_msg(self.config()['a_type']) +
                'There are no snapshots in {} region...\n'.format(
                    self.config()['a_region']))

            if self.config()['a_snapshot_type'] != 'all':
                snaps_by_type = self.filter_snaps_by_type(
                    snapshots,
                    self.config()['a_snapshot_type'])
            else:
                snaps_by_type = [i for i in snapshots['DBSnapshots']]

            validate_empty_snapshots(
                snaps_by_type,
                get_msg(self.config()['a_type']) +
                'There are no {} snapshots in {} region...\n'.format(
                    self.config()['a_snapshot_type'],
                    self.config()['a_region']))

            adapted = self.adapty_snapshots(snaps_by_type)
            snaps_filtered = f_main(self.config()['a_filters'], adapted)

            self.delete_snapshot(self.config()['a_region'], snaps_filtered)

        if self.kwargs['action'] == 'restore':
            restore_r = self.restore_from_snapshot(self.config()['a_region'])
            print(
                get_msg(self.config()['a_type']) +
                'Instance creation is in progress in {} region...\n'.format(
                    self.config()['a_region']))

            i = 0
            while i != 'available':
                i = self.instance_is_available(self.config()['a_region'])
                sleep(60)

            print(
                get_msg(self.config()['a_type']) +
                'Instance was restored in {} region...\n'.format(self.config()
                                                                 ['a_region']))