コード例 #1
0
def main():

    # Get and log the config from the Env variable
    config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"])
    INFO(config)

    # Get PC info from the config dict
    pc_info = config.get("tdaas_pc")
    pc_external_ip = pc_info.get("ips")[0][0]
    pc_internal_ip = pc_info.get("ips")[0][1]
    pc_password = pc_info.get("prism_password")

    try:

        # Create the API call body
        body = {"showback": True}

        # Make API call to enable marketplace
        resp = create_via_v3_post(pc_external_ip, "app_showback/enable",
                                  pc_password, body)

        # Log appropriately based on response
        if (resp.code == 200 or resp.code == 202):
            INFO("Showback enabled successfully.")
        else:
            raise Exception(f"Showback enable failed with:\n" +
                            f"Error Code: {resp.code}\n" +
                            f"Error Message: {resp.message}")

    except Exception as ex:
        INFO(ex)
コード例 #2
0
def main():

    # Get and log the config from the Env variable
    config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"])
    print(config)

    # Get PC info from the config dict
    pc_info = config.get("tdaas_pc")
    pc_external_ip = pc_info.get("ips")[0][0]
    pc_internal_ip = pc_info.get("ips")[0][1]
    pc_password = pc_info.get("prism_password")

    try:

        # Create the API call body
        body = {"enable_nutanix_apps": True}

        # Make API call to enable marketplace
        resp = create_via_v3_post(pc_external_ip, "marketplace_items/config",
                                  pc_password, body)

        # Log appropriately based on response
        if resp.code == 200 or resp.code == 202:
            print("Marketplace enabled successfully.")
        else:
            raise Exception(f"Marketplace enable failed with:\n" +
                            f"Resp: {resp}\n" + f"Error Code: {resp.code}\n" +
                            f"Error Message: {resp.message}")

    except Exception as ex:
        print(traceback.format_exc())
        sys.exit(1)
コード例 #3
0
def main(project_name):

    # Get and log the config from the Env variable
    config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"])
    print(config)

    # Get PC info from the config dict
    pc_info = config.get("tdaas_pc")
    pc_external_ip = pc_info.get("ips")[0][0]
    pc_internal_ip = pc_info.get("ips")[0][1]
    pc_password = pc_info.get("prism_password")

    try:

        # Read in the spec files and conver to dicts
        project_spec = file_to_dict("specs/calm_project.json")
        print(f"project_spec pre-update: {project_spec}")
        subnet_spec = file_to_dict("specs/calm_subnet.json")
        print(f"subnet_spec pre-update: {subnet_spec}")

        # Get our info from the infra
        subnet_info = get_subnet_info(pc_external_ip, pc_password,
                                      subnet_spec["entities"][0]["vlan"])
        account_info = body_via_v3_post(pc_external_ip, "accounts",
                                        pc_password, None)

        # Cycle through our accounts to find the right one
        for account in account_info.json["entities"]:
            if account["status"]["resources"]["type"] == "nutanix_pc":

                # Update our project_spec
                project_spec["spec"]["name"] = project_name
                project_spec["spec"]["resources"]["subnet_reference_list"][0][
                    "name"] = subnet_info["name"]
                project_spec["spec"]["resources"]["subnet_reference_list"][0][
                    "uuid"] = subnet_info["uuid"]
                project_spec["spec"]["resources"]["account_reference_list"][0][
                    "name"] = account["metadata"]["name"]
                project_spec["spec"]["resources"]["account_reference_list"][0][
                    "uuid"] = account["metadata"]["uuid"]
                print(f"project_spec post-update: {project_spec}")

                # Make API call to create project
                resp = create_via_v3_post(pc_external_ip, "projects",
                                          pc_password, project_spec)

                # Log appropriately based on response
                if resp.code == 200 or resp.code == 202:
                    print(
                        f"{project_spec['spec']['name']} Project created successfully."
                    )
                else:
                    raise Exception(
                        f"{project_spec['spec']['name']} Project create failed with:\n"
                        + f"Resp: {resp}\n" + f"Error Code: {resp.code}\n" +
                        f"Error Message: {resp.message}")

    except Exception as ex:
        print(traceback.format_exc())
        sys.exit(1)
