def run(self): if self.kwargs['action'] == 'create': self.create_snapshot() print( get_msg(self.kwargs['type']) + self.kwargs['action'] + ' is in progress...\n') i = 0 while i != 'available': i = self.snapshot_is_available() sleep(60) if self.kwargs['action'] == '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.cache_cluster_is_available() sleep(60) if self.kwargs['action'] == 'delete': self.delete_snapshot() print( get_msg(self.kwargs['type']) + self.kwargs['action'] + ' completed in {} region...\n'.format(self.parameters['region']))
def _processing(self, command, timeout, type): proc = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) out, err = proc.communicate(timeout=timeout) if proc.returncode == 0: self.logger.info( get_msg(type, ' Snapshot created successfully ...')) else: self.logger.error( get_msg(type, ' {} ...'.format(err.decode("utf-8").rstrip())))
def run(self): #TODO: add all mongodump params if self.kwargs['action'] == 'create': command = 'mongodump' if self.kwargs['action'] == 'restore': command = 'mongorestore' if self.config()['a_port'] is None: port = mongodbPort else: port = self.config()['a_port'] if self.config()['a_waitTimeout'] is not None: if self.config()['a_waitTimeout'] == 0: timeout = None else: timeout = self.config()['a_waitTimeout'] else: timeout = waitTimeout if self.kwargs['action'] == 'create': args = "{command} --out {path} --host {host} --port {port}".format( path=self.config()['a_path'], host=self.config()['a_host'], port=port, command=command) if self.kwargs['action'] == 'restore': args = "{command} --host {host} --port {port} {path}".format( path=self.config()['a_path'], host=self.config()['a_host'], port=port, command=command) if self.config()['a_gzip'] is not None and self.config()['a_gzip']: args += ' --gzip' if self.config()['a_collection'] is not None: args += ' --collection {}'.format(self.config()['a_collection']) if self.config()['a_dbname'] is not None: args += ' --db {}'.format(self.config()['a_dbname']) print(args) create_r = self.processing(args, timeout) if create_r[0] == 0: print( get_msg(self.config()['a_type']) + 'Snapshot created successfully ...') else: print( get_msg(self.config()['a_type']) + '{} ...'.format(create_r[2].decode("utf-8").rstrip()))
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)
def create(self, params): self._create_snapshot(params['snapshot_id'], params['database_id']) i = None while i != 'available': self.logger.info( get_msg(self.type, self.action + ' is in progress...\n')) i = self._snapshot_is_available(params['snapshot_id']) sleep(60)
def wait_snapshot(self, snapshotId, region): if self.parameters.get('waitTimeout') is None: counter = waitTimeout else: counter = self.parameters['waitTimeout'] print(get_msg(self.kwargs['type']) + self.kwargs['action'] + ' is in progress...\n') while counter >= 0: status = self.snapshot_status(snapshotId, region) if status == 'available': print(get_msg(self.kwargs['type']) + '{} snapshot is available in region...\n'.format( snapshotId)) break else: sleep(30) counter -= 30
def delete_snapshot(self, snapshots): r = [] for snapshot in snapshots: response = self.client.delete_db_snapshot( DBSnapshotIdentifier=snapshot['DBSnapshotIdentifier']) print( get_msg(self.kwargs['type']) + self.kwargs['action'] + ' is in progress...\n') r.append(response) return r
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']))
def wait_snapshot(self, region, snapshot_identifier): if self.config()['a_wait_timeout'] is None: counter = wait_timeout else: counter = self.config()['a_wait_timeout'] while counter >= 0: status = self.snapshot_status(region, snapshot_identifier) print( get_msg(self.config()['a_type']) + 'Creating of {} is in process in {} region...'.format( snapshot_identifier, region)) if status == 'available': print( get_msg(self.config()['a_type']) + '{} snapshot is available in {} region...\n'.format( snapshot_identifier, region)) break else: sleep(30) counter -= 30
def delete_snapshot(self, region, snapshots): c = get_amazon_client(self.config()['a_type'], region) r = [] for i in snapshots: response = c.delete_db_snapshot( DBSnapshotIdentifier=i['DBSnapshotIdentifier']) print( get_msg(self.config()['a_type']) + 'Deleting snapshot {} in {} region...'.format( i['DBSnapshotIdentifier'], region)) r.append(response) return r
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']))