Exemplo n.º 1
0
    def _getBlockDeviceMapping(cls, instanceType, rootVolSize=50):
        # determine number of ephemeral drives via cgcloud-lib (actually this is moved into toil's lib
        bdtKeys = [''] + ['/dev/xvd{}'.format(c) for c in string.ascii_lowercase[1:]]
        bdm = BlockDeviceMapping()
        # Change root volume size to allow for bigger Docker instances
        root_vol = BlockDeviceType(delete_on_termination=True)
        root_vol.size = rootVolSize
        bdm["/dev/xvda"] = root_vol
        # The first disk is already attached for us so start with 2nd.
        # Disk count is weirdly a float in our instance database, so make it an int here.
        for disk in range(1, int(instanceType.disks) + 1):
            bdm[bdtKeys[disk]] = BlockDeviceType(
                ephemeral_name='ephemeral{}'.format(disk - 1))  # ephemeral counts start at 0

        logger.debug('Device mapping: %s', bdm)
        return bdm
Exemplo n.º 2
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
Exemplo n.º 3
0
    def _getBlockDeviceMapping(cls, instanceType, rootVolSize=50):
        # determine number of ephemeral drives via cgcloud-lib
        bdtKeys = ['', '/dev/xvdb', '/dev/xvdc', '/dev/xvdd']
        bdm = BlockDeviceMapping()
        # Change root volume size to allow for bigger Docker instances
        root_vol = BlockDeviceType(delete_on_termination=True)
        root_vol.size = rootVolSize
        bdm["/dev/xvda"] = root_vol
        # the first disk is already attached for us so start with 2nd.
        for disk in xrange(1, instanceType.disks + 1):
            bdm[bdtKeys[disk]] = BlockDeviceType(
                ephemeral_name='ephemeral{}'.format(
                    disk - 1))  # ephemeral counts start at 0

        logger.debug('Device mapping: %s', bdm)
        return bdm
Exemplo n.º 4
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
Exemplo n.º 5
0
def _get_block_device_mapping(device_name, size):
    """ Returns a block device mapping object for the specified device and size.

    Block Device Mapping is used to associate a device on the VM with an EBS Volume.

    parameters:
    device_name -- The name of the device in the VM, such as /dev/sda1, /dev/sdb1. etc
    size -- The amount of space to allocate for the EBS drive.

    """
    block_device = BlockDeviceType()
    block_device.size = size
    bdm = BlockDeviceMapping()
    bdm[device_name] = block_device

    return bdm
Exemplo n.º 6
0
    def test_launch_config(self):
        # This unit test is based on #753 and #1343
        self.set_http_response(status_code=200)
        dev_sdf = EBSBlockDeviceType(snapshot_id='snap-12345')

        bdm = BlockDeviceMapping()
        bdm['/dev/sdf'] = dev_sdf

        lc = launchconfig.LaunchConfiguration(
            connection=self.service_connection,
            name='launch_config',
            image_id='123456',
            instance_type='m1.large',
            user_data='#!/bin/bash',
            security_groups=['group1'],
            spot_price='price',
            block_device_mappings=[bdm],
            associate_public_ip_address=True,
            volume_type='atype',
            delete_on_termination=False,
            iops=3000,
            classic_link_vpc_id='vpc-1234',
            classic_link_vpc_security_groups=['classic_link_group']
        )

        response = self.service_connection.create_launch_configuration(lc)

        self.assert_request_parameters({
            'Action': 'CreateLaunchConfiguration',
            'BlockDeviceMappings.member.1.DeviceName': '/dev/sdf',
            'BlockDeviceMappings.member.1.Ebs.DeleteOnTermination': 'false',
            'BlockDeviceMappings.member.1.Ebs.SnapshotId': 'snap-12345',
            'EbsOptimized': 'false',
            'LaunchConfigurationName': 'launch_config',
            'ImageId': '123456',
            'UserData': base64.b64encode(b'#!/bin/bash').decode('utf-8'),
            'InstanceMonitoring.Enabled': 'false',
            'InstanceType': 'm1.large',
            'SecurityGroups.member.1': 'group1',
            'SpotPrice': 'price',
            'AssociatePublicIpAddress': 'true',
            'VolumeType': 'atype',
            'DeleteOnTermination': 'false',
            'Iops': 3000,
            'ClassicLinkVPCId': 'vpc-1234',
            'ClassicLinkVPCSecurityGroups.member.1': 'classic_link_group'
        }, ignore_params_values=['Version'])
