def _test(count, hostname): awscontext.GLOBAL = mock.MagicMock() ec2_conn = mock.MagicMock() ipa_client = mock.MagicMock() awscontext.GLOBAL.iam.list_account_aliases.return_value = { "AccountAliases": ["foo"] } ipa_client.enroll_host.return_value = { 'result': { 'result': { 'randompassword': '******' } } } return hostmanager.create_host(ec2_conn=ec2_conn, ipa_client=ipa_client, image_id='foo', key='foo', secgroup_ids='foo', instance_type='foo', subnets=['foo'], disk='foo', instance_vars=None, count=count, domain='foo.com', role='bar', hostname=hostname)
def create( image, image_account, count, disk_size, key, role, secgroup, size, subnet): """Create instance(s)""" ipa_client = awscontext.GLOBAL.ipaclient ec2_conn = awscontext.GLOBAL.ec2 sts_conn = awscontext.GLOBAL.sts ipa_domain = awscontext.GLOBAL.ipa_domain image_id = aws_cli.admin.image_id( ec2_conn, sts_conn, image, image_account) secgroup_id = aws_cli.admin.secgroup_id(ec2_conn, secgroup) subnet_id = aws_cli.admin.subnet_id(ec2_conn, subnet) if not key: key = metadata.instance_keys()[0] hostnames = hostmanager.create_host( ipa_client=ipa_client, ec2_conn=ec2_conn, image_id=image_id, count=count, disk=disk_size, domain=ipa_domain, key=key, secgroup_ids=secgroup_id, instance_type=size, subnet_id=subnet_id, role=role, ) for hostname in hostnames: click.echo(hostname)
def _create_host(ipa_client, ec2_conn, hostname, instance_type, spot, subnets, otp, tracker, **host_params): for subnet in subnets: if not tracker.feasible(instance_type, spot, subnet): continue _LOGGER.info('Creating host %s, type: %s, spot: %s, subnet: %s', hostname, instance_type, spot, subnet) for i in range(_CREATE_HOST_MAX_TRIES): try: hostmanager.create_host( ipa_client=ipa_client, ec2_conn=ec2_conn, hostname=hostname, subnet=subnet, instance_type=instance_type, spot=spot, otp=otp, **host_params ) return { 'hostname': hostname, 'type': instance_type, 'lifecycle': 'spot' if spot else 'on-demand', 'subnet': subnet, } except botoexc.ClientError as err: err_code = err.response['Error']['Code'] if err_code in ('SpotMaxPriceTooLow', 'InsufficientInstanceCapacity'): _LOGGER.info('Instance not feasible, trying next: %r', err) tracker.exclude_instance(instance_type, spot, subnet) break if err_code == 'InsufficientFreeAddressesInSubnet': _LOGGER.info('Subnet exhausted, trying next: %r', err) tracker.exclude_subnet(subnet) break if err_code == 'InternalError': if i == _CREATE_HOST_MAX_TRIES - 1: raise _LOGGER.error('Internal error, retrying: %r', err) else: raise return None
def create_hosts(ami, count, disk, domain, key, role, secgroup, size, subnet): """Create host.""" ipa_client = awscontext.GLOBAL.ipaclient ec2_conn = awscontext.GLOBAL.ec2 hostmanager.create_host(ipa_client=ipa_client, ec2_conn=ec2_conn, image_id=ami, count=count, domain=domain, key=key, role=role, secgroup_ids=secgroup, instance_type=size, subnet_id=subnet, disk=disk)
def create(image, image_account, count, disk_size, key, role, ip_address, eni, spot, secgroup, size, subnet, data, instance_profile, hostgroup, hostname, domain, host_location, otp, ipa_enroll): """Create instance(s)""" if ipa_enroll: ipa_client = awscontext.GLOBAL.ipaclient else: ipa_client = None ec2_conn = awscontext.GLOBAL.ec2 if not domain: domain = awscontext.GLOBAL.ipa_domain image_id = aws_cli.admin.image_id(ec2_conn, image, image_account) secgroup_id = aws_cli.admin.secgroup_id(ec2_conn, secgroup) subnet_id = aws_cli.admin.subnet_id(ec2_conn, subnet) if data: instance_vars = yaml.load(stream=data) else: instance_vars = {} hosts_created = hostmanager.create_host( ipa_client=ipa_client, ec2_conn=ec2_conn, image_id=image_id, count=count, disk=disk_size, domain=domain, key=key, secgroup_ids=secgroup_id, instance_type=size, subnet=subnet_id, role=role, instance_vars=instance_vars, instance_profile=instance_profile, hostgroups=hostgroup, hostname=hostname, ip_address=ip_address, eni=eni, spot=spot, nshostlocation=host_location, otp=otp, ipa_enroll=ipa_enroll, ) for host_created in hosts_created: click.echo(host_created)
def create( image, image_account, count, disk_size, key, role, secgroup, size, subnet, data, instance_profile, hostgroup, hostname): """Create instance(s)""" ipa_client = awscontext.GLOBAL.ipaclient ec2_conn = awscontext.GLOBAL.ec2 sts_conn = awscontext.GLOBAL.sts ipa_domain = awscontext.GLOBAL.ipa_domain image_id = aws_cli.admin.image_id( ec2_conn, sts_conn, image, image_account) secgroup_id = aws_cli.admin.secgroup_id(ec2_conn, secgroup) subnet_id = aws_cli.admin.subnet_id(ec2_conn, subnet) if data: instance_vars = yaml.load(stream=data) else: instance_vars = {} if not key: key = metadata.instance_keys()[0] hosts_created = hostmanager.create_host( ipa_client=ipa_client, ec2_conn=ec2_conn, image_id=image_id, count=count, disk=disk_size, domain=ipa_domain, key=key, secgroup_ids=secgroup_id, instance_type=size, subnet_id=subnet_id, role=role, instance_vars=instance_vars, instance_profile=instance_profile, hostgroups=hostgroup, hostname=hostname ) for host_created in hosts_created: click.echo(host_created)
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)