def _Create(self): """Create a VM instance.""" if self.image is None: # This is here and not in the __init__ method bceauese _GetDefaultImage # does a nontrivial amount of work (it calls the aliyuncli). self.image = self._GetDefaultImage(self.region) self.password = util.GeneratePassword() create_cmd = util.ALI_PREFIX + [ 'ecs', 'CreateInstance', '--InstanceName perfkit-%s' % FLAGS.run_uri, '--RegionId %s' % self.region, '--ZoneId %s' % self.zone, '--ImageId %s' % self.image, '--InstanceType %s' % self.machine_type, '--SecurityGroupId %s' % self.network.security_group.group_id, '--Password %s' % self.password] if FLAGS.scratch_disk_type == disk.LOCAL: disk_cmd = [ '--SystemDiskCategory ephemeral_ssd', '--DataDisk1Category ephemeral_ssd', '--DataDisk1Size %s' % self.scratch_disk_size, '--DataDisk1Device %s%s' % (util.GetDrivePathPrefix(), DRIVE_START_LETTER)] create_cmd.extend(disk_cmd) if FLAGS.ali_io_optimized is not None: create_cmd.extend(['--IoOptimized optimized', '--SystemDiskCategory %s' % self.system_disk_type]) if FLAGS.ali_use_vpc: create_cmd.extend(['--VpcId %s' % self.network.vpc.id, '--VSwitchId %s' % self.network.vswitch.id]) else: create_cmd.extend([ '--InternetChargeType PayByTraffic', '--InternetMaxBandwidthIn %s' % self.bandwidth_in, '--InternetMaxBandwidthOut %s' % self.bandwidth_out]) create_cmd = util.GetEncodedCmd(create_cmd) stdout, _ = vm_util.IssueRetryableCommand(create_cmd) response = json.loads(stdout) self.id = response['InstanceId'] self._AllocatePubIp(self.region, self.id) start_cmd = util.ALI_PREFIX + [ 'ecs', 'StartInstance', '--RegionId %s' % self.region, '--InstanceId %s' % self.id] start_cmd = util.GetEncodedCmd(start_cmd) vm_util.IssueRetryableCommand(start_cmd)
def GetLocalDisks(self): """Returns a list of local disks on the VM. Returns: A list of strings, where each string is the absolute path to the local disks on the VM (e.g. '/dev/xvdb'). """ return [ '%s%s' % (util.GetDrivePathPrefix(), chr(ord(DRIVE_START_LETTER) + i)) for i in xrange(NUM_LOCAL_VOLUMES[self.machine_type]) ]
def _Create(self): """Create a VM instance.""" if self.image is None: # This is here and not in the __init__ method bceauese _GetDefaultImage # does a nontrivial amount of work (it calls the aliyuncli). self.image = self._GetDefaultImage(self.region) create_cmd = util.ALI_PREFIX + [ 'ecs', 'CreateInstance', '--InstanceName perfkit-%s' % FLAGS.run_uri, '--RegionId %s' % self.region, '--ZoneId %s' % self.zone, '--ImageId %s' % self.image, '--InstanceType %s' % self.machine_type, '--SecurityGroupId %s' % self.network.security_group.group_id, '--KeyPairName %s' % self.key_pair_name, '--SystemDisk.Category %s' % self.system_disk_type, '--SystemDisk.Size %s' % self.system_disk_size] if FLAGS.scratch_disk_type == disk.LOCAL: disk_cmd = [ '--DataDisk1Category ephemeral_ssd', '--DataDisk1Size %s' % self.scratch_disk_size, '--DataDisk1Device %s%s' % (util.GetDrivePathPrefix(), DRIVE_START_LETTER)] create_cmd.extend(disk_cmd) if FLAGS.ali_io_optimized is not None: create_cmd.extend(['--IoOptimized optimized']) if FLAGS.ali_use_vpc: create_cmd.extend(['--VSwitchId %s' % self.network.vswitch.id]) else: create_cmd.extend([ '--InternetChargeType PayByTraffic', '--InternetMaxBandwidthIn %s' % self.bandwidth_in, '--InternetMaxBandwidthOut %s' % self.bandwidth_out]) # Create user and add SSH key public_key = AliCloudKeyFileManager.GetPublicKey() user_data = util.ADD_USER_TEMPLATE.format(user_name=self.user_name, public_key=public_key) logging.debug('encoding startup script: %s', user_data) create_cmd.extend(['--UserData', six.ensure_str( base64.b64encode(user_data.encode('utf-8')))]) create_cmd = util.GetEncodedCmd(create_cmd) stdout, _ = vm_util.IssueRetryableCommand(create_cmd) response = json.loads(stdout) self.id = response['InstanceId'] self._AllocatePubIp(self.region, self.id) start_cmd = util.ALI_PREFIX + [ 'ecs', 'StartInstance', '--InstanceId %s' % self.id] start_cmd = util.GetEncodedCmd(start_cmd) vm_util.IssueRetryableCommand(start_cmd)