def get_instances_for(infra, topology_path): instances = [] group_instances = get_deploy_instances(topology_path) for count, group in enumerate(group_instances): for instance_type in group: instance_name = get_vm_name(infra.name_prefix, infra.name_stamp, count + 1) try: instance = infra.instances.get( Q(hostname__hostname__startswith=instance_name) | Q(dns__startswith=instance_name), port=instance_type.port, ) except Instance.DoesNotExist: instance = Instance() instance.dns = instance_name instance.databaseinfra = infra instance.port = instance_type.port instance.instance_type = instance_type.instance_type instance.vm_name = instance.dns instances.append(instance) return instances
def get_instances_for(infra, topology_path): instances = [] number_of_vms = get_deploy_instances_size(topology_path) for i in range(number_of_vms): instance_name = get_vm_name(infra.name_prefix, infra.name_stamp, i + 1) try: instance = infra.instances.get( Q(hostname__hostname__startswith=instance_name) | Q(dns__startswith=instance_name) ) except Instance.DoesNotExist: instance = Instance() instance.dns = instance_name instance.databaseinfra = infra driver = infra.get_driver() instance.port = driver.get_default_database_port() instance.instance_type = driver.get_default_instance_type() instance.vm_name = instance.dns instances.append(instance) return instances
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']) bundles = list(cs_plan_attrs.bundle.all()) workflow_dict['target_hosts'] = [] workflow_dict['target_instances'] = [] 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( bundle=bundle, bundles=bundles) if bundle.networkid == source_network_id: bundle = LastUsedBundle.get_next_bundle( 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.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.is_arbiter = source_instance.is_arbiter 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) return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0020) workflow_dict['exceptions']['traceback'].append(traceback) return False
def create_database( name, plan, environment, team, project, description, task, subscribe_to_email_events=True, is_protected=False, user=None, retry_from=None ): topology_path = plan.replication_topology.class_path number_of_vms = get_deploy_instances_size(topology_path) name = slugify(name) base_name = gen_infra_names(name, number_of_vms) infra = get_or_create_infra(base_name, plan, environment, retry_from) instances = [] for i in range(number_of_vms): try: instance_name = '{}-0{}-{}'.format( base_name['name_prefix'], i+1, base_name['name_stamp'] ) instance = infra.instances.get( Q(hostname__hostname__startswith=instance_name) | Q(dns__startswith=instance_name) ) except Instance.DoesNotExist: instance = Instance() instance.dns = base_name['vms'][i] instance.databaseinfra = infra driver = infra.get_driver() instance.port = driver.get_default_database_port() instance.instance_type = driver.get_default_instance_type() instance.vm_name = instance.dns instances.append(instance) database_create = DatabaseCreate() database_create.task = task database_create.name = name database_create.plan = plan database_create.environment = environment database_create.team = team database_create.project = project database_create.description = description database_create.subscribe_to_email_events = subscribe_to_email_events database_create.is_protected = is_protected database_create.user = '******' database_create.infra = infra database_create.database = infra.databases.first() database_create.save() steps = get_deploy_settings(topology_path) since_step = None if retry_from: since_step = retry_from.current_step if steps_for_instances( steps, instances, task, database_create.update_step, since_step=since_step ): database_create.set_success() task.set_status_success('Database created') else: database_create.set_error() task.set_status_error( 'Could not create database\n' 'Please check error message and do retry' )
def do(self, workflow_dict): try: cs_credentials = get_credentials_for( environment=workflow_dict['target_environment'], credential_type=CredentialType.CLOUDSTACK) vm_credentials = get_credentials_for( environment=workflow_dict['target_environment'], credential_type=CredentialType.VM) cs_provider = CloudStackProvider(credentials=cs_credentials) target_offering = workflow_dict['target_offering'] cs_plan_attrs = PlanAttr.objects.get( plan=workflow_dict['target_plan']) bundles = list(cs_plan_attrs.bundle.all()) workflow_dict['target_hosts'] = [] workflow_dict['target_instances'] = [] for index, source_host in enumerate(workflow_dict['source_hosts']): sentinel_source_instance = Instance.objects.filter( hostname=source_host, instance_type=Instance.REDIS_SENTINEL)[0] if index < 2: redis_source_instance = Instance.objects.filter( hostname=source_host, instance_type=Instance.REDIS)[0] vm_name = source_host.hostname.split('.')[0] if len(bundles) == 1: bundle = bundles[0] else: bundle = LastUsedBundle.get_next_bundle( plan=workflow_dict['target_plan'], bundle=bundles) if index == 2: offering = cs_plan_attrs.get_weaker_offering() else: offering = target_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.save() LOG.info("Host attrs custom attributes created!") if index < 2: redis_instance = Instance() redis_instance.address = host.address redis_instance.dns = host.address redis_instance.port = redis_source_instance.port redis_instance.is_active = redis_source_instance.is_active redis_instance.is_arbiter = redis_source_instance.is_arbiter redis_instance.instance_type = redis_source_instance.instance_type redis_instance.hostname = host redis_instance.databaseinfra = workflow_dict[ 'databaseinfra'] redis_instance.save() LOG.info("Instance created!") redis_source_instance.future_instance = redis_instance redis_source_instance.save() workflow_dict['target_instances'].append(redis_instance) sentinel_instance = Instance() sentinel_instance.address = host.address sentinel_instance.dns = host.address sentinel_instance.port = sentinel_source_instance.port sentinel_instance.is_active = sentinel_source_instance.is_active sentinel_instance.is_arbiter = sentinel_source_instance.is_arbiter sentinel_instance.instance_type = sentinel_source_instance.instance_type sentinel_instance.hostname = host sentinel_instance.databaseinfra = workflow_dict[ 'databaseinfra'] sentinel_instance.save() LOG.info("Instance created!") sentinel_source_instance.future_instance = sentinel_instance sentinel_source_instance.save() workflow_dict['target_instances'].append(sentinel_instance) 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: cs_credentials = get_credentials_for( environment=workflow_dict['target_environment'], credential_type=CredentialType.CLOUDSTACK) vm_credentials = get_credentials_for( environment=workflow_dict['target_environment'], credential_type=CredentialType.VM) cs_provider = CloudStackProvider(credentials=cs_credentials) target_offering = workflow_dict['target_offering'] cs_plan_attrs = PlanAttr.objects.get(plan=workflow_dict['target_plan']) bundles = list(cs_plan_attrs.bundle.all()) workflow_dict['target_hosts'] = [] workflow_dict['target_instances'] = [] for index, source_instance in enumerate(workflow_dict['source_instances']): source_host = workflow_dict['source_hosts'][index] vm_name = source_host.hostname.split('.')[0] if len(bundles) == 1: bundle = bundles[0] else: bundle = LastUsedBundle.get_next_bundle(plan=workflow_dict['target_plan'], bundle=bundles) if index == 2: offering = cs_plan_attrs.get_weaker_offering() else: offering = target_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.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.is_arbiter = source_instance.is_arbiter 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) return True except Exception: traceback = full_stack() workflow_dict['exceptions']['error_codes'].append(DBAAS_0020) workflow_dict['exceptions']['traceback'].append(traceback) return False