Add "compose init" command

Add the "compose init" command to allow users to create an empty
ostree repository.

Story: 2010867
Task: 48556

Test Plan
PASSED Installed apt-ostree from git repo.
PASSED Run sudo apt-ostree compose init \
       --repo /repo
PASSED Check for /repo/config

Change-Id: Iecd63a1ae6c1726914b16566df32f1adec382955
Signed-off-by: Charles Short <charles.short@windriver.com>
This commit is contained in:
Charles Short 2023-10-11 13:23:55 -04:00
parent 90368ae783
commit b18f70e4e1
5 changed files with 61 additions and 9 deletions

View File

@ -27,15 +27,6 @@ class Bootstrap:
def create_rootfs(self):
"""Create a Debian system from a configuration file."""
if not self.state.repo.exists():
click.secho(f"Creating ostree repository: {self.state.repo}")
run_command(["ostree", "init", f"--repo={self.state.repo}",
"--mode=archive-z2"])
else:
click.secho(f"Found ostree repository: {self.state.repo}")
click.secho(f"Found ostree branch: {self.state.branch}")
if not self.state.base.exists():
click.secho("Configuration directory does not exist.", fg="red")
sys.exit(1)
@ -72,6 +63,8 @@ class Bootstrap:
self.state.branch), "--target", str(rootfs),
"--output", str(workdir)], cwd=self.state.base)
self.ostree.ostree_init()
click.secho(f"Found ostree branch: {self.state.branch}")
self.create_ostree(rootfs)
r = self.ostree.ostree_commit(
rootfs,

View File

@ -10,6 +10,7 @@ import click
from apt_ostree.cmd.compose.commit import commit
from apt_ostree.cmd.compose.create import create
from apt_ostree.cmd.compose.image import image
from apt_ostree.cmd.compose.init import init
from apt_ostree.cmd.compose.install import install
from apt_ostree.cmd.compose.repo import repo
from apt_ostree.cmd.compose.uninstall import uninstall
@ -25,6 +26,7 @@ def compose(ctxt):
compose.add_command(commit)
compose.add_command(create)
compose.add_command(image)
compose.add_command(init)
compose.add_command(install)
compose.add_command(upgrade)
compose.add_command(repo)

View File

@ -0,0 +1,32 @@
"""
Copyright (c) 2023 Wind River Systems, Inc.
SPDX-License-Identifier: Apache-2.0
"""
import errno
import sys
import click
from apt_ostree.cmd.options import repo_option
from apt_ostree.cmd import pass_state_context
from apt_ostree.compose import Compose
@click.command(
help="Initialize an OStree repository.")
@pass_state_context
@repo_option
def init(state, repo):
try:
Compose(state).create()
except KeyboardInterrupt:
click.secho("\n" + ("Exiting at your request."))
sys.exit(130)
except BrokenPipeError:
sys.exit()
except OSError as error:
if error.errno == errno.ENOSPC:
sys.exit("error - No space left on device.")

View File

@ -27,6 +27,16 @@ class Compose:
self.workdir.mkdir(parents=True, exist_ok=True)
self.rootfs = None
def create(self):
"""Create an OSTree repository."""
if self.state.repo.exists():
click.secho(
f"Repository already exists: {self.state.repo}", fg="red")
sys.exit(1)
click.secho(f"Found ostree repository: {self.state.repo}")
self.ostree.init()
def enablerepo(self):
"""Enable Debian package feed."""
try:

View File

@ -9,6 +9,7 @@ import subprocess
import sys
import click
from rich.console import Console
from apt_ostree.utils import run_command
@ -24,6 +25,20 @@ AT_FDCWD = -100
class Ostree:
def __init__(self, state):
self.state = state
self.console = Console()
def init(self):
"""Create a new ostree repo."""
repo = OSTree.Repo.new(Gio.File.new_for_path(
str(self.state.repo)))
mode = OSTree.RepoMode.ARCHIVE_Z2
try:
repo.create(mode)
self.console.print("Sucessfully initialized ostree repository.")
except GLib.GError as e:
click.secho(f"Failed to create repo: {e}", fg="red")
sys.exit(1)
def ostree_commit(self,
root=None,