def main():
    module = KatelloAnsibleModule(argument_spec=dict(
        product=dict(required=True),
        repository=dict(),
    ), )

    module.task_timeout = 12 * 60 * 60

    params = module.clean_params()

    with module.api_connection():
        params, scope = module.handle_organization_param(params)

        params['product'] = module.find_resource_by_name('products',
                                                         params['product'],
                                                         params=scope,
                                                         thin=True)
        if 'repository' in params:
            product_scope = {'product_id': params['product']['id']}
            params['repository'] = module.find_resource_by_name(
                'repositories',
                params['repository'],
                params=product_scope,
                thin=True)
            task = module.resource_action('repositories', 'sync',
                                          {'id': params['repository']['id']})
        else:
            task = module.resource_action('products', 'sync',
                                          {'id': params['product']['id']})

        module.exit_json(task=task)
def main():
    module = KatelloAnsibleModule(
        argument_spec=dict(
            friendly_name=dict(required=True),
            scc_account=dict(required=True),
        ),
    )

    module.task_timeout = 4 * 60

    params = module.clean_params()

    module.connect()

    params, scope = module.handle_organization_param(params)

    params['scc_account'] = module.find_resource_by_name('scc_accounts', name=params['scc_account'], params=scope, thin=True)

    scc_account_scope = {'scc_account_id': params['scc_account']['id']}

    # Try to find the SccProduct to work on
    # name is however not unique, but friendly_name is

    search_string = 'friendly_name="{0}"'.format(params['friendly_name'])
    scc_product = module.find_resource('scc_products', search_string, params=scc_account_scope)

    if not scc_product.get('product_id'):
        module.resource_action('scc_products', 'subscribe', {'id': scc_product['id'], 'scc_account_id': scc_account_scope['scc_account_id']})

    module.exit_json()
def main():
    module = KatelloAnsibleModule(
        argument_spec=dict(
            product=dict(required=True),
            repository=dict(),
            synchronous=dict(type='bool', default=True),
        ),
    )

    module.task_timeout = 30 * 60

    params = module.clean_params()

    module.connect()

    params['organization'] = module.find_resource_by_name('organizations', params['organization'], thin=True)
    scope = {'organization_id': params['organization']['id']}
    params['product'] = module.find_resource_by_name('products', params['product'], params=scope, thin=True)
    if 'repository' in params:
        product_scope = {'product_id': params['product']['id']}
        params['repository'] = module.find_resource_by_name('repositories', params['repository'], params=product_scope, thin=True)
        changed, task = module.resource_action('repositories', 'sync', {'id': params['repository']['id']}, synchronous=params['synchronous'])
    else:
        changed, task = module.resource_action('products', 'sync', {'id': params['product']['id']}, synchronous=params['synchronous'])
    module.exit_json(changed=changed, task=task)
Пример #4
0
def main():
    module = KatelloAnsibleModule(
        foreman_spec=dict(
            scc_product=dict(required=True, type='entity', aliases=['friendly_name'], scope=['scc_account'], thin=False),
            scc_account=dict(required=True, type='entity', scope=['organization']),
        ),
        required_plugins=[('scc_manager', ['*'])],
    )

    module.task_timeout = 4 * 60

    with module.api_connection():
        scc_product = module.lookup_entity('scc_product')

        if not scc_product.get('product_id'):
            payload = {'id': scc_product['id']}
            payload.update(module.scope_for('scc_account'))
            module.resource_action('scc_products', 'subscribe', payload)
Пример #5
0
def main():
    module = KatelloAnsibleModule(
        foreman_spec=dict(
            product=dict(type='entity', scope=['organization'], required=True),
            repository=dict(type='entity', scope=['product'], failsafe=True),
            # This should be scoped more explicit for better serch performance, but needs rerecording
            # repository=dict(type='entity', scope=['organization', 'product'], failsafe=True),
        ), )

    module.task_timeout = 12 * 60 * 60

    with module.api_connection():
        product = module.lookup_entity('product')
        repository = module.lookup_entity('repository')
        if repository:
            task = module.resource_action('repositories', 'sync',
                                          {'id': repository['id']})
        else:
            task = module.resource_action('products', 'sync',
                                          {'id': product['id']})

        module.exit_json(task=task)
