photoncloud-monorepo/nix/test-cluster/run-local-baseline.sh

198 lines
6.7 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
export PATH="/run/current-system/sw/bin:/usr/bin:/bin:${PATH}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
TASK_ID="b1e811fb-158f-415c-a011-64c724e84c5c"
WORK_ROOT="${ULTRACLOUD_WORK_ROOT:-${REPO_ROOT}/work}"
LOG_ROOT="${1:-${WORK_ROOT}/baselines/${TASK_ID}}"
mkdir -p "${LOG_ROOT}"
log() {
printf '[local-baseline] %s\n' "$*"
}
host_cpu_count() {
local count
count="$(getconf _NPROCESSORS_ONLN 2>/dev/null || nproc 2>/dev/null || echo 1)"
if [[ ! "${count}" =~ ^[0-9]+$ ]] || (( count < 1 )); then
count=1
fi
printf '%s\n' "${count}"
}
default_local_nix_max_jobs() {
local cpu_count="$1"
if (( cpu_count <= 2 )); then
printf '1\n'
return 0
fi
printf '%s\n' "$(( (cpu_count + 1) / 2 ))"
}
default_local_nix_build_cores() {
local cpu_count="$1"
local max_jobs="$2"
local build_cores=1
if (( max_jobs > 0 )); then
build_cores="$(( cpu_count / max_jobs ))"
fi
if (( build_cores < 1 )); then
build_cores=1
fi
printf '%s\n' "${build_cores}"
}
append_nix_config_line() {
local line="$1"
if [[ -n "${NIX_CONFIG:-}" ]]; then
NIX_CONFIG+=$'\n'
fi
NIX_CONFIG+="${line}"
}
host_nested_param_path() {
if [[ -f /sys/module/kvm_intel/parameters/nested ]]; then
printf '%s\n' /sys/module/kvm_intel/parameters/nested
elif [[ -f /sys/module/kvm_amd/parameters/nested ]]; then
printf '%s\n' /sys/module/kvm_amd/parameters/nested
fi
}
prepare_runtime_dirs() {
local cpu_count default_max_jobs default_build_cores
cpu_count="$(host_cpu_count)"
default_max_jobs="$(default_local_nix_max_jobs "${cpu_count}")"
default_build_cores="$(default_local_nix_build_cores "${cpu_count}" "${default_max_jobs}")"
export ULTRACLOUD_WORK_ROOT="${WORK_ROOT}"
export TMPDIR="${TMPDIR:-${WORK_ROOT}/tmp}"
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-${WORK_ROOT}/xdg-cache}"
export PHOTON_CLUSTER_WORK_ROOT="${PHOTON_CLUSTER_WORK_ROOT:-${WORK_ROOT}/test-cluster}"
export PHOTON_VM_DIR="${PHOTON_VM_DIR:-${PHOTON_CLUSTER_WORK_ROOT}/state}"
export PHOTON_CLUSTER_VDE_SWITCH_DIR="${PHOTON_CLUSTER_VDE_SWITCH_DIR:-${PHOTON_CLUSTER_WORK_ROOT}/vde-switch}"
export ULTRACLOUD_LOCAL_NIX_MAX_JOBS="${ULTRACLOUD_LOCAL_NIX_MAX_JOBS:-${default_max_jobs}}"
export ULTRACLOUD_LOCAL_NIX_BUILD_CORES="${ULTRACLOUD_LOCAL_NIX_BUILD_CORES:-${default_build_cores}}"
export PHOTON_CLUSTER_NIX_MAX_JOBS="${PHOTON_CLUSTER_NIX_MAX_JOBS:-${ULTRACLOUD_LOCAL_NIX_MAX_JOBS}}"
export PHOTON_CLUSTER_NIX_BUILD_CORES="${PHOTON_CLUSTER_NIX_BUILD_CORES:-${ULTRACLOUD_LOCAL_NIX_BUILD_CORES}}"
append_nix_config_line "builders ="
append_nix_config_line "max-jobs = ${ULTRACLOUD_LOCAL_NIX_MAX_JOBS}"
append_nix_config_line "cores = ${ULTRACLOUD_LOCAL_NIX_BUILD_CORES}"
append_nix_config_line "experimental-features = nix-command flakes"
append_nix_config_line "warn-dirty = false"
export NIX_CONFIG
mkdir -p \
"${TMPDIR}" \
"${XDG_CACHE_HOME}" \
"${PHOTON_CLUSTER_WORK_ROOT}" \
"${PHOTON_CLUSTER_VDE_SWITCH_DIR}" \
"${LOG_ROOT}"
}
capture_environment() {
{
printf 'task_id=%s\n' "${TASK_ID}"
printf 'started_at=%s\n' "$(date -Is)"
printf 'repo_root=%s\n' "${REPO_ROOT}"
printf 'work_root=%s\n' "${WORK_ROOT}"
printf 'log_root=%s\n' "${LOG_ROOT}"
printf 'branch=%s\n' "$(git -C "${REPO_ROOT}" branch --show-current)"
printf 'commit=%s\n' "$(git -C "${REPO_ROOT}" rev-parse HEAD)"
printf 'host_cpu_count=%s\n' "$(host_cpu_count)"
printf 'ultracloud_local_nix_max_jobs=%s\n' "${ULTRACLOUD_LOCAL_NIX_MAX_JOBS}"
printf 'ultracloud_local_nix_build_cores=%s\n' "${ULTRACLOUD_LOCAL_NIX_BUILD_CORES}"
printf 'photon_cluster_nix_max_jobs=%s\n' "${PHOTON_CLUSTER_NIX_MAX_JOBS}"
printf 'photon_cluster_nix_build_cores=%s\n' "${PHOTON_CLUSTER_NIX_BUILD_CORES}"
printf 'tmpdir=%s\n' "${TMPDIR}"
printf 'xdg_cache_home=%s\n' "${XDG_CACHE_HOME}"
printf 'photon_cluster_work_root=%s\n' "${PHOTON_CLUSTER_WORK_ROOT}"
printf 'photon_vm_dir=%s\n' "${PHOTON_VM_DIR}"
printf 'photon_cluster_vde_switch_dir=%s\n' "${PHOTON_CLUSTER_VDE_SWITCH_DIR}"
printf 'nix_version=%s\n' "$(nix --version)"
printf 'nix_builders=%s\n' "$(nix config show builders 2>/dev/null | awk -F' = ' 'NR==1 { print $2 }')"
printf 'kvm_present=%s\n' "$([[ -e /dev/kvm ]] && echo yes || echo no)"
printf 'kvm_access=%s\n' "$([[ -r /dev/kvm && -w /dev/kvm ]] && echo rw || echo no)"
if [[ -e /dev/kvm ]]; then
printf 'kvm_stat=%s\n' "$(stat -c '%A %U %G %t:%T' /dev/kvm)"
fi
local nested_path
nested_path="$(host_nested_param_path || true)"
if [[ -n "${nested_path}" ]]; then
printf 'nested_param_path=%s\n' "${nested_path}"
printf 'nested_param_value=%s\n' "$(<"${nested_path}")"
fi
} >"${LOG_ROOT}/environment.txt"
}
run_case() {
local name="$1"
local timeout_secs="$2"
shift 2
local logfile="${LOG_ROOT}/${name}.log"
local metafile="${LOG_ROOT}/${name}.meta"
local started_at ended_at rc
started_at="$(date -Is)"
printf 'name=%s\n' "${name}" >"${metafile}"
printf 'started_at=%s\n' "${started_at}" >>"${metafile}"
printf 'timeout_secs=%s\n' "${timeout_secs}" >>"${metafile}"
printf 'command=' >>"${metafile}"
printf '%q ' "$@" >>"${metafile}"
printf '\n' >>"${metafile}"
log "running ${name}: $*"
set +e
(
cd "${REPO_ROOT}"
timeout --signal=TERM --kill-after=60 "${timeout_secs}" "$@"
) 2>&1 | tee "${logfile}"
rc=${PIPESTATUS[0]}
set -e
ended_at="$(date -Is)"
printf 'ended_at=%s\n' "${ended_at}" >>"${metafile}"
printf 'exit_code=%s\n' "${rc}" >>"${metafile}"
if (( rc == 124 )); then
log "${name} timed out after ${timeout_secs}s"
elif (( rc == 0 )); then
log "${name} passed"
else
log "${name} failed with exit ${rc}"
fi
return 0
}
main() {
prepare_runtime_dirs
capture_environment
run_case canonical-profile-eval-guards 3600 \
nix build .#checks.x86_64-linux.canonical-profile-eval-guards --no-link
run_case supported-surface-guard 3600 \
nix build .#checks.x86_64-linux.supported-surface-guard --no-link
run_case portable-control-plane-regressions 10800 \
nix build .#checks.x86_64-linux.portable-control-plane-regressions
run_case deployer-bootstrap-e2e 10800 \
nix build .#checks.x86_64-linux.deployer-bootstrap-e2e
run_case host-lifecycle-e2e 10800 \
nix build .#checks.x86_64-linux.host-lifecycle-e2e
run_case fleet-scheduler-e2e 10800 \
nix build .#checks.x86_64-linux.fleet-scheduler-e2e
run_case single-node-quickstart 14400 \
nix run .#single-node-quickstart
run_case baremetal-iso 21600 \
nix run ./nix/test-cluster#cluster -- baremetal-iso
run_case fresh-smoke 28800 \
nix run ./nix/test-cluster#cluster -- fresh-smoke
}
main "$@"