Exemplo n.º 7
0
def launch_ondemand_request(conn, request, tenant, job):
    try:

        mapping = BlockDeviceMapping()
        sda1 = BlockDeviceType()
        eph0 = BlockDeviceType()
        eph1 = BlockDeviceType()
        eph2 = BlockDeviceType()
        eph3 = BlockDeviceType()
        sda1.size = 10
        eph0.ephemeral_name = 'ephemeral0'
        eph1.ephemeral_name = 'ephemeral1'
        eph2.ephemeral_name = 'ephemeral2'
        eph3.ephemeral_name = 'ephemeral3'
        mapping['/dev/sda1'] = sda1
        mapping['/dev/sdb'] = eph0
        mapping['/dev/sdc'] = eph1
        mapping['/dev/sdd'] = eph2
        mapping['/dev/sde'] = eph3

        # issue a run_instances command for this request
        res = conn.run_instances(min_count=request.count,
                                 max_count=request.count,
                                 key_name=tenant.key_pair,
                                 image_id=request.ami,
                                 security_group_ids=[tenant.security_group],
                                 user_data=customise_cloudinit(tenant, job),
                                 instance_type=request.instance_type,
                                 subnet_id=tenant.subnet,
                                 block_device_map=mapping)
        my_req_ids = [req.id for req in res.instances]
        # address = ""
        for req in my_req_ids:
            # tag each request
            tag_requests(req, tenant.name, conn)
            # update the database to include the new request
            ProvisionerConfig().dbconn.execute(
                ("insert into instance_request (tenant, instance_type, " +
                 "price, job_runner_id, request_type, request_id, " +
                 "subnet) values ('%s', '%s', %s, %s, '%s', '%s', %s)") %
                (tenant.db_id, request.instance.db_id,
                 request.instance.ondemand, job.id, "ondemand", req,
                 tenant.subnet_id))
            # ProvisionerConfig().dbconn.commit()
            return
    except boto.exception.EC2ResponseError:
        logger.exception("There was an error communicating with EC2.")
Exemplo n.º 8
0
    def run(cls, info):
        registration_params = {
            'name': info._ec2['ami_name'],
            'description': info._ec2['ami_description']
        }
        registration_params['architecture'] = {
            'i386': 'i386',
            'amd64': 'x86_64'
        }.get(info.manifest.system['architecture'])

        if info.manifest.volume['backing'] == 's3':
            registration_params['image_location'] = info._ec2[
                'manifest_location']
        else:
            root_dev_name = {
                'pvm': '/dev/sda',
                'hvm': '/dev/xvda'
            }.get(info.manifest.provider['virtualization'])
            registration_params['root_device_name'] = root_dev_name

            from boto.ec2.blockdevicemapping import BlockDeviceType
            from boto.ec2.blockdevicemapping import BlockDeviceMapping
            block_device = BlockDeviceType(
                snapshot_id=info._ec2['snapshot'].id,
                delete_on_termination=True,
                size=info.volume.size.bytes.get_qty_in('GiB'),
                volume_type='gp2')
            registration_params['block_device_map'] = BlockDeviceMapping()
            registration_params['block_device_map'][
                root_dev_name] = block_device

        if info.manifest.provider['virtualization'] == 'hvm':
            registration_params['virtualization_type'] = 'hvm'
        else:
            registration_params['virtualization_type'] = 'paravirtual'
            akis_path = os.path.join(os.path.dirname(__file__), 'ami-akis.yml')
            from bootstrapvz.common.tools import config_get
            registration_params['kernel_id'] = config_get(
                akis_path,
                [info._ec2['region'], info.manifest.system['architecture']])

        if info.manifest.provider.get('enhanced_networking', None) == 'simple':
            registration_params['sriov_net_support'] = 'simple'

        info._ec2['image'] = info._ec2['connection'].register_image(
            **registration_params)
