コード例 #1
0
def get_job_info(module, jobId, return_output):
    result = dict()
    try:
        output = query_jobs_status(jobId)
    except SubmitJCLError:
        raise

    result = job_output(module, job_id=jobId)

    if not return_output:
        for job in result.get("jobs", []):
            job["ddnames"] = []

    result["changed"] = True

    return result
コード例 #2
0
def run_module():

    module_args = dict(
        src=dict(type="str", required=True),
        wait=dict(type="bool", required=False),
        location=dict(
            type="str",
            default="DATA_SET",
            choices=["DATA_SET", "USS", "LOCAL"],
        ),
        encoding=dict(
            type="str",
            default="UTF-8",
            choices=[
                "UTF-8", "ASCII", "ISO-8859-1", "EBCDIC", "IBM-037", "IBM-1047"
            ],
        ),
        volume=dict(type="str", required=False),
        return_output=dict(type="bool", required=False, default=True),
        wait_time_s=dict(type="int", default=60),
        max_rc=dict(type="int", required=False),
        temp_file=dict(type="path", required=False),
    )

    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    arg_defs = dict(
        src=dict(arg_type="data_set_or_path", required=True),
        wait=dict(arg_type="bool", required=False),
        location=dict(
            arg_type="str",
            default="DATA_SET",
            choices=["DATA_SET", "USS", "LOCAL"],
        ),
        encoding=dict(arg_type="encoding", default="UTF-8"),
        volume=dict(arg_type="volume", required=False),
        return_output=dict(arg_type="bool", default=True),
        wait_time_s=dict(arg_type="int", required=False, default=60),
        max_rc=dict(arg_type="int", required=False),
        temp_file=dict(arg_type="path", required=False),
    )

    parser = BetterArgParser(arg_defs)
    parsed_args = parser.parse_args(module.params)

    result = dict(changed=False)

    location = parsed_args.get("location")
    volume = parsed_args.get("volume")
    wait = parsed_args.get("wait")
    src = parsed_args.get("src")
    return_output = parsed_args.get("return_output")
    wait_time_s = parsed_args.get("wait_time_s")
    max_rc = parsed_args.get("max_rc")
    # get temporary file names for copied files
    temp_file = parsed_args.get("temp_file")
    if temp_file:
        temp_file_2 = NamedTemporaryFile(delete=True)

    if wait_time_s <= 0:
        module.fail_json(
            msg=
            "The option wait_time_s is not valid it just be greater than 0.",
            **result)

    DSN_REGEX = r"^(([A-Z]{1}[A-Z0-9]{0,7})([.]{1})){1,21}[A-Z]{1}[A-Z0-9]{0,7}([(]([A-Z]{1}[A-Z0-9]{0,7})[)]){0,1}?$"

    try:
        if location == "DATA_SET":
            data_set_name_pattern = re.compile(DSN_REGEX, re.IGNORECASE)
            check = data_set_name_pattern.fullmatch(src)
            if check:
                if volume is None or volume == "":
                    jobId = submit_pds_jcl(src, module)
                else:
                    jobId = submit_jcl_in_volume(src, volume, module)
            else:
                module.fail_json(
                    msg=
                    "The parameter src for data set is not a valid name pattern. Please check the src input.",
                    **result)
        elif location == "USS":
            jobId = submit_uss_jcl(src, module)
        else:
            # For local file, it has been copied to the temp directory in action plugin.
            encoding = parsed_args.get("encoding")
            if encoding == "EBCDIC" or encoding == "IBM-037" or encoding == "IBM-1047":
                jobId = submit_uss_jcl(temp_file, module)
            # 'UTF-8' 'ASCII' encoding will be converted.
            elif (encoding == "UTF-8" or encoding == "ISO-8859-1"
                  or encoding == "ASCII" or encoding is None):
                (conv_rc, stdout, stderr) = module.run_command(
                    "iconv -f ISO8859-1 -t IBM-1047 %s > %s" %
                    (quote(temp_file), quote(temp_file_2.name)),
                    use_unsafe_shell=True,
                )
                if conv_rc == 0:
                    jobId = submit_uss_jcl(temp_file_2.name, module)
                else:
                    module.fail_json(
                        msg=
                        "The Local file encoding conversion failed. Please check the source file."
                        + stderr,
                        **result)
            else:
                module.fail_json(msg=(
                    "The Local file encoding format is not supported."
                    "The supported encoding is UTF-8, ASCII, ISO-8859-1, EBCDIC, IBM-037, IBM-1047. Default is UTF-8."
                ),
                                 **result)
    except SubmitJCLError as e:
        module.fail_json(msg=repr(e), **result)
    if jobId is None or jobId == "":
        result["job_id"] = jobId
        module.fail_json(
            msg=
            "JOB ID RETURNED IS None. PLEASE CHECK WHETHER THE JCL IS CORRECT.",
            **result)

    result["job_id"] = jobId
    if wait is True:
        # calculate the job elapse time
        duration = 0
        try:
            waitJob = query_jobs_status(module, jobId)
            job_msg = waitJob[0].get("ret_code").get("msg")
        except SubmitJCLError as e:
            module.fail_json(msg=repr(e), **result)
        # while (job_msg.startswith("CC") or job_msg.startswith("ABEND")) is False:
        while not re.search(
                "^(?:{0})".format("|".join(JOB_COMPLETION_MESSAGES)), job_msg):
            sleep(1)
            duration = duration + 1
            waitJob = job_output(job_id=jobId)
            job_msg = waitJob[0].get("ret_code").get("msg")
            if re.search("^(?:{0})".format("|".join(JOB_COMPLETION_MESSAGES)),
                         job_msg):
                break
            if duration == wait_time_s:  # Long running task. timeout return
                break

    try:
        result = get_job_info(module, jobId, return_output)
        if wait is True and return_output is True and max_rc is not None:
            assert_valid_return_code(
                max_rc,
                result.get("jobs")[0].get("ret_code").get("code"))
    except SubmitJCLError as e:
        module.fail_json(msg=repr(e), **result)
    except Exception as e:
        module.fail_json(msg=repr(e), **result)
    finally:
        if temp_file:
            remove(temp_file)
    result["duration"] = duration
    if duration == wait_time_s:
        result["message"] = {
            "stdout":
            "Submit JCL operation succeeded but it is a long running job. Timeout is "
            + str(wait_time_s) + " seconds."
        }
    else:
        result["message"] = {"stdout": "Submit JCL operation succeeded."}
    result["changed"] = True
    module.exit_json(**result)