コード例 #1
0
def main():
    module = ForemanAnsibleModule(
        argument_spec=dict(
            src=dict(required=True, type='path', aliases=['file']),
            repository=dict(required=True),
            product=dict(required=True),
            organization=dict(required=True),
        ),
        supports_check_mode=True,
    )

    entity_dict = module.parse_params()

    module.connect()

    entity_dict['organization'] = find_organization(
        module, name=entity_dict['organization'])
    entity_dict['product'] = find_product(
        module,
        name=entity_dict['product'],
        organization=entity_dict['organization'])
    entity_dict['repository'] = find_repository(module,
                                                name=entity_dict['repository'],
                                                product=entity_dict['product'])

    content_unit = None
    if entity_dict['repository'].content_type == "yum":
        name, version, release, arch = check_output(
            "rpm --queryformat '%%{NAME} %%{VERSION} %%{RELEASE} %%{ARCH}' -qp %s"
            % entity_dict['src'],
            shell=True).decode('ascii').split()
        query = "name = \"{}\" and version = \"{}\" and release = \"{}\" and arch = \"{}\"".format(
            name, version, release, arch)
        content_unit = find_package(module,
                                    query,
                                    repository=entity_dict['repository'],
                                    failsafe=True)
    elif entity_dict['repository'].content_type == "file":
        h = hashlib.sha256()
        with open(entity_dict['src'], "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                h.update(chunk)
        checksum = h.hexdigest()
        name = os.path.basename(entity_dict['src'])
        query = "name = \"{}\" and checksum = \"{}\"".format(name, checksum)
        content_unit = find_file(module,
                                 query,
                                 repository=entity_dict['repository'],
                                 failsafe=True)

    changed = False
    if not content_unit:
        try:
            changed = upload(module, entity_dict['src'],
                             entity_dict['repository'])
        except Exception as e:
            module.fail_json(msg=to_native(e))

    module.exit_json(changed=changed)
コード例 #2
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            server_url=dict(required=True),
            username=dict(required=True, no_log=True),
            password=dict(required=True, no_log=True),
            verify_ssl=dict(type='bool', default=True),
            src=dict(required=True, type='path', aliases=['file']),
            repository=dict(required=True),
            product=dict(required=True),
            organization=dict(required=True),
        ),
        supports_check_mode=True,
    )

    if has_import_error:
        module.fail_json(msg=import_error_msg)

    entity_dict = dict(
        [(k, v) for (k, v) in module.params.items() if v is not None])

    server_url = entity_dict.pop('server_url')
    username = entity_dict.pop('username')
    password = entity_dict.pop('password')
    verify_ssl = entity_dict.pop('verify_ssl')

    try:
        create_server(server_url, (username, password), verify_ssl)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

    ping_server(module)

    name, version, release, arch = check_output("rpm --queryformat '%%{NAME} %%{VERSION} %%{RELEASE} %%{ARCH}' -qp %s" % entity_dict['src'],
                                                shell=True).split()

    entity_dict['organization'] = find_organization(module, name=entity_dict['organization'])
    entity_dict['product'] = find_product(module, name=entity_dict['product'], organization=entity_dict['organization'])
    entity_dict['repository'] = find_repository(module, name=entity_dict['repository'], product=entity_dict['product'])

    package = False
    if entity_dict['repository'].content_type == "yum":
        query = "name = \"{}\" and version = \"{}\" and release = \"{}\" and arch = \"{}\"".format(name, version, release, arch)
        package = find_package(module, query, repository=entity_dict['repository'], failsafe=True)

    changed = False
    if package is None or package is False:
        try:
            changed = upload(module, entity_dict['src'], entity_dict['repository'])
        except Exception as e:
            module.fail_json(msg=to_native(e))

    module.exit_json(changed=changed)
コード例 #3
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            server_url=dict(required=True),
            username=dict(required=True, no_log=True),
            password=dict(required=True, no_log=True),
            verify_ssl=dict(type='bool', default=True),
            src=dict(required=True, type='path', aliases=['file']),
            repository=dict(required=True),
            product=dict(required=True),
            organization=dict(required=True),
        ),
        supports_check_mode=True,
    )

    if has_import_error:
        module.fail_json(msg=import_error_msg)

    entity_dict = dict(
        [(k, v) for (k, v) in module.params.items() if v is not None])

    server_url = entity_dict.pop('server_url')
    username = entity_dict.pop('username')
    password = entity_dict.pop('password')
    verify_ssl = entity_dict.pop('verify_ssl')

    try:
        create_server(server_url, (username, password), verify_ssl)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

    ping_server(module)

    entity_dict['organization'] = find_organization(module, name=entity_dict['organization'])
    entity_dict['product'] = find_product(module, name=entity_dict['product'], organization=entity_dict['organization'])
    entity_dict['repository'] = find_repository(module, name=entity_dict['repository'], product=entity_dict['product'])

    content_unit = None
    if entity_dict['repository'].content_type == "yum":
        name, version, release, arch = check_output("rpm --queryformat '%%{NAME} %%{VERSION} %%{RELEASE} %%{ARCH}' -qp %s" % entity_dict['src'],
                                                    shell=True).decode('ascii').split()
        query = "name = \"{}\" and version = \"{}\" and release = \"{}\" and arch = \"{}\"".format(name, version, release, arch)
        content_unit = find_package(module, query, repository=entity_dict['repository'], failsafe=True)
    elif entity_dict['repository'].content_type == "file":
        h = hashlib.sha256()
        with open(entity_dict['src'], "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                h.update(chunk)
        checksum = h.hexdigest()
        name = os.path.basename(entity_dict['src'])
        query = "name = \"{}\" and checksum = \"{}\"".format(name, checksum)
        content_unit = find_file(module, query, repository=entity_dict['repository'], failsafe=True)

    changed = False
    if not content_unit:
        try:
            changed = upload(module, entity_dict['src'], entity_dict['repository'])
        except Exception as e:
            module.fail_json(msg=to_native(e))

    module.exit_json(changed=changed)