Exemplo n.º 9
0
def test_launch_config_add(sys, user_input):
    sys.argv = [
        'autoscaler_launch_config',
        'add',
        'web',
    ]

    # "image_id", "key_name", "security_groups", "user_data", "instance_type",
    # "kernel_id", "ramdisk_id", "block_device_mappings", "instance_monitoring",
    # "instance_profile_name", "spot_price", "ebs_optimized", "associate_public_ip_address"
    user_input.side_effect = [
        'ami-1234abcd',
        'the_key',
        "default,web",
        "echo 'web' > /etc/config",
        "m1.small",
        "",
        "",
        "",
        "yes",
        "arn:aws:iam::123456789012:instance-profile/tester",
        "0.2",
        "yes",
        "",
    ]

    # Simulate CLI call
    launch_config()

    conn = boto.connect_autoscale(use_block_device_types=True)
    configs = conn.get_all_launch_configurations()
    configs.should.have.length_of(1)
    config = configs[0]
    config.name.should.equal("web")
    config.image_id.should.equal("ami-1234abcd")
    config.key_name.should.equal("the_key")
    set(config.security_groups).should.equal(set(["web", "default"]))
    config.user_data.should.equal("echo 'web' > /etc/config")
    config.instance_type.should.equal("m1.small")
    config.kernel_id.should.equal("")
    config.ramdisk_id.should.equal("")
    config.block_device_mappings.should.equal(BlockDeviceMapping())
    config.instance_monitoring.enabled.should.equal('true')
    config.spot_price.should.equal(0.2)
    config.ebs_optimized.should.equal(True)
    config.associate_public_ip_address.should.equal(False)
Exemplo n.º 10
0
def get_block_device(instance_type, ebs_vol_size):
    block_map = BlockDeviceMapping()

    if ebs_vol_size > 0:
        device = EBSBlockDeviceType()
        device.size = ebs_vol_size
        device.delete_on_termination = True
        block_map["/dev/sdv"] = device

    for i in range(get_num_disks(instance_type)):
        dev = BlockDeviceType()
        dev.ephemeral_name = 'ephemeral%d' % i
        # The first ephemeral drive is /dev/sdb.
        name = '/dev/sd' + string.ascii_letters[i + 1]
        block_map[name] = dev

    return block_map
Exemplo n.º 11
0
Arquivo: ec2.py Projeto: phieber/metro
    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")
Exemplo n.º 12
0
def create_image(module, ec2):
    """
    Creates new AMI

    module : AnsibleModule object
    ec2: authenticated ec2 connection object
    """

    instance_id = module.params.get('instance_id')
    name = module.params.get('name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))
    description = module.params.get('description')
    no_reboot = module.params.get('no_reboot')
    device_mapping = module.params.get('device_mapping')
    tags =  module.params.get('tags')

    try:
        params = {'instance_id': instance_id,
                  'name': name,
                  'description': description,
                  'no_reboot': no_reboot}

        if device_mapping:
            bdm = BlockDeviceMapping()
            for device in device_mapping:
                if 'device_name' not in device:
                    module.fail_json(msg = 'Device name must be set for volume')
                device_name = device['device_name']
                del device['device_name']
                bd = BlockDeviceType(**device)
                bdm[device_name] = bd
            params['block_device_mapping'] = bdm

        image_id = ec2.create_image(**params)
    except boto.exception.BotoServerError, e:
        if e.error_code == 'InvalidAMIName.Duplicate':
            images = ec2.get_all_images()
            for img in images:
                if img.name == name:
                    module.exit_json(msg="AMI name already present", image_id=img.id, state=img.state, changed=False)
                    sys.exit(0)
            else:
                module.fail_json(msg="Error in retrieving duplicate AMI details")
        else:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))
