def opsmgr_launch_instance(stack, version=None, verbose=False): version = cloudformation.get_tag(stack, "pcf-version") if version is None else version image = opsmgr_select_image(version, verbose) if verbose: print "Launching Ops Manager instance from", image["ImageId"] + ":", image.get("Description", "-") command = [ 'ec2', 'run-instances', '--image-id', image["ImageId"], '--instance-type', 'm3.large', '--subnet-id', cloudformation.get_output(stack, "PcfPublicSubnetId"), '--associate-public-ip-address', '--block-device-mapping', 'DeviceName=/dev/sda1,Ebs={VolumeSize=100}', '--security-group-ids', cloudformation.get_output(stack, "PcfOpsManagerSecurityGroupId"), '--key-name', config.get('aws', 'nat-key-pair') ] instance = json.loads(aws.aws_cli_verbose(command))["Instances"][0] tags = [ { "Key": "Name", "Value": "Ops Manager" }, { "Key": "Stack", "Value": stack["StackName"] }, { "Key": "Image", "Value": image.get("Description", "-") } ] command = [ 'ec2', 'create-tags', '--resources', instance["InstanceId"], '--tags', json.dumps(tags) ] aws.aws_cli_verbose(command) return instance
def opsmgr_terminate_instances(stack): instances = opsmgr_find_instances(stack) if len(instances) < 1: return instance_ids = [ i["InstanceId"] for i in instances] command = [ 'ec2', 'terminate-instances', '--instance-ids' ] aws.aws_cli_verbose(command + instance_ids)
def delete_tree(tree, indent=0): for item in tree: children = item.get("children", []) if len(children) > 0: print " " * indent + "deleting dependencies of", item["type"], item["id"] delete_tree(children, indent + 3) type_def = type_defs[item["type"]] command = type_def.get("delete", None) if command is not None: print " " * indent + "deleting", item["type"], item["id"] command = [element.replace("{}", item["id"]) for element in command] aws.aws_cli_verbose(command)
def get_security_group_name(group_id): command = [ 'ec2', 'describe-security-groups', '--group-ids', group_id ] group = json.loads(aws.aws_cli_verbose(command))["SecurityGroups"][0] return group["GroupName"]
def get_server_certificate(stack): certificate_arn = config.get("aws", "ssl-certificate-arn", stack=stack["StackName"]) certificate_name = certificate_arn.split('/')[-1] command = [ 'iam', 'get-server-certificate', '--server-certificate-name', certificate_name ] certificate = json.loads(aws.aws_cli_verbose(command))["ServerCertificate"] certificate_body = certificate["CertificateBody"] return certificate_body
def find_load_balancer(stack, dns_name): command = [ 'elb', 'describe-load-balancers' ] load_balancers = json.loads(aws.aws_cli_verbose(command))["LoadBalancerDescriptions"] load_balancers = [ lb for lb in load_balancers if lb["DNSName"] == dns_name] if len(load_balancers) != 1: print "Could not resolve load balancer for dns name", dns_name sys.exit(1) return load_balancers[0]
def get_items(type_name, item_id=None, filter=[]): type_def = type_defs.get(type_name, None) if type_def is None: print "Unknown type", type_name sys.exit(1) container = json.loads(aws.aws_cli_verbose(type_def["list"][:-1] + filter)) fields = string.split(type_def["list"][-1], ",") items = get_array(type_name, type_def, container, fields) if item_id is not None: items = [item for item in items if item["id"] == item_id] return items
def opsmgr_list_images(region = None): command = [ 'ec2', 'describe-images', '--filters', 'Name=name,Values=pivotal-ops-manager-v*' ] if region is not None: command.extend(['--region', region]) images = json.loads(aws.aws_cli_verbose(command))["Images"] images = sorted(images, key=lambda image: image["CreationDate"]) images.reverse() return images
def opsmgr_find_instances(stack=None): filters = [ { "Name": "tag:Name", "Values": [ "Ops Manager" ] }, { "Name": "tag-key", "Values": [ "Stack" ] }, { "Name": "instance-state-name", "Values": [ "pending", "running"] } ] if stack is not None: filters.extend([ { "Name": "subnet-id", "Values": [ cloudformation.get_output(stack, "PcfPublicSubnetId") ] } ]) command = [ 'ec2', 'describe-instances', '--filters', json.dumps(filters) ] reservations = json.loads(aws.aws_cli_verbose(command))["Reservations"] instances = [] for r in reservations: instances += r["Instances"] return instances