---
# System prepended metadata

title: koji plugin

---

# koji plugin

## Links
* documentation: https://docs.pagure.org/koji/writing_a_plugin/

# OSBS

# Plugin
* [Source](https://github.com/containerbuildsystem/koji-containerbuild)

## Server
Where the import happens

* [`CGImport`](https://github.com/containerbuildsystem/atomic-reactor/blob/8ee4d96270bb1611a8a3e0b14c4b2c1391be14c0/atomic_reactor/plugins/exit_koji_import.py#L470)

# Plugin

* Parameters for Tasks:
  From [`tasks.py`](https://github.com/koji-project/koji/blob/2460a00117f8f7c9a64c96415cf638d9385a824b/koji/tasks.py):
```python
    'appliance' : [
        [['name', 'version', 'arch', 'target', 'ksfile', 'opts'], None, None, (None,)],
    ],
    'image' : [
        [['name', 'version', 'arches', 'target', 'inst_tree', 'opts'], None, None, (None,)],
    ],
    'createImage' : [
        [['name', 'version', 'release', 'arch', 'target_info', 'build_tag', 'repo_info', 'inst_tree', 'opts'], None, None, (None,)],
    ],
```

* Skeleton:
```python
from koji.tasks import BaseTaskHandler

class BaseImageTask(BaseTaskHandler):
    Methods = ['image']
    _taskWeight = 2.0

    def handler(self, name, version, arches, target, inst_tree, opts):
        self.logger.debug("Building all the images...")
subtasks = {}
        
        # This is taken from koji itself
        # spawn a new task for each arch
        self.logger.debug("Spawning jobs for image arches: %r" % (arches))
        canfail = []
        for arch in arches:
            inst_url = inst_tree.replace('$arch', arch)
            subtasks[arch] = self.session.host.subtask(
                method='createImage',
                arglist=[name, version, release, arch, target_info, build_tag, repo_info, inst_url, opts],
                label=arch, parent=self.id, arch=arch)
            if arch in opts.get('optional_arches', []):
                canfail.append(subtasks[arch])
            self.logger.debug("Got image subtasks: %r" % (subtasks))
            self.logger.debug("Waiting on image subtasks (%s can fail)..." % canfail)
            results = self.wait(to_list(subtasks.values()), all=True, failany=True, canfail=canfail)
            
        # ...
        # fail a build
        self.session.host.failBuild(self.id, bld_info['id'])
        
        # tag a build
        tag_task_id = self.session.host.subtask(method='tagBuild', arglist=[target_info['dest_tag'], bld_info['id'], False, None, True], label='tag', parent=self.id, arch='noarch')
        self.wait(tag_task_id)
        
        # report results
        report = ''
        if opts.get('scratch'):
            respath = ', '.join(
                [os.path.join(koji.pathinfo.work(), koji.pathinfo.taskrelpath(tid)) for tid in subtasks.values()])
            report += 'Scratch '
        else:
            respath = koji.pathinfo.imagebuild(bld_info)
        report += 'image build results in: %s' % respath
        return report
            

class SingleImageTask(BaseTaskHandler):
    Methods = ['createImage']
    _taskWeight = 2.0

     def handler(self, name, version, release, arch, target_info, build_tag, repo_info, inst_tree, opts=None):
         pass
```



## kojid

Tasks:

* Derive from [`BaseTaskHandler`](https://github.com/koji-project/koji/blob/2460a00117f8f7c9a64c96415cf638d9385a824b/koji/tasks.py#L262)
    * `.workdir`
    * `.session`
    * `.uploadFile()`

* [`image`](https://github.com/koji-project/koji/blob/master/builder/kojid#L2267)
   * Seems like the base type for an image for all architectures
   * Spawns new tasks for each arch via 
   * example: [image (f32, Fedora-Cloud-Base-32, fedora-cloud-base.ks)](https://koji.fedoraproject.org/koji/taskinfo?taskID=50112963)
* [`createImage`](https://github.com/koji-project/koji/blob/master/builder/kojid#L3694)
    * Architecture specific image
    * example: [createImage (f32, Fedora-Cloud-Base-32-20200825.0, fedora-cloud-base.ks, ppc64le)](https://koji.fedoraproject.org/koji/taskinfo?taskID=50112966)

# Client
## Current image builder
```
 koji image-build <image_name> <image_version> <build_target>
                         <install_tree_url> <arch>
                         --release <image_release>
                         --distro <distro_name_version>
                         --kickstart <local_kickstart_file>
                         --format <format_type>
                         --disk-size <disk_size_in_gb>
                         --repo <repo_url> (optional)
                         --scratch (optional)
                         --nowait (optional)
```

For example:

```>
koji image-build \
   centos-7-imcleod-test 1 atomic7-el7.centos \
   http://mirror.centos.org/centos/7/os/x86_64/ x86_64 \
   --release=1 \
   --distro RHEL-7.0 \
   --kickstart=/tmp/RHEL7.auto \
   --format=qcow2 \
   --scratch \
   --nowait \
   --disk-size=10 
```