Exemplo n.º 13
0
def create_server():
    """
    Creates EC2 Instance and saves it state in a local json file
    """
    # looks for an existing 'data.json' file, so that we don't start
    # additional ec2 instances when we don't need them.
    #
    if is_there_state():
        return True
    else:
        conn = connect_to_ec2()

        print(_green("Started..."))
        print(_yellow("...Creating EC2 instance..."))

        # we need a larger boot device to store our cached images
        dev_sda1 = EBSBlockDeviceType()
        dev_sda1.size = 120
        bdm = BlockDeviceMapping()
        bdm['/dev/sda1'] = dev_sda1

        # get an ec2 ami image object with our choosen ami
        image = conn.get_all_images(env.ec2_ami)[0]
        # start a new instance
        reservation = image.run(1,
                                1,
                                key_name=env.ec2_key_pair,
                                security_groups=env.ec2_security,
                                block_device_map=bdm,
                                instance_type=env.ec2_instancetype)

        # and get our instance_id
        instance = reservation.instances[0]
        # add a tag to our instance
        conn.create_tags([instance.id], {"Name": env.ec2_instance_name})
        #  and loop and wait until ssh is available
        while instance.state == u'pending':
            yellow("Instance state: %s" % instance.state)
            sleep(10)
            instance.update()
        wait_for_ssh(instance.public_dns_name)

        green("Instance state: %s" % instance.state)
        green("Public dns: %s" % instance.public_dns_name)
        # finally save the details or our new instance into the local state file
        save_state_locally(instance.id)
Exemplo n.º 14
0
def clone_instance(instance):
    """
    Make a clone of an existing Instance object.

    instance      The Instance object to clone.
    """
    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 !
        if user_data:
            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]
Exemplo n.º 15
0
    def run_instance(self,
                     image_id,
                     security_group_ids=None,
                     instance_type='c3.xlarge',
                     placement=None,
                     block_device_map=None,
                     subnet_id=None,
                     user_data=None,
                     ebs_optimized=True,
                     instance_profile_name=None):
        instance = Instance()
        instance.id = new_id()
        instance.image_id = image_id
        instance.root_device_name = '/dev/sda1'
        instance._state.code = 0
        instance._state.name = 'pending'

        # Create volumes based on block device data from the image.
        image = self.get_image(image_id)
        instance_bdm = BlockDeviceMapping()
        for device_name, bdm in image.block_device_mapping.iteritems():
            # Create a new volume and attach it to the instance.
            volume = Volume()
            volume.size = 8
            volume.id = new_id()
            self.volumes[volume.id] = volume

            bdt = BlockDeviceType(volume_id=volume.id, size=8)
            instance_bdm[device_name] = bdt

        instance.block_device_mapping = instance_bdm
        self.instances[instance.id] = instance

        if self.run_instance_callback:
            args = RunInstanceArgs()
            args.image_id = image_id
            args.instance_type = instance_type
            args.ebs_optimized = ebs_optimized
            args.security_group_ids = security_group_ids
            args.subnet_id = subnet_id
            args.user_data = user_data
            args.instance = instance
            self.run_instance_callback(args)

        return instance
Exemplo n.º 16
0
    def test_detect_double_encryption(self):
        """ Test that we disallow encryption of an already encrypted AMI.
        """
        aws_svc = test_aws_service.DummyAWSService()

        # Register guest image
        bdm = BlockDeviceMapping()
        bdm['/dev/sda1'] = BlockDeviceType()
        id = aws_svc.register_image(kernel_id=None,
                                    name='Guest image',
                                    block_device_map=bdm)
        guest_image = aws_svc.get_image(id)

        # Make the guest image look like it was already encrypted and
        # make sure that validation fails.
        guest_image.tags[encrypt_ami.TAG_ENCRYPTOR] = 'ami-' + new_id()
        with self.assertRaises(ValidationError):
            brkt_cli.aws._validate_guest_ami(aws_svc, id)