コード例 #4
0
def main():

    # Get and log the config from the Env variable
    config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"])
    print(config)

    # Get PC info from the config dict
    pc_info = config.get("tdaas_pc")
    pc_external_ip = pc_info.get("ips")[0][0]
    pc_internal_ip = pc_info.get("ips")[0][1]
    pc_password = pc_info.get("prism_password")

    try:

        # Read in the spec file and conver to dict
        image_spec = file_to_dict("specs/pc_image.json")
        print(f"image_spec: {image_spec}")

        # Loop through each image
        for image in image_spec["entities"]:

            # Make API call to create image
            resp = create_via_v3_post(pc_external_ip, "images", pc_password,
                                      image)

            # Log appropriately based on response
            if resp.code == 200 or resp.code == 202:
                print(f"image['spec']['name'] Image created successfully.")
            else:
                raise Exception(
                    f"image['spec']['name'] Image create failed with:\n" +
                    f"Resp: {resp}\n" + f"Error Code: {resp.code}\n" +
                    f"Error Message: {resp.message}")

    except Exception as ex:
        print(traceback.format_exc())
        sys.exit(1)
コード例 #5
0
def main():

    # Get and log the config from the Env variable
    config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"])
    INFO(config)

    # Get PC info from the config dict
    pc_info = config.get("tdaas_pc")
    pc_external_ip = pc_info.get("ips")[0][0]
    pc_internal_ip = pc_info.get("ips")[0][1]
    pc_password = pc_info.get("prism_password")

    try:

        # Read in the spec files and convert to dicts
        env_spec = file_to_dict("specs/calm_environment.json")
        image_spec = file_to_dict("specs/pc_image.json")
        subnet_spec = file_to_dict("specs/calm_subnet.json")
        secret_spec = file_to_dict("specs/calm_secrets.json")

        # Get our subnet info from the infra
        subnet_info = get_subnet_info(pc_external_ip, pc_password,
                                      subnet_spec["entities"][0]["vlan"])
        INFO(f"subnet_uuid: {subnet_info['uuid']}")

        # Get our image info from the infra
        cent_image_name = image_spec["entities"][0]["metadata"]["name"]
        cent_image_uuid = uuid_via_v3_post(pc_external_ip, "images",
                                           pc_password, cent_image_name)
        INFO(f"cent_image_uuid: {cent_image_uuid}")
        # win_image_name = image_spec["entities"][1]["metadata"]["name"]
        # win_image_uuid = uuid_via_v3_post(pc_external_ip, "images",
        #                                   pc_password, win_image_name)
        # INFO(f"win_image_uuid: {win_image_uuid}")

        # Generate UUIDs for new components
        env_name = str(uuid.uuid4())
        env_uuid = str(uuid.uuid4())
        cent_substrate_uuid = str(uuid.uuid4())
        # win_substrate_uuid = str(uuid.uuid4())
        cent_key_uuid = str(uuid.uuid4())
        cent_pass_uuid = str(uuid.uuid4())
        # win_pass_uuid = str(uuid.uuid4())

        # Sub in our UUIDs and names:
        # env
        env_spec["spec"]["name"] = env_name
        env_spec["metadata"]["name"] = env_name
        env_spec["metadata"]["uuid"] = env_uuid

        # substrate
        env_spec["spec"]["resources"]["substrate_definition_list"][0][
            "uuid"] = cent_substrate_uuid
        # env_spec["spec"]["resources"]["substrate_definition_list"][1]\
        #        ["uuid"] = win_substrate_uuid

        # account
        env_spec["spec"]["resources"]["substrate_definition_list"][0][
            "readiness_probe"]["login_credential_local_reference"][
                "uuid"] = cent_key_uuid
        # env_spec["spec"]["resources"]["substrate_definition_list"][1]\
        #        ["readiness_probe"]["login_credential_local_reference"]\
        #        ["uuid"] = win_pass_uuid

        # subnet
        env_spec["spec"]["resources"]["substrate_definition_list"][0][
            "create_spec"]["resources"]["nic_list"][0]["subnet_reference"][
                "uuid"] = subnet_info["uuid"]
        # env_spec["spec"]["resources"]["substrate_definition_list"][1]\
        #        ["create_spec"]["resources"]["nic_list"][0]\
        #        ["subnet_reference"]["uuid"] = subnet_info["uuid"]

        # image
        env_spec["spec"]["resources"]["substrate_definition_list"][0][
            "create_spec"]["resources"]["disk_list"][0][
                "data_source_reference"]["name"] = cent_image_name
        env_spec["spec"]["resources"]["substrate_definition_list"][0][
            "create_spec"]["resources"]["disk_list"][0][
                "data_source_reference"]["uuid"] = cent_image_uuid
        # env_spec["spec"]["resources"]["substrate_definition_list"][1]\
        #        ["create_spec"]["resources"]["disk_list"][0]\
        #        ["data_source_reference"]["name"] = win_image_name
        # env_spec["spec"]["resources"]["substrate_definition_list"][1]\
        #        ["create_spec"]["resources"]["disk_list"][0]\
        #        ["data_source_reference"]["uuid"] = win_image_uuid

        # secrets
        for secret in secret_spec["entities"]:
            if secret["name"] == "CENTOS_KEY":
                suuid = cent_key_uuid
            elif secret["name"] == "CENTOS_PASS":
                suuid = cent_pass_uuid
            else:
                continue
            env_spec["spec"]["resources"]["credential_definition_list"].append(
                {
                    "name": secret["name"],
                    "type": secret["type"],
                    "username": secret["username"],
                    "secret": {
                        "attrs": {
                            "is_secret_modified": True
                        },
                        "value": secret["secret"],
                    },
                    "uuid": suuid,
                })

        # Make the API call to create the environment
        INFO(f"env_spec: {env_spec}")
        resp = create_via_v3_post(pc_external_ip, "environments", pc_password,
                                  env_spec)

        # Log appropriately based on response
        if resp.code == 200 or resp.code == 202:
            INFO(f"{env_spec['spec']['name']} Env created successfully.")
        else:
            raise Exception(
                f"{env_spec['spec']['name']} Env create failed with:\n" +
                f"Resp: {resp}\n" + f"Error Code: {resp.code}\n" +
                f"Error Message: {resp.message}")

    except Exception as ex:
        ERROR(traceback.format_exc())
