Exemple #1
0
def create_runbook_command(runbook_file, name, description, force):
    """Creates a runbook"""

    client = get_api_client()

    if runbook_file.endswith(".json"):
        res, err = create_runbook_from_json(client,
                                            runbook_file,
                                            name=name,
                                            description=description,
                                            force_create=force)
    elif runbook_file.endswith(".py"):
        res, err = create_runbook_from_dsl(client,
                                           runbook_file,
                                           name=name,
                                           description=description,
                                           force_create=force)
    else:
        LOG.error("Unknown file format {}".format(runbook_file))
        return

    if err:
        LOG.error(err["error"])
        return

    runbook = res.json()
    runbook_uuid = runbook["metadata"]["uuid"]
    runbook_name = runbook["metadata"]["name"]
    runbook_status = runbook.get("status", {})
    runbook_state = runbook_status.get("state", "DRAFT")
    LOG.debug("Runbook {} has state: {}".format(runbook_name, runbook_state))

    if runbook_state != "ACTIVE":
        msg_list = runbook_status.get("message_list", [])
        if not msg_list:
            LOG.error("Runbook {} created with errors.".format(runbook_name))
            LOG.debug(json.dumps(runbook_status))
            sys.exit(-1)

        msgs = []
        for msg_dict in msg_list:
            msgs.append(msg_dict.get("message", ""))

        LOG.error("Runbook {} created with {} error(s): {}".format(
            runbook_name, len(msg_list), msgs))
        sys.exit(-1)

    LOG.info("Runbook {} created successfully.".format(runbook_name))
    config = get_config()
    pc_ip = config["SERVER"]["pc_ip"]
    pc_port = config["SERVER"]["pc_port"]
    link = "https://{}:{}/console/#page/explore/calm/runbooks/{}".format(
        pc_ip, pc_port, runbook_uuid)
    stdout_dict = {
        "name": runbook_name,
        "link": link,
        "state": runbook_state,
    }
    click.echo(json.dumps(stdout_dict, indent=4, separators=(",", ": ")))
Exemple #2
0
def describe_runbook(runbook_name, out):
    """Displays runbook data"""

    client = get_api_client()
    runbook = get_runbook(client, runbook_name, all=True)

    res, err = client.runbook.read(runbook["metadata"]["uuid"])
    if err:
        raise Exception("[{}] - {}".format(err["code"], err["error"]))

    runbook = res.json()

    if out == "json":
        runbook.pop("status", None)
        click.echo(json.dumps(runbook, indent=4, separators=(",", ": ")))
        return

    click.echo("\n----Runbook Summary----\n")
    click.echo(
        "Name: "
        + highlight_text(runbook_name)
        + " (uuid: "
        + highlight_text(runbook["metadata"]["uuid"])
        + ")"
    )
    click.echo("Description: " + highlight_text(runbook["status"]["description"]))
    click.echo("Status: " + highlight_text(runbook["status"]["state"]))
    click.echo(
        "Owner: " + highlight_text(runbook["metadata"]["owner_reference"]["name"]),
        nl=False,
    )
    project = runbook["metadata"].get("project_reference", {})
    click.echo(" Project: " + highlight_text(project.get("name", "")))

    created_on = int(runbook["metadata"]["creation_time"]) // 1000000
    past = arrow.get(created_on).humanize()
    click.echo(
        "Created: {} ({})".format(
            highlight_text(time.ctime(created_on)), highlight_text(past)
        )
    )
    last_updated = int(runbook["metadata"]["last_update_time"]) // 1000000
    past = arrow.get(last_updated).humanize()
    click.echo(
        "Last Updated: {} ({})\n".format(
            highlight_text(time.ctime(last_updated)), highlight_text(past)
        )
    )
    runbook_resources = runbook.get("status").get("resources", {})
    runbook_dict = runbook_resources.get("runbook", {})

    click.echo("Runbook :")

    task_list = runbook_dict.get("task_definition_list", [])
    task_map = {}
    for task in task_list:
        task_map[task.get("uuid")] = task

    # creating task tree for runbook
    main_task = runbook_dict.get("main_task_local_reference").get("uuid")
    root = addTaskNodes(main_task, task_map)
    for pre, _, node in RenderTree(root):
        displayTaskNode(node, pre)

    click.echo("\n")

    variable_types = [
        var["label"] if var.get("label", "") else var.get("name")
        for var in runbook_dict.get("variable_list", [])
    ]
    click.echo("\tVariables [{}]:".format(highlight_text(len(variable_types))))
    click.echo("\t\t{}\n".format(highlight_text(", ".join(variable_types))))

    credential_types = [
        "{} ({})".format(cred.get("name", ""), cred.get("type", ""))
        for cred in runbook_resources.get("credential_definition_list", [])
    ]
    click.echo("Credentials [{}]:".format(highlight_text(len(credential_types))))
    click.echo("\t{}\n".format(highlight_text(", ".join(credential_types))))

    default_target = runbook_resources.get("default_target_reference", {}).get(
        "name", "-"
    )
    click.echo("Default Endpoint Target: {}\n".format(highlight_text(default_target)))