Charles Short fd336c49ba compose: Add "image" sub-command
The purpose of the "image" sub-command is to allow a user
to create a raw disk-image from an ostree branch to be used
by libvirt. This allows the user to test an ostree branch
by making it bootable.

The way that it works is that the user provides a debos
configuration file in order to create a raw disk. A sample
configuration file is included in this commit. In order
to create a disk image apt-ostree will create a scratch
ostree repository and copy the desired branch into
the scratch repo. Once that happens debos will run
with a specific branch chosen by the user.

Debos is a golang based tool that allows the creation of
Debian based OS images simpler.

To build an image from a provided configuration file, one
simply runs the following command:

   sudo apt-ostree compose image --repo <path to ostree repo> \
     --base <path to configuration directory> \
     <ostree branch>

More details can be found in the man page.

Testing:
PASSED Installed apt-ostree from git repo.
PASSED Run "apt-ostree compose image --base config/debian/image \
        --repo=/repo test"
PASSED Checked for raw disk image in
       /var/tmp/apt-ostree/build/test/image

Story: 2010867
Task: 48556

Depends-On: https://review.opendev.org/c/starlingx/apt-ostree/+/890704

Change-Id: I4ce64214dc3bb59ef03b35c2c27def017ea35487
Signed-off-by: Charles Short <charles.short@windriver.com>
2023-08-14 14:01:24 -04:00

57 lines
1.7 KiB
Python

"""
Copyright (c) 2023 Wind River Systems, Inc.
SPDX-License-Identifier: Apache-2.0
"""
import shutil
import click
from apt_ostree.cmd.options import compose_options
from apt_ostree.cmd import pass_state_context
from apt_ostree.log import complete_step
from apt_ostree.log import log_step
from apt_ostree.utils import run_command
@click.command(help="Create an raw image from ostree branch")
@pass_state_context
@compose_options
def image(state, repo, base, branch):
click.secho(f"Found ostree repository: {state.repo}")
click.secho(f"Found ostree branch: {state.branch}")
with complete_step(f"Setting up workspace for {state.branch}"):
workdir = state.workspace.joinpath(f"build/{state.branch}")
img_dir = workdir.joinpath("image")
ostree_repo = img_dir.joinpath("ostree_repo")
if img_dir.exists():
shutil.rmtree(img_dir)
shutil.copytree(
state.base.joinpath("image"),
img_dir, dirs_exist_ok=True)
with complete_step("Creating local ostree repository"):
log_step("Creating image build repository")
run_command(
["ostree", "init", f"--repo={str(ostree_repo)}"],
cwd=img_dir)
log_step(f"Pulling {state.branch} in image build repository")
run_command(
["ostree", "pull-local", f"--repo={str(ostree_repo)}",
str(state.repo), str(state.branch)],
cwd=img_dir)
log_step("Running debos...")
cmd = ["debos",
"-t", f"branch:{state.branch}",
]
if state.debug:
cmd += ["-v"]
cmd += ["image.yaml"]
run_command(cmd, cwd=img_dir)
click.secho(f"Image can be found in {img_dir}")