Ejemplo n.º 1
0
def get_ps_argument_spec(filename):
    # This uses a very small skeleton of Ansible.Basic.AnsibleModule to return the argspec defined by the module. This
    # is pretty rudimentary and will probably require something better going forward.
    pwsh = find_executable('pwsh')
    if not pwsh:
        raise FileNotFoundError(
            'Required program for PowerShell arg spec inspection "pwsh" not found.'
        )

    module_path = os.path.join(os.getcwd(), filename)
    b_module_path = to_bytes(module_path, errors='surrogate_or_strict')
    with open(b_module_path, mode='rb') as module_fd:
        b_module_data = module_fd.read()

    ps_dep_finder = PSModuleDepFinder()
    ps_dep_finder.scan_module(b_module_data)

    util_manifest = json.dumps({
        'module_path':
        to_text(module_path, errors='surrogiate_or_strict'),
        'ps_utils':
        dict([(name, info['path'])
              for name, info in ps_dep_finder.ps_modules.items()])
    })

    script_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               'ps_argspec.ps1')
    proc = subprocess.Popen([script_path, util_manifest],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            shell=False)
    stdout, stderr = proc.communicate()

    if proc.returncode != 0:
        raise AnsibleModuleImportError(stderr.decode('utf-8'))

    kwargs = json.loads(stdout)

    # the validate-modules code expects the options spec to be under the argument_spec key not options as set in PS
    kwargs['argument_spec'] = kwargs.pop('options', {})

    return kwargs['argument_spec'], (), kwargs
def get_ps_argument_spec(filename, collection):
    fqc_name = get_module_name_from_filename(filename, collection)

    pwsh = find_executable('pwsh')
    if not pwsh:
        raise FileNotFoundError(
            'Required program for PowerShell arg spec inspection "pwsh" not found.'
        )

    module_path = os.path.join(os.getcwd(), filename)
    b_module_path = to_bytes(module_path, errors='surrogate_or_strict')
    with open(b_module_path, mode='rb') as module_fd:
        b_module_data = module_fd.read()

    ps_dep_finder = PSModuleDepFinder()
    ps_dep_finder.scan_module(b_module_data, fqn=fqc_name)

    # For ps_argspec.ps1 to compile Ansible.Basic it also needs the AddType module_util.
    ps_dep_finder._add_module(name=b"Ansible.ModuleUtils.AddType",
                              ext=".psm1",
                              fqn=None,
                              optional=False,
                              wrapper=False)

    util_manifest = json.dumps({
        'module_path':
        to_text(module_path, errors='surrogiate_or_strict'),
        'ansible_basic':
        ps_dep_finder.cs_utils_module["Ansible.Basic"]['path'],
        'ps_utils': {
            name: info['path']
            for name, info in ps_dep_finder.ps_modules.items()
        }
    })

    script_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               'ps_argspec.ps1')
    proc = subprocess.Popen(['pwsh', script_path, util_manifest],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            shell=False)
    stdout, stderr = proc.communicate()

    if proc.returncode != 0:
        raise AnsibleModuleImportError(
            "STDOUT:\n%s\nSTDERR:\n%s" %
            (stdout.decode('utf-8'), stderr.decode('utf-8')))

    kwargs = json.loads(stdout)

    # the validate-modules code expects the options spec to be under the argument_spec key not options as set in PS
    kwargs['argument_spec'] = kwargs.pop('options', {})

    return kwargs['argument_spec'], kwargs