def _get_key(key_name, config): ec2 = _resource("ec2", config) try: for key in ec2.key_pairs.filter(Filters=[{ "Name": "key-name", "Values": [key_name] }]): if key.name == key_name: return key except botocore.exceptions.ClientError as exc: handle_boto_error(exc, "Failed to fetch EC2 key pair {} from AWS.", cf.bold(key_name)) raise exc
def _get_role(role_name, config): iam = _resource("iam", config) role = iam.Role(role_name) try: role.load() return role except botocore.exceptions.ClientError as exc: if exc.response.get("Error", {}).get("Code") == "NoSuchEntity": return None else: handle_boto_error( exc, "Failed to fetch IAM role data for {} from AWS.", cf.bold(role_name)) raise exc
def _configure_subnet(config): ec2 = _resource("ec2", config) use_internal_ips = config["provider"].get("use_internal_ips", False) try: subnets = sorted( (s for s in ec2.subnets.all() if s.state == "available" and ( use_internal_ips or s.map_public_ip_on_launch)), reverse=True, # sort from Z-A key=lambda subnet: subnet.availability_zone) except botocore.exceptions.ClientError as exc: handle_boto_error(exc, "Failed to fetch available subnets from AWS.") raise exc if not subnets: cli_logger.abort( "No usable subnets found, try manually creating an instance in " "your specified region to populate the list of subnets " "and trying this again.\n" "Note that the subnet must map public IPs " "on instance launch unless you set `use_internal_ips: true` in " "the `provider` config.") # todo: err msg raise Exception( "No usable subnets found, try manually creating an instance in " "your specified region to populate the list of subnets " "and trying this again. Note that the subnet must map public IPs " "on instance launch unless you set 'use_internal_ips': True in " "the 'provider' config.") if "availability_zone" in config["provider"]: azs = config["provider"]["availability_zone"].split(",") subnets = [s for s in subnets if s.availability_zone in azs] if not subnets: cli_logger.abort( "No usable subnets matching availability zone {} found.\n" "Choose a different availability zone or try " "manually creating an instance in your specified region " "to populate the list of subnets and trying this again.", config["provider"]["availability_zone"]) # todo: err msg raise Exception( "No usable subnets matching availability zone {} " "found. Choose a different availability zone or try " "manually creating an instance in your specified region " "to populate the list of subnets and trying this again.". format(config["provider"]["availability_zone"])) subnet_ids = [s.subnet_id for s in subnets] subnet_descr = [(s.subnet_id, s.availability_zone) for s in subnets] if "SubnetIds" not in config["head_node"]: _set_config_info(head_subnet_src="default") config["head_node"]["SubnetIds"] = subnet_ids cli_logger.old_info( logger, "_configure_subnet: " "SubnetIds not specified for head node, using {}", subnet_descr) else: _set_config_info(head_subnet_src="config") if "SubnetIds" not in config["worker_nodes"]: _set_config_info(workers_subnet_src="default") config["worker_nodes"]["SubnetIds"] = subnet_ids cli_logger.old_info( logger, "_configure_subnet: " "SubnetId not specified for workers," " using {}", subnet_descr) else: _set_config_info(workers_subnet_src="config") return config