def get_block_device_map(bdmapping_json=None): """Parse block_device_mapping JSON and return a configured BlockDeviceMapping object Mapping JSON structure... {"/dev/sda": {"snapshot_id": "snap-23E93E09", "volume_type": null, "delete_on_termination": true, "size": 1} } """ if bdmapping_json: mapping = json.loads(bdmapping_json) if mapping: bdm = BlockDeviceMapping() for key, val in mapping.items(): device = BlockDeviceType() if val.get('virtual_name') is not None and val.get( 'virtual_name').startswith('ephemeral'): device.ephemeral_name = val.get('virtual_name') else: device.volume_type = 'standard' device.snapshot_id = val.get('snapshot_id') or None device.size = val.get('size') device.delete_on_termination = val.get( 'delete_on_termination', False) bdm[key] = device return bdm return None return None
def register_snapshot_by_id( self, snap_id, rdn="/dev/sda1", description="bfebs", windows=False, bdmdev=None, name=None, ramdisk=None, kernel=None, dot=True ): ''' Register an image snapshot snap_id (mandatory string) snapshot id name (mandatory string) name of image to be registered description (optional string) description of image to be registered bdmdev (optional string) block-device-mapping device for image rdn (optional string) root-device-name for image dot (optional boolean) Delete On Terminate boolean windows (optional boolean) Is windows image boolean kernel (optional string) kernal (note for windows this name should be "windows" ''' if (bdmdev is None): bdmdev=rdn if (name is None): name="bfebs_"+ snap_id if ( windows is True ) and ( kernel is not None): kernel="windows" bdmap = BlockDeviceMapping() block_dev_type = BlockDeviceType() block_dev_type.snapshot_id = snap_id block_dev_type.delete_on_termination = dot bdmap[bdmdev] = block_dev_type self.debug("Register image with: snap_id:"+str(snap_id)+", rdn:"+str(rdn)+", desc:"+str(description)+", windows:"+str(windows)+", bdname:"+str(bdmdev)+", name:"+str(name)+", ramdisk:"+str(ramdisk)+", kernel:"+str(kernel)) image_id = self.ec2.register_image(name=name, description=description, kernel_id=kernel, ramdisk_id=ramdisk, block_device_map=bdmap, root_device_name=rdn) self.debug("Image now registered as " + image_id) return image_id
def test_create_launch_configuration_with_block_device_mappings(): block_device_mapping = BlockDeviceMapping() ephemeral_drive = BlockDeviceType() ephemeral_drive.ephemeral_name = 'ephemeral0' block_device_mapping['/dev/xvdb'] = ephemeral_drive snapshot_drive = BlockDeviceType() snapshot_drive.snapshot_id = "snap-1234abcd" snapshot_drive.volume_type = "standard" block_device_mapping['/dev/xvdp'] = snapshot_drive ebs_drive = BlockDeviceType() ebs_drive.volume_type = "io1" ebs_drive.size = 100 ebs_drive.iops = 1000 ebs_drive.delete_on_termination = False block_device_mapping['/dev/xvdh'] = ebs_drive conn = boto.connect_autoscale(use_block_device_types=True) config = LaunchConfiguration( name='tester', image_id='ami-abcd1234', instance_type='m1.small', key_name='the_keys', security_groups=["default", "default2"], user_data="This is some user_data", instance_monitoring=True, instance_profile_name='arn:aws:iam::123456789012:instance-profile/testing', spot_price=0.1, block_device_mappings=[block_device_mapping] ) conn.create_launch_configuration(config) launch_config = conn.get_all_launch_configurations()[0] launch_config.name.should.equal('tester') launch_config.image_id.should.equal('ami-abcd1234') launch_config.instance_type.should.equal('m1.small') launch_config.key_name.should.equal('the_keys') set(launch_config.security_groups).should.equal(set(['default', 'default2'])) launch_config.user_data.should.equal("This is some user_data") launch_config.instance_monitoring.enabled.should.equal('true') launch_config.instance_profile_name.should.equal('arn:aws:iam::123456789012:instance-profile/testing') launch_config.spot_price.should.equal(0.1) len(launch_config.block_device_mappings).should.equal(3) returned_mapping = launch_config.block_device_mappings set(returned_mapping.keys()).should.equal(set(['/dev/xvdb', '/dev/xvdp', '/dev/xvdh'])) returned_mapping['/dev/xvdh'].iops.should.equal(1000) returned_mapping['/dev/xvdh'].size.should.equal(100) returned_mapping['/dev/xvdh'].volume_type.should.equal("io1") returned_mapping['/dev/xvdh'].delete_on_termination.should.be.false returned_mapping['/dev/xvdp'].snapshot_id.should.equal("snap-1234abcd") returned_mapping['/dev/xvdp'].volume_type.should.equal("standard") returned_mapping['/dev/xvdb'].ephemeral_name.should.equal('ephemeral0')
def _parse_block_device_mappings(user_input): """ Parse block device mappings per AWS CLI tools syntax (modified to add IOPS) http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html Syntax: /dev/xvd[a-z]=[snapshot-id|ephemeral]:[size in GB]:[Delete on Term]:[IOPS] - Leave inapplicable fields blank - Delete on Termination defaults to True - IOPS limits are not validated - EBS sizing is not validated Mount an Ephemeral Drive: /dev/xvdb1=ephemeral0 Mount multiple Ephemeral Drives: /dev/xvdb1=ephemeral0,/dev/xvdb2=ephemeral1 Mount a Snapshot: /dev/xvdp=snap-1234abcd Mount a Snapshot to a 100GB drive: /dev/xvdp=snap-1234abcd:100 Mount a Snapshot to a 100GB drive and do not delete on termination: /dev/xvdp=snap-1234abcd:100:false Mount a Fresh 100GB EBS device /dev/xvdp=:100 Mount a Fresh 100GB EBS Device and do not delete on termination: /dev/xvdp=:100:false Mount a Fresh 100GB EBS Device with 1000 IOPS /dev/xvdp=:100::1000 """ block_device_map = BlockDeviceMapping() mappings = user_input.split(",") for mapping in mappings: block_type = BlockDeviceType() mount_point, drive_type, size, delete, iops = _parse_drive_mapping( mapping) if 'ephemeral' in drive_type: block_type.ephemeral_name = drive_type elif 'snap' in drive_type: block_type.snapshot_id = drive_type block_type.volume_type = "standard" else: block_type.volume_type = "standard" block_type.size = size block_type.delete_on_termination = delete if iops: block_type.iops = iops block_type.volume_type = "io1" block_device_map[mount_point] = block_type return block_device_map
def _parse_block_device_mappings(user_input): """ Parse block device mappings per AWS CLI tools syntax (modified to add IOPS) http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html Syntax: /dev/xvd[a-z]=[snapshot-id|ephemeral]:[size in GB]:[Delete on Term]:[IOPS] - Leave inapplicable fields blank - Delete on Termination defaults to True - IOPS limits are not validated - EBS sizing is not validated Mount an Ephemeral Drive: /dev/xvdb1=ephemeral0 Mount multiple Ephemeral Drives: /dev/xvdb1=ephemeral0,/dev/xvdb2=ephemeral1 Mount a Snapshot: /dev/xvdp=snap-1234abcd Mount a Snapshot to a 100GB drive: /dev/xvdp=snap-1234abcd:100 Mount a Snapshot to a 100GB drive and do not delete on termination: /dev/xvdp=snap-1234abcd:100:false Mount a Fresh 100GB EBS device /dev/xvdp=:100 Mount a Fresh 100GB EBS Device and do not delete on termination: /dev/xvdp=:100:false Mount a Fresh 100GB EBS Device with 1000 IOPS /dev/xvdp=:100::1000 """ block_device_map = BlockDeviceMapping() mappings = user_input.split(",") for mapping in mappings: block_type = BlockDeviceType() mount_point, drive_type, size, delete, iops = _parse_drive_mapping(mapping) if 'ephemeral' in drive_type: block_type.ephemeral_name = drive_type elif 'snap' in drive_type: block_type.snapshot_id = drive_type block_type.volume_type = "standard" else: block_type.volume_type = "standard" block_type.size = size block_type.delete_on_termination = delete if iops: block_type.iops = iops block_type.volume_type = "io1" block_device_map[mount_point] = block_type return block_device_map
def vol2ami(self, volume_id, name, description=None): snap = self.ctxt.cnx_ec2.create_snapshot(volume_id, 'boto snapshot for %s' % name) block_map = BlockDeviceMapping() sda = BlockDeviceType() sda.snapshot_id = snap.id sda.ebs = True root_device_name = '/dev/sda1' block_map[root_device_name] = sda return self.ctxt.cnx_ec2.register_image(name=name, architecture='x86_64', root_device_name=root_device_name, block_device_map=block_map, description=description)
def register(snapshot_id, region, arch, size=None, name=None, desc=None, pvm=False): conn = utils.connect(region) if None in (name, size): log.debug('getting snapshot - %s', snapshot_id) snapshot = conn.get_all_snapshots(snapshot_ids=[snapshot_id])[0] size = size if size else snapshot.volume_size name = name if name else snapshot.description virt = 'hvm' kernel_id = None device_base = '/dev/xvd' ec2_arch = "x86_64" if arch == "amd64" else arch if pvm: kernel_id = utils.get_kernel(region, arch) virt = 'paravirtual' device_base = '/dev/sd' name += '-pvm' log.debug('creating block_device_map') block_device_map = BlockDeviceMapping() rootfs = BlockDeviceType() rootfs.delete_on_termination = True rootfs.size = size rootfs.snapshot_id = snapshot_id rootfs_device_name = device_base + 'a' block_device_map[rootfs_device_name] = rootfs ephemeral = BlockDeviceType() ephemeral.ephemeral_name = 'ephemeral0' ephemeral_device_name = device_base + 'b' block_device_map[ephemeral_device_name] = ephemeral log.debug('registering image - %s', name) ami_id = conn.register_image(name=name, description=desc, kernel_id=kernel_id, architecture=ec2_arch, root_device_name=rootfs_device_name, block_device_map=block_device_map, virtualization_type=virt) log.info('registered image - %s %s %s', ami_id, name, region) return ami_id, name
def clone_instance(instance): new_bdm = None ec2 = instance.connection if instance.block_device_mapping: root_device_name = instance.get_attribute('rootDeviceName')['rootDeviceName'] user_data = instance.get_attribute('userData')['userData'] # user_data comes back base64 encoded. Need to decode it so it # can get re-encoded by run_instance ! user_data = base64.b64decode(user_data) new_bdm = BlockDeviceMapping() for dev in instance.block_device_mapping: # if this entry is about the root device, skip it if dev != root_device_name: bdt = instance.block_device_mapping[dev] if bdt.volume_id: volume = ec2.get_all_volumes([bdt.volume_id])[0] snaps = volume.snapshots() if len(snaps) == 0: print 'No snapshots available for %s' % volume.id else: # sort the list of snapshots, newest is at the end now snaps.sort(key=lambda snap: snap.start_time) latest_snap = snaps[-1] new_bdt = BlockDeviceType() new_bdt.snapshot_id = latest_snap.id new_bdm[dev] = new_bdt return ec2.run_instances( instance.image_id, key_name=instance.key_name, security_groups=[g.name for g in instance.groups], user_data=user_data, instance_type=instance.instance_type, kernel_id=instance.kernel, ramdisk_id=instance.ramdisk, monitoring_enabled=instance.monitored, placement=instance.placement, block_device_map=new_bdm ).instances[0]
def _process_block_device_mappings(self, launch_config, zone=None): """ Processes block device mapping information and returns a Boto BlockDeviceMapping object. If new volumes are requested (source is None and destination is VOLUME), they will be created and the relevant volume ids included in the mapping. """ bdm = BlockDeviceMapping() # Assign letters from f onwards # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html next_letter = iter(list(string.ascii_lowercase[6:])) # assign ephemeral devices from 0 onwards ephemeral_counter = 0 for device in launch_config.block_devices: bd_type = BlockDeviceType() if device.is_volume: if device.is_root: bdm['/dev/sda1'] = bd_type else: bdm['sd' + next(next_letter)] = bd_type if isinstance(device.source, Snapshot): bd_type.snapshot_id = device.source.id elif isinstance(device.source, Volume): bd_type.volume_id = device.source.id elif isinstance(device.source, MachineImage): # Not supported pass else: # source is None, but destination is volume, therefore # create a blank volume. If the Zone is None, this # could fail since the volume and instance may be created # in two different zones. if not zone: raise InvalidConfigurationException( "A zone must be specified when launching with a" " new blank volume block device mapping.") new_vol = self.provider.block_store.volumes.create( '', device.size, zone) bd_type.volume_id = new_vol.id bd_type.delete_on_terminate = device.delete_on_terminate if device.size: bd_type.size = device.size else: # device is ephemeral bd_type.ephemeral_name = 'ephemeral%s' % ephemeral_counter return bdm
def clone_instance(instance): new_bdm = None ec2 = instance.connection if instance.block_device_mapping: root_device_name = instance.get_attribute( 'rootDeviceName')['rootDeviceName'] user_data = instance.get_attribute('userData')['userData'] # user_data comes back base64 encoded. Need to decode it so it # can get re-encoded by run_instance ! user_data = base64.b64decode(user_data) new_bdm = BlockDeviceMapping() for dev in instance.block_device_mapping: # if this entry is about the root device, skip it if dev != root_device_name: bdt = instance.block_device_mapping[dev] if bdt.volume_id: volume = ec2.get_all_volumes([bdt.volume_id])[0] snaps = volume.snapshots() if len(snaps) == 0: print 'No snapshots available for %s' % volume.id else: # sort the list of snapshots, newest is at the end now snaps.sort(key=lambda snap: snap.start_time) latest_snap = snaps[-1] new_bdt = BlockDeviceType() new_bdt.snapshot_id = latest_snap.id new_bdm[dev] = new_bdt return ec2.run_instances(instance.image_id, key_name=instance.key_name, security_groups=[g.name for g in instance.groups], user_data=user_data, instance_type=instance.instance_type, kernel_id=instance.kernel, ramdisk_id=instance.ramdisk, monitoring_enabled=instance.monitored, placement=instance.placement, block_device_map=new_bdm).instances[0]
def _parse_block_device_mappings(self): block_device_map = BlockDeviceMapping() for mapping in self.block_device_mapping_dict: block_type = BlockDeviceType() mount_point = mapping.get('device_name') if 'ephemeral' in mapping.get('virtual_name', ''): block_type.ephemeral_name = mapping.get('virtual_name') else: block_type.volume_type = mapping.get('ebs._volume_type') block_type.snapshot_id = mapping.get('ebs._snapshot_id') block_type.delete_on_termination = mapping.get('ebs._delete_on_termination') block_type.size = mapping.get('ebs._volume_size') block_type.iops = mapping.get('ebs._iops') block_device_map[mount_point] = block_type return block_device_map
def _parse_block_device_mappings(self): block_device_map = BlockDeviceMapping() for mapping in self.block_device_mapping_dict: block_type = BlockDeviceType() mount_point = mapping.get("device_name") if "ephemeral" in mapping.get("virtual_name", ""): block_type.ephemeral_name = mapping.get("virtual_name") else: block_type.volume_type = mapping.get("ebs._volume_type") block_type.snapshot_id = mapping.get("ebs._snapshot_id") block_type.delete_on_termination = mapping.get( "ebs._delete_on_termination") block_type.size = mapping.get("ebs._volume_size") block_type.iops = mapping.get("ebs._iops") block_device_map[mount_point] = block_type return block_device_map
def register(snapshot_id, region, arch, size=None, name=None, desc=None, pvm=False): conn = utils.connect(region) if None in (name, size): log.debug('getting snapshot - %s', snapshot_id) snapshot = conn.get_all_snapshots(snapshot_ids=[snapshot_id])[0] size = size if size else snapshot.volume_size name = name if name else snapshot.description virt = 'hvm' kernel_id = None device_base = '/dev/xvd' ec2_arch = "x86_64" if arch == "amd64" else arch if pvm: kernel_id = utils.get_kernel(region, arch) virt = 'paravirtual' device_base = '/dev/sd' name += '-pvm' log.debug('creating block_device_map') block_device_map = BlockDeviceMapping() rootfs = BlockDeviceType() rootfs.delete_on_termination = True rootfs.size = size rootfs.snapshot_id = snapshot_id rootfs_device_name = device_base + 'a' block_device_map[rootfs_device_name] = rootfs ephemeral = BlockDeviceType() ephemeral.ephemeral_name = 'ephemeral0' ephemeral_device_name = device_base + 'b' block_device_map[ephemeral_device_name] = ephemeral log.debug('registering image - %s', name) ami_id = conn.register_image( name=name, description=desc, kernel_id=kernel_id, architecture=ec2_arch, root_device_name=rootfs_device_name, block_device_map=block_device_map, virtualization_type=virt) log.info('registered image - %s %s %s', ami_id, name, region) return ami_id, name
def capture(self): volume = self.ec2.get_all_volumes( filters={ 'attachment.instance-id': self.instance.id, 'attachment.device': self.root_device, })[0] snapshot = self.ec2.create_snapshot(volume.id) sys.stdout.write("waiting for snapshot to complete ..") while snapshot.status != 'completed': sys.stdout.write(".") sys.stdout.flush() time.sleep(5) snapshot.update() sys.stdout.write("\n") # create EBS mapping device = BlockDeviceType() device.snapshot_id = snapshot.id mapping = BlockDeviceMapping() mapping['/dev/sda'] = device self.get_instance_kernel() image = self.ec2.register_image(name=self.name, description=self.name, architecture=self.arch, kernel_id=self.instance_kernel.id, root_device_name='/dev/sda', block_device_map=mapping) if self.settings["target/permission"] == "public": self.ec2.modify_image_attribute(image, groups='all') with open(self.settings["path/mirror/target"], "w") as fd: cmd = [ "ec2-run-instances", "--region", self.region, "--instance-type", "t1.micro", image, ] fd.write(" ".join(cmd)) fd.write("\n")
def capture(self): volume = self.ec2.get_all_volumes(filters={ 'attachment.instance-id': self.instance.id, 'attachment.device': self.root_device, })[0] snapshot = self.ec2.create_snapshot(volume.id) sys.stdout.write("waiting for snapshot to complete ..") while snapshot.status != 'completed': sys.stdout.write(".") sys.stdout.flush() time.sleep(5) snapshot.update() sys.stdout.write("\n") # create EBS mapping device = BlockDeviceType() device.snapshot_id = snapshot.id mapping = BlockDeviceMapping() mapping['/dev/sda'] = device self.get_instance_kernel() image = self.ec2.register_image(name=self.name, description=self.name, architecture=self.arch, kernel_id=self.instance_kernel.id, root_device_name='/dev/sda', block_device_map=mapping) if self.settings["target/permission"] == "public": self.ec2.modify_image_attribute(image, groups='all') with open(self.settings["path/mirror/target"], "w") as fd: cmd = [ "ec2-run-instances", "--region", self.region, "--instance-type", "t1.micro", image, ] fd.write(" ".join(cmd)) fd.write("\n")
def register(snapshot_id, region, arch, size=None, name=None, desc=None): conn = utils.connect(region) if None in (name, size): log.debug("getting snapshot - %s", snapshot_id) snapshot = conn.get_all_snapshots(snapshot_ids=[snapshot_id])[0] size = size if size else snapshot.volume_size name = name if name else snapshot.description ec2_arch = "x86_64" if arch == "amd64" else arch log.debug("creating block_device_map") block_device_map = BlockDeviceMapping() rootfs = BlockDeviceType() rootfs.delete_on_termination = True rootfs.size = size rootfs.snapshot_id = snapshot_id rootfs_device_name = "/dev/xvda" block_device_map[rootfs_device_name] = rootfs ephemeral = BlockDeviceType() ephemeral.ephemeral_name = "ephemeral0" ephemeral_device_name = "/dev/xvdb" block_device_map[ephemeral_device_name] = ephemeral log.debug("registering image - %s", name) ami_id = conn.register_image( name=name, description=desc, architecture=ec2_arch, root_device_name=rootfs_device_name, block_device_map=block_device_map, virtualization_type="hvm", ) log.info("registered image - %s %s %s", ami_id, name, region) return ami_id, name
def register(snapshot_id, region, size=None, arch=None, name=None, desc=None): conn = utils.connect(region) log.debug('getting snapshot - %s', snapshot_id) snapshot = conn.get_all_snapshots(snapshot_ids=[snapshot_id])[0] size = size if size else snapshot.volume_size name = name if name else snapshot.description desc = desc if desc else utils.parse_imagename(name)['url'] arch = arch if arch else utils.parse_imagename(name)['architecture'] kernel_id = utils.get_kernel(region, arch) arch_ec2 = "x86_64" if arch == "amd64" else arch log.debug('creating block_device_map') rootfs = BlockDeviceType() rootfs.delete_on_termination = True rootfs.size = size rootfs.snapshot_id = snapshot_id ephemeral = BlockDeviceType() ephemeral.ephemeral_name = 'ephemeral0' block_device_map = BlockDeviceMapping() block_device_map['/dev/sda1'] = rootfs block_device_map['/dev/sda2'] = ephemeral log.debug('registering image - %s', name) ami_id = conn.register_image( name=name, description=desc, architecture=arch_ec2, kernel_id=kernel_id, root_device_name="/dev/sda1", block_device_map=block_device_map) log.info('registered image - %s %s %s', ami_id, name, region) return ami_id
def parse_block_device_args(self, block_device_maps_args): block_device_map = BlockDeviceMapping() for block_device_map_arg in block_device_maps_args: parts = block_device_map_arg.split('=') if len(parts) > 1: device_name = parts[0] block_dev_type = BlockDeviceType() value_parts = parts[1].split(':') if value_parts[0].startswith('snap'): block_dev_type.snapshot_id = value_parts[0] else: if value_parts[0].startswith('ephemeral'): block_dev_type.ephemeral_name = value_parts[0] if len(value_parts) > 1: try: block_dev_type.size = int(value_parts[1]) except ValueError: pass if len(value_parts) > 2: if value_parts[2] == 'true': block_dev_type.delete_on_termination = True block_device_map[device_name] = block_dev_type return block_device_map
def get_block_device_map(bdmapping_json=None): """Parse block_device_mapping JSON and return a configured BlockDeviceMapping object Mapping JSON structure... {"/dev/sda": {"snapshot_id": "snap-23E93E09", "volume_type": null, "delete_on_termination": true, "size": 1} } """ if bdmapping_json: mapping = json.loads(bdmapping_json) if mapping: bdm = BlockDeviceMapping() for key, val in mapping.items(): device = BlockDeviceType() if val.get('virtual_name') is not None and val.get('virtual_name').startswith('ephemeral'): device.ephemeral_name = val.get('virtual_name') else: device.volume_type = 'standard' device.snapshot_id = val.get('snapshot_id') or None device.size = val.get('size') device.delete_on_termination = val.get('delete_on_termination', False) bdm[key] = device return bdm return None return None
def register(snapshot_id, region, size=None, arch=None, name=None, desc=None): conn = utils.connect(region) log.debug('getting snapshot - %s', snapshot_id) snapshot = conn.get_all_snapshots(snapshot_ids=[snapshot_id])[0] size = size if size else snapshot.volume_size name = name if name else snapshot.description desc = desc if desc else utils.parse_imagename(name)['url'] arch = arch if arch else utils.parse_imagename(name)['architecture'] kernel_id = utils.get_kernel(region, arch) arch_ec2 = "x86_64" if arch == "amd64" else arch log.debug('creating block_device_map') rootfs = BlockDeviceType() rootfs.delete_on_termination = True rootfs.size = size rootfs.snapshot_id = snapshot_id ephemeral = BlockDeviceType() ephemeral.ephemeral_name = 'ephemeral0' block_device_map = BlockDeviceMapping() block_device_map['/dev/sda1'] = rootfs block_device_map['/dev/sda2'] = ephemeral log.debug('registering image - %s', name) ami_id = conn.register_image(name=name, description=desc, architecture=arch_ec2, kernel_id=kernel_id, root_device_name="/dev/sda1", block_device_map=block_device_map) log.info('registered image - %s %s %s', ami_id, name, region) return ami_id
def __update_bdm(self, bdm, bd_spec): """Update the BlockDeviceMapping bdm with the block device spec bd_spec. """ try: dev_name, dev_value = bd_spec.split('=', 1) except Exception: raise CommandError( "Block device spec missing '=' : %s" % (bd_spec,)) dot = None bdt = BlockDeviceType() if ':' in dev_value: blockdev_origin, dot = dev_value.split(':', 1) else: blockdev_origin = dev_value if blockdev_origin is None: raise CommandError("No source specified for %s" % (dev_name,)) if blockdev_origin.startswith('ephemeral'): bdt.ephemeral_name = blockdev_origin elif blockdev_origin.startswith('snap-'): bdt.snapshot_id = blockdev_origin else: raise CommandError("Bad source specified for %s: %s" % (dev_name, blockdev_origin)) if dot is not None: if dot == 'delete': bdt.delete_on_termination = True elif dot == 'nodelete': bdt.delete_on_termination = False else: raise CommandError( "Bad delete-on-termination specified for %s: %s" % (dev_name, dot)) else: bdt.delete_on_termination = False dev_path = '/dev/' + dev_name bdm[dev_path] = bdt
def handleImages(self, action, clc, callback=None): if action == "DescribeImages": owner = self.get_argument("Owner", None) if not owner: owners = None else: owners = [owner] filters = self.get_filter_args() return clc.get_all_images(owners, filters, callback) elif action == "DescribeImageAttribute": imageid = self.get_argument("ImageId") attribute = self.get_argument("Attribute") return clc.get_image_attribute(imageid, attribute, callback) elif action == "ModifyImageAttribute": imageid = self.get_argument("ImageId") attribute = self.get_argument("Attribute") operation = self.get_argument("OperationType") users = self.get_argument_list("UserId") groups = self.get_argument_list("UserGroup") return clc.modify_image_attribute(imageid, attribute, operation, users, groups, callback) elif action == "ResetImageAttribute": imageid = self.get_argument("ImageId") attribute = self.get_argument("Attribute") return clc.reset_image_attribute(imageid, attribute, callback) elif action == "DeregisterImage": image_id = self.get_argument("ImageId") return clc.deregister_image(image_id, callback) elif action == "RegisterImage": image_location = self.get_argument("ImageLocation", None) name = self.get_argument("Name") description = self.get_argument("Description", None) if description != None: description = base64.b64decode(description) architecture = self.get_argument("Architecture", None) kernel_id = self.get_argument("KernelId", None) ramdisk_id = self.get_argument("RamdiskId", None) root_dev_name = self.get_argument("RootDeviceName", None) snapshot_id = self.get_argument("SnapshotId", None) # get block device mappings bdm = BlockDeviceMapping() mapping = self.get_argument("BlockDeviceMapping.1.DeviceName", None) idx = 1 while mapping: pre = "BlockDeviceMapping.%d" % idx dev_name = mapping block_dev_type = BlockDeviceType() block_dev_type.ephemeral_name = self.get_argument("%s.VirtualName" % pre, None) if not (block_dev_type.ephemeral_name): block_dev_type.no_device = self.get_argument("%s.NoDevice" % pre, "") == "true" block_dev_type.snapshot_id = self.get_argument("%s.Ebs.SnapshotId" % pre, None) block_dev_type.size = self.get_argument("%s.Ebs.VolumeSize" % pre, None) block_dev_type.delete_on_termination = ( self.get_argument("%s.Ebs.DeleteOnTermination" % pre, "") == "true" ) bdm[dev_name] = block_dev_type idx += 1 mapping = self.get_argument("BlockDeviceMapping.%d.DeviceName" % idx, None) if snapshot_id: rootbdm = BlockDeviceType() rootbdm.snapshot_id = snapshot_id bdm["/dev/sda1"] = rootbdm if len(bdm) == 0: bdm = None return clc.register_image( name, image_location, description, architecture, kernel_id, ramdisk_id, root_dev_name, bdm, callback )
def handleRunInstances(self, action, clc, user_data_file, callback): image_id = self.get_argument("ImageId") min = self.get_argument("MinCount", "1") max = self.get_argument("MaxCount", "1") key = self.get_argument("KeyName", None) groups = self.get_argument_list("SecurityGroup") sec_group_ids = self.get_argument_list("SecurityGroupId") if user_data_file: user_data = user_data_file else: user_data = self.get_argument("UserData", "") user_data = base64.b64decode(user_data) addr_type = self.get_argument("AddressingType", None) vm_type = self.get_argument("InstanceType", None) placement = self.get_argument("Placement.AvailabilityZone", None) placement_group = self.get_argument("Placement.GroupName", None) tenancy = self.get_argument("Placement.Tenancy", None) kernel = self.get_argument("KernelId", None) ramdisk = self.get_argument("RamdiskId", None) monitoring = False if self.get_argument("Monitoring.Enabled", "") == "true": monitoring = True subnet = self.get_argument("SubnetId", None) private_ip = self.get_argument("PrivateIpAddress", None) # get block device mappings bdm = BlockDeviceMapping() mapping = self.get_argument("BlockDeviceMapping.1.DeviceName", None) idx = 1 while mapping: pre = "BlockDeviceMapping.%d" % idx dev_name = mapping block_dev_type = BlockDeviceType() block_dev_type.ephemeral_name = self.get_argument("%s.VirtualName" % pre, None) if not (block_dev_type.ephemeral_name): block_dev_type.no_device = self.get_argument("%s.NoDevice" % pre, "") == "true" block_dev_type.snapshot_id = self.get_argument("%s.Ebs.SnapshotId" % pre, None) block_dev_type.size = self.get_argument("%s.Ebs.VolumeSize" % pre, None) block_dev_type.delete_on_termination = ( self.get_argument("%s.Ebs.DeleteOnTermination" % pre, "") == "true" ) bdm[dev_name] = block_dev_type idx += 1 mapping = self.get_argument("BlockDeviceMapping.%d.DeviceName" % idx, None) if len(bdm) == 0: bdm = None api_termination = False if self.get_argument("DisableApiTermination", "") == "true": api_termination = True instance_shutdown = False if self.get_argument("InstanceInitiatedShutdownBehavior", "") == "true": instance_shutdown = True token = self.get_argument("ClientToken", None) addition_info = self.get_argument("AdditionInfo", None) instance_profile_name = self.get_argument("IamInstanceProfile.Name", None) instance_profile_arn = self.get_argument("IamInstanceProfile.Arn", None) return clc.run_instances( image_id, min_count=min, max_count=max, key_name=key, security_groups=groups, user_data=user_data, addressing_type=addr_type, instance_type=vm_type, placement=placement, kernel_id=kernel, ramdisk_id=ramdisk, monitoring_enabled=monitoring, subnet_id=subnet, block_device_map=bdm, disable_api_termination=api_termination, instance_initiated_shutdown_behavior=instance_shutdown, private_ip_address=private_ip, placement_group=placement_group, client_token=token, security_group_ids=sec_group_ids, additional_info=addition_info, instance_profile_name=instance_profile_name, instance_profile_arn=instance_profile_arn, tenancy=tenancy, callback=callback, )
def test_create_launch_configuration_with_block_device_mappings(): block_device_mapping = BlockDeviceMapping() ephemeral_drive = BlockDeviceType() ephemeral_drive.ephemeral_name = 'ephemeral0' block_device_mapping['/dev/xvdb'] = ephemeral_drive snapshot_drive = BlockDeviceType() snapshot_drive.snapshot_id = "snap-1234abcd" snapshot_drive.volume_type = "standard" block_device_mapping['/dev/xvdp'] = snapshot_drive ebs_drive = BlockDeviceType() ebs_drive.volume_type = "io1" ebs_drive.size = 100 ebs_drive.iops = 1000 ebs_drive.delete_on_termination = False block_device_mapping['/dev/xvdh'] = ebs_drive conn = boto.connect_autoscale(use_block_device_types=True) config = LaunchConfiguration( name='tester', image_id='ami-abcd1234', instance_type='m1.small', key_name='the_keys', security_groups=["default", "default2"], user_data=b"This is some user_data", instance_monitoring=True, instance_profile_name= 'arn:aws:iam::123456789012:instance-profile/testing', spot_price=0.1, block_device_mappings=[block_device_mapping]) conn.create_launch_configuration(config) launch_config = conn.get_all_launch_configurations()[0] launch_config.name.should.equal('tester') launch_config.image_id.should.equal('ami-abcd1234') launch_config.instance_type.should.equal('m1.small') launch_config.key_name.should.equal('the_keys') set(launch_config.security_groups).should.equal( set(['default', 'default2'])) launch_config.user_data.should.equal(b"This is some user_data") launch_config.instance_monitoring.enabled.should.equal('true') launch_config.instance_profile_name.should.equal( 'arn:aws:iam::123456789012:instance-profile/testing') launch_config.spot_price.should.equal(0.1) len(launch_config.block_device_mappings).should.equal(3) returned_mapping = launch_config.block_device_mappings set(returned_mapping.keys()).should.equal( set(['/dev/xvdb', '/dev/xvdp', '/dev/xvdh'])) returned_mapping['/dev/xvdh'].iops.should.equal(1000) returned_mapping['/dev/xvdh'].size.should.equal(100) returned_mapping['/dev/xvdh'].volume_type.should.equal("io1") returned_mapping['/dev/xvdh'].delete_on_termination.should.be.false returned_mapping['/dev/xvdp'].snapshot_id.should.equal("snap-1234abcd") returned_mapping['/dev/xvdp'].volume_type.should.equal("standard") returned_mapping['/dev/xvdb'].ephemeral_name.should.equal('ephemeral0')
def rebundle(reboot_if_needed=False): """ Rebundles the EC2 instance that is passed as the -H parameter This script handles all aspects of the rebundling process and is (almost) fully automated. Two things should be edited and provided before invoking it: AWS account information and the desired size of the root volume for the new instance. :rtype: bool :return: If instance was successfully rebundled and an AMI ID was received, return True. False, otherwise. """ _check_fabric_version() time_start = dt.datetime.utcnow() print "Rebundling instance '%s'. Start time: %s" % (env.hosts[0], time_start) _amazon_ec2_environment() if boto: # Select appropriate region: availability_zone = run("curl --silent http://169.254.169.254/latest/meta-data/placement/availability-zone") instance_region = availability_zone[:-1] # Truncate zone letter to get region name ec2_conn = _get_ec2_conn(instance_region) # hostname = env.hosts[0] # -H flag to fab command sets this variable so get only 1st hostname instance_id = run("curl --silent http://169.254.169.254/latest/meta-data/instance-id") # Get the size (in GB) of the root partition for the new image vol_size = _get_root_vol_size(ec2_conn, instance_id) # Handle reboot if required if not _reboot(instance_id, reboot_if_needed): return False # Indicates that rebundling was not completed and should be restarted _clean() # Clean up the environment before rebundling image_id = None kernel_id = run("curl --silent http://169.254.169.254/latest/meta-data/kernel-id") if ec2_conn and instance_id and availability_zone and kernel_id: print "Rebundling instance with ID '%s' in region '%s'" % (instance_id, ec2_conn.region.name) try: # Need 2 volumes - one for image (rsync) and the other for the snapshot (see instance-to-ebs-ami.sh) vol = ec2_conn.create_volume(vol_size, availability_zone) vol2 = ec2_conn.create_volume(vol_size, availability_zone) # TODO: wait until it becomes 'available' print "Created 2 new volumes of size '%s' with IDs '%s' and '%s'" % (vol_size, vol.id, vol2.id) except EC2ResponseError, e: print(red("Error creating volume: %s" % e)) return False if vol: try: # Attach newly created volumes to the instance dev_id = '/dev/sdh' if not _attach(ec2_conn, instance_id, vol.id, dev_id): print(red("Error attaching volume '%s' to the instance. Aborting." % vol.id)) return False dev_id = '/dev/sdj' if not _attach(ec2_conn, instance_id, vol2.id, dev_id): print(red("Error attaching volume '%s' to the instance. Aborting." % vol2.id)) return False # Move the file system onto the new volume (with a help of a script) url = os.path.join(REPO_ROOT_URL, "instance-to-ebs-ami.sh") # with contextlib.nested(cd('/tmp'), settings(hide('stdout', 'stderr'))): with cd('/tmp'): if exists('/tmp/'+os.path.split(url)[1]): sudo('rm /tmp/'+os.path.split(url)[1]) sudo('wget %s' % url) sudo('chmod u+x /tmp/%s' % os.path.split(url)[1]) sudo('./%s' % os.path.split(url)[1]) # Detach the new volume _detach(ec2_conn, instance_id, vol.id) _detach(ec2_conn, instance_id, vol2.id) answer = confirm("Would you like to terminate the instance used during rebundling?", default=False) if answer: ec2_conn.terminate_instances([instance_id]) # Create a snapshot of the new volume commit_num = local('cd %s; hg tip | grep changeset | cut -d: -f2' % os.getcwd()).strip() snap_id = _create_snapshot(ec2_conn, vol.id, "AMI: galaxy-cloudman (using mi-deployment at commit %s)" % commit_num) # Register the snapshot of the new volume as a machine image (i.e., AMI) arch = 'x86_64' root_device_name = '/dev/sda1' # Extra info on how EBS image registration is done: http://markmail.org/message/ofgkyecjktdhofgz # http://www.elastician.com/2009/12/creating-ebs-backed-ami-from-s3-backed.html # http://www.shlomoswidler.com/2010/01/creating-consistent-snapshots-of-live.html ebs = BlockDeviceType() ebs.snapshot_id = snap_id ebs.delete_on_termination = True ephemeral0_device_name = '/dev/sdb' ephemeral0 = BlockDeviceType() ephemeral0.ephemeral_name = 'ephemeral0' ephemeral1_device_name = '/dev/sdc' ephemeral1 = BlockDeviceType() ephemeral1.ephemeral_name = 'ephemeral1' # ephemeral2_device_name = '/dev/sdd' # Needed for instances w/ 3 ephemeral disks # ephemeral2 = BlockDeviceType() # ephemeral2.ephemeral_name = 'ephemeral2' # ephemeral3_device_name = '/dev/sde' # Needed for instances w/ 4 ephemeral disks # ephemeral3 = BlockDeviceType() # ephemeral3.ephemeral_name = 'ephemeral3' block_map = BlockDeviceMapping() block_map[root_device_name] = ebs block_map[ephemeral0_device_name] = ephemeral0 block_map[ephemeral1_device_name] = ephemeral1 print(yellow('galaxy-cloudman-%s' % time_start.strftime("%Y-%m-%d"))) name, desc = _get_image_name() image_id = ec2_conn.register_image(name, description=desc, architecture=arch, kernel_id=kernel_id, root_device_name=root_device_name, block_device_map=block_map) answer = confirm("Volume with ID '%s' was created and used to make this AMI but is not longer needed. Would you like to delete it?" % vol.id) if answer: ec2_conn.delete_volume(vol.id) print "Deleting the volume (%s) used for rsync only" % vol2.id ec2_conn.delete_volume(vol2.id) print(green("--------------------------")) print(green("Finished creating new machine image. Image ID: '%s'" % (image_id))) print(green("--------------------------")) answer = confirm("Would you like to make this machine image public?", default=False) if image_id and answer: ec2_conn.modify_image_attribute(image_id, attribute='launchPermission', operation='add', groups=['all']) except EC2ResponseError, e: print(red("Error creating image: %s" % e)) return False else: print(red("Error creating new volume")) return False
def vm(install_list): region = install_list["region"] subnet_id = install_list["subnet_id"] ami_id = install_list["ami_id"] snap_id = install_list["snap_id"] volume_capacity = install_list["volume_capacity"] key_name = install_list["key_name"] instance_type = install_list["instance_type"] sg_id = install_list["sg_id"] user_data = install_list["user_data"] usage = install_list["usage"] block_device_map = BlockDeviceMapping() block_dev_type = BlockDeviceType() block_dev_type.snapshot_id = snap_id block_dev_type.delete_on_termination = False block_dev_type.size = volume_capacity block_device_map['/dev/sdb'] = block_dev_type if region not in EXTRA_INFO: aws_access_key_id = EXTRA_INFO["common"]["aws_access_key_id"] aws_secret_access_key = EXTRA_INFO["common"]["aws_secret_access_key"] name_key = EXTRA_INFO["common"]["name_key"] else: aws_access_key_id = EXTRA_INFO[region]["aws_access_key_id"] aws_secret_access_key = EXTRA_INFO[region]["aws_secret_access_key"] name_key = EXTRA_INFO[region]["name_key"] ret = asset_hostname.get(region, usage) hostname = ret["hostname"].split(".")[0] user_data = user_data.replace("region=","region=%s" % region) user_data = user_data.replace("hostname=","hostname=%s" % hostname) user_data = user_data.replace("dns_vip=","dns_vip=%s" % \ DNS_INFO[region]["dns_vip"]) user_data = user_data.replace("ns_servers=","ns_servers='%s'" % \ " ".join(DNS_INFO[region]["ns_servers"]) ) network_interface = NetworkInterfaceSpecification(subnet_id=subnet_id, \ groups=[sg_id]) network_interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(\ network_interface) conn = boto.ec2.connect_to_region(region, \ aws_access_key_id=aws_access_key_id, \ aws_secret_access_key=aws_secret_access_key) reservation = conn.run_instances(ami_id, key_name=key_name, network_interfaces=network_interfaces, instance_type=instance_type, block_device_map=block_device_map, # security_groups=[security_groups], min_count=1, max_count=1, user_data=user_data, ) instance = reservation.instances[0] time_init = 0 time_total = 300 time_interval = 5 while time_init < time_total: status = instance.update() if status == 'running': instance.add_tag(u"名称", hostname) instance.add_tag("Name", hostname) break else: time.sleep(time_interval) time_init += time_interval install_list["instance_id"] = str(instance).split(":")[-1] install_list["placement"] = instance.placement install_list["status"] = instance.update() install_list["hostname"] = hostname return install_list
def test_create_launch_configuration_with_block_device_mappings(): block_device_mapping = BlockDeviceMapping() ephemeral_drive = BlockDeviceType() ephemeral_drive.ephemeral_name = "ephemeral0" block_device_mapping["/dev/xvdb"] = ephemeral_drive snapshot_drive = BlockDeviceType() snapshot_drive.snapshot_id = "snap-1234abcd" snapshot_drive.volume_type = "standard" block_device_mapping["/dev/xvdp"] = snapshot_drive ebs_drive = BlockDeviceType() ebs_drive.volume_type = "io1" ebs_drive.size = 100 ebs_drive.iops = 1000 ebs_drive.delete_on_termination = False block_device_mapping["/dev/xvdh"] = ebs_drive conn = boto.connect_autoscale(use_block_device_types=True) config = LaunchConfiguration( name="tester", image_id="ami-abcd1234", instance_type="m1.small", key_name="the_keys", security_groups=["default", "default2"], user_data=b"This is some user_data", instance_monitoring=True, instance_profile_name="arn:aws:iam::{}:instance-profile/testing".format( ACCOUNT_ID ), spot_price=0.1, block_device_mappings=[block_device_mapping], ) conn.create_launch_configuration(config) launch_config = conn.get_all_launch_configurations()[0] launch_config.name.should.equal("tester") launch_config.image_id.should.equal("ami-abcd1234") launch_config.instance_type.should.equal("m1.small") launch_config.key_name.should.equal("the_keys") set(launch_config.security_groups).should.equal(set(["default", "default2"])) launch_config.user_data.should.equal(b"This is some user_data") launch_config.instance_monitoring.enabled.should.equal("true") launch_config.instance_profile_name.should.equal( "arn:aws:iam::{}:instance-profile/testing".format(ACCOUNT_ID) ) launch_config.spot_price.should.equal(0.1) len(launch_config.block_device_mappings).should.equal(3) returned_mapping = launch_config.block_device_mappings set(returned_mapping.keys()).should.equal( set(["/dev/xvdb", "/dev/xvdp", "/dev/xvdh"]) ) returned_mapping["/dev/xvdh"].iops.should.equal(1000) returned_mapping["/dev/xvdh"].size.should.equal(100) returned_mapping["/dev/xvdh"].volume_type.should.equal("io1") returned_mapping["/dev/xvdh"].delete_on_termination.should.be.false returned_mapping["/dev/xvdp"].snapshot_id.should.equal("snap-1234abcd") returned_mapping["/dev/xvdp"].volume_type.should.equal("standard") returned_mapping["/dev/xvdb"].ephemeral_name.should.equal("ephemeral0")
def handleImages(self, action, clc): if action == 'DescribeImages': owner = self.get_argument('Owner', None) if not owner: owners = None else: owners = [owner] return clc.get_all_images(owners) elif action == 'DescribeImageAttribute': imageid = self.get_argument('ImageId') attribute = self.get_argument('Attribute') return clc.get_image_attribute(imageid, attribute) elif action == 'ModifyImageAttribute': imageid = self.get_argument('ImageId') attribute = self.get_argument('Attribute') operation = self.get_argument('OperationType') users = self.get_argument_list('UserId') groups = self.get_argument_list('UserGroup') return clc.modify_image_attribute(imageid, attribute, operation, users, groups) elif action == 'ResetImageAttribute': imageid = self.get_argument('ImageId') attribute = self.get_argument('Attribute') return clc.reset_image_attribute(imageid, attribute) elif action == 'RegisterImage': image_location = self.get_argument('ImageLocation', None) name = self.get_argument('Name') description = self.get_argument('Description', None) description = base64.b64decode(description) architecture = self.get_argument('Architecture', None) kernel_id = self.get_argument('KernelId', None) ramdisk_id = self.get_argument('RamdiskId', None) root_dev_name = self.get_argument('RootDeviceName', None) snapshot_id = self.get_argument('SnapshotId', None) # get block device mappings bdm = BlockDeviceMapping() mapping = self.get_argument('BlockDeviceMapping.1.DeviceName', None) idx = 1 while mapping: pre = 'BlockDeviceMapping.%d' % idx dev_name = mapping block_dev_type = BlockDeviceType() block_dev_type.ephemeral_name = self.get_argument( '%s.VirtualName' % pre, None) if not (block_dev_type.ephemeral_name): block_dev_type.no_device = \ (self.get_argument('%s.NoDevice' % pre, '') == 'true') block_dev_type.snapshot_id = \ self.get_argument('%s.Ebs.SnapshotId' % pre, None) block_dev_type.size = \ self.get_argument('%s.Ebs.VolumeSize' % pre, None) block_dev_type.delete_on_termination = \ (self.get_argument('%s.DeleteOnTermination' % pre, '') == 'true') bdm[dev_name] = block_dev_type idx += 1 mapping = self.get_argument( 'BlockDeviceMapping.%d.DeviceName' % idx, None) if snapshot_id: rootbdm = BlockDeviceType() rootbdm.snapshot_id = snapshot_id bdm['/dev/sda1'] = rootbdm if len(bdm) == 0: bdm = None return clc.register_image(name, image_location, description, architecture, kernel_id, ramdisk_id, root_dev_name, bdm)
def handleRunInstances(self, action, clc, user_data_file): image_id = self.get_argument('ImageId') min = self.get_argument('MinCount', '1') max = self.get_argument('MaxCount', '1') key = self.get_argument('KeyName', None) groups = self.get_argument_list('SecurityGroup') sec_group_ids = self.get_argument_list('SecurityGroupId') if user_data_file: user_data = user_data_file else: user_data = self.get_argument('UserData', None) addr_type = self.get_argument('AddressingType', None) vm_type = self.get_argument('InstanceType', None) placement = self.get_argument('Placement.AvailabilityZone', None) placement_group = self.get_argument('Placement.GroupName', None) tenancy = self.get_argument('Placement.Tenancy', None) kernel = self.get_argument('KernelId', None) ramdisk = self.get_argument('RamdiskId', None) monitoring = False if self.get_argument('Monitoring.Enabled', '') == 'true': monitoring = True subnet = self.get_argument('SubnetId', None) private_ip = self.get_argument('PrivateIpAddress', None) # get block device mappings bdm = BlockDeviceMapping() mapping = self.get_argument('BlockDeviceMapping.1.DeviceName', None) idx = 1 while mapping: pre = 'BlockDeviceMapping.%d' % idx dev_name = mapping block_dev_type = BlockDeviceType() block_dev_type.ephemeral_name = self.get_argument( '%s.VirtualName' % pre, None) if not (block_dev_type.ephemeral_name): block_dev_type.no_device = \ (self.get_argument('%s.NoDevice' % pre, '') == 'true') block_dev_type.snapshot_id = \ self.get_argument('%s.Ebs.SnapshotId' % pre, None) block_dev_type.size = \ self.get_argument('%s.Ebs.VolumeSize' % pre, None) block_dev_type.delete_on_termination = \ (self.get_argument('%s.DeleteOnTermination' % pre, '') == 'true') bdm[dev_name] = block_dev_type idx += 1 mapping = self.get_argument( 'BlockDeviceMapping.%d.DeviceName' % idx, None) if len(bdm) == 0: bdm = None api_termination = False if self.get_argument('DisableApiTermination', '') == 'true': api_termination = True instance_shutdown = False if self.get_argument('InstanceInitiatedShutdownBehavior', '') == 'true': instance_shutdown = True token = self.get_argument('ClientToken', None) addition_info = self.get_argument('AdditionInfo', None) instance_profile_name = self.get_argument('IamInstanceProfile.Name', None) instance_profile_arn = self.get_argument('IamInstanceProfile.Arn', None) return self.__normalize_instances__([ clc.run_instances( image_id, min_count=min, max_count=max, key_name=key, security_groups=groups, user_data=user_data, addressing_type=addr_type, instance_type=vm_type, placement=placement, kernel_id=kernel, ramdisk_id=ramdisk, monitoring_enabled=monitoring, subnet_id=subnet, block_device_map=bdm, disable_api_termination=api_termination, instance_initiated_shutdown_behavior=instance_shutdown, private_ip_address=private_ip, placement_group=placement_group, client_token=token, security_group_ids=sec_group_ids, additional_info=addition_info, instance_profile_name=instance_profile_name, instance_profile_arn=instance_profile_arn, tenancy=tenancy) ])
def handleRunInstances(self, action, clc, user_data_file, callback): image_id = self.get_argument('ImageId') min = self.get_argument('MinCount', '1') max = self.get_argument('MaxCount', '1') key = self.get_argument('KeyName', None) groups = self.get_argument_list('SecurityGroup') sec_group_ids = self.get_argument_list('SecurityGroupId') if user_data_file: user_data = user_data_file else: user_data = self.get_argument('UserData', "") user_data = base64.b64decode(user_data) addr_type = self.get_argument('AddressingType', None) vm_type = self.get_argument('InstanceType', None) placement = self.get_argument('Placement.AvailabilityZone', None) placement_group = self.get_argument('Placement.GroupName', None) tenancy = self.get_argument('Placement.Tenancy', None) kernel = self.get_argument('KernelId', None) ramdisk = self.get_argument('RamdiskId', None) monitoring=False if self.get_argument('Monitoring.Enabled', '') == 'true': monitoring=True subnet = self.get_argument('SubnetId', None); private_ip = self.get_argument('PrivateIpAddress', None); # get block device mappings bdm = BlockDeviceMapping() mapping = self.get_argument('BlockDeviceMapping.1.DeviceName', None) idx = 1 while mapping: pre = 'BlockDeviceMapping.%d' % idx dev_name = mapping block_dev_type = BlockDeviceType() block_dev_type.ephemeral_name = self.get_argument('%s.VirtualName' % pre, None) if not(block_dev_type.ephemeral_name): block_dev_type.no_device = \ (self.get_argument('%s.NoDevice' % pre, '') == 'true') block_dev_type.snapshot_id = \ self.get_argument('%s.Ebs.SnapshotId' % pre, None) block_dev_type.size = \ self.get_argument('%s.Ebs.VolumeSize' % pre, None) block_dev_type.delete_on_termination = \ (self.get_argument('%s.Ebs.DeleteOnTermination' % pre, '') == 'true') bdm[dev_name] = block_dev_type idx += 1 mapping = self.get_argument('BlockDeviceMapping.%d.DeviceName' % idx, None) if len(bdm) == 0: bdm = None api_termination=False if self.get_argument('DisableApiTermination', '') == 'true': api_termination=True instance_shutdown=False if self.get_argument('InstanceInitiatedShutdownBehavior', '') == 'true': instance_shutdown=True token = self.get_argument('ClientToken', None); addition_info = self.get_argument('AdditionInfo', None); instance_profile_name = self.get_argument('IamInstanceProfile.Name', None); instance_profile_arn = self.get_argument('IamInstanceProfile.Arn', None); return clc.run_instances(image_id, min_count=min, max_count=max, key_name=key, security_groups=groups, user_data=user_data, addressing_type=addr_type, instance_type=vm_type, placement=placement, kernel_id=kernel, ramdisk_id=ramdisk, monitoring_enabled=monitoring, subnet_id=subnet, block_device_map=bdm, disable_api_termination=api_termination, instance_initiated_shutdown_behavior=instance_shutdown, private_ip_address=private_ip, placement_group=placement_group, client_token=token, security_group_ids=sec_group_ids, additional_info=addition_info, instance_profile_name=instance_profile_name, instance_profile_arn=instance_profile_arn, tenancy=tenancy, callback=callback)
def handleImages(self, action, clc, callback=None): if action == 'DescribeImages': owner = self.get_argument('Owner', None); if not owner: owners = None else: owners = [owner] filters = self.get_filter_args() return clc.get_all_images(owners, filters, callback) elif action == 'DescribeImageAttribute': imageid = self.get_argument('ImageId') attribute = self.get_argument('Attribute') return clc.get_image_attribute(imageid, attribute, callback) elif action == 'ModifyImageAttribute': imageid = self.get_argument('ImageId') attribute = self.get_argument('Attribute') operation = self.get_argument('OperationType') users = self.get_argument_list('UserId') groups = self.get_argument_list('UserGroup') return clc.modify_image_attribute(imageid, attribute, operation, users, groups, callback) elif action == 'ResetImageAttribute': imageid = self.get_argument('ImageId') attribute = self.get_argument('Attribute') return clc.reset_image_attribute(imageid, attribute, callback) elif action == 'DeregisterImage': image_id = self.get_argument('ImageId') return clc.deregister_image(image_id, callback) elif action == 'RegisterImage': image_location = self.get_argument('ImageLocation', None) name = self.get_argument('Name') description = self.get_argument('Description', None) if description != None: description = base64.b64decode(description); architecture = self.get_argument('Architecture', None) kernel_id = self.get_argument('KernelId', None) ramdisk_id = self.get_argument('RamdiskId', None) root_dev_name = self.get_argument('RootDeviceName', None) snapshot_id = self.get_argument('SnapshotId', None) # get block device mappings bdm = BlockDeviceMapping() mapping = self.get_argument('BlockDeviceMapping.1.DeviceName', None) idx = 1 while mapping: pre = 'BlockDeviceMapping.%d' % idx dev_name = mapping block_dev_type = BlockDeviceType() block_dev_type.ephemeral_name = self.get_argument('%s.VirtualName' % pre, None) if not(block_dev_type.ephemeral_name): block_dev_type.no_device = \ (self.get_argument('%s.NoDevice' % pre, '') == 'true') block_dev_type.snapshot_id = \ self.get_argument('%s.Ebs.SnapshotId' % pre, None) block_dev_type.size = \ self.get_argument('%s.Ebs.VolumeSize' % pre, None) block_dev_type.delete_on_termination = \ (self.get_argument('%s.Ebs.DeleteOnTermination' % pre, '') == 'true') bdm[dev_name] = block_dev_type idx += 1 mapping = self.get_argument('BlockDeviceMapping.%d.DeviceName' % idx, None) if snapshot_id: rootbdm = BlockDeviceType() rootbdm.snapshot_id = snapshot_id bdm['/dev/sda1'] = rootbdm if len(bdm) == 0: bdm = None return clc.register_image(name, image_location, description, architecture, kernel_id, ramdisk_id, root_dev_name, bdm, callback)
def register(snapshot_id, region, arch, size=None, name=None, desc=None, pvm=False): conn = utils.connect(region) if None in (name, size): log.debug('getting snapshot - %s', snapshot_id) snapshot = conn.get_all_snapshots(snapshot_ids=[snapshot_id])[0] size = size if size else snapshot.volume_size name = name if name else snapshot.description virt = 'hvm' kernel_id = None device_base = '/dev/xvd' ec2_arch = "x86_64" if arch == "amd64" else arch if pvm: kernel_id = utils.get_kernel(region, arch) virt = 'paravirtual' device_base = '/dev/sd' name += '-pvm' log.debug('creating block_device_map') block_device_map = BlockDeviceMapping() rootfs = BlockDeviceType() rootfs.delete_on_termination = True rootfs.size = size rootfs.snapshot_id = snapshot_id rootfs_device_name = device_base + 'a' block_device_map[rootfs_device_name] = rootfs ephemeral = BlockDeviceType() ephemeral.ephemeral_name = 'ephemeral0' ephemeral_device_name = device_base + 'b' block_device_map[ephemeral_device_name] = ephemeral log.debug('registering image - %s', name) client3 = utils.connect_boto3(region) response = client3.register_image(Name=name, Architecture=ec2_arch, RootDeviceName=rootfs_device_name, BlockDeviceMappings=[{ 'DeviceName': '/dev/xvda', 'Ebs': { 'DeleteOnTermination': True, 'VolumeSize': size, 'SnapshotId': snapshot_id, }, }, { 'DeviceName': '/dev/xvdb', 'VirtualName': 'ephemeral0', }], VirtualizationType=virt, EnaSupport=True) ami_id = response['ImageId'] log.info('registered image - %s %s %s', ami_id, name, region) return ami_id, name