def main():
    module = KatelloAnsibleModule(argument_spec=dict(
        src=dict(required=True, type='path', aliases=['file']),
        repository=dict(required=True),
        product=dict(required=True),
        organization=dict(required=True),
    ), )

    entity_dict = module.clean_params()

    module.connect()

    entity_dict['organization'] = module.find_resource_by_name(
        'organizations', entity_dict['organization'], thin=True)
    scope = {'organization_id': entity_dict['organization']['id']}
    entity_dict['product'] = module.find_resource_by_name(
        'products', entity_dict['product'], params=scope, thin=True)
    product_scope = {'product_id': entity_dict['product']['id']}
    entity_dict['repository'] = module.find_resource_by_name(
        'repositories', entity_dict['repository'], params=product_scope)
    repository_scope = {'repository_id': entity_dict['repository']['id']}

    filename = os.path.basename(entity_dict['src'])

    checksum = hashlib.sha256()
    with open(entity_dict['src'], 'rb') as contentfile:
        for chunk in iter(lambda: contentfile.read(CONTENT_CHUNK_SIZE), b""):
            checksum.update(chunk)
    checksum = checksum.hexdigest()

    content_unit = None
    if entity_dict['repository']['content_type'] == 'deb':
        if not HAS_DEBFILE:
            module.fail_json(msg='The python-debian module is required',
                             exception=DEBFILE_IMP_ERR)

        name, version, architecture = get_deb_info(entity_dict['src'])
        query = 'name = "{0}" and version = "{1}" and architecture = "{2}"'.format(
            name, version, architecture)
        content_unit = module.find_resource('debs',
                                            query,
                                            params=repository_scope,
                                            failsafe=True)
    elif entity_dict['repository']['content_type'] == 'yum':
        if not HAS_RPM:
            module.fail_json(msg='The rpm Python module is required',
                             exception=RPM_IMP_ERR)

        name, epoch, version, release, arch = get_rpm_info(entity_dict['src'])
        query = 'name = "{0}" and epoch = "{1}" and version = "{2}" and release = "{3}" and arch = "{4}"'.format(
            name, epoch, version, release, arch)
        content_unit = module.find_resource('packages',
                                            query,
                                            params=repository_scope,
                                            failsafe=True)
    elif entity_dict['repository']['content_type'] == 'file':
        query = 'name = "{0}" and checksum = "{1}"'.format(filename, checksum)
        content_unit = module.find_resource('file_units',
                                            query,
                                            params=repository_scope,
                                            failsafe=True)
    else:
        # possible types in 3.12: docker, ostree, yum, puppet, file, deb
        module.fail_json(
            msg="Uploading to a {0} repository is not supported yet.".format(
                entity_dict['repository']['content_type']))

    if not content_unit:
        if not module.check_mode:
            content_upload = module.resource_action('content_uploads',
                                                    'create', repository_scope)
            content_upload_scope = {'id': content_upload['upload_id']}
            content_upload_scope.update(repository_scope)

            offset = 0

            with open(entity_dict['src'], 'rb') as contentfile:
                for chunk in iter(lambda: contentfile.read(CONTENT_CHUNK_SIZE),
                                  b""):
                    data = {'content': chunk, 'offset': offset}
                    module.resource_action('content_uploads',
                                           'update',
                                           params=content_upload_scope,
                                           data=data)

                    offset += len(chunk)

            uploads = [{
                'id': content_upload['upload_id'],
                'name': filename,
                'size': offset,
                'checksum': checksum
            }]
            import_params = {
                'id': entity_dict['repository']['id'],
                'uploads': uploads
            }
            module.resource_action('repositories', 'import_uploads',
                                   import_params)

            module.resource_action('content_uploads', 'destroy',
                                   content_upload_scope)
        else:
            module.set_changed()

    module.exit_json()
def main():
    module = KatelloAnsibleModule(argument_spec=dict(
        src=dict(required=True, type='path', aliases=['file']),
        repository=dict(required=True),
        product=dict(required=True),
        organization=dict(required=True),
    ), )

    entity_dict = module.clean_params()

    module.connect()

    entity_dict['organization'] = module.find_resource_by_name(
        'organizations', entity_dict['organization'], thin=True)
    scope = {'organization_id': entity_dict['organization']['id']}
    entity_dict['product'] = module.find_resource_by_name(
        'products', entity_dict['product'], params=scope, thin=True)
    product_scope = {'product_id': entity_dict['product']['id']}
    entity_dict['repository'] = module.find_resource_by_name(
        'repositories', entity_dict['repository'], params=product_scope)
    repository_scope = {'repository_id': entity_dict['repository']['id']}

    filename = os.path.basename(entity_dict['src'])

    checksum = hashlib.sha256()
    with open(entity_dict['src'], 'rb') as contentfile:
        for chunk in iter(lambda: contentfile.read(CONTENT_CHUNK_SIZE), b""):
            checksum.update(chunk)
    checksum = checksum.hexdigest()

    content_unit = None
    if entity_dict['repository']['content_type'] == 'yum':
        name, epoch, version, release, arch = check_output([
            'rpm', '--queryformat',
            '%{NAME} %{EPOCHNUM} %{VERSION} %{RELEASE} %{ARCH}', '-qp',
            entity_dict['src']
        ]).decode('ascii').split()
        query = 'name = "{0}" and epoch = "{1}" and version = "{2}" and release = "{3}" and arch = "{4}"'.format(
            name, epoch, version, release, arch)
        content_unit = module.find_resource('packages',
                                            query,
                                            params=repository_scope,
                                            failsafe=True)
    elif entity_dict['repository']['content_type'] == 'file':
        query = 'name = "{0}" and checksum = "{1}"'.format(filename, checksum)
        content_unit = module.find_resource('file_units',
                                            query,
                                            params=repository_scope,
                                            failsafe=True)
    else:
        # possible types in 3.12: docker, ostree, yum, puppet, file, deb
        module.fail_json(
            msg="Uploading to a {0} repository is not supported yet.".format(
                entity_dict['repository']['content_type']))

    changed = False
    if not content_unit:
        _content_upload_changed, content_upload = module.resource_action(
            'content_uploads', 'create', repository_scope)
        content_upload_scope = {'id': content_upload['upload_id']}
        content_upload_scope.update(repository_scope)

        offset = 0

        with open(entity_dict['src'], 'rb') as contentfile:
            for chunk in iter(lambda: contentfile.read(CONTENT_CHUNK_SIZE),
                              b""):
                data = {'content': chunk, 'offset': offset}
                module.resource_action('content_uploads',
                                       'update',
                                       params=content_upload_scope,
                                       data=data)

                offset += len(chunk)

        uploads = [{
            'id': content_upload['upload_id'],
            'name': filename,
            'size': offset,
            'checksum': checksum
        }]
        import_params = {
            'id': entity_dict['repository']['id'],
            'uploads': uploads
        }
        module.resource_action('repositories', 'import_uploads', import_params)

        module.resource_action('content_uploads', 'destroy',
                               content_upload_scope)

        changed = True

    module.exit_json(changed=changed)