vauxite-build/ostree-engine
2024-02-28 08:40:58 -06:00

140 lines
4.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
CLEAN_BUILD=""
RELVER="39"
SOURCE_URL="https://pagure.io/workstation-ostree-config"
OSTREE_FILES_DIR="$(pwd)/src"
CACHE_DIR="$(pwd)/.cache"
BUILD_REPO="$(pwd)/.build-repo"
SOURCE_REPO="$(pwd)/.source-repo"
TMP_WORK_DIR="$(pwd)/.tmp"
TREEFILE="${TMP_WORK_DIR}/vauxite.json"
REGISTRY="git.hyperreal.coffee:5050"
REGISTRY_PASSWD="$(cat /home/jas/.vauxite-build-registry-token)"
REGISTRY_USER="hyperreal"
if [ "$(id -u)" != "0" ]; then
echo "Please run build with sudo"
exit 1
fi
# Ensure gum is installed
if ! test -x "$(command -v gum)"; then
echo '[charm]
name=Charm
baseurl=https://repo.charm.sh/yum/
enabled=1
gpgcheck=1
gpgkey=https://repo.charm.sh/yum/gpg.key' | sudo tee /etc/yum.repos.d/charm.repo
dnf install -y gum
fi
# Helper functions
function log_struc_info() {
gum log --time datetime --structured --level info "$@"
}
function log_info() {
gum log --time datetime --level info "$@"
}
function log_struc_error() {
gum log --time datetime --structured --level error "$@"
}
# Ensure dependencies are installed
if ! test -f "/etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-${RELVER}-x86_64"; then
if ! dnf install -y distribution-gpg-keys; then
log_struc_error "Error installing distribution-gpg-keys" status "$?"
exit 1
fi
rsync -aAX /usr/share/distribution-gpg-keys/fedora/ /etc/pki/rpm-gpg
ln -sf "/etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-${RELVER}-primary" "/etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-${RELVER}-x86_64"
fi
if ! dnf install -y ostree podman rpm-ostree; then
log_struc_error "Error installing ostree, podman, and rpm-ostree" status "$?"
exit 1
fi
# Clean working directory
log_struc_info "Clean cache directory" directory "${CACHE_DIR}"
rm -rf "${CACHE_DIR}"
log_struc_info "Clean source repo" directory "${SOURCE_REPO}"
rm -rf "${SOURCE_REPO}"
log_struc_info "Clean temporary working directory" directory "${TMP_WORK_DIR}"
rm -rf "${TMP_WORK_DIR}"
log_struc_info "Clean /tmp/rpmostree*" files /tmp/rpmostree*
rm -rf /tmp/rpmostree*
if [ -n "${CLEAN_BUILD}" ]; then
log_struc_info "Clean build repo" directory "${BUILD_REPO}"
rm -rf "${BUILD_REPO}"
fi
# Prepare build env
log_struc_info "Ensure cache directory exists" directory "${CACHE_DIR}"
mkdir -p "${CACHE_DIR}"
log_struc_info "Ensure temporary working directory exists" directory "${TMP_WORK_DIR}"
mkdir -p "${TMP_WORK_DIR}"
if [ ! -d "${BUILD_REPO}/objects" ]; then
log_info "Previous build repo not found. Initialize new build repo in archive mode"
if ! ostree --repo="${BUILD_REPO}" init --mode=archive; then
log_struc_error "Error initializing new build repo in archive mode" status "$?"
exit 1
fi
fi
log_struc_info "Clone source repo" url "${SOURCE_URL}" branch "f${RELVER}" directory "${SOURCE_REPO}"
if ! git clone -b "f${RELVER}" "${SOURCE_URL}" "${SOURCE_REPO}"; then
log_struc_error "Error cloning source repo" status "$?"
exit 1
fi
log_struc_info "Copy contents of source repo into temporary work directory" source_repo "${SOURCE_REPO}" directory "${TMP_WORK_DIR}"
rsync -aAX "${SOURCE_REPO}"/ "${TMP_WORK_DIR}"
log_struc_info "Copy contents of ostree files directory into temporary work directory" source "${OSTREE_FILES_DIR}" dest "${TMP_WORK_DIR}"
rsync -aAX "${OSTREE_FILES_DIR}"/ "${TMP_WORK_DIR}"
# Compose ostree
METADATA_STR="$(date '+%Y-%m-%dT%H%M%S')"
log_struc_info "Compose ostree" cachedir "${CACHE_DIR}" repo "${BUILD_REPO}" metadata-string "${METADATA_STR}" treefile "${TREEFILE}"
if ! rpm-ostree compose tree --unified-core --cachedir="${CACHE_DIR}" --repo="${BUILD_REPO}" --add-metadata-string=Build="${METADATA_STR}" "${TREEFILE}"; then
log_struc_error "Error composing ostree" status "$?"
exit 1
fi
# Prepare deploy
log_info "Prune refs older than 30 days"
if ! ostree --repo="${BUILD_REPO}" prune --refs-only --keep-younger-than='30 days ago'; then
log_struc_error "Error pruning refs" status "$?"
exit 1
fi
# Get commit ID
log_info "Get commit ID"
OSTREE_BRANCH=$(jq ."ref" "${TREEFILE}" | tr -d '"')
COMMIT_ID=$(ostree --repo="${BUILD_REPO}" rev-parse "${OSTREE_BRANCH}")
# Login to registry
log_struc_info "Login to registry" registry "${REGISTRY}"
if ! podman login -p "${REGISTRY_PASSWD}" -u "${REGISTRY_USER}"; then
log_struc_error "Error logging into container registry" registry "${REGISTRY}" user "${REGISTRY_USER}" passwd "${REGISTRY_PASSWD}" status "$?"
exit 1
fi
# Compose container image and push to registry
log_struc_info "Compose container image from commit" commit "${COMMIT_ID}"
if ! ostree container encapsulate --repo="${BUILD_REPO}" "${OSTREE_BRANCH}" "docker://${REGISTRY}/fedora-atomic/vauxite-build/vauxite:${RELVER}"; then
log_struc_error "Error composing container image" status "$?"
exit 1
fi