def create_host_attr(self, host, vm_id, bundle): from dbaas_cloudstack.models import HostAttr host_attr = HostAttr() host_attr.vm_id = vm_id host_attr.host = host host_attr.vm_user = self.vm_credentials.user host_attr.vm_password = self.vm_credentials.password host_attr.bundle = bundle host_attr.save()
def create_host_attr(self, host, vm_id, bundle): from dbaas_cloudstack.models import HostAttr vm_credentials = get_credentials_for(environment=self.environment, credential_type=CredentialType.VM) host_attr = HostAttr() host_attr.vm_id = vm_id host_attr.host = host host_attr.vm_user = vm_credentials.user host_attr.vm_password = vm_credentials.password host_attr.bundle = bundle host_attr.save()
def do(self, workflow_dict): try: cs_credentials = get_credentials_for( environment=workflow_dict['environment'], credential_type=CredentialType.CLOUDSTACK) vm_credentials = get_credentials_for( environment=workflow_dict['environment'], credential_type=CredentialType.VM) cs_provider = CloudStackProvider(credentials=cs_credentials) offering = workflow_dict['offering'] cs_plan_attrs = PlanAttr.objects.get( plan=workflow_dict['target_plan']) workflow_dict['target_hosts'] = [] workflow_dict['target_instances'] = [] workflow_dict['target_plan'].validate_min_environment_bundles( workflow_dict['environment']) bundles = list(cs_plan_attrs.bundle.filter(is_active=True)) for index, source_instance in enumerate( workflow_dict['source_instances']): source_host = workflow_dict['source_hosts'][index] vm_name = source_host.hostname.split('.')[0] source_host_attr = HostAttr.objects.get(host=source_host) source_network_id = cs_provider.get_vm_network_id( vm_id=source_host_attr.vm_id, project_id=cs_credentials.project) if len(bundles) == 1: bundle = bundles[0] else: if index == 0: bundle = LastUsedBundle.get_next_infra_bundle( plan=workflow_dict['target_plan'], bundles=bundles) else: bundle = LastUsedBundle.get_next_bundle( current_bundle=bundle, bundles=bundles) if bundle.networkid == source_network_id: bundle = LastUsedBundle.get_next_bundle( current_bundle=bundle, bundles=bundles) LOG.debug( "Deploying new vm on cs with bundle %s and offering %s" % (bundle, offering)) vm = cs_provider.deploy_virtual_machine( offering=offering.serviceofferingid, bundle=bundle, project_id=cs_credentials.project, vmname=vm_name, affinity_group_id=cs_credentials.get_parameter_by_name( 'affinity_group_id'), ) if not vm: raise Exception( "CloudStack could not create the virtualmachine") host = Host() host.address = vm['virtualmachine'][0]['nic'][0]['ipaddress'] host.hostname = host.address host.save() workflow_dict['target_hosts'].append(host) source_host.future_host = host source_host.save() host_attr = HostAttr() host_attr.vm_id = vm['virtualmachine'][0]['id'] host_attr.vm_user = vm_credentials.user host_attr.vm_password = vm_credentials.password host_attr.host = host host_attr.bundle = bundle host_attr.save() LOG.info("Host attrs custom attributes created!") instance = Instance() instance.address = host.address instance.dns = host.address instance.port = source_instance.port instance.is_active = source_instance.is_active instance.instance_type = source_instance.instance_type instance.hostname = host instance.databaseinfra = workflow_dict['databaseinfra'] instance.save() LOG.info("Instance created!") source_instance.future_instance = instance source_instance.save() workflow_dict['target_instances'].append(instance) databaseinfra = workflow_dict['databaseinfra'] databaseinfra.last_vm_created += 1 databaseinfra.save() workflow_dict['databaseinfra'] = databaseinfra LastUsedBundleDatabaseInfra.set_last_infra_bundle( databaseinfra=databaseinfra, bundle=host_attr.bundle) return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0020) workflow_dict['exceptions']['traceback'].append(traceback) return False
def do(self, workflow_dict): try: if 'environment' not in workflow_dict or 'plan' not in workflow_dict: return False cs_credentials = get_credentials_for( environment=workflow_dict['environment'], credential_type=CredentialType.CLOUDSTACK) vm_credentials = get_credentials_for( environment=workflow_dict['environment'], credential_type=CredentialType.VM) cs_provider = CloudStackProvider(credentials=cs_credentials) cs_plan_attrs = PlanAttr.objects.get(plan=workflow_dict['plan']) workflow_dict['hosts'] = [] workflow_dict['instances'] = [] workflow_dict['databaseinfraattr'] = [] workflow_dict['vms_id'] = [] workflow_dict['plan'].validate_min_environment_bundles( workflow_dict['environment']) bundles = list(cs_plan_attrs.bundle.filter(is_active=True)) for index, vm_name in enumerate(workflow_dict['names']['vms']): offering = cs_plan_attrs.get_stronger_offering() if len(bundles) == 1: bundle = bundles[0] else: if index == 0: bundle = LastUsedBundle.get_next_infra_bundle( plan=workflow_dict['plan'], bundles=bundles) else: bundle = LastUsedBundle.get_next_bundle( current_bundle=bundle, bundles=bundles) try: DatabaseInfraOffering.objects.get( databaseinfra=workflow_dict['databaseinfra']) except ObjectDoesNotExist: LOG.info("Creating databaseInfra Offering...") dbinfra_offering = DatabaseInfraOffering() dbinfra_offering.offering = offering dbinfra_offering.databaseinfra = workflow_dict[ 'databaseinfra'] dbinfra_offering.save() LOG.debug( "Deploying new vm on cs with bundle %s and offering %s" % (bundle, offering)) vm = cs_provider.deploy_virtual_machine( offering=offering.serviceofferingid, bundle=bundle, project_id=cs_credentials.project, vmname=vm_name, affinity_group_id=cs_credentials.get_parameter_by_name( 'affinity_group_id'), ) if not vm: raise Exception( "CloudStack could not create the virtualmachine") LOG.debug("New virtualmachine: %s" % vm) workflow_dict['vms_id'].append(vm['virtualmachine'][0]['id']) host = Host() host.address = vm['virtualmachine'][0]['nic'][0]['ipaddress'] host.hostname = host.address host.cloud_portal_host = True host.save() LOG.info("Host created!") workflow_dict['hosts'].append(host) host_attr = HostAttr() host_attr.vm_id = vm['virtualmachine'][0]['id'] host_attr.vm_user = vm_credentials.user host_attr.vm_password = vm_credentials.password host_attr.host = host host_attr.bundle = bundle host_attr.save() LOG.info("Host attrs custom attributes created!") instance = Instance() instance.address = host.address instance.port = 3306 instance.hostname = host instance.databaseinfra = workflow_dict['databaseinfra'] instance.instance_type = Instance.MYSQL instance.save() LOG.info("Instance created!") workflow_dict['instances'].append(instance) databaseinfra = workflow_dict['databaseinfra'] databaseinfra.last_vm_created += 1 databaseinfra.save() workflow_dict['databaseinfra'] = databaseinfra LastUsedBundleDatabaseInfra.set_last_infra_bundle( databaseinfra=databaseinfra, bundle=host_attr.bundle) if workflow_dict['qt'] == 1: LOG.info("Updating databaseinfra endpoint...") databaseinfra = workflow_dict['databaseinfra'] databaseinfra.endpoint = instance.address + \ ":%i" % (instance.port) databaseinfra.save() workflow_dict['databaseinfra'] = databaseinfra return True return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0011) workflow_dict['exceptions']['traceback'].append(traceback) return False