コード例 #6
0
def main(launch):

    # Get and log the config from the Env variable
    config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"])
    print(config)

    # Get PC info from the config dict
    pc_info = config.get("tdaas_pc")
    pc_external_ip = pc_info.get("ips")[0][0]
    pc_internal_ip = pc_info.get("ips")[0][1]
    pc_password = pc_info.get("prism_password")

    try:

        # Read in the spec files and convert to dicts
        launch_spec = file_to_dict(f"specs/{launch}")
        print(f"launch_spec: {launch_spec}")

        # Loop through the blueprints to launch
        for launch in launch_spec["entities"]:

            # Get our blueprint uuid
            payload = {"filter": f"name=={launch['bp_name']}"}
            bp = body_via_v3_post(
                pc_external_ip, "blueprints", pc_password, payload
            ).json
            if bp["metadata"]["total_matches"] != 1:
                raise Exception(
                    str(bp["metadata"]["total_matches"])
                    + " blueprints found, when 1 should"
                    + " have been found."
                )
            else:
                bp_uuid = bp["entities"][0]["metadata"]["uuid"]

            # Get our runtime editables
            editables = body_via_v3_get(
                pc_external_ip,
                "blueprints",
                pc_password,
                bp_uuid + "/runtime_editables",
            ).json
            for profile in editables["resources"]:
                if profile["app_profile_reference"]["name"] == launch["profile_name"]:
                    profile_ref = profile["app_profile_reference"]
                    run_editables = profile["runtime_editables"]
            print(f"{launch['bp_name']} profile_ref: {profile_ref}")
            print(f"{launch['bp_name']} run_editables: {run_editables}")

            # Determine if this blueprint has an app dependency
            if "dependencies" in launch and len(launch["dependencies"]) > 0:
                # Get a list of running apps
                apps = body_via_v3_post(pc_external_ip, "apps", pc_password, None).json
                # Cycle through the apps
                for app in apps["entities"]:
                    # Cycle through our launch dependencies
                    for depend in launch["dependencies"]:
                        # Find the matching app name
                        if app["status"]["name"] == depend["app_name"]:
                            # Get app body
                            app_body = body_via_v3_get(
                                pc_external_ip,
                                "apps",
                                pc_password,
                                app["metadata"]["uuid"],
                            ).json
                            # Loop through our dependency values
                            for value in depend["values"]:
                                # Get value from our body+key combo
                                app_val = recursive_dict_lookup(app_body, value["keys"])
                                # Set our app_val to the appropriate variable value
                                for launch_var in launch["variables"]:
                                    if value["name"] == launch_var["name"]:
                                        launch_var["value"] = app_val
                print(f'{launch["bp_name"]} vars after depend: {launch["variables"]}')

            # Set our runtime variables
            if "variable_list" in run_editables:
                for run_edit_var in run_editables["variable_list"]:
                    for launch_var in launch["variables"]:
                        if run_edit_var["name"] == launch_var["name"]:
                            run_edit_var["value"]["value"] = launch_var["value"]
                print(f"{launch['bp_name']} run_editables: {run_editables}")

            # Create our payload and launch our app
            payload = {
                "spec": {
                    "app_name": launch["app_name"],
                    "app_description": launch["app_description"],
                    "app_profile_reference": profile_ref,
                    "runtime_editables": run_editables,
                }
            }
            resp = create_via_v3_post(
                pc_external_ip,
                "blueprints/" + bp_uuid + "/simple_launch",
                pc_password,
                payload,
            )

            # Log appropriately based on response
            if resp.code == 200 or resp.code == 202:
                print(f"{launch['app_name']} app launched successfully.")
            else:
                raise Exception(
                    f"{launch['app_name']} app launch failed with:\n"
                    + f"Resp: {resp}\n"
                    + f"Error Code: {resp.code}\n"
                    + f"Error Message: {resp.message}"
                )

            time.sleep(2)

    except Exception as ex:
        print(traceback.format_exc())
        sys.exit(1)
