Ejemplo n.º 1
0
def providers_push(socket_path, api_version, args, show_json=False, testmode=0):
    """Add a new provider profile or overwrite an existing one

    :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

    providers push <profile.toml>

    """
    if len(args) == 0:
        log.error("push is missing the profile TOML file")
        return 1
    if not os.path.exists(args[0]):
        log.error("Missing profile TOML file: %s", args[0])
        return 1

    api_route = client.api_url(api_version, "/upload/providers/save")
    profile = toml.load(args[0])
    result = client.post_url_json(socket_path, api_route, json.dumps(profile))
    return handle_api_result(result, show_json)[0]
Ejemplo n.º 2
0
def run_module():
    """Module main function
    """
    # define available arguments/parameters a user can pass to the module
    module_args = dict(
        blueprint_name=dict(type='str', required=True),
        compose_type=dict(type='str', required=True),
        ref=dict(type='str', required=False, default='rhel/8/x86_64/edge'),
        parent=dict(type='str', required=False, default=''),
        wait=dict(type='bool', required=False, default=False),
    )

    # 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=True, 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, 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)

    compose_params = {
        'blueprint_name': module.params['blueprint_name'],
        'compose_type': module.params['compose_type'],
        'ref': module.params['ref'],
        'parent': module.params['parent']
    }
    ret = client.post_url_json(SOCKET, '/api/v1/compose',
                               json.dumps(compose_params))
    build_id = ret['build_id']
    if module.params['wait']:
        while True:
            ret = client.get_url_json(SOCKET,
                                      '/api/v1/compose/info/' + build_id)
            if ret['queue_status'] not in ('RUNNING', 'QUEUED'):
                break
            time.sleep(1)

    result['ansible_module_results'] = client.get_url_json(
        SOCKET, '/api/v1/compose/info/' + build_id)

    if result['ansible_module_results']['queue_status'] == 'FAILED':
        result['failed'] = True
    # 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)
Ejemplo n.º 3
0
Archivo: upload.py Proyecto: whot/lorax
def upload_start(socket_path, api_version, args, show_json=False, testmode=0):
    """Start upload up a build uuid image

    :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

    upload start <build-uuid> <image-name> [<provider> <profile> | <profile.toml>]
    """
    if len(args) == 0:
        log.error("start is missing the compose build id")
        return 1
    if len(args) == 1:
        log.error("start is missing the image name")
        return 1
    if len(args) == 2:
        log.error("start is missing the provider and profile details")
        return 1

    body = {"image_name": args[1]}
    if len(args) == 3:
        try:
            body.update(toml.load(args[2]))
        except toml.TomlDecodeError as e:
            log.error(str(e))
            return 1
    elif len(args) == 4:
        body["provider"] = args[2]
        body["profile"] = args[3]
    else:
        log.error("start has incorrect number of arguments")
        return 1

    api_route = client.api_url(api_version, "/compose/uploads/schedule/%s" % args[0])
    result = client.post_url_json(socket_path, api_route, json.dumps(body))
    (rc, exit_now) = handle_api_result(result, show_json)
    if exit_now:
        return rc

    print("Upload %s added to the queue" % result["upload_id"])
    return rc
Ejemplo n.º 4
0
def compose_start(socket_path, api_version, args, show_json=False, testmode=0):
    """Start a new compose using the selected blueprint and type

    :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: Set to 1 to simulate a failed compose, set to 2 to simulate a finished one.
    :type testmode: int

    compose start <blueprint-name> <compose-type>
    """
    if len(args) == 0:
        log.error("start is missing the blueprint name and output type")
        return 1
    if len(args) == 1:
        log.error("start is missing the output type")
        return 1

    config = {
        "blueprint_name": args[0],
        "compose_type": args[1],
        "branch": "master"
        }
    if testmode:
        test_url = "?test=%d" % testmode
    else:
        test_url = ""
    api_route = client.api_url(api_version, "/compose" + test_url)
    result = client.post_url_json(socket_path, api_route, json.dumps(config))

    if show_json:
        print(json.dumps(result, indent=4))
        return 0

    for err in result.get("errors", []):
        log.error(err)

    if result["status"] == False or result.get("errors", False):
        return 1

    print("Compose %s added to the queue" % result["build_id"])
    return 0
