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()

    with module.api_connection():
        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']
                })
Пример #3
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)
Пример #4
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(
        name=dict(required=True),
        description=dict(),
        repositories=dict(type='list', default=[]),
        inclusion=dict(type='bool', default=False),
        original_packages=dict(type='bool'),
        content_view=dict(required=True),
        filter_type=dict(required=True,
                         choices=['rpm', 'package_group', 'erratum',
                                  'docker']),
        filter_state=dict(default='present', choices=['present', 'absent']),
        rule_state=dict(default='present', choices=['present', 'absent']),
        rule_name=dict(aliases=['package_name', 'package_group', 'tag']),
        date_type=dict(default='updated', choices=['issued', 'updated']),
        end_date=dict(),
        errata_id=dict(),
        max_version=dict(),
        min_version=dict(),
        start_date=dict(),
        types=dict(default=["bugfix", "enhancement", "security"], type='list'),
        version=dict(),
    ), )

    entity_dict = module.clean_params()
    filter_state = entity_dict.pop('filter_state')
    rule_state = entity_dict.pop('rule_state')

    if entity_dict['filter_type'] == 'erratum':
        entity_dict['rule_name'] = None
    elif 'rule_name' not in entity_dict:
        entity_dict['rule_name'] = entity_dict['name']

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

        entity_dict['content_view'] = module.find_resource_by_name(
            'content_views',
            entity_dict['content_view'],
            params=scope,
            thin=True)
        cv_scope = {'content_view_id': entity_dict['content_view']['id']}
        if entity_dict['repositories']:
            repositories = []
            for repo in entity_dict['repositories']:
                product = module.find_resource_by_name('products',
                                                       repo['product'],
                                                       params=scope,
                                                       thin=True)
                product_scope = {'product_id': product['id']}
                repositories.append(
                    module.find_resource_by_name('repositories',
                                                 repo['name'],
                                                 params=product_scope,
                                                 thin=True))
            entity_dict['repositories'] = repositories

        entity = module.find_resource_by_name('content_view_filters',
                                              entity_dict['name'],
                                              params=cv_scope,
                                              failsafe=True)
        content_view_filter = module.ensure_entity(
            'content_view_filters',
            entity_dict,
            entity,
            params=cv_scope,
            state=filter_state,
            entity_spec=content_filter_spec,
        )

        if content_view_filter is not None:
            cv_filter_scope = {
                'content_view_filter_id': content_view_filter['id']
            }
            if 'errata_id' in entity_dict:
                # should we try to find the errata the user is asking for? or just pass it blindly?
                # errata = module.find_resource('errata', 'id={0}'.format(entity_dict['errata_id']), params=scope)
                rule_spec = content_filter_rule_erratum_id_spec
                search_scope = {'errata_id': entity_dict['errata_id']}
                search_scope.update(cv_filter_scope)
                search = None
            else:
                rule_spec = globals()['content_filter_rule_%s_spec' %
                                      (entity_dict['filter_type'])]
                search_scope = cv_filter_scope
                if entity_dict['rule_name'] is not None:
                    search = 'name="{0}"'.format(entity_dict['rule_name'])
                else:
                    search = None
            # not using find_resource_by_name here, because not all filters (errata) have names
            content_view_filter_rule = module.find_resource(
                'content_view_filter_rules',
                search,
                params=search_scope,
                failsafe=True) if entity else None

            if entity_dict['filter_type'] == 'package_group':
                package_group = module.find_resource_by_name(
                    'package_groups', entity_dict['rule_name'], params=scope)
                entity_dict['uuid'] = package_group['uuid']

            # drop 'name' from the dict, as otherwise it might override 'rule_name'
            rule_dict = entity_dict.copy()
            rule_dict.pop('name', None)

            module.ensure_entity(
                'content_view_filter_rules',
                rule_dict,
                content_view_filter_rule,
                params=cv_filter_scope,
                state=rule_state,
                entity_spec=rule_spec,
            )
Пример #6
0
def main():
    module = KatelloAnsibleModule(foreman_spec=dict(
        src=dict(required=True, type='path', aliases=['file']),
        repository=dict(required=True,
                        type='entity',
                        scope=['product'],
                        thin=False),
        product=dict(required=True, type='entity', scope=['organization']),
    ), )

    with module.api_connection():
        repository_scope = module.scope_for('repository')

        b_src = to_bytes(module.foreman_params['src'])
        filename = os.path.basename(module.foreman_params['src'])

        checksum = module.sha256(module.foreman_params['src'])

        content_unit = None
        if module.foreman_params['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(b_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 module.foreman_params['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(b_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 module.foreman_params['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(module.foreman_params['repository']['content_type']))

        if not content_unit:
            if not module.check_mode:
                size = os.stat(module.foreman_params['src']).st_size
                content_upload_payload = {'size': size}
                content_upload_payload.update(repository_scope)
                content_upload = module.resource_action(
                    'content_uploads', 'create', content_upload_payload)
                content_upload_scope = {'id': content_upload['upload_id']}
                content_upload_scope.update(repository_scope)

                offset = 0

                with open(b_src, 'rb') as contentfile:
                    for chunk in iter(
                            lambda: contentfile.read(CONTENT_CHUNK_SIZE), b""):
                        data = {
                            'content': chunk,
                            'offset': offset,
                            'size': len(chunk)
                        }
                        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': module.foreman_params['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()