def create(self): if self._create_external_volume(): return ctx.logger.info('Creating volume by resource config: {}'.format( json.dumps(self.resource_config, indent=2))) params = self._prepare_request_params() region_urn = validate_parameter('region', self.resource_config) ctx.logger.info('Volume is creating, the params is {}.'.format(params)) try: volume_id = self.fc_client.volumes.create(region_urn, params)['urn'] except Exception as e: raise NonRecoverableError( 'Create volume failed! The params of create volume is {0},' 'the error message is {1}'.format(params, e)) self.wait_for_target_state(volume_id, fc_constants.FC_VOLUME_STATE_AVAILABLE) volume_info = self.fc_client.volumes.get(volume_id) self.update_runtime_properties('volume', volume_info) target_instance, instance_id = self.get_target_instance_and_id() self.attach_volume(instance_id, volume_id) server_instance_id = target_instance.runtime_properties.get( common_constants.EXTERNAL_HOSTNAME) size = validate_parameter('size', self.resource_config) extra_values = {'volume': {'size': size}} self.update_runtime_properties('volume', volume_info, extra_values) self.write_vm_info(instance_id, server_instance_id)
def custom_attach(self, **kwargs): compute = self.get_client() volume_id = ctx.instance.runtime_properties['external_id'] server_id = validate_parameter('server_id', kwargs) server_instance_id = validate_parameter('server_instance_id', kwargs) self.attach_volume(compute, server_id, volume_id) self.write_vm_info(compute, server_id, server_instance_id)
def resize(self, **kwargs): vm_id = ctx.instance.runtime_properties.get(EXTERNAL_ID) with ComputeHelper(Client()) as helper: vm_state = self.get_status(helper, vm_id) if vm_state != SMARTX_INSTANCE_STATE_STOPPED: ctx.logger.error( "The virtual machine's status should be stopped, current status is:{}" .format(vm_state)) return new_cpu = validate_parameter('cpus', kwargs) new_memory = validate_parameter('memory', kwargs) if (not new_cpu) and (not new_memory): ctx.logger.error( "The param `CPU` and `Memory` is required, received param is:{}" .format(kwargs)) return vm_info = helper.get_vm(vm_id) _cpu = vm_info['cpu'] _params = {'ha': vm_info['ha']} if new_cpu: _cpu['topology']['cores'] = new_cpu _params.update({'vcpu': new_cpu}) _params.update({'cpu': _cpu}) if new_memory: new_memory = new_memory << 20 # memory->Byte _params.update({'memory': new_memory}) job_info = helper.modify_configure(vm_id, _params) ret = self.wait_job(helper, job_info['job_id']) ctx.logger.info("smartx resized result: {}".format(ret)) vm_info = helper.get_vm(vm_id) self.save_runtime_properties('compute', vm_info) ctx.logger.info("smartx resized done")
def prepare_params(self): zone = self.resource_config['available_zone_id'] password = decrypt_password( validate_parameter('password', self.resource_config)) instance_name = self.resource_config.get('instance_name') hostname = None if not instance_name: instance_name, hostname = self.get_instance_names() hostname = self.resource_config.get( 'hostname') or hostname or instance_name image_id = validate_parameter('image_id', self.resource_config) params_info = { 'Region': self.resource_config.get('region'), 'Zone': zone, 'ImageId': image_id, 'SubnetId': self.get_subnet(), 'Password': base64.b64encode(password.encode('utf-8')).decode('utf-8'), 'Name': instance_name, 'ChargeType': self.resource_config.get('charge_type') or 'Dynamic', 'LoginMode': self.resource_config.get('login_mode') or 'Password', 'Disks.0.IsBoot': 'True', 'Disks.0.Type': constants.UC_VOLUME_TYPE, 'Disks.0.Size': self.get_size(image_id), 'MachineType': constants.MachineType, 'CPU': self.resource_config['cpus'], 'Memory': self.resource_config['memory'] } return params_info
def _prepare_request_body(self): dhcp_config = {} ip_ranges = self.resource_config.get('ip_ranges') if ip_ranges: dhcp_config['ip_ranges'] = ip_ranges gateway_ip = self.resource_config.get('gateway_ip') if gateway_ip: dhcp_config['gateway_ip'] = gateway_ip cidr = self.resource_config.get('cidr') if cidr: dhcp_config['cidr'] = cidr dhcp_interface_ip = self.resource_config.get('dhcp_interface_ip') if dhcp_interface_ip: dhcp_config['dhcp_interface_ip'] = dhcp_interface_ip dns_servers = self.resource_config.get('dns_servers') if dns_servers: dhcp_config['dns_servers'] = dns_servers network_dict = { "vds_id": validate_parameter("vds_id", self.resource_config), "name": validate_parameter("name", self.resource_config), "vlan_id": validate_parameter('vlan_id', self.resource_config), "dhcp_config": dhcp_config } return network_dict
def create(self, **kwargs): # todo: Create new network try: with NetworkHelper(Client()) as helper: network_id = validate_parameter('resource_id', self.resource_config) subnet_id = validate_parameter('subnet_id', self.resource_config) ctx.logger.info( 'Use existed SmartX network: {}, subnet: {}.'.format( network_id, subnet_id)) network_info = helper.get_vds(network_id) subnet_info = helper.get_network(network_id, subnet_id) network_index = ctx.node.name[-1] if ctx.node.name[-1].isdigit( ) else 0 set_runtime_properties('network_index', network_index, ctx.instance) set_runtime_properties('subnet_info', subnet_info, ctx.instance) extra_values = { EXTERNAL_ID: subnet_id, } self.save_runtime_properties('network', network_info, extra_values) except Exception as e: raise NonRecoverableError( 'Create network from SmartX failed: {}.'.format(e))
def _prepare_vm_config(self, image_id, disk_name): vcpu = validate_parameter('CPU', self.resource_config, not_empty=True) memory = validate_parameter('Memory', self.resource_config, not_empty=True) volume, datastore_urn = self.get_template_size_and_datastore(image_id) nics = self._get_nics_params() vm_config = { "cpu": { "quantity": vcpu, }, "memory": { "quantityMB": memory, }, "disks": [{ "diskName": disk_name, "systemVolume": True, "sequenceNum": 1, "isDataCopy": True, "quantityGB": volume, "isThin": True, "datastoreUrn": datastore_urn, }], "nics": nics, } return vm_config
def __init__(self, params): self.region = validate_parameter('region', params) self.g_params = { 'PublicKey': validate_parameter('access_key_id', params) } self.private_key = decrypt_password( validate_parameter('access_key_secret', params)) base_url = params.get('base_url', 'https://api.ucloud.cn') self.conn = UConnection(base_url)
def create(self, **kwargs): if self._create_system_disk(): return volume_category = self.resource_config.get('is_system_disk') if volume_category and str(volume_category).lower() == "true": ctx.logger.info("SmartX doesn't support system disk.") return try: target_instance = self.get_related_vm() server_id = target_instance.runtime_properties.get(EXTERNAL_ID) with ComputeHelper(Client()) as helper: vm_info = helper.get_vm(server_id) with VolumeHelper(Client()) as helper: if self.use_external_resource: volume_id = validate_parameter('resource_id', self.resource_config) ctx.logger.info( 'Use existed SmartX volume: {}.'.format(volume_id)) else: size = int(validate_parameter('size', self.resource_config)) size_in_byte = size << 30 volume_dict = { "name": self.resource_config.get('name') or ctx.instance.id, "size_in_byte": size_in_byte } self.set_optional_values(self.resource_config, volume_dict, optional_keys) ctx.logger.info( 'Creating SmartX volume with parameters: {}'.format( volume_dict)) job_info = helper.create_volume(volume_dict) helper.wait_job(job_info['job_id']) volume_id = self.wait_job(helper, job_info['job_id'], resource_type='KVM_VOLUME') ctx.logger.info('Create volume from SmartX successfully.') volume_info = helper.get_volume(volume_id) self.attach_volume(helper, vm_info, volume_info) extra_values = { 'volume': { 'size': size }, 'host_id': target_instance.id } self.save_runtime_properties('volume', volume_info, extra_values) except Exception as e: raise NonRecoverableError( 'Create volume from SmartX failed: {}.'.format(e))
def __init__(self): self.connection_config = get_connection_config() self.resource_config = ctx.node.properties['resource_config'] self.region = validate_parameter('region', self.resource_config) self.ks_access_key_id = validate_parameter('access_key_id', self.connection_config) self.ks_secret_access_key = decrypt_password( validate_parameter('access_key_secret', self.connection_config)) self.domain = self.connection_config.get('domain')
def prepare_params(self): params = { 'VolumeName': self.resource_config.get('name') or ctx.instance.id, 'VolumeType': validate_parameter('volume_type', self.resource_config), 'VolumeDesc': self.resource_config.get('volume_description'), 'Size': validate_parameter('size', self.resource_config), 'AvailabilityZone': validate_parameter('available_zone_id', self.resource_config), 'ChargeType': self.resource_config.get('charge_type') or 'HourlyInstantSettlement', 'PurchaseTime': self.resource_config.get('purchase_time') or 0, } return drop_none(params)
def __init__(self, params, api_name, api_version): self.api_name = api_name self.api_version = api_version self.key_id = validate_parameter('private_key_id', params) self.private_key = decrypt_password( validate_parameter('private_key', params)).replace('\\n', '\n') self.project = validate_parameter('project_id', params) self.client_email = validate_parameter('client_email', params) self.client_id = validate_parameter('client_id', params) self.key_json = self.generate_key_json() self.scope = ['https://www.googleapis.com/auth/' + api_name]
def _create_external_instance(self, helper): if not self.use_external_resource: return False instance_id = validate_parameter('resource_id', self.node_properties) ctx.logger.info('Use existed SmartX VM: {}.'.format(instance_id)) vm_info = helper.get_vm(instance_id) extra_values = { EXTERNAL_HOSTNAME: validate_parameter('vm_name', vm_info) } self.save_runtime_properties('compute', vm_info, extra_values) self.update_networks() return True
def _prepare_request_params(self): target_instance, instance_id = self.get_target_instance_and_id() volume_name = self.resource_config.get( 'volume_name') or ctx.instance.id params = { 'name': volume_name, 'type': validate_parameter('volume_type', self.resource_config), 'quantityGB': validate_parameter('size', self.resource_config), 'datastoreUrn': self.get_system_disk_datastore(instance_id), 'isThin': True, } return utils.drop_none(params)
def create(self): ctx.logger.info('Start to create Vpc...') vpc_name = validate_parameter('vpc_name', self.resource_config) cidr_block = validate_parameter('cidr_block', self.resource_config) request_body = { 'VpcName': vpc_name, 'CidrBlock': cidr_block } ctx.logger.info('Try to create Vpc with parameters {}'.format(request_body)) vpc_info = NetworkHelper().create_vpc(request_body) ctx.instance.runtime_properties['vpc_info'] = vpc_info ctx.logger.info('Created Vpc Successfully...')
def create(self, **kwargs): vpc_id = validate_parameter('resource_id', self.resource_config) try: vpc_info = NetworkHelper().list_vpcs(ids=[vpc_id])[0] except Exception as e: raise NonRecoverableError( "Vpc {} not exists.Error Message:{}".format(vpc_id, e)) if self.use_external_resource: subnet_id = validate_parameter('subnet_id', self.resource_config) try: subnet_info = NetworkHelper().list_subnets(ids=[subnet_id])[0] ctx.logger.debug( "Use existed tencentcloud network: {}, subnet: {}.".format( vpc_id, subnet_id)) self.set_base_runtime_props( resource_id=subnet_info['SubnetId'], name=subnet_info['SubnetName']) ctx.instance.runtime_properties['vpc_info'] = vpc_info ctx.instance.runtime_properties['subnet_info'] = subnet_info except Exception as e: raise NonRecoverableError( "Create network failed: {}.".format(e)) else: ctx.logger.info('Start to create subnet...') subnet_name = validate_parameter('subnet_name', self.resource_config) cidr_block = validate_parameter('cidr_block', self.resource_config) zone = validate_parameter('available_zone_id', self.resource_config) request_body = { 'SubnetName': subnet_name, 'CidrBlock': cidr_block, 'Zone': zone, 'VpcId': vpc_id } ctx.logger.info( 'Try to Create subnet with parameters:{}'.format(request_body)) try: subnet_info = NetworkHelper().create_subnet(request_body) except Exception as e: raise NonRecoverableError( "Create network failed...messages: {}.".format(e)) self.set_base_runtime_props(resource_id=subnet_info['SubnetId'], name=subnet_info['SubnetName']) ctx.instance.runtime_properties['vpc_info'] = vpc_info ctx.instance.runtime_properties['subnet_info'] = subnet_info ctx.logger.info( 'Created subnet successfully...vpc:{},subnet:{}'.format( vpc_id, subnet_info['SubnetId']))
def custom_resize(self, **kwargs): volume_id = ctx.instance.runtime_properties.get(EXTERNAL_ID) size = validate_parameter('size', kwargs) size_in_byte = int(size) << 30 data = {'size_in_byte': size_in_byte} if kwargs.get('description'): data['description'] = kwargs['description'] ctx.logger.debug('Resizing volume {} with parameters {}.'.format( volume_id, data)) try: with VolumeHelper(Client()) as helper: volume_info = helper.get_volume(volume_id) volume_name = volume_info.get("name") if not volume_name: ctx.logger.error( 'Resizing volume {} failed. volume_name is required'. format(volume_id)) return data.update({"name": volume_name}) helper.update_volume(volume_id, data) except Exception as e: raise NonRecoverableError('Resize volume {} failed: {}.'.format( volume_id, e)) extra_values = {'volume': {'size': size}} self.save_runtime_properties('volume', extra_values=extra_values) ctx.logger.debug('Resized volume {}'.format(volume_id))
def modify_display_name(self, **kwargs): new_name = validate_parameter(ctx.instance.id, kwargs.get('names', {})) instance_id = ctx.instance.runtime_properties.get(EXTERNAL_ID) ctx.logger.info( 'Updating name of tencent instance {}.'.format(instance_id)) ComputeHelper().modify_display_name(instance_id, new_name) ctx.instance.runtime_properties['external_name'] = new_name
def create(self): bucket_name = validate_parameter('bucket_name', self.resource_config) app_id = validate_parameter('app_id', self.connection_config) bucket_name = '-'.join([bucket_name, app_id]) if not self.use_external_resource: request_body = {'Bucket': bucket_name} try: OssHelper().create_bucket(request_body) except Exception as e: raise NonRecoverableError( 'Create bucket failed...Messages:{}'.format(e)) self.set_base_runtime_props(resource_id=bucket_name, name=bucket_name) else: ctx.logger.info( 'Use external resource...Bucket name:{}'.format(bucket_name))
def modify_display_name(self, **kwargs): runtime_properties = ctx.instance.runtime_properties instance_id = runtime_properties.get(common_constants.EXTERNAL_ID) ctx.logger.info( 'modify FusionCompute VM name instance_id:{}. kwargs: {}'.format( instance_id, kwargs)) param_dict = validate_parameter('names', kwargs, not_empty=True) old_name, new_name = list(param_dict.items())[0] request_data = {"name": new_name} try: self.fc_client.servers.modify_confige(instance_id, request_data) instance_info = self.fc_client.servers.get(instance_id) ctx.logger.info( 'FusionComputer VM, created instance_info: {}'.format( instance_info)) instance_info.update({"name": new_name, "external_name": new_name}) self.update_runtime_properties('compute', instance_info) ctx.instance.update() ctx.logger.info('modify FusionCompute VM name DONE :{}.'.format( ctx.instance.runtime_properties)) except Exception as e: raise NonRecoverableError( 'modify FusionCompute VM name FAIL :{}'.format(e))
def _get_instances_params(self): vm_name = self.resource_config.get('instance_name') hostname = None if not vm_name: vm_name, hostname = self.get_instance_names() hostname = self.resource_config.get('hostname') or hostname or vm_name password = decrypt_password(self.resource_config.get('password')) description = self.resource_config.get('description', '') ha = self.resource_config.get('ha', False) vcpu = validate_parameter('CPU', self.resource_config, not_empty=True) memory = int( validate_parameter('Memory', self.resource_config, not_empty=True)) << 20 image_id = validate_parameter('image_id', self.resource_config, not_empty=True) nested_virtualization = self.resource_config.get( 'nested_virtualization', False) # nics nics, cloud_init_nics, gateway, nameservers = self._get_nics_params() cloud_init_data = self._get_cloud_init_data(hostname, password, cloud_init_nics, nameservers, gateway) data = { "vm_name": vm_name, "status": "stopped", "description": description, 'image_id': image_id, 'is_full_copy': False, "ha": ha, "vcpu": vcpu, "cpu": { "topology": { "cores": vcpu, "sockets": 1 } }, "memory": memory, "nested_virtualization": nested_virtualization, "nics": nics, "cloud_init": cloud_init_data } self.set_optional_values(self.resource_config, data, optional_keys) return data
def _create(self): ctx.logger.info('Start to create subnet...') cidr = validate_parameter('cidr_block', self.resource_config) ip_range_from, ip_range_to = self.get_ip_range(cidr) params = { 'AvailabilityZone': validate_parameter('available_zone_id', self.resource_config), 'SubnetName': self.resource_config.get('subnet_name'), 'CidrBlock': validate_parameter('cidr_block', self.resource_config), 'SubnetType': validate_parameter('subnet_type', self.resource_config), 'DhcpIpFrom': ip_range_from, 'DhcpIpTo': ip_range_to, 'GatewayIp': validate_parameter('gateway', self.resource_config), 'VpcId': validate_parameter('resource_id', self.resource_config), 'Dns1': validate_parameter('dns1', self.resource_config), 'Dns2': self.resource_config.get('dns2'), } params = dict((k, v) for k, v in list(params.items()) if v) ctx.logger.info( 'Try to create subnet with parameters {}'.format(params)) res = Helper().execute_request('vpc', 'create_subnet', params) subnet_id = res['Subnet']['SubnetId'] self.write_runtime(subnet_id) ctx.logger.info("Create subnet {0} successfully".format(subnet_id))
def custom_resize(self, **kwargs): ctx.logger.info('runtime_properties: {}'.format( ctx.instance.runtime_properties)) volume_id = ctx.instance.runtime_properties['external_id'] size = validate_parameter('size', kwargs) size = size * 1024 # transfer to MB vm_id = ctx.instance.runtime_properties[ 'vm_id'] # '/service/sites/3F4B0701/vms/i-0000029B' self.resize_volume(volume_id, size, vm_id)
def modify_display_name(self, **kwargs): new_name = validate_parameter(ctx.instance.id, kwargs.get('names', {})) vm_id = ctx.instance.runtime_properties.get(EXTERNAL_ID) ctx.logger.info('Updating name of SmartX VM {}.'.format(vm_id)) with ComputeHelper(Client()) as helper: helper.modify_display_name(vm_id, new_name) ctx.instance.runtime_properties[EXTERNAL_NAME] = new_name ctx.logger.info('Updated name of SmartX VM {}.'.format(vm_id))
def resize(self, **kwargs): instance_id = ctx.instance.runtime_properties.get(EXTERNAL_ID) if not instance_id: ctx.logger.debug('The instance was not created successfully!!!') return instance_type = validate_parameter('flavor', kwargs) ctx.logger.info('Attempt to update instance {0} configuration.'.format( instance_id)) ComputeHelper().resize(instance_id, instance_type)
def _prepare_request_config(self, compute): volume_type = validate_parameter('volume_type', self.resource_config) volume_response = compute.diskTypes().get( project=self.project, zone=self.zone, diskType=volume_type).execute() volume_self_link = volume_response.get('selfLink') volume_name = self.resource_config.get( 'volume_name') or ctx.instance.id config = { "name": volume_name.replace('_', '-').lower(), "description": self.resource_config.get('description'), "sizeGb": validate_parameter('size', self.resource_config), "type": volume_self_link, "physicalBlockSizeBytes": '4096', } return drop_none(config)
def custom_resize(self, **kwargs): volume_id = ctx.instance.runtime_properties.get(EXTERNAL_ID) ctx.logger.info('Resizing volume {}.'.format(volume_id)) size = validate_parameter('size', kwargs) VolumeHelper().resize_disk(volume_id, size) ctx.logger.info('Volume {} resized to {} GB.'.format(volume_id, size)) ctx.instance.runtime_properties['volume'] = {'size': size} ctx.instance.update()
def prepare_params(self): # change key-value according to concrete cloud api template_id = validate_parameter('image_id', self.resource_config) login_id = validate_parameter('login_id', self.resource_config) memory = self.resource_config.get('mem') * 1024 if self.resource_config.get('mem') \ else self._template_mem(template_id) params = { 'resourceGroupName': constants.resourceGroupName, 'resourceGroupType': constants.resourceGroupType, 'farmId': constants.farmId, 'templateId': template_id, 'name': self.resource_config.get('name') or ctx.instance.id, 'cores': self.resource_config.get('cpu') or self._template_cpu(template_id), 'memory': memory, # MB 'diskList': self._template_disks(template_id), 'instanceNum': 1, 'siteId': self.resource_config.get('region'), 'galaxVersion': constants.galaxVersion, 'clusterId': self.resource_config.get('available_zone_id'), 'dgName': constants.dgName, 'dgType': constants.dgType, 'namingPolicy': constants.namingPolicy, 'domain': constants.domain, 'isMacBind': 1 } new_params = self.update_network_params(params, login_id) return new_params
def create_snapshot(self, **kwargs): ctx.logger.info('Creating snapshot, parameters: {}.'.format(kwargs)) name = validate_parameter('snapshotName', kwargs) instance_id = ctx.instance.runtime_properties.get(EXTERNAL_ID) snapshots = VolumeHelper().create_snapshot(name, instance_id) existed_snapshots = ctx.instance.runtime_properties.get( 'snapshot_ids', []) ctx.instance.runtime_properties[ 'snapshot_ids'] = existed_snapshots + snapshots ctx.logger.info('Snapshot created.')
def create(self): if self.use_external_resource is True: instance_id = validate_parameter('resource_id', self.node_properties) else: instance_id = self._create() self.wait_for_target_state( instance_id, ksyun_constants.KS_INSTANCE_STATE_ACTIVE) self.update_runtime_properties(instance_id) self.associate_eip()