Exemplo n.º 1
0
def blueprints_freeze_save(socket_path, api_version, args, show_json=False):
    """Save the frozen blueprint to a TOML file

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param api_version: Version of the API to talk to. eg. "0"
    :type api_version: str
    :param args: List of remaining arguments from the cmdline
    :type args: list of str
    :param show_json: Set to True to show the JSON output instead of the human readable output
    :type show_json: bool

    blueprints freeze save <blueprint,...> Save the frozen blueprint to a file, <blueprint-name>.frozen.toml.
    """
    if len(args) == 0:
        log.error("freeze save is missing the blueprint name")
        return 1

    for blueprint in argify(args):
        api_route = client.api_url(
            api_version, "/blueprints/freeze/%s?format=toml" % blueprint)
        blueprint_toml = client.get_url_raw(socket_path, api_route)
        open(frozen_toml_filename(blueprint), "w").write(blueprint_toml)

    return 0
Exemplo n.º 2
0
def sources_info(socket_path, api_version, args, show_json=False):
    """Output info on a list of projects

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param api_version: Version of the API to talk to. eg. "0"
    :type api_version: str
    :param args: List of remaining arguments from the cmdline
    :type args: list of str
    :param show_json: Set to True to show the JSON output instead of the human readable output
    :type show_json: bool

    sources info <source-name>
    """
    if len(args) == 0:
        log.error("sources info is missing the name of the source")
        return 1

    if show_json:
        api_route = client.api_url(api_version, "/projects/source/info/%s" % ",".join(args))
        result = client.get_url_json(socket_path, api_route)
        print(json.dumps(result, indent=4))
        return 0
    else:
        api_route = client.api_url(api_version, "/projects/source/info/%s?format=toml" % ",".join(args))
        result = client.get_url_raw(socket_path, api_route)
        print(result)
    return 0
Exemplo n.º 3
0
def compose_log(socket_path,
                api_version,
                args,
                show_json=False,
                testmode=0,
                api=None):
    """Show the last part of the compose log

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param api_version: Version of the API to talk to. eg. "0"
    :type api_version: str
    :param args: List of remaining arguments from the cmdline
    :type args: list of str
    :param show_json: Set to True to show the JSON output instead of the human readable output
    :type show_json: bool
    :param testmode: unused in this function
    :type testmode: int

    compose log <uuid> [<size>kB]

    This will display the last 1kB of the compose's log file. Can be used to follow progress
    during the build.
    """
    if len(args) == 0:
        log.error("log is missing the compose build id")
        return 1
    if len(args) == 2:
        try:
            log_size = int(args[1])
        except ValueError:
            log.error("Log size must be an integer.")
            return 1
    else:
        log_size = 1024

    api_route = client.api_url(api_version,
                               "/compose/log/%s?size=%d" % (args[0], log_size))
    try:
        result = client.get_url_raw(socket_path, api_route)
    except RuntimeError as e:
        print(str(e))
        return 1

    print(result)
    return 0
Exemplo n.º 4
0
def blueprints_show(socket_path, api_version, args, show_json=False):
    """Show the blueprints, in TOML format

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param api_version: Version of the API to talk to. eg. "0"
    :type api_version: str
    :param args: List of remaining arguments from the cmdline
    :type args: list of str
    :param show_json: Set to True to show the JSON output instead of the human readable output
    :type show_json: bool

    blueprints show <blueprint,...>        Display the blueprint in TOML format.

    Multiple blueprints will be separated by \n\n
    """
    for blueprint in argify(args):
        api_route = client.api_url(api_version, "/blueprints/info/%s?format=toml" % blueprint)
        print(client.get_url_raw(socket_path, api_route) + "\n\n")

    return 0
