Exemple #1
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(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=missing_required_lib("python-debian"),
                                 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=missing_required_lib("rpm"),
                                 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()