Skip to content

Commit

Permalink
Add podman support
Browse files Browse the repository at this point in the history
Fixes cekit#363.
  • Loading branch information
rnc committed Mar 7, 2019
1 parent 4f12391 commit 249deff
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cekit/builder.py
Expand Up @@ -60,7 +60,7 @@ def execute(self):
self.run()

def prepare(self):
if self.build_engine == 'docker' or self.build_engine == 'buildah':
if self.build_engine == 'docker' or self.build_engine == 'buildah' or self.build_engine == "podman":
from cekit.generator.docker import DockerGenerator as generator_impl
LOGGER.info('Generating files for {} engine'.format(self.build_engine))
elif self.build_engine == 'osbs':
Expand Down
55 changes: 55 additions & 0 deletions cekit/builders/podman.py
@@ -0,0 +1,55 @@
import logging
import os
import subprocess

from cekit.builder import Builder
from cekit.errors import CekitError

LOGGER = logging.getLogger('cekit')


class PodmanBuilder(Builder):
"""This class represents podman builder in build mode."""

def __init__(self, common_params, params):
super(PodmanBuilder, self).__init__('podman', common_params, params)

@staticmethod
def dependencies():
deps = {}

deps['podman'] = {
'package': 'podman',
'executable': 'podman'
}

return deps

def run(self):
"""Build container image using podman."""
tags = self.params.tags
cmd = ["/usr/bin/podman", "build"]

if self.params.pull:
cmd.append('--pull-always')

# Custom tags for the container image
LOGGER.debug("Building image with tags: '%s'" %
"', '".join(tags))

for tag in tags:
cmd.extend(["-t", tag])

LOGGER.info("Building container image...")

cmd.append(os.path.join(self.target, 'image'))

LOGGER.debug("Running Podman build: '%s'" % " ".join(cmd))

try:
subprocess.check_call(cmd)

LOGGER.info("Image built and available under following tags: %s"
% ", ".join(tags))
except:
raise CekitError("Image build failed, see logs above.")
19 changes: 17 additions & 2 deletions cekit/cli.py
Expand Up @@ -70,7 +70,7 @@ def build(dry_run, overrides, addhelp): # pylint: disable=unused-argument
BUILDERS
Currently supported builders: Docker, OSBS and Buildah.
Currently supported builders: Docker, OSBS, Podman and Buildah.
$ cekit build BUILDER
Expand Down Expand Up @@ -125,6 +125,21 @@ def build_buildah(ctx, pull, tags): # pylint: disable=unused-argument
run_build(ctx, 'buildah')


@build.command(name="podman", short_help="Build using Podman engine")
@click.option('--pull', help="Always try to fetch latest base image.", is_flag=True)
@click.option('--tag', 'tags', metavar="TAG", help="Use specified tag to tag the image after build, can be specified multiple times.", multiple=True)
@click.pass_context
def build_podman(ctx, pull, tags):
"""
DESCRIPTION
Executes container image build locally using Podman builder.
https://podman.io/
"""
run_build(ctx, 'buildah')


@build.command(name="osbs", short_help="Build using OSBS engine")
@click.option('--release', help="Execute a release build.", is_flag=True)
# TODO: Ensure this still makes sense
Expand Down Expand Up @@ -308,7 +323,7 @@ def configure(self):
})

def cleanup(self):
""" Prepates target/image directory to be regenerated."""
""" Prepares target/image directory to be regenerated."""
directories_to_clean = [os.path.join(self.params.target, 'image', 'modules'),
os.path.join(self.params.target, 'image', 'repos'),
os.path.join(self.params.target, 'repo')]
Expand Down
2 changes: 1 addition & 1 deletion cekit/generator/base.py
Expand Up @@ -11,8 +11,8 @@
from cekit.config import Config
from cekit.descriptor import Env, Image, Label, Module, Overrides, Repository
from cekit.errors import CekitError
from cekit.version import version as cekit_version
from cekit.template_helper import TemplateHelper
from cekit.version import version as cekit_version

LOGGER = logging.getLogger('cekit')
CONFIG = Config()
Expand Down
4 changes: 2 additions & 2 deletions cekit/tools.py
Expand Up @@ -283,7 +283,7 @@ def _check_for_executable(self, dependency, executable, package=None):
dependency, executable)

if package:
msg += " To satisfy this requrement you can install the '{}' package.".format(package)
msg += " To satisfy this requirement you can install the '{}' package.".format(package)

raise CekitError(msg)

Expand Down Expand Up @@ -318,6 +318,6 @@ def handle(self, o):

# Check if we have a method
if callable(dependencies):
# Execute that method to get list of dependecies and try to handle them
# Execute that method to get list of dependencies and try to handle them
self._handle_dependencies(o.dependencies())
return
39 changes: 34 additions & 5 deletions tests/test_builder.py
@@ -1,21 +1,21 @@
from cekit.tools import Map
from cekit.errors import CekitError
from cekit.descriptor import Image
import glob
import logging
import os
import pytest
import subprocess
import time

import pytest
import yaml

from cekit.errors import CekitError
from cekit.tools import Map

try:
from unittest.mock import call
except ImportError:
from mock import call

from cekit.builders.docker_builder import DockerBuilder
from cekit.builder import Builder
from cekit.config import Config


Expand Down Expand Up @@ -156,6 +156,8 @@ def create_builder_object(mocker, builder, params, common_params={'target': 'som
from cekit.builders.docker_builder import DockerBuilder as BuilderImpl
elif 'osbs' == builder:
from cekit.builders.osbs import OSBSBuilder as BuilderImpl
elif 'podman' == builder:
from cekit.builders.podman import PodmanBuilder as BuilderImpl
elif 'buildah' == builder:
from cekit.builders.buildah import BuildahBuilder as BuilderImpl
else:
Expand Down Expand Up @@ -543,3 +545,30 @@ def test_buildah_builder_run_pull(mocker):
'-t', 'foo',
'-t', 'bar',
'something/image'])


def test_podman_builder_run(mocker):
params = {'tags': ['foo', 'bar']}
check_call = mocker.patch.object(subprocess, 'check_call')
builder = create_builder_object(mocker, 'podman', params)
builder.run()

check_call.assert_called_once_with(['/usr/bin/podman',
'build',
'-t', 'foo',
'-t', 'bar',
'something/image'])


def test_podman_builder_run_pull(mocker):
params = {'tags': ['foo', 'bar'], 'pull': True}
check_call = mocker.patch.object(subprocess, 'check_call')
builder = create_builder_object(mocker, 'podman', params)
builder.run()

check_call.assert_called_once_with(['/usr/bin/podman',
'build',
'--pull-always',
'-t', 'foo',
'-t', 'bar',
'something/image'])

0 comments on commit 249deff

Please sign in to comment.