def share(account, image): """Share Image""" ec2_conn = awscontext.GLOBAL.ec2 ec2client.get_image(ec2_conn, ids=[image]) share_image = ec2_conn.modify_image_attribute( ImageId=image, Attribute='launchPermission', OperationType='add', UserIds=[account]) click.echo('%s has been shared with %s' % (image, account))
def create_zk( ec2_conn, ipa_client, master, subnet_id=None, ip_address=None, instance_type=None): """ Create new Zookeeper """ sts_conn = awscontext.GLOBAL.sts ipa_domain = awscontext.GLOBAL.ipa_domain admin_cell = admin.Cell(context.GLOBAL.ldap.conn) cell = admin_cell.get(context.GLOBAL.cell) data = cell['data'] image_id = data['image'] if not image_id.startswith('ami-'): account = sts_conn.get_caller_identity().get('Account') image_id = ec2client.get_image( ec2_conn, owners=[account], name=image_id )['ImageId'] # FIXME; subnet not unique among ZK, not AZ aware if not subnet_id: subnet_id = random.choice(data['subnets']) shortname = master['hostname'].replace('.' + context.GLOBAL.dns_domain, '') if not instance_type: instance_type = 'm5.large' # Instance vars instance_vars = { 'treadmill_cell': context.GLOBAL.cell, 'treadmill_ldap': ','.join(context.GLOBAL.ldap.url), 'treadmill_ldap_suffix': context.GLOBAL.ldap_suffix, 'treadmill_dns_domain': context.GLOBAL.dns_domain, 'treadmill_isa': 'zookeeper', 'treadmill_profile': 'aws', 'treadmill_krb_realm': krb5.get_host_realm(sysinfo.hostname())[0], 'treadmill_zookeeper_myid': str(master['idx']), } # Build user-data and start new instance create_host(ec2_conn=ec2_conn, ipa_client=ipa_client, image_id=image_id, count=1, domain=ipa_domain, secgroup_ids=data['secgroup'], instance_type=instance_type, subnets=[subnet_id], disk=30, instance_vars=instance_vars, role='zookeeper', hostgroups=['zookeepers'], hostname=shortname, ip_address=ip_address) return master['hostname']
def image_id(ec2_conn, image, account): """Resolve CLI image arguments to image id.""" if not image and metadata.image_id(): image = {'ids': [metadata.image_id()]} if not account: account = 'self' return ec2client.get_image(ec2_conn, owners=[account], **image)['ImageId']
def image_id(ec2_conn, sts_conn, image, account): """Resolve CLI image arguments to image id.""" if not image and metadata.image_id(): image = {'ids': [metadata.image_id()]} if not account: account = sts_conn.get_caller_identity().get('Account') return ec2client.get_image(ec2_conn, owners=[account], **image)['ImageId']
def get(rsrc_id, account=None): """Get AWS image configuration.""" _LOGGER.info('get: %s', rsrc_id) ec2_conn = awscontext.GLOBAL.ec2 if not account: account = self._account image = {'name': rsrc_id} return ec2client.get_image(ec2_conn, owners=[account], **image)
def configure(account, image): """Configure AMI image.""" if not image: image = {'ids': [metadata.image_id()]} ec2_conn = awscontext.GLOBAL.ec2 owners = [] if not account: account = 'self' image_obj = ec2client.get_image(ec2_conn, owners=[account], **image) cli.out(formatter(image_obj))
def create_n_servers(count, partition=None, min_on_demand=None, max_on_demand=None, pool=None): """Create new servers in the cell.""" partition = partition or '_default' # FIXME: Import name from treadmill. _LOGGER.info( 'Creating %s servers in %s partition, min on-demand: %s, max: %s', count, partition, min_on_demand, max_on_demand) ec2_conn = awscontext.GLOBAL.ec2 ipa_domain = awscontext.GLOBAL.ipa_domain admin_cell = context.GLOBAL.admin.cell() cell = context.GLOBAL.cell cell_data = admin_cell.get(cell)['data'] admin_part = context.GLOBAL.admin.partition() try: _ldap_data = admin_part.get([partition, cell], dirty=True) partition_data = _ldap_data.get('data', {}) except admin_exceptions.NoSuchObjectResult: partition_data = {} image_id = partition_data.get('image', cell_data['image']) if not image_id.startswith('ami-'): image_id = ec2client.get_image(ec2_conn, owners=['self'], name=image_id)['ImageId'] instance_type = partition_data.get('size', cell_data['size']) instance_types = partition_data.get('instance_types', [instance_type]) spot_instance_types = partition_data.get('spot_instance_types', instance_types) subnets = partition_data.get('subnets', cell_data['subnets']) secgroup_id = partition_data.get('secgroup', cell_data['secgroup']) hostgroups = partition_data.get('hostgroups', cell_data['hostgroups']) instance_profile = partition_data.get('instance_profile', cell_data['instance_profile']) disk_size = int(partition_data.get('disk_size', cell_data['disk_size'])) nshostlocation = cell_data['aws_account'] instance_vars = { 'treadmill_cell': cell, 'treadmill_ldap': ','.join(context.GLOBAL.ldap.url), 'treadmill_ldap_suffix': context.GLOBAL.ldap_suffix, 'treadmill_dns_domain': context.GLOBAL.dns_domain, 'treadmill_isa': 'node', 'treadmill_profile': 'aws', 'treadmill_krb_realm': krb5.get_host_realm(sysinfo.hostname())[0], } tags = [ { 'Key': 'Cell', 'Value': cell }, { 'Key': 'Partition', 'Value': partition }, ] hostnames = _generate_hostnames(ipa_domain, cell, partition, count, min_on_demand=min_on_demand, max_on_demand=max_on_demand) instance_types = _instance_types(instance_types, spot_instance_types) host_params = dict(image_id=image_id, count=1, disk=disk_size, domain=ipa_domain, key=None, secgroup_ids=secgroup_id, role='node', instance_vars=instance_vars, instance_profile=instance_profile, hostgroups=hostgroups, ip_address=None, eni=None, tags=tags, nshostlocation=nshostlocation) if pool: func = functools.partial(_create_hosts_no_exc, instance_types=instance_types, subnets=subnets, cell=cell, partition=partition, **host_params) hosts_created = [] for res, err in pool.map(func, _split_list(hostnames, pool.workers)): if err: raise err hosts_created.extend(res) return hosts_created else: return _create_hosts(hostnames, instance_types, subnets, cell, partition, **host_params)
def create_n_servers(count, partition=None): """Create new servers in the cell.""" ipa_client = awscontext.GLOBAL.ipaclient ec2_conn = awscontext.GLOBAL.ec2 sts_conn = awscontext.GLOBAL.sts ipa_domain = awscontext.GLOBAL.ipa_domain admin_srv = admin.Server(context.GLOBAL.ldap.conn) admin_cell = admin.Cell(context.GLOBAL.ldap.conn) cell = admin_cell.get(context.GLOBAL.cell) data = cell['data'] image_id = data['image'] if not image_id.startswith('ami-'): account = sts_conn.get_caller_identity().get('Account') image_id = ec2client.get_image(ec2_conn, owners=[account], name=image_id)['ImageId'] instance_type = data['size'] subnets = data['subnets'] secgroup_id = data['secgroup'] hostgroups = data['hostgroups'] instance_profile = data['instance_profile'] disk_size = int(data['disk_size']) hostname_template = '{}-{}-{}'.format(context.GLOBAL.cell, partition if partition else 'node', '{time}') instance_vars = { 'treadmill_cell': context.GLOBAL.cell, 'treadmill_ldap': ','.join(context.GLOBAL.ldap.url), 'treadmill_ldap_suffix': context.GLOBAL.ldap_suffix, 'treadmill_dns_domain': context.GLOBAL.dns_domain, 'treadmill_isa': 'node', 'treadmill_profile': 'aws', 'treadmill_krb_realm': krb5.get_host_realm(sysinfo.hostname())[0], } # FIXME: Add Partition: $partition to tags when Autoscaler is cell aware tags = [{'Key': 'Cell', 'Value': context.GLOBAL.cell}] key = None for idx in range(0, count): hostnames = hostmanager.create_host(ipa_client=ipa_client, ec2_conn=ec2_conn, image_id=image_id, count=1, disk=disk_size, domain=ipa_domain, key=key, secgroup_ids=secgroup_id, instance_type=instance_type, subnets=subnets, role='node', instance_vars=instance_vars, instance_profile=instance_profile, hostgroups=hostgroups, hostname=hostname_template, ip_address=None, eni=None, tags=tags) # Count is one, but it is more robust to treat it as list. for hostname in hostnames: print(hostname) attrs = {'cell': context.GLOBAL.cell, 'partition': partition} admin_srv.create(hostname, attrs)