Exemplo n.º 17
0
 def startElement(self, name, attrs, connection):
     retval = TaggedEC2Object.startElement(self, name, attrs, connection)
     if retval is not None:
         return retval
     if name == 'monitoring':
         self._in_monitoring_element = True
     elif name == 'blockDeviceMapping':
         self.block_device_mapping = BlockDeviceMapping()
         return self.block_device_mapping
     elif name == 'productCodes':
         return self.product_codes
     elif name == 'stateReason':
         self.state_reason = StateReason()
         return self.state_reason
     elif name == 'groupSet':
         self.groups = ResultSet([('item', Group)])
         return self.groups
     return None
Exemplo n.º 18
0
    def create_root_block_device(self):
        # if the root volume size is not the same as the AMI default value:
        if self.root_volume_size is not None:
            # sda rather than xvda (for Windows)
            dev_sda1 = BlockDeviceType()
            dev_sda1.size = self.root_volume_size
            dev_sda1.delete_on_termination = True
            volume = BlockDeviceMapping()

            # Check the OS type, if its windows we use sda, linux: xvda
            images = self.ec2.get_all_images(image_ids=[self.node_obj.ami])
            image = images[0]

            if image.platform is None:
                volume['/dev/xvda'] = dev_sda1
            else:
                volume['/dev/sda1'] = dev_sda1
            self.volumes = volume
Exemplo n.º 19
0
def ebs_mapping(size=100):
    '''Letter A is used for root device
    Letter B to E are used for Instance Store
    EBS starts from letter F'''
    # 先给个通用配置(非DB)
    # 为了简化,根分区固定为20G,可以用实例存储做swap, 函数参数指定额外EBS磁盘大小,默认100G
    # gp2类型,iops自动为磁盘GB数的3倍
    block_device_map = BlockDeviceMapping()
    xvda = BlockDeviceType(delete_on_termination=True, size=20)
    xvdb = BlockDeviceType(ephemeral_name='ephemeral0')
    xvdf = BlockDeviceType(delete_on_termination=False, size=size, volume_type='gp2')
    #xvdg = BlockDeviceType(delete_on_termination=False, size=100, volume_type='io1', iops=1000)
    block_device_map['/dev/xvda'] = xvda
    block_device_map['/dev/sdb'] = xvdb
    block_device_map['/dev/sdf'] = xvdf
    #block_device_map['/dev/sdg'] = xvdg
    
    return block_device_map
Exemplo n.º 20
0
def get_block_device_map(source_image):
    block_device_map = BlockDeviceMapping()

    for item in source_image.block_device_mapping:
        source_snapshot_id = source_image.block_device_mapping[
            item].snapshot_id

        target_snapshot_size = source_image.block_device_mapping[item].size
        delete_on_termination = source_image.block_device_mapping[
            item].delete_on_termination
        volume_type = source_image.block_device_mapping[item].volume_type
        device_name = str(item)
        block_device_map[device_name] = BlockDeviceType(
            snapshot_id=source_snapshot_id,
            size=target_snapshot_size,
            volume_type=volume_type,
            delete_on_termination=delete_on_termination)
    return block_device_map