コード例 #7
0
def main():

    # Get and log the config from the Env variable
    config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"])
    INFO(config)

    # Get PC info from the config dict
    pc_info = config.get("tdaas_pc")
    pc_external_ip = pc_info.get("ips")[0][0]
    pc_internal_ip = pc_info.get("ips")[0][1]
    pc_password = pc_info.get("prism_password")

    try:

        # Read in the spec files and conver to dicts
        publish_spec = file_to_dict("specs/calm_bp_publish.json")
        INFO(f"publish_spec: {publish_spec}")

        # Get user and icon info
        user_info = body_via_v3_get(pc_external_ip, "users", pc_password,
                                    "me").json
        icon_info = body_via_v3_post(pc_external_ip, "app_icons", pc_password,
                                     None).json

        # Loop through the blueprints to upload
        for publish in publish_spec["entities"]:

            # Create our payload and get bp uuid
            payload = {"filter": f"name=={publish['bp_name']}"}
            bp_info = body_via_v3_post(pc_external_ip, "blueprints",
                                       pc_password, payload).json
            bp_uuid = bp_info["entities"][0]["metadata"]["uuid"]

            # Get bp spec with uuid
            bp = body_via_v3_get(
                pc_external_ip,
                "blueprints",
                pc_password,
                f"{bp_uuid}/export_json?keep_secrets=true",
            ).json

            # Modify our body
            bp["spec"]["name"] = publish["mp_name"]
            bp["spec"]["description"] = publish["mp_description"]
            bp["status"]["name"] = publish["mp_name"]
            bp["status"]["description"] = publish["mp_description"]
            spec = copy.deepcopy(bp["spec"])
            status = copy.deepcopy(bp["status"])
            del bp["spec"]["resources"]
            del bp["status"]
            bp["metadata"]["kind"] = "marketplace_item"
            bp["spec"]["resources"] = {"app_attribute_list": ["FEATURED"]}
            bp["spec"]["resources"]["app_state"] = "PENDING"
            bp["spec"]["resources"]["project_reference_list"] = []
            bp["spec"]["resources"]["change_log"] = ""
            bp["spec"]["resources"]["app_source"] = "LOCAL"
            bp["spec"]["resources"]["app_group_uuid"] = str(uuid.uuid4())
            bp["spec"]["resources"]["author"] = user_info["status"]["name"]
            for icon in icon_info["entities"]:
                if icon["status"]["name"] == publish["icon_name"]:
                    bp["spec"]["resources"]["icon_reference_list"] = [{
                        "icon_reference": {
                            "kind": "file_item",
                            "uuid": icon["metadata"]["uuid"],
                        },
                        "icon_type":
                        "ICON",
                    }]
            bp["spec"]["resources"]["version"] = publish["bp_version"]
            bp["spec"]["resources"]["app_blueprint_template"] = {"spec": spec}
            bp["spec"]["resources"]["app_blueprint_template"][
                "status"] = status

            # Upload our marketplace item
            resp = create_via_v3_post(pc_external_ip, "calm_marketplace_items",
                                      pc_password, bp)

            # Log appropriately based on response
            if resp.code == 200 or resp.code == 202:
                INFO(f"{publish['mp_name']} bp published successfully.")
            else:
                raise Exception(
                    f"{publish['mp_name']} bp publish failed with:\n" +
                    f"Resp: {resp}\n" + f"Error Code: {resp.code}\n" +
                    f"Error Message: {resp.message}")

    except Exception as ex:
        ERROR(traceback.format_exc())
