Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Podman support #30

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# xcp-ng-build-env

This docker config and collection of supporting scripts allows for creating
a docker container to work on and build a XCP-ng package from an SRPM or from
a directory containing a `SOURCES/` and a `SPECS/` directory along with appropriate
RPM spec file and software sources.
It will build a Docker container with the right build environment (including some
This container config and collection of supporting scripts allows for
creating a container to work on and build a XCP-ng package from an
SRPM or from a directory containing a `SOURCES/` and a `SPECS/`
directory along with appropriate RPM spec file and software sources.

It will build a container with the right build environment (including some
useful tools).
Depending on the parameters, it will either do everything automatically to build a
given package, or just install build-dependencies and let you work manually from a shell
Expand All @@ -13,14 +14,21 @@ want.

## Configuration

You'll need to install docker. Follow the instructions for your platform on
https://www.docker.com/
You'll need to install docker or podman. Podman should be available
from your distro repositories, for Docker follow the instructions for
your platform on https://www.docker.com/

If you have both installed, docker will be used by default. If you
want to use a specific container runtime, set `XCPNG_OCI_RUNNER` to
the docker-compatible command to use (typically `podman` or `docker`).

## Building the docker image(s)
## Building the container image(s)

You need one docker image per target version of XCP-ng.
You need one container image per target version of XCP-ng.

Clone this repository (outside any docker container), then use `build.sh` to generate the docker images for the wanted releases of XCP-ng.
Clone this repository (outside any container), then use `build.sh` to
generate the images for the wanted releases of XCP-ng.
Note that Docker and Podman store container images separately.

```
Usage: ./build.sh {version_of_XCP_ng}
Expand Down Expand Up @@ -115,7 +123,7 @@ git clone https:/xcp-ng-rpms/xapi.git

* `-b` / `--branch` allows to select which version of XCP-ng to work on (defaults to the latest known version if not specified).
* `--no-exit` drops you to a shell after the build, instead of closing the container. Useful if the build fails and you need to debug.
* `--rm` destroys the container on exit. Helps preventing docker from using too much space on disk. You can still reclaim space afterwards by running `docker container prune` and `docker image prune`
* `--rm` destroys the container on exit. Helps preventing containers from using too much space on disk. You can still reclaim space afterwards by running `docker container prune` and `docker image prune`
* `-v` / `--volume` (see *Mounting repos from outside the container* below)


Expand Down Expand Up @@ -149,9 +157,7 @@ make

If you'd like to develop using the tools on your host and preserve the changes
to source and revision control but still use the container for building, you
can do using by using a docker volume.

Once you have built your image you can run it with an extra argument to mount
can do using by mouning a volume in the container, using the `-v` option to mount
a directory from your host to a suitable point inside the container. For
example, if I clone some repos into a directory on my host, say `/work/code/`,
then I can mount it inside the container as follows:
Expand Down
19 changes: 18 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ if [ -z "$1" ]; then
exit
fi

RUNNER=""
if [ -n "$XCPNG_OCI_RUNNER" ]; then
RUNNER="$XCPNG_OCI_RUNNER"
else
SUPPORTED_RUNNERS="docker podman"
for COMMAND in $SUPPORTED_RUNNERS; do
if command -v $COMMAND >/dev/null; then
RUNNER="$COMMAND"
break
fi
done
if [ -z "$RUNNER" ]; then
echo >&2 "cannot find a supported runner: $SUPPORTED_RUNNERS"
exit 1
fi
fi

cd $(dirname "$0")

CUSTOM_ARGS=()
Expand Down Expand Up @@ -56,7 +73,7 @@ fi
CUSTOM_ARGS+=( "--build-arg" "CUSTOM_BUILDER_UID=${CUSTOM_UID}" )
CUSTOM_ARGS+=( "--build-arg" "CUSTOM_BUILDER_GID=${CUSTOM_GID}" )

docker build \
"$RUNNER" build \
"${CUSTOM_ARGS[@]}" \
-t xcp-ng/xcp-ng-build-env:${1} \
--ulimit nofile=1024 \
Expand Down
26 changes: 24 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

""" Thin wrapper around "docker run" which simplifies the creation of a build environment for XCP-ng packages. """
"""
Thin wrapper around "docker run" or "podman run".

Simplifies the creation of a build environment for XCP-ng packages.
"""

import argparse
import os
Expand All @@ -16,6 +20,16 @@
DEFAULT_BRANCH = '8.3'
DEFAULT_ULIMIT_NOFILE = 1024

RUNNER = os.getenv("XCPNG_OCI_RUNNER")
if RUNNER is None:
SUPPORTED_RUNNERS = "docker podman"
for command in SUPPORTED_RUNNERS.split():
if shutil.which(command):
RUNNER = command
break
else:
raise Exception(f"cannot find a supported runner: {SUPPORTED_RUNNERS}")

def make_mount_dir():
""" Make a randomly-named directory under SRPMS_MOUNT_ROOT. """
srpm_mount_dir = os.path.join(SRPMS_MOUNT_ROOT, str(uuid.uuid4()))
Expand All @@ -32,6 +46,12 @@ def copy_srpms(srpm_mount_dir, srpms):
srpm_name = os.path.basename(srpm)
shutil.copyfile(srpm, os.path.join(srpm_mount_dir, srpm_name))

def is_podman(runner):
if os.path.basename(runner) == "podman":
return True
if subprocess.getoutput(f"{runner} --version").startswith("podman "):
return True
return False

def main():
""" Main entry point. """
Expand Down Expand Up @@ -93,7 +113,9 @@ def main():

args = parser.parse_args(sys.argv[1:])

docker_args = ["docker", "run", "-i", "-t", "-u", "builder"]
docker_args = [RUNNER, "run", "-i", "-t", "-u", "builder"]
if is_podman(RUNNER):
docker_args += ["--userns=keep-id"]
if os.uname()[4] != "x86_64":
docker_args += ["--platform", "linux/amd64"]
if args.rm:
Expand Down
Loading