Exemplo n.º 5
0
def blueprints_save(socket_path, api_version, args, show_json=False):
    """Save the blueprint to a TOML file

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param api_version: Version of the API to talk to. eg. "0"
    :type api_version: str
    :param args: List of remaining arguments from the cmdline
    :type args: list of str
    :param show_json: Set to True to show the JSON output instead of the human readable output
    :type show_json: bool

    blueprints save <blueprint,...>        Save the blueprint to a file, <blueprint-name>.toml
    """
    for blueprint in argify(args):
        api_route = client.api_url(api_version, "/blueprints/info/%s?format=toml" % blueprint)
        blueprint_toml = client.get_url_raw(socket_path, api_route)
        with open(toml_filename(blueprint), "w") as f:
            f.write(blueprint_toml)

    return 0
Exemplo n.º 6
0
def blueprints_freeze_show(socket_path, api_version, args, show_json=False):
    """Show the frozen blueprint in TOML format

    :param socket_path: Path to the Unix socket to use for API communication
    :type socket_path: str
    :param api_version: Version of the API to talk to. eg. "0"
    :type api_version: str
    :param args: List of remaining arguments from the cmdline
    :type args: list of str
    :param show_json: Set to True to show the JSON output instead of the human readable output
    :type show_json: bool

    blueprints freeze show <blueprint,...> Display the frozen blueprint in TOML format.
    """
    if len(args) == 0:
        log.error("freeze show is missing the blueprint name")
        return 1

    for blueprint in argify(args):
        api_route = client.api_url(api_version, "/blueprints/freeze/%s?format=toml" % blueprint)
        print(client.get_url_raw(socket_path, api_route))

    return 0
def run_module():
    """Module main function
    """
    # define available arguments/parameters a user can pass to the module
    module_args = dict(
        name=dict(type='str', required=False),
        definition=dict(type='str', required=False),
        state=dict(type='str', required=False, default='present'),
    )

    mutually_exclusive = [('name', 'definition')]

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, ansible_module_results='')

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=module_args,
        mutually_exclusive=mutually_exclusive,
        supports_check_mode=True,
    )

    # if the user is working with this module in only check mode we do not
    # want to make any changes to the environment, just return the current
    # state with no modifications
    if module.check_mode:
        module.exit_json(**result)

    # Figure out the name of the blueprint, either from the name or the TOML definition
    if module.params['name']:
        blueprint_name = module.params['name']
    else:
        blueprint_name = toml.loads(module.params['definition'])['name']

    result['blueprint_name'] = blueprint_name

    # See what is already present

    blueprint_present = True
    try:
        available_blueprint = client.get_url_raw(
            SOCKET,
            '/api/v1/blueprints/info/' + blueprint_name + '?format=toml')
    except RuntimeError:
        # The composer.http_client throws a Runtime Error if the API returns 400
        blueprint_present = False

    if module.params['state'] == 'present' and not blueprint_present:
        result['ansible_module_results'] = client.post_url_toml(
            SOCKET, '/api/v1/blueprints/new', module.params['definition'])
        result['ansible_module_results']['definition'] = client.get_url_raw(
            SOCKET,
            '/api/v1/blueprints/info/' + blueprint_name + '?format=toml')
        result['changed'] = True
    elif module.params['state'] == 'present' \
                                and blueprint_present \
                                and available_blueprint != module.params['definition']:
        result['ansible_module_results'] = client.post_url_toml(
            SOCKET, '/api/v1/blueprints/new', module.params['definition'])
        result['ansible_module_results']['definition'] = client.get_url_raw(
            SOCKET,
            '/api/v1/blueprints/info/' + blueprint_name + '?format=toml')
        result['changed'] = True
    elif module.params['state'] == 'absent' and blueprint_present:
        result['ansible_module_results'] = client.delete_url_json(
            SOCKET, '/api/v1/blueprints/delete/' + blueprint_name)
        result['changed'] = True
    else:
        result['ansible_module_results']['definition'] = client.get_url_raw(
            SOCKET,
            '/api/v1/blueprints/info/' + blueprint_name + '?format=toml')
        result['changed'] = False

    # in the event of a successful module execution, you will want to
    # simple AnsibleModule.exit_json(), passing the key/value results
    module.exit_json(**result)