コード例 #8
0
def main():

    # Get and log the config from the Env variable
    config = json.loads(os.environ["CUSTOM_SCRIPT_CONFIG"])
    INFO(config)

    # Get PC info from the config dict
    pc_info = config.get("tdaas_pc")
    pc_external_ip = pc_info.get("ips")[0][0]
    pc_internal_ip = pc_info.get("ips")[0][1]
    pc_password = pc_info.get("prism_password")

    try:

        # Read in the spec files and convert to dicts
        launch_spec = file_to_dict("specs/calm_bp_launch.spec")
        INFO(f"launch_spec: {launch_spec}")

        # Loop through the blueprints to launch
        for launch in launch_spec["entities"]:

            # Get our blueprint uuid
            payload = {"filter": f"name=={launch['bp_name']}"}
            bp = body_via_v3_post(pc_external_ip, "blueprints", pc_password,
                                  payload).json
            if bp["metadata"]["total_matches"] != 1:
                raise Exception(
                    str(bp["metadata"]["total_matches"]) +
                    " blueprints found, when 1 should" + " have been found.")
            else:
                bp_uuid = bp["entities"][0]["metadata"]["uuid"]

            # Get our runtime editables
            editables = body_via_v3_get(pc_external_ip, "blueprints",
                                        pc_password,
                                        bp_uuid + "/runtime_editables").json
            for profile in editables["resources"]:
                if profile["app_profile_reference"]["name"] ==\
                   launch["profile_name"]:
                    profile_ref = profile["app_profile_reference"]
                    run_editables = profile["runtime_editables"]
            INFO(f"{launch['bp_name']} profile_ref: {profile_ref}")
            INFO(f"{launch['bp_name']} run_editables: {run_editables}")

            # Set our runtime variables
            if "variable_list" in run_editables:
                for run_edit_var in run_editables["variable_list"]:
                    for launch_var in launch["variables"]:
                        if run_edit_var["name"] == launch_var["name"]:
                            run_edit_var["value"]["value"] = launch_var[
                                "value"]
                INFO(f"{launch['bp_name']} run_editables: {run_editables}")

            # Create our payload and launch our app
            payload = {
                "spec": {
                    "app_name": launch["app_name"],
                    "app_description": launch["app_description"],
                    "app_profile_reference": profile_ref,
                    "runtime_editables": run_editables
                }
            }
            resp = create_via_v3_post(
                pc_external_ip, "blueprints/" + bp_uuid + "/simple_launch",
                pc_password, payload)

            # Log appropriately based on response
            if (resp.code == 200 or resp.code == 202):
                INFO(f"{launch['app_name']} app launched successfully.")
            else:
                raise Exception(f"{launch['app_name']} app launch" +
                                f" failed with:\n" +
                                f"Error Code: {resp.code}\n" +
                                f"Error Message: {resp.message}")

            time.sleep(2)

    except Exception as ex:
        INFO(ex)