Ejemplo n.º 5
0
Archivo: upload.py Proyecto: whot/lorax
def upload_reset(socket_path, api_version, args, show_json=False, testmode=0):
    """Reset the upload and execute it again

    :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

    upload reset <build-uuid>
    """
    if len(args) == 0:
        log.error("reset is missing the upload uuid")
        return 1

    api_route = client.api_url(api_version, "/upload/reset/%s" % args[0])
    result = client.post_url_json(socket_path, api_route, json.dumps({}))
    return handle_api_result(result, show_json)[0]
Ejemplo n.º 6
0
def compose_ostree(socket_path,
                   api_version,
                   args,
                   show_json=False,
                   testmode=0,
                   api=None):
    """Start a new ostree compose using the selected blueprint and type

    :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: Set to 1 to simulate a failed compose, set to 2 to simulate a finished one.
    :type testmode: int
    :param api: Details about the API server, "version" and "backend"
    :type api: dict

    compose start-ostree [--size XXXX] [--parent PARENT] [--ref REF] <BLUEPRINT> <TYPE> [<IMAGE-NAME> <PROFILE.TOML>]
    """
    if api == None:
        log.error("Missing api version/backend")
        return 1

    if api["backend"] == "lorax-composer":
        log.warning("lorax-composer doesn not support start-ostree.")
        return 1

    # Get the optional size before checking other parameters
    try:
        args, size = get_size(args)
        args, parent = get_parent(args)
        args, ref = get_ref(args)
    except (RuntimeError, ValueError) as e:
        log.error(str(e))
        return 1

    if len(args) == 0:
        log.error(
            "start-ostree is missing the blueprint name, output type, and ostree details"
        )
        return 1
    if len(args) == 1:
        log.error("start-ostree is missing the output type")
        return 1
    if len(args) == 3:
        log.error("start-ostree is missing the provider TOML file")
        return 1

    config = {
        "blueprint_name": args[0],
        "compose_type": args[1],
        "branch": "master",
        "ostree": {
            "ref": ref,
            "parent": parent
        },
    }
    if size > 0:
        config["size"] = size

    if len(args) == 4:
        config["upload"] = {"image_name": args[2]}
        # profile TOML file (maybe)
        try:
            config["upload"].update(toml.load(args[3]))
        except toml.TomlDecodeError as e:
            log.error(str(e))
            return 1

    if testmode:
        test_url = "?test=%d" % testmode
    else:
        test_url = ""
    api_route = client.api_url(api_version, "/compose" + test_url)
    result = client.post_url_json(socket_path, api_route, json.dumps(config))
    (rc, exit_now) = handle_api_result(result, show_json)
    if exit_now:
        return rc

    print("Compose %s added to the queue" % result["build_id"])

    if "upload_id" in result and result["upload_id"]:
        print("Upload %s added to the upload queue" % result["upload_id"])

    return rc
Ejemplo n.º 7
0
def compose_start(socket_path, api_version, args, show_json=False, testmode=0):
    """Start a new compose using the selected blueprint and type

    :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: Set to 1 to simulate a failed compose, set to 2 to simulate a finished one.
    :type testmode: int

    compose start <blueprint-name> <compose-type> [<image-name> <provider> <profile> | <image-name> <profile.toml>]
    """
    if len(args) == 0:
        log.error("start is missing the blueprint name and output type")
        return 1
    if len(args) == 1:
        log.error("start is missing the output type")
        return 1
    if len(args) == 3:
        log.error("start is missing the provider and profile details")
        return 1

    config = {
        "blueprint_name": args[0],
        "compose_type": args[1],
        "branch": "master"
    }
    if len(args) == 4:
        config["upload"] = {"image_name": args[2]}
        # profile TOML file (maybe)
        try:
            config["upload"].update(toml.load(args[3]))
        except toml.TomlDecodeError as e:
            log.error(str(e))
            return 1
    elif len(args) == 5:
        config["upload"] = {
            "image_name": args[2],
            "provider": args[3],
            "profile": args[4]
        }

    if testmode:
        test_url = "?test=%d" % testmode
    else:
        test_url = ""
    api_route = client.api_url(api_version, "/compose" + test_url)
    result = client.post_url_json(socket_path, api_route, json.dumps(config))
    (rc, exit_now) = handle_api_result(result, show_json)
    if exit_now:
        return rc

    print("Compose %s added to the queue" % result["build_id"])

    if "upload_id" in result and result["upload_id"]:
        print("Upload %s added to the upload queue" % result["upload_id"])

    return rc