def create_image(args): AWSACCID = _getawsaccid() conn = boto.ec2.connect_to_region(args.region, aws_access_key_id=AWSAKEY, aws_secret_access_key=AWSSKEY) if args.snapshotid == "" or args.snapshotid is None: print 'You have to pass the snapshot ID used to create the image with --snapshotid="snapid"' raise SystemExit(1) else: namei = raw_input("Enter name of image: ") descr = raw_input("Enter a description for image: ") print "Creating image from snapshot %s ..." % args.snapshotid ebs = EBSBlockDeviceType() ebs.snapshot_id = args.snapshotid block_map = BlockDeviceMapping() block_map['/dev/sda1'] = ebs try: if args.region == "eu-west-1": ret = conn.register_image(name=namei,description=descr,architecture='x86_64',kernel_id='aki-71665e05',\ root_device_name='/dev/sda1', block_device_map=block_map) else: ret = conn.register_image(name=namei,description=descr,architecture='x86_64',kernel_id='aki-b6aa75df',\ root_device_name='/dev/sda1', block_device_map=block_map) print "Image creation successful" except EC2ResponseError: print "Image creation error"
def register_ebs_ami(self, snapshot_id, arch = 'x86_64', default_ephem_map = True, img_name = None, img_desc = None): # register against snapshot try: aki=PVGRUB_AKIS[self.region.name][arch] except KeyError: raise Exception("Unable to determine pvgrub hd00 AKI for region (%s) arch (%s)" % (self.region.name, arch)) if not img_name: rand_id = random.randrange(2**32) # These names need to be unique, hence the pseudo-uuid img_name='EBSHelper AMI - %s - uuid-%x' % (snapshot_id, rand_id) if not img_desc: img_desc='Created directly from volume snapshot %s' % (snapshot_id) self.log.debug("Registering snapshot (%s) as new EBS AMI" % (snapshot_id)) ebs = EBSBlockDeviceType() ebs.snapshot_id = snapshot_id ebs.delete_on_termination = True block_map = BlockDeviceMapping() block_map['/dev/sda'] = ebs # The ephemeral mappings are automatic with S3 images # For EBS images we need to make them explicit # These settings are required to make the same fstab work on both S3 and EBS images if default_ephem_map: e0 = EBSBlockDeviceType() e0.ephemeral_name = 'ephemeral0' e1 = EBSBlockDeviceType() e1.ephemeral_name = 'ephemeral1' block_map['/dev/sdb'] = e0 block_map['/dev/sdc'] = e1 result = self.conn.register_image(name=img_name, description=img_desc, architecture=arch, kernel_id=aki, root_device_name='/dev/sda', block_device_map=block_map) return str(result)
def register_snap(self, snap_id, arch, name, aki=None, desc=None, ari=None, pub=True, disk=False): """ Register an EBS volume snapshot as an AMI. Returns the AMI ID. An arch, snapshot ID, and name for the AMI must be provided. Optionally a description, AKI ID, ARI ID and billing code may be specified too. disk is whether or not we are registering a disk image. """ self.logger.info('Registering snap: %s' % (snap_id)) snap = self.conn.get_all_snapshots([snap_id])[0] #Makes block device map ebs = EBSBlockDeviceType() ebs.snapshot_id = snap_id block_map = BlockDeviceMapping() if aki == None: raise Fedora_EC2Error('Need to specify an AKI') if disk: disk = '/dev/sda=%s' % snap_id root = '/dev/sda' else: disk = '/dev/sda1=%s' % snap_id root = '/dev/sda1' block_map[root] = ebs ami_id = self.conn.register_image(name=name, description=desc, image_location = '', architecture=arch, kernel_id=aki, ramdisk_id=ari,root_device_name=root, block_device_map=block_map) if not ami_id.startswith('ami-'): self._log_error('Could not register an AMI') self.logger.info('Registered an AMI: %s' % ami_id) return ami_id
def register_ebs_ami(self, snapshot_id, arch="x86_64", default_ephem_map=True, img_name=None, img_desc=None): # register against snapshot try: aki = PVGRUB_AKIS[self.region.name][arch] except KeyError: raise Exception("Unable to find pvgrub hd00 AKI for %s, arch (%s)" % (self.region.name, arch)) if not img_name: rand_id = random.randrange(2 ** 32) # These names need to be unique, hence the pseudo-uuid img_name = "EBSHelper AMI - %s - uuid-%x" % (snapshot_id, rand_id) if not img_desc: img_desc = "Created directly from volume snapshot %s" % snapshot_id self.log.debug("Registering %s as new EBS AMI" % snapshot_id) self.create_sgroup("ec2helper-vnc-ssh-%x" % random.randrange(2 ** 32), allow_vnc=True) ebs = EBSBlockDeviceType() ebs.snapshot_id = snapshot_id ebs.delete_on_termination = True block_map = BlockDeviceMapping() block_map["/dev/sda"] = ebs # The ephemeral mappings are automatic with S3 images # For EBS images we need to make them explicit # These settings are required to make the same fstab work on both S3 # and EBS images if default_ephem_map: e0 = EBSBlockDeviceType() e0.ephemeral_name = "ephemeral0" e1 = EBSBlockDeviceType() e1.ephemeral_name = "ephemeral1" block_map["/dev/sdb"] = e0 block_map["/dev/sdc"] = e1 result = self.conn.register_image( name=img_name, description=img_desc, architecture=arch, kernel_id=aki, root_device_name="/dev/sda", block_device_map=block_map, ) sleep(10) new_amis = self.conn.get_all_images([result]) new_amis[0].add_tag("Name", resource_tag) return str(result)
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 = EBSBlockDeviceType() 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: block_dev_type.size = int(value_parts[1]) 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 create_image(args): AWSACCID = _getawsaccid() conn = boto.ec2.connect_to_region(args.region,aws_access_key_id=AWSAKEY,aws_secret_access_key=AWSSKEY) if args.snapshotid == "" or args.snapshotid is None: print 'You have to pass the snapshot ID used to create the image with --snapshotid="snapid"' raise SystemExit(1) else: namei = raw_input("Enter name of image: ") descr = raw_input("Enter a description for image: ") vtype = raw_input("Enter a virtualization type for image:[hvm|paravirtual] ") print "Creating image from snapshot %s ..." % args.snapshotid ebs = EBSBlockDeviceType() ebs.snapshot_id = args.snapshotid block_map = BlockDeviceMapping() block_map['/dev/sda1'] = ebs print vtype try: if args.region == "eu-west-1": if vtype == "hvm": #ret = conn.register_image(name=namei,description=descr,architecture='x86_64',kernel_id='aki-71665e05',\ # root_device_name='/dev/sda1', block_device_map=block_map, virtualization_type='hvm') ret = conn.register_image(name=namei,description=descr,architecture='x86_64',\ root_device_name='/dev/sda1', block_device_map=block_map, virtualization_type='hvm') else: ret = conn.register_image(name=namei,description=descr,architecture='x86_64',kernel_id='aki-71665e05',\ root_device_name='/dev/sda1', block_device_map=block_map, virtualization_type='paravirtual') else: if vtype == "hvm": ret = conn.register_image(name=namei,description=descr,architecture='x86_64',kernel_id='aki-b6aa75df',\ root_device_name='/dev/sda1', block_device_map=block_map, virtualization_type='hvm') else: ret = conn.register_image(name=namei,description=descr,architecture='x86_64',kernel_id='aki-b6aa75df',\ root_device_name='/dev/sda1', block_device_map=block_map, virtualization_type='paravirtual') print "Image creation successful" except EC2ResponseError: print "Image creation error"
def make_ebs_based_image(ami_name, docker_image_name, fstype='ext3', mount_point=None, desc='', arch='x86_64', kernel=None, disk_size=10240): '''size of ebs is passed in Mb''' availability_zone = boto.utils.get_instance_metadata()[ 'placement']['availability-zone'] instance_id = boto.utils.get_instance_metadata()['instance_id'] # needs a ec2 connection here conn = boto.ec2.connection.EC2Connection() vol = conn.create_volume(disk_size, availability_zone) devpath = random.choice( [devp for devp in map(lambda x: '/dev/sd%s' % x, string.ascii_lowercase) if not os.path.exists(devp)] ) vol.attach(instance_id, devpath) run('/sbin/mkfs -t %s %s' % (fstype, devpath)) if mount_point is None: mount_point = tempfile.mkdtemp('ebs-based-mount-point') # copy files cid = dockerc.create_container( image=docker_image_name, command='/bin/bash', tty=True, volume=['dev'] ) export_fileobj = dockerc.export(cid) run('/bin/mount -t %s %s %s' % (fstype, devpath, mount_point)) try: with tempfile.TemporaryFile() as fp: data = export_fileobj.read(2048) while data: fp.write(data) data = export_fileobj.read(2048) else: # if it goes well, seek to 0 fp.seek(0) tar = tarfile.open(fileobj=fp) os.chdir(mount_point) tar.extractall() tar.close() os.chdir(os.pardir) finally: run('/bin/umount %s' % mount_point) vol.detach() snapshot = vol.create_snapshot('initial snapshot for ebs') ebs = EBSBlockDeviceType() ebs.snapshot_id = snapshot.id block_map = BlockDeviceMapping() block_map['/dev/sda1'] = ebs ami = conn.register_image( ami_name, description=desc, architecture=arch, kernel_id=kernel, root_device_name='dev/sda1', block_device_map=block_map ) log.info('ebs-based ami: %s' % ami.id)
def create_ami(region, snap_id, force=None, root_dev='/dev/sda1', zone_name=None, default_arch=None, default_type='t1.micro', security_groups=''): """ Creates AMI image from given snapshot. Force option removes prompt request and creates new instance from created ami image. region, snap_id specify snapshot to be processed. Snapshot description in json format will be used to restore instance with same parameters. Will automaticaly process snapshots for same instance with near time (10 minutes or shorter), but for other devices (/dev/sdb, /dev/sdc, etc); force Run instance from ami after creation without confirmation. To enable set value to "RUN"; default_arch architecture to use if not mentioned in snapshot description; default_type instance type to use if not mentioned in snapshot description. Used only if ``force`` is "RUN"; security_groups list of AWS Security Groups names formatted as string separated with semicolon ';'. Used only if ``force`` is "RUN". """ conn = get_region_conn(region) snap = conn.get_all_snapshots(snapshot_ids=[snap_id, ])[0] instance_id = get_snap_instance(snap) _device = get_snap_device(snap) snaps = conn.get_all_snapshots(owner='self') snapshots = [snp for snp in snaps if get_snap_instance(snp) == instance_id and get_snap_device(snp) != _device and abs(get_snap_time(snap) - get_snap_time(snp)) <= timedelta(minutes=10)] snapshot = sorted(snapshots, key=get_snap_time, reverse=True) if snapshots else None # setup for building an EBS boot snapshot default_arch = default_arch or config.get('DEFAULT', 'ARCHITECTURE') arch = get_descr_attr(snap, 'Arch') or default_arch kernel = config.get(conn.region.name, 'KERNEL' + arch.upper()) dev = re.match(r'^/dev/sda$', _device) # if our instance encrypted if dev: kernel = config.get(conn.region.name, 'KERNEL_ENCR_' + arch.upper()) ebs = EBSBlockDeviceType() ebs.snapshot_id = snap_id ebs.delete_on_termination = True block_map = BlockDeviceMapping() block_map[_device] = ebs sdb = BlockDeviceType() sdb.ephemeral_name = 'ephemeral0' block_map['/dev/sdb'] = sdb if snapshot: for s in snapshot: s_dev = get_snap_device(s) s_ebs = EBSBlockDeviceType() s_ebs.delete_on_termination = True s_ebs.snapshot_id = s.id block_map[s_dev] = s_ebs name = 'Created {0} using access key {1}'.format(timestamp(), conn.access_key) name = name.replace(":", ".").replace(" ", "_") # create the new AMI all options from snap JSON description: wait_for(snap, '100%', limit=SNAP_TIME) result = conn.register_image( name=name, description=snap.description, architecture=get_descr_attr(snap, 'Arch') or default_arch, root_device_name=get_descr_attr(snap, 'Root_dev_name') or root_dev, block_device_map=block_map, kernel_id=kernel) sleep(2) image = conn.get_all_images(image_ids=[result, ])[0] wait_for(image, 'available', limit=10 * 60) add_tags(image, snap.tags) logger.info('The new AMI ID = {0}'.format(result)) new_instance = None if force == 'RUN': instance_type = get_descr_attr(snap, 'Type') or default_type new_instance = launch_instance_from_ami( region, image.id, inst_type=instance_type, security_groups=security_groups, zone_name=zone_name) return image, new_instance
def create_ami(region, snap_id, force=None, root_dev='/dev/sda1', zone_name=None, default_arch=None, default_type='t1.micro', security_groups=''): """ Creates AMI image from given snapshot. Force option removes prompt request and creates new instance from created ami image. region, snap_id specify snapshot to be processed. Snapshot description in json format will be used to restore instance with same parameters. Will automaticaly process snapshots for same instance with near time (10 minutes or shorter), but for other devices (/dev/sdb, /dev/sdc, etc); force Run instance from ami after creation without confirmation. To enable set value to "RUN"; default_arch architecture to use if not mentioned in snapshot description; default_type instance type to use if not mentioned in snapshot description. Used only if ``force`` is "RUN"; security_groups list of AWS Security Groups names formatted as string separated with semicolon ';'. Used only if ``force`` is "RUN". """ conn = get_region_conn(region) snap = conn.get_all_snapshots(snapshot_ids=[ snap_id, ])[0] instance_id = get_snap_instance(snap) _device = get_snap_device(snap) snaps = conn.get_all_snapshots(owner='self') snapshots = [ snp for snp in snaps if get_snap_instance(snp) == instance_id and get_snap_device(snp) != _device and abs(get_snap_time(snap) - get_snap_time(snp)) <= timedelta(minutes=10) ] snapshot = sorted(snapshots, key=get_snap_time, reverse=True) if snapshots else None # setup for building an EBS boot snapshot default_arch = default_arch or config.get('DEFAULT', 'ARCHITECTURE') arch = get_descr_attr(snap, 'Arch') or default_arch kernel = config.get(conn.region.name, 'KERNEL' + arch.upper()) dev = re.match(r'^/dev/sda$', _device) # if our instance encrypted if dev: kernel = config.get(conn.region.name, 'KERNEL_ENCR_' + arch.upper()) ebs = EBSBlockDeviceType() ebs.snapshot_id = snap_id ebs.delete_on_termination = True block_map = BlockDeviceMapping() block_map[_device] = ebs sdb = BlockDeviceType() sdb.ephemeral_name = 'ephemeral0' block_map['/dev/sdb'] = sdb if snapshot: for s in snapshot: s_dev = get_snap_device(s) s_ebs = EBSBlockDeviceType() s_ebs.delete_on_termination = True s_ebs.snapshot_id = s.id block_map[s_dev] = s_ebs name = 'Created {0} using access key {1}'.format(timestamp(), conn.access_key) name = name.replace(":", ".").replace(" ", "_") # create the new AMI all options from snap JSON description: wait_for(snap, '100%', limit=SNAP_TIME) result = conn.register_image( name=name, description=snap.description, architecture=get_descr_attr(snap, 'Arch') or default_arch, root_device_name=get_descr_attr(snap, 'Root_dev_name') or root_dev, block_device_map=block_map, kernel_id=kernel) sleep(2) image = conn.get_all_images(image_ids=[ result, ])[0] wait_for(image, 'available', limit=10 * 60) add_tags(image, snap.tags) logger.info('The new AMI ID = {0}'.format(result)) info = ('\nEnter RUN if you want to launch instance using ' 'just created {0}: '.format(image)) new_instance = None if force == 'RUN' or raw_input(info).strip() == 'RUN': instance_type = get_descr_attr(snap, 'Type') or default_type new_instance = launch_instance_from_ami( region, image.id, inst_type=instance_type, security_groups=security_groups, zone_name=zone_name) return image, new_instance