def test_cloud_init(self): """Test construction of cloud-init data.""" cloud_init = userdata.CloudInit() cloud_init.add('#!/bin/sh') cloud_init.add('#cloud-config\na=1') self.assertEqual(cloud_init.content_by_mimetype['text/x-shellscript'], [('-', '#!/bin/sh')])
def create(base_image, base_image_account, userdata, instance_profile, secgroup, subnet, image, key): """Create image""" ec2_conn = awscontext.GLOBAL.ec2 sts_conn = awscontext.GLOBAL.sts cloud_init = ud.CloudInit() for filename in userdata: with io.open(filename, 'rb') as f: content = f.read() if filename.endswith('.gz'): content = gzip.decompress(content) cloud_init.add(content.decode()) cloud_init.add_cloud_config({ 'image_description': '', 'image_name': image, }) base_image_id = aws_cli.admin.image_id(ec2_conn, sts_conn, base_image, account=base_image_account) secgroup_id = aws_cli.admin.secgroup_id(ec2_conn, secgroup) subnet_id = aws_cli.admin.subnet_id(ec2_conn, subnet) tags = [{ 'ResourceType': 'instance', 'Tags': [{ 'Key': 'Name', 'Value': 'ImageBuild-{}'.format(image) }] }] instance = ec2client.create_instance(ec2_conn, user_data=cloud_init.userdata(), image_id=base_image_id, instance_type='t2.small', key=key, tags=tags, secgroup_ids=secgroup_id, subnet_id=subnet_id, instance_profile=instance_profile, disk=10) click.echo(instance['Instances'][0]['InstanceId'])
def create(base_image, base_image_account, userdata, instance_profile, secgroup, subnet, key, image): """Create image""" ec2_conn = awscontext.GLOBAL.ec2 sts_conn = awscontext.GLOBAL.sts cloud_init = ud.CloudInit() for filename in userdata: with io.open(filename, 'rb') as f: content = f.read() if filename.endswith('.gz'): content = gzip.decompress(content) cloud_init.add(content.decode()) cloud_init.add_cloud_config({ 'image_description': '', 'image_name': image, }) base_image_id = aws_cli.admin.image_id(ec2_conn, sts_conn, base_image, account=base_image_account) secgroup_id = aws_cli.admin.secgroup_id(ec2_conn, secgroup) subnet_id = aws_cli.admin.subnet_id(ec2_conn, subnet) tags = [] instance = ec2client.create_instance(ec2_conn, user_data=cloud_init.userdata(), image_id=base_image_id, instance_type='t2.small', key=key, tags=tags, secgroup_ids=secgroup_id, subnet_id=subnet_id, instance_profile=instance_profile, disk=10) print(instance)
def create(rsrc_id, rsrc): """Create (configure) AWS image.""" image = rsrc_id _LOGGER.info('Create image: %s', image) ec2_conn = awscontext.GLOBAL.ec2 sts_conn = awscontext.GLOBAL.sts # Check if image exists. Create is async, so this is optimization # to return Found early. found = False try: get(image) found = True except Exception as _err: # pylint: disable=broad-except # TODO: catch appropriate exception. pass if found: raise exc.FoundError('Image {} already exists.'.format(image)) base_image_account = rsrc.get('base_image_account') _LOGGER.info('base account: %s', base_image_account) base_image = rsrc.get('base_image') base_image_id = aws_cli_admin.image_id(ec2_conn, sts_conn, base_image, account=base_image_account) _LOGGER.info('base image id: %s', base_image_id) secgroup = rsrc.get('secgroup') if secgroup: secgroup_id = aws_cli_admin.secgroup_id(ec2_conn, secgroup) else: secgroup_id = self._secgroup _LOGGER.info('secgroup id: %s', secgroup_id) subnet = rsrc.get('subnet') if subnet: subnet_id = aws_cli_admin.subnet_id(ec2_conn, subnet) else: subnet_id = self._subnet _LOGGER.info('subnet id: %s', secgroup_id) key = rsrc.get('key') cloud_init = ud.CloudInit() for content in rsrc.get('userdata', []): cloud_init.add(content) cloud_init.add_cloud_config({ 'image_description': '', 'image_name': image, }) tags = [{ 'ResourceType': 'instance', 'Tags': [{ 'Key': 'Name', 'Value': 'ImageBuild-{}'.format(image) }] }] instance = ec2client.create_instance( ec2_conn, user_data=cloud_init.userdata(), image_id=base_image_id, instance_type='t2.small', key=key, tags=tags, secgroup_ids=secgroup_id, subnet_id=subnet_id, instance_profile=instance_profile, disk=10) _LOGGER.info('Started instance: %s', instance['Instances'][0]['InstanceId']) return {}