Exemplo n.º 21
0
    def test_launch_config(self):
        # This unit test is based on #753 and #1343
        self.set_http_response(status_code=200)
        dev_sdf = EBSBlockDeviceType(snapshot_id='snap-12345')
        dev_sdg = EBSBlockDeviceType(snapshot_id='snap-12346')

        bdm = BlockDeviceMapping()
        bdm['/dev/sdf'] = dev_sdf
        bdm['/dev/sdg'] = dev_sdg

        lc = launchconfig.LaunchConfiguration(
            connection=self.service_connection,
            name='launch_config',
            image_id='123456',
            instance_type='m1.large',
            security_groups=['group1', 'group2'],
            spot_price='price',
            block_device_mappings=[bdm],
            associate_public_ip_address=True)

        response = self.service_connection.create_launch_configuration(lc)

        self.assert_request_parameters(
            {
                'Action': 'CreateLaunchConfiguration',
                'BlockDeviceMappings.member.1.DeviceName': '/dev/sdf',
                'BlockDeviceMappings.member.1.Ebs.DeleteOnTermination':
                'false',
                'BlockDeviceMappings.member.1.Ebs.SnapshotId': 'snap-12345',
                'BlockDeviceMappings.member.2.DeviceName': '/dev/sdg',
                'BlockDeviceMappings.member.2.Ebs.DeleteOnTermination':
                'false',
                'BlockDeviceMappings.member.2.Ebs.SnapshotId': 'snap-12346',
                'EbsOptimized': 'false',
                'LaunchConfigurationName': 'launch_config',
                'ImageId': '123456',
                'InstanceMonitoring.Enabled': 'false',
                'InstanceType': 'm1.large',
                'SecurityGroups.member.1': 'group1',
                'SecurityGroups.member.2': 'group2',
                'SpotPrice': 'price',
                'AssociatePublicIpAddress': 'true'
            },
            ignore_params_values=['Version'])
Exemplo n.º 22
0
def copy_snapshots_by_ami(conn, source_image, region):
    block_device_map = BlockDeviceMapping()

    for item in source_image.block_device_mapping:
        source_snapshot_id = source_image.block_device_mapping[
            item].snapshot_id
        target_snapshot_size = source_image.block_device_mapping[item].size
        delete_on_termination = source_image.block_device_mapping[
            item].delete_on_termination
        volume_type = source_image.block_device_mapping[item].volume_type
        target_snapshot_id = copy_snapshot(conn, region, source_snapshot_id)
        print 'New snapshot created: {}'.format(target_snapshot_id)
        device_name = str(item)
        block_device_map[device_name] = BlockDeviceType(
            snapshot_id=target_snapshot_id,
            size=target_snapshot_size,
            volume_type=volume_type,
            delete_on_termination=delete_on_termination)
    return block_device_map
Exemplo n.º 23
0
 def launch_instance(self):
     if not self.verify_settings():
         return
     is_instance_store = self.conn.get_all_images(
         self.config['ec2_ami_id'],
         filters={'root-device-type': 'instance-store'})
     if is_instance_store:
         block_map = None
     else:
         block_map = BlockDeviceMapping()
         root_device = self.config['ec2_root_device']
         block_map[root_device] = EBSBlockDeviceType()
         if self.config['ec2_size']:
             block_map[root_device].size = self.config['ec2_size']
         block_map[root_device].delete_on_termination = True
     reservation = self.conn.run_instances(
         self.config['ec2_ami_id'],
         key_name=self.config['ec2_key_name'],
         security_groups=self.config['ec2_security_groups']
         or [self.config['ec2_security_group']],
         instance_type=self.config['ec2_instance_type'],
         placement=self.config['ec2_zone'],
         placement_group=self.config['ec2_placement_group'],
         monitoring_enabled=self.config['ec2_monitoring_enabled'],
         block_device_map=block_map,
         user_data=self.user_data)
     self.instance = reservation.instances[0]
     secs = RUN_INSTANCE_TIMEOUT
     rest_interval = 5
     while secs and not self.instance.state == 'running':
         time.sleep(rest_interval)
         secs = secs - rest_interval
         try:
             self.instance.update()
         except boto.exception.EC2ResponseError:
             pass
     if secs <= 0:
         errmsg = "run instance {0} failed after {1} seconds".format(
             self.instance.id, RUN_INSTANCE_TIMEOUT)
         LOG.error(errmsg)
     else:
         if self.config['hostname']:
             self.assign_name_tag()
Exemplo n.º 24
0
def create_image(module, ec2):
    """
    Creates new AMI

    module : AnsibleModule object
    ec2: authenticated ec2 connection object
    """

    instance_id = module.params.get('instance_id')
    name = module.params.get('name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))
    description = module.params.get('description')
    no_reboot = module.params.get('no_reboot')
    device_mapping = module.params.get('device_mapping')
    tags =  module.params.get('tags')
    launch_permissions = module.params.get('launch_permissions')

    try:
        params = {'instance_id': instance_id,
                  'name': name,
                  'description': description,
                  'no_reboot': no_reboot}

        images = ec2.get_all_images(filters={'name': name})

        if images and images[0]:
            module.exit_json(msg="AMI name already present", image_id=images[0].id, state=images[0].state, changed=False)

        if device_mapping:
            bdm = BlockDeviceMapping()
            for device in device_mapping:
                if 'device_name' not in device:
                    module.fail_json(msg = 'Device name must be set for volume')
                device_name = device['device_name']
                del device['device_name']
                bd = BlockDeviceType(**device)
                bdm[device_name] = bd
            params['block_device_mapping'] = bdm

        image_id = ec2.create_image(**params)
    except boto.exception.BotoServerError, e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))
Exemplo n.º 25
0
Arquivo: ec2.py Projeto: phieber/metro
    def start_remote(self):
        self.get_bootstrap_kernel()
        self.get_bootstrap_image()

        # create EBS volume for /mnt/gentoo
        device = BlockDeviceType()
        device.size = self.settings["ec2/instance/device/size"]
        device.delete_on_termination = True

        mapping = BlockDeviceMapping()
        self.root_device = "/dev/" + self.settings["ec2/instance/device/name"]
        mapping[self.root_device] = device

        # start bootstrapping instance
        reservation = self.ec2.run_instances(
            self.bootstrap_image.id,
            kernel_id=self.bootstrap_kernel.id,
            instance_type=self.settings["ec2/instance/type"],
            security_groups=[self.name],
            key_name=self.name,
            block_device_map=mapping)

        self.instance = reservation.instances[0]

        sys.stdout.write("waiting for instance to come up ..")
        while self.instance.update() != 'running':
            sys.stdout.write(".")
            sys.stdout.flush()
            time.sleep(5)
        sys.stdout.write("\n")
        time.sleep(120)

        self.ssh_uri = "ec2-user@" + self.instance.public_dns_name
        self.remote_upload_path = "/tmp"

        # enable sudo without a tty
        cmd = "sudo sed -i -e '/requiretty/d' /etc/sudoers"
        cmd = ["ssh", "-t"] + self.ssh_options() + [self.ssh_uri, cmd]
        ssh = subprocess.Popen(cmd)
        ssh.wait()

        self.run_script_at_remote("steps/remote/postboot")
Exemplo n.º 26
0
    def run(cls, info):
        registration_params = {
            'name': info.ami_name,
            'description': info.ami_description
        }
        registration_params['architecture'] = {
            'i386': 'i386',
            'amd64': 'x86_64'
        }.get(info.manifest.system['architecture'])

        if info.manifest.volume['backing'] == 's3':
            registration_params[
                'image_location'] = info.manifest.manifest_location
        else:
            root_dev_name = {
                'pvm': '/dev/sda',
                'hvm': '/dev/xvda'
            }.get(info.manifest.data['virtualization'])
            registration_params['root_device_name'] = root_dev_name

            from boto.ec2.blockdevicemapping import BlockDeviceType
            from boto.ec2.blockdevicemapping import BlockDeviceMapping
            block_device = BlockDeviceType(
                snapshot_id=info.snapshot.id,
                delete_on_termination=True,
                size=info.volume.size.get_qty_in('GiB'))
            registration_params['block_device_map'] = BlockDeviceMapping()
            registration_params['block_device_map'][
                root_dev_name] = block_device

        if info.manifest.data['virtualization'] == 'hvm':
            registration_params['virtualization_type'] = 'hvm'
        else:
            registration_params['virtualization_type'] = 'paravirtual'
            akis_path = os.path.join(os.path.dirname(__file__),
                                     'ami-akis.json')
            from common.tools import config_get
            registration_params['kernel_id'] = config_get(
                akis_path,
                [info.host['region'], info.manifest.system['architecture']])

        info.image = info.connection.register_image(**registration_params)
Exemplo n.º 27
0
def create_image(name=IMAGE_NAME, description=IMAGE_DESCRIPTION):
    """
    Create an EBS AMI from the build volume.
    
    :type name: string
    :param name: The name of the AMI to use.
    
    :type description: string
    :param description: The description of the AMI.
    
    :rtype: class:`boto.ec2.Image` or ``None``
    :return: The image produced.
    """
    instance, volume, device_name = get_volume()
    snapshot = create_snapshot(IMAGE_NAME)
    image = None
    if snapshot is None:
        print red('Cannot create image with no snapshot')
    else:
        # Create block device mapping
        ebs = EBSBlockDeviceType(snapshot_id=snapshot.id,
                                 delete_on_termination=True)
        ephemeral0 = BlockDeviceType(ephemeral_name='ephemeral0')
        swap = BlockDeviceType(ephemeral_name='ephemeral1')
        block_map = BlockDeviceMapping()
        block_map['/dev/sda1'] = ebs
        block_map['/dev/sda2'] = ephemeral0
        block_map['/dev/sda3'] = swap

        image_id = instance.connection.register_image(
            name,
            description,
            architecture=instance.architecture,
            kernel_id=get_kernel(),
            root_device_name='/dev/sda1',
            block_device_map=block_map)

        print green('Image id is %s' % image_id)
        time.sleep(5)
        image = instance.connection.get_all_images((image_id, ))[0]
        add_name(image, name)
    return image
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"
Exemplo n.º 29
0
def volume_to_ami(volume,
                  ami_name,
                  arch,
                  virtualization_type,
                  root_device_name,
                  tags,
                  kernel_id=None):
    log.info('Creating a snapshot')
    snap = volume.create_snapshot(ami_name)
    wait_for_status(snap, "status", "completed", "update")
    snap.add_tag("Name", ami_name)

    bdm = BlockDeviceMapping()
    bdm[root_device_name] = BlockDeviceType(snapshot_id=snap.id)

    log.info('Creating AMI')

    ami_id = volume.connection.register_image(
        ami_name,
        ami_name,
        architecture=arch,
        kernel_id=kernel_id,
        root_device_name=root_device_name,
        block_device_map=bdm,
        virtualization_type=virtualization_type,
    )
    log.info('Waiting...')
    while True:
        try:
            ami = volume.connection.get_image(ami_id)
            ami.add_tag('Name', ami_name)
            ami.add_tag('moz-created', int(time.time()))
            for tag, value in tags.iteritems():
                ami.add_tag(tag, value)
            log.info('AMI created')
            log.info('ID: {id}, name: {name}'.format(id=ami.id, name=ami.name))
            break
        except:  # noqa: E722
            log.info('Wating for AMI')
            time.sleep(10)
    wait_for_status(ami, "state", "available", "update")
    return ami
Exemplo n.º 30
0
def build_block_device_map(source_image, target_snapshot_id,
                           source_volume_size):
    """Create a block device map which is used for the copied AMI.
    The created block device map contains a root volumes with 8GB of storage
    on general purpose SSD (gp2).
    """

    root_device_name = source_image.root_device_name

    del_root_volume = source_image.block_device_mapping[
        root_device_name].delete_on_termination

    block_device_map = BlockDeviceMapping()
    block_device_map[root_device_name] = EBSBlockDeviceType(
        snapshot_id=target_snapshot_id,
        size=source_volume_size,
        volume_type='gp2',
        delete_on_termination=del_root_volume)

    return block_device_map