Skip to content
Open
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
69 changes: 51 additions & 18 deletions kt/KT.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ common_repos:
```

NOTE:
Lts kernel and cbr reference `dist-git-tree-lts` and `dist-git-tree-cbr` that
are taken from a local config file in `<config.base_dir>/.private_repos.yaml`
Lts and cbr kernels use the public `dist-git-tree` by default. A kernel can be
pointed at a private dist-git repo via `kernel_overrides` in
`<config.base_path>/.private_repos.yaml` (see Setup above).

A python dataclass KernelInfo that matches every kernel configuration is
introduced in kt/ktlib/kernels.py. The dataclass contains the absolute
Expand Down Expand Up @@ -243,24 +244,23 @@ $ kt setup --help

It prepares the working directory for later commands:

It clones the common_repos from kernels.yaml file in the config.base_path
directory.
If config.base_path = ~/ciq, these will be created:
It clones the common_repos from kernels.yaml and the private_repos from
`<config.base_path>/.private_repos.yaml` into the config.base_path directory.
Repos that already exist are updated instead.
If config.base_path = ~/ciq, these will be created from common_repos:

~/ciq/kernel-src-tree

~/ciq/dist-git-tree-fips

~/ciq/dist-git-tree-cbr
~/ciq/dist-git-tree

~/ciq/dit-git-tree-lts
~/ciq/dist-git-tree-fips

~/ciq/kernel-src-tree-tools

~/ciq/kernel-tools
plus one directory per private_repos entry.

If there's a repo that needs to be cloned relevant for any future command,
this is when it should be cloned.
this is when it should be cloned. Re-run `kt setup` to refresh all repos.

### kt checkout
Prepares the working directory for a kernel.
Expand All @@ -278,7 +278,8 @@ The working directory location is based on configuration:
They will point out to their root sources. Check kt setup for more info.
They should be located in <config.base_path>.
The worktrees reference the remote <branch> from kernels.yaml.
The local branch is `{<user>}/<branch>`.
The local branch is `{<user>}_<branch>`, or `{<user>}_<extra>_<branch>` if
`--extra`/`-e <extra>` is used.

If `--change-dir` or `-c` option is used, it will also go to the working
directory of the kernel.
Expand Down Expand Up @@ -309,12 +310,12 @@ This is the working directory for this kernel:

1. kernel-dist-git

This representes branch `{<user>}/lts9.2:origin/lts9.2`.
The source repo is ~/ciq/dist-git-tree-lts
This representes branch `{<user>}_el-9.2:origin/el-9.2`.
The source repo is ~/ciq/dist-git-tree

2. kernel-src-tree

This representes branch `{<user>}/ciqlts9.2:origin/ciqlts9.2`
This representes branch `{<user>}_ciqlts9_2:origin/ciqlts9_2`
The source repo is ~/ciq/kernel-src-tree

### kt vm
Expand All @@ -336,6 +337,23 @@ to type your root password multiple times when getting access to the vm:
$ sudo usermod -a -G libvirt $(whoami)
```

CentOS 7 vms (`os_variant: centos7` in kernels.yaml, i.e. cbr-7.9) mount
`<config.base_path>` from the host over NFS at 192.168.122.1 instead of
virtiofs. kt does not set up the export; without it the mount fails
silently and `kt content-release --test` cannot find the RPMs. Export
base_path to the libvirt network and allow NFS in the libvirt firewall zone.
The export must be writable (`kt vm --test` builds and installs the kernel
and kselftests in the shared source tree, partly under sudo), but use
all_squash to your own uid/gid so that root in a vm writes as your user and
never as root on the host:
```
$ echo "/home/<user>/ciq 192.168.122.0/24(rw,sync,all_squash,anonuid=$(id -u),anongid=$(id -g),no_subtree_check)" | sudo tee /etc/exports.d/kt.exports
$ sudo systemctl enable --now nfs-server
$ sudo exportfs -ra
$ sudo firewall-cmd --permanent --zone=libvirt --add-service=nfs --add-service=mountd --add-service=rpc-bind
$ sudo firewall-cmd --reload
```

#### Example:
```
$ kt vm lts-9.2
Expand Down Expand Up @@ -390,11 +408,22 @@ and then
Manages the complete content release workflow for kernel packages. This command
automates the process of preparing, building, and testing kernel releases.

The command has three steps that can be run individually or all together:
The command has three steps that can be run individually or all together.

Prerequisites:
- `<base_path>/kernel-tools` (provides mkdistgitdiff.py, used by --prepare) and
`<base_path>/mock-configs` (provides the mock configs, used by --build).
These are internal repos and are not part of kernels.yaml.
- `kt checkout <kernel>` done for the kernel being released

#### --prepare
Releases whatever is on the local src branch (`{<user>}_<src_branch>`), so make
sure it is up to date with the remote before running.

Prepares the content release by:
- Validating git user.name and user.email are configured
- Refusing to run if the src HEAD already has a release tag (nothing new to
release, e.g. --prepare was already run; delete the local tag to redo it)
- Running mkdistgitdiff.py to generate the staging branch and release files
- Checking out the staging branch {automation_tmp}_<src_branch>
- Creating and displaying the new release tag
Expand All @@ -404,6 +433,8 @@ Builds kernel RPMs by:
- Verifying mock is installed and user is in mock group
- Checking DEPOT_USER and DEPOT_TOKEN environment variables are set
- Creating a temporary mock config with depot credentials
- Moving artifacts of a previous build to build_files.prev (replacing an older
build_files.prev) so build_files only contains the new build
- Downloading sources using getsrc.sh
- Building SRPM with mock
- Building binary RPMs from the SRPM
Expand All @@ -412,7 +443,8 @@ Builds kernel RPMs by:
Requirements:
- mock must be installed
- User must be in the mock group
- DEPOT_USER and DEPOT_TOKEN environment variables must be set
- DEPOT_USER and DEPOT_TOKEN environment variables must be set (not needed for cbr)
- `<base_path>/mock-configs` must contain the mock config for the kernel

#### --test
Tests the built kernel by:
Expand All @@ -422,7 +454,8 @@ Tests the built kernel by:
- Running kselftests using /usr/libexec/kselftests/run_kselftest.sh
- Reporting number of tests passed

Output logs:
Output logs (in `<kernels_dir>/<kernel>/`, along with mkdistgitdiff.log,
mock_buildsrpm.log and mock_build.log; RPMs are in build_files/):
- install.log: RPM installation output
- selftest-<kernel_version>.log: Kselftest results

Expand Down
66 changes: 63 additions & 3 deletions kt/commands/content_release/impl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import re
import shutil
import sys

from git import GitCommandError, Repo
Expand Down Expand Up @@ -42,9 +43,38 @@
}


RELEASE_TAG_PREFIXES = ("ciq_kernel-", "fips_kernel-")


class ContentRelease:
"""Manages the complete content release workflow for kernel packages."""

@classmethod
def _check_not_already_released(cls, src_path):
"""
Refuse to prepare if the src HEAD already carries a release tag.

mkdistgitdiff.py collects commits from the last tag (git describe) to
HEAD. If HEAD is already tagged, e.g. because --prepare was run before,
that range is empty and a new release with no patches would still be
created and the version bumped.

Args:
src_path: Path to the kernel-src-tree worktree

Raises:
RuntimeError: If HEAD already has a release tag
"""
tags = Repo(src_path).git.tag("--points-at", "HEAD").split()
release_tags = [t for t in tags if t.startswith(RELEASE_TAG_PREFIXES)]
if release_tags:
raise RuntimeError(
f"HEAD of {src_path} is already tagged as {', '.join(release_tags)}, so there are no new "
f"commits to release.\n"
f"If you are redoing a release that was never pushed, delete the tag first:\n"
+ "".join(f" git -C {src_path} tag -d {t}\n" for t in release_tags)
)

@classmethod
def prepare(cls, kernel_workspace: str):
"""
Expand Down Expand Up @@ -91,6 +121,8 @@ def prepare(cls, kernel_workspace: str):

logging.info(f"Git config validated: user.name='{user_name}', user.email='{user_email}'")

cls._check_not_already_released(kernel_workspace_obj.src_worktree.folder)

# Run mkdistgitdiff.py
mkdistgitdiff_script = config.base_path / "kernel-tools" / "mkdistgitdiff.py"
if not mkdistgitdiff_script.exists():
Expand Down Expand Up @@ -243,9 +275,8 @@ def build(cls, kernel_workspace: str):
# Prepare mock configuration
mock_config = Mock.prepare_config(kernel_workspace, kernel_workspace_obj, config)

# Create build_files directory
build_files_dir = kernel_workspace_obj.folder / "build_files"
build_files_dir.mkdir(exist_ok=True)
# Create an empty build_files directory
build_files_dir = cls._fresh_build_files_dir(kernel_workspace_obj.folder)
if not build_files_dir.exists():
raise RuntimeError(f"Failed to create build_files directory: {build_files_dir}")
logging.info(f"Build files directory: {build_files_dir}")
Expand Down Expand Up @@ -286,6 +317,35 @@ def build(cls, kernel_workspace: str):

logging.info("Build step completed")

@classmethod
def _fresh_build_files_dir(cls, workspace_folder):
"""
Return an empty build_files directory for a new build.

The SRPM is picked by globbing build_files and --test installs every
RPM found there, so artifacts left from an earlier build (e.g. of the
previous version) could be built or installed instead of the new ones.
mock also appends to the logs in its resultdir. A non-empty
build_files is therefore moved to build_files.prev, replacing any
older build_files.prev.

Args:
workspace_folder: The kernel workspace folder

Returns:
Path to the empty build_files directory
"""
build_files_dir = workspace_folder / "build_files"
if build_files_dir.exists() and any(build_files_dir.iterdir()):
prev_dir = workspace_folder / "build_files.prev"
if prev_dir.exists():
logging.info(f"Removing {prev_dir}")
shutil.rmtree(prev_dir)
logging.info(f"Moving previous build artifacts to {prev_dir}")
build_files_dir.rename(prev_dir)
build_files_dir.mkdir(exist_ok=True)
return build_files_dir

@classmethod
def test(cls, kernel_workspace: str):
"""
Expand Down
41 changes: 41 additions & 0 deletions tests/kt/commands/test_content_release_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from pathlib3x import Path

from kt.commands.content_release.impl import ContentRelease


def test_creates_missing_dir(tmp_path):
ws = Path(tmp_path)
d = ContentRelease._fresh_build_files_dir(ws)
assert d == ws / "build_files"
assert d.is_dir() and not any(d.iterdir())
assert not (ws / "build_files.prev").exists()


def test_keeps_empty_dir(tmp_path):
ws = Path(tmp_path)
(ws / "build_files").mkdir()
ContentRelease._fresh_build_files_dir(ws)
assert not (ws / "build_files.prev").exists()


def test_moves_previous_build(tmp_path):
ws = Path(tmp_path)
(ws / "build_files").mkdir()
(ws / "build_files" / "kernel-1.src.rpm").write_text("old")

d = ContentRelease._fresh_build_files_dir(ws)

assert not any(d.iterdir())
assert (ws / "build_files.prev" / "kernel-1.src.rpm").read_text() == "old"


def test_replaces_older_prev(tmp_path):
ws = Path(tmp_path)
(ws / "build_files.prev").mkdir()
(ws / "build_files.prev" / "kernel-0.src.rpm").write_text("oldest")
(ws / "build_files").mkdir()
(ws / "build_files" / "kernel-1.src.rpm").write_text("old")

ContentRelease._fresh_build_files_dir(ws)

assert [p.name for p in (ws / "build_files.prev").iterdir()] == ["kernel-1.src.rpm"]
50 changes: 50 additions & 0 deletions tests/kt/commands/test_content_release_prepare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
from git import Repo

from kt.commands.content_release.impl import ContentRelease


@pytest.fixture
def src_repo(tmp_path):
repo = Repo.init(tmp_path)
with repo.config_writer() as cw:
cw.set_value("user", "name", "Test")
cw.set_value("user", "email", "test@example.com")
(tmp_path / "file").write_text("1\n")
repo.index.add(["file"])
repo.index.commit("first")
return repo


def _commit(repo, content):
f = repo.working_tree_dir + "/file"
with open(f, "w") as fh:
fh.write(content)
repo.index.add(["file"])
repo.index.commit(content)


def test_no_tags(src_repo):
ContentRelease._check_not_already_released(src_repo.working_tree_dir)


def test_release_tag_on_older_commit(src_repo):
src_repo.create_tag("ciq_kernel-5.14.0-284.30.1+46.1.el9_2_ciq")
_commit(src_repo, "2\n")
ContentRelease._check_not_already_released(src_repo.working_tree_dir)


def test_unrelated_tag_on_head(src_repo):
src_repo.create_tag("my-test-tag")
ContentRelease._check_not_already_released(src_repo.working_tree_dir)


@pytest.mark.parametrize(
"tag",
["ciq_kernel-5.14.0-284.30.1+47.1.el9_2_ciq", "fips_kernel-4.18.0-425.13.1.el8_7.35.1"],
)
def test_release_tag_on_head(src_repo, tag):
src_repo.create_tag(tag)
with pytest.raises(RuntimeError, match="already tagged") as e:
ContentRelease._check_not_already_released(src_repo.working_tree_dir)
assert f"tag -d {tag}" in str(e.value)