def do(self, workflow_dict): try: for host in workflow_dict['source_hosts']: LOG.info("Removing database files on host %s" % host) host_csattr = CsHostAttr.objects.get(host=host) output = {} exec_remote_command( server=host.address, username=host_csattr.vm_user, password=host_csattr.vm_password, command="/opt/dbaas/scripts/dbaas_deletedatabasefiles.sh", output=output) LOG.info(output) LOG.info("Removing disks on host %s" % host) NfsaasProvider().destroy_disk( environment=workflow_dict['source_environment'], host=host) return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0020) workflow_dict['exceptions']['traceback'].append(traceback) return False
def clean_unused_data(export_id, export_path, host, databaseinfra): provider = NfsaasProvider() provider.grant_access(environment=databaseinfra.environment, host=host, export_id=export_id) mount_path = "/mnt_{}_{}".format(databaseinfra.name, export_id) command = "mkdir -p {}".format(mount_path) command += "\nmount -t nfs -o bg,intr {} {}".format( export_path, mount_path) command += "\nrm -rf {}/*".format(mount_path) command += "\numount {}".format(mount_path) command += "\nrm -rf {}".format(mount_path) LOG.info(command) cs_host_attr = CsHostAttr.objects.get(host=host) output = {} exec_remote_command(server=host.address, username=cs_host_attr.vm_user, password=cs_host_attr.vm_password, command=command, output=output) LOG.info(output)
def undo(self, workflow_dict): try: environment = workflow_dict['environment'] if 'volume' in workflow_dict: volume = workflow_dict['volume'] LOG.info("Destroying nfsaas volume...") provider = NfsaasProvider() provider.revoke_access(environment=environment, host=volume.host, export_id=volume.nfsaas_export_id) provider.drop_export(environment=environment, export_id=volume.nfsaas_export_id) volume.delete() return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0022) workflow_dict['exceptions']['traceback'].append(traceback) return False
def do(self, workflow_dict): try: workflow_dict['disks'] = [] for instance in workflow_dict['instances']: host = instance.hostname if instance.is_arbiter: LOG.info("Do not creat nfsaas disk for Arbiter...") continue LOG.info("Creating nfsaas disk...") disk = NfsaasProvider().create_disk( environment=workflow_dict['environment'], plan=workflow_dict['plan'], host=host) if not disk: return False workflow_dict['disks'].append(disk) return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0009) workflow_dict['exceptions']['traceback'].append(traceback) return False
def do(self, workflow_dict): try: workflow_dict['disks'] = [] for instance in workflow_dict['target_instances']: if instance.instance_type == instance.MONGODB_ARBITER: LOG.info("Do not creat nfsaas disk for Arbiter...") continue LOG.info("Creating nfsaas disk...") host = instance.hostname disk = NfsaasProvider().create_disk( environment=workflow_dict['target_environment'], plan=workflow_dict['target_plan'], host=host) if not disk: LOG.info("nfsaas disk could not be created...") return False workflow_dict['disks'].append(disk) return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0019) workflow_dict['exceptions']['traceback'].append(traceback) return False
def do(self, workflow_dict): try: environment = workflow_dict['environment'] plan = workflow_dict['plan'] host = workflow_dict['host'] LOG.info("Creating nfsaas volume...") volume = NfsaasProvider().create_disk(environment=environment, plan=plan, host=host) if not volume: return False volume = HostAttr.objects.get(host=host, nfsaas_path=volume['path']) workflow_dict['volume'] = volume return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0022) workflow_dict['exceptions']['traceback'].append(traceback) return False
def purge_unused_exports(): from dbaas_nfsaas.models import HostAttr databaseinfras = DatabaseInfra.objects.filter(plan__provider=Plan.CLOUDSTACK).prefetch_related('instances') for databaseinfra in databaseinfras: instances = databaseinfra.instances.exclude(instance_type__in=[Instance.MONGODB_ARBITER, Instance.REDIS_SENTINEL]) environment = databaseinfra.environment plan = databaseinfra.plan for instance in instances: exports = HostAttr.objects.filter(host=instance.hostname, is_active=False) for export in exports: snapshots = Snapshot.objects.filter(export_path=export.nfsaas_path, purge_at=None) if snapshots: continue LOG.info('Export {} will be removed'.format(export.nfsaas_export_id)) host = export.host export_id = export.nfsaas_export_id nfsaas_client = NfsaasProvider() nfsaas_client.revoke_access(environment=environment, host=host, export_id=export_id) nfsaas_client.drop_export(environment=environment, export_id=export_id) export.delete()
def do(self, workflow_dict): try: workflow_dict['hosts_and_exports'] = [] databaseinfra = workflow_dict['databaseinfra'] snapshot_id = workflow_dict['snapshot_id'] nfsaas_export_id = workflow_dict['export_id_snapshot'] provider = NfsaasProvider() restore_result = provider.restore_snapshot( environment=databaseinfra.environment, export_id=nfsaas_export_id, snapshot_id=snapshot_id) job_result = provider.check_restore_nfsaas_job( environment=databaseinfra.environment, job_id=restore_result['job']) if 'id' in job_result['result']: new_export_id = job_result['result']['id'] new_export_path = job_result['result']['path'] else: raise Exception('Error while restoring nfs snapshot') host = workflow_dict['host'] workflow_dict['hosts_and_exports'].append({ 'host': host, 'old_export_id': workflow_dict['export_id'], 'old_export_path': workflow_dict['export_path'], 'new_export_id': new_export_id, 'new_export_path': new_export_path, }) old_host_attr = HostAttr.objects.get( nfsaas_export_id=nfsaas_export_id) new_host_attr = HostAttr() new_host_attr.host = old_host_attr.host new_host_attr.nfsaas_export_id = new_export_id new_host_attr.nfsaas_path = new_export_path new_host_attr.is_active = False new_host_attr.nfsaas_team_id = old_host_attr.nfsaas_team_id new_host_attr.nfsaas_project_id = old_host_attr.nfsaas_project_id new_host_attr.nfsaas_environment_id = old_host_attr.nfsaas_environment_id new_host_attr.nfsaas_size_id = old_host_attr.nfsaas_size_id new_host_attr.save() return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0021) workflow_dict['exceptions']['traceback'].append(traceback) return False
def undo(self, workflow_dict): LOG.info("Running undo...") try: for host in workflow_dict['target_hosts']: LOG.info("Destroying nfsaas disk...") NfsaasProvider().destroy_disk( environment=workflow_dict['target_environment'], host=host) return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0020) workflow_dict['exceptions']['traceback'].append(traceback) return False
def undo(self, workflow_dict): try: for host in workflow_dict['hosts']: LOG.info("Destroying nfsaas disk...") disk = NfsaasProvider().destroy_disk( environment=workflow_dict['environment'], host=host) if not disk: return False return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0009) workflow_dict['exceptions']['traceback'].append(traceback) return False
def purge_unused_exports(): from dbaas_nfsaas.models import HostAttr databaseinfras = DatabaseInfra.objects.filter( plan__provider=Plan.CLOUDSTACK).prefetch_related('instances') for databaseinfra in databaseinfras: instances = databaseinfra.get_driver().get_database_instances() environment = databaseinfra.environment for instance in instances: exports = HostAttr.objects.filter(host=instance.hostname, is_active=False) for export in exports: snapshots = Snapshot.objects.filter( export_path=export.nfsaas_path, purge_at=None) if snapshots: continue LOG.info('Export {} will be removed'.format( export.nfsaas_export_id)) host = export.host export_id = export.nfsaas_export_id clean_unused_data(export_id=export_id, export_path=export.nfsaas_path, host=instance.hostname, databaseinfra=databaseinfra) nfsaas_client = NfsaasProvider() nfsaas_client.revoke_access(environment=environment, host=host, export_id=export_id) nfsaas_client.drop_export(environment=environment, export_id=export_id) export.delete()
def destroy_unused_export(export_id, export_path, host, databaseinfra): clean_unused_data(export_id, export_path, host, databaseinfra) provider = NfsaasProvider() provider.drop_export(environment=databaseinfra.environment, export_id=export_id)