|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 7 | +cd "$REPO_ROOT" |
| 8 | + |
| 9 | +CACHE_BIN_DIR="${CACHE_BIN_DIR:-.cache/bin}" |
| 10 | +if [[ "$CACHE_BIN_DIR" != /* ]]; then |
| 11 | + CACHE_BIN_DIR="$REPO_ROOT/$CACHE_BIN_DIR" |
| 12 | +fi |
| 13 | + |
| 14 | +FC_INIT_BIN="${FC_INIT_BIN:-$CACHE_BIN_DIR/fc-init}" |
| 15 | +if [[ "$FC_INIT_BIN" != /* ]]; then |
| 16 | + FC_INIT_BIN="$REPO_ROOT/$FC_INIT_BIN" |
| 17 | +fi |
| 18 | + |
| 19 | +configure_tool_paths() { |
| 20 | + if command -v mkfs.ext4 >/dev/null 2>&1; then |
| 21 | + return |
| 22 | + fi |
| 23 | + |
| 24 | + if command -v brew >/dev/null 2>&1; then |
| 25 | + local e2fsprogs_prefix |
| 26 | + e2fsprogs_prefix="$(brew --prefix e2fsprogs 2>/dev/null || true)" |
| 27 | + if [ -n "$e2fsprogs_prefix" ]; then |
| 28 | + export PATH="${e2fsprogs_prefix}/sbin:${e2fsprogs_prefix}/bin:${PATH}" |
| 29 | + fi |
| 30 | + fi |
| 31 | +} |
| 32 | + |
| 33 | +install_fc_init_from_main() { |
| 34 | + local go_path="$REPO_ROOT/.cache/go" |
| 35 | + local installed_bin |
| 36 | + |
| 37 | + mkdir -p "$go_path" "$(dirname "$FC_INIT_BIN")" |
| 38 | + |
| 39 | + GOPATH="$go_path" GOOS=linux GOARCH=arm64 CGO_ENABLED=0 \ |
| 40 | + go install github.com/artemnikitin/firework/cmd/fc-init@main |
| 41 | + |
| 42 | + installed_bin="$go_path/bin/fc-init" |
| 43 | + if [ ! -f "$installed_bin" ]; then |
| 44 | + installed_bin="$go_path/bin/linux_arm64/fc-init" |
| 45 | + fi |
| 46 | + |
| 47 | + if [ ! -f "$installed_bin" ]; then |
| 48 | + echo "ERROR: go install completed, but fc-init was not found in $go_path/bin" >&2 |
| 49 | + return 1 |
| 50 | + fi |
| 51 | + |
| 52 | + cp "$installed_bin" "$FC_INIT_BIN" |
| 53 | + chmod +x "$FC_INIT_BIN" |
| 54 | +} |
| 55 | + |
| 56 | +resolve_fc_init() { |
| 57 | + mkdir -p "$CACHE_BIN_DIR" |
| 58 | + |
| 59 | + if [ -n "${FC_INIT_VERSION:-}" ]; then |
| 60 | + local tag |
| 61 | + tag="${FC_INIT_VERSION#v}" |
| 62 | + tag="v${tag}" |
| 63 | + |
| 64 | + local url |
| 65 | + url="https://github.com/artemnikitin/firework/releases/download/${tag}/fc-init-linux-arm64" |
| 66 | + echo "Downloading fc-init from release ${tag}" |
| 67 | + curl -fsSL "$url" -o "$FC_INIT_BIN" |
| 68 | + chmod +x "$FC_INIT_BIN" |
| 69 | + return |
| 70 | + fi |
| 71 | + |
| 72 | + echo "FC_INIT_VERSION not set; building fc-init from firework@main" |
| 73 | + if [ -n "${FIREWORK_GITHUB_TOKEN:-}" ]; then |
| 74 | + export GOPRIVATE='github.com/artemnikitin/*' |
| 75 | + export GIT_CONFIG_COUNT=1 |
| 76 | + export GIT_CONFIG_KEY_0="url.https://x-access-token:${FIREWORK_GITHUB_TOKEN}@github.com/.insteadOf" |
| 77 | + export GIT_CONFIG_VALUE_0="https://github.com/" |
| 78 | + fi |
| 79 | + |
| 80 | + if ! install_fc_init_from_main; then |
| 81 | + echo "::warning::Failed to build fc-init from firework@main. Falling back to bundled source." |
| 82 | + GOOS=linux GOARCH=arm64 CGO_ENABLED=0 \ |
| 83 | + go build -ldflags "-s -w" -o "$FC_INIT_BIN" ./scripts/fc-init/main.go |
| 84 | + fi |
| 85 | +} |
| 86 | + |
| 87 | +overlay_arg_for() { |
| 88 | + local tenant_id="$1" |
| 89 | + local base_name="$2" |
| 90 | + |
| 91 | + if [ -d "configs/${base_name}" ] && [ -d "configs/${tenant_id}-${base_name}" ]; then |
| 92 | + printf '%s\n' "configs/${base_name}:configs/${tenant_id}-${base_name}" |
| 93 | + elif [ -d "configs/${tenant_id}-${base_name}" ]; then |
| 94 | + printf '%s\n' "configs/${tenant_id}-${base_name}" |
| 95 | + elif [ -d "configs/${base_name}" ]; then |
| 96 | + printf '%s\n' "configs/${base_name}" |
| 97 | + fi |
| 98 | +} |
| 99 | + |
| 100 | +yaml_value() { |
| 101 | + local file="$1" |
| 102 | + local key="$2" |
| 103 | + local default_value="$3" |
| 104 | + |
| 105 | + if command -v yq >/dev/null 2>&1; then |
| 106 | + yq ".${key} // \"${default_value}\"" "$file" |
| 107 | + return |
| 108 | + fi |
| 109 | + |
| 110 | + if command -v ruby >/dev/null 2>&1; then |
| 111 | + ruby -ryaml -e ' |
| 112 | + file, key, default_value = ARGV |
| 113 | + data = YAML.load_file(file) || {} |
| 114 | + value = data[key] |
| 115 | + print(value.nil? ? default_value : value) |
| 116 | + ' "$file" "$key" "$default_value" |
| 117 | + return |
| 118 | + fi |
| 119 | + |
| 120 | + awk -v key="$key" -v default_value="$default_value" ' |
| 121 | + BEGIN { value = default_value } |
| 122 | + $0 ~ "^[[:space:]]*" key ":" { |
| 123 | + sub("^[[:space:]]*" key ":[[:space:]]*", "") |
| 124 | + gsub(/^"|"$/, "") |
| 125 | + value = $0 |
| 126 | + exit |
| 127 | + } |
| 128 | + END { print value } |
| 129 | + ' "$file" |
| 130 | +} |
| 131 | + |
| 132 | +build_images() { |
| 133 | + for tenant_dir in tenants/*/; do |
| 134 | + [ -d "$tenant_dir" ] || continue |
| 135 | + local tenant_id |
| 136 | + tenant_id="$(basename "$tenant_dir")" |
| 137 | + echo "::group::Tenant: $tenant_id" |
| 138 | + |
| 139 | + for svc_file in "${tenant_dir}"*.yaml "${tenant_dir}"*.yml; do |
| 140 | + [ -f "$svc_file" ] || continue |
| 141 | + |
| 142 | + local base_name |
| 143 | + base_name="$(basename "${svc_file%.*}")" |
| 144 | + |
| 145 | + local source_image |
| 146 | + source_image="$(yaml_value "$svc_file" source_image "")" |
| 147 | + if [ -z "$source_image" ]; then |
| 148 | + echo "Skipping ${tenant_id}-${base_name} - no source_image" |
| 149 | + continue |
| 150 | + fi |
| 151 | + |
| 152 | + local size_mb |
| 153 | + size_mb="$(yaml_value "$svc_file" rootfs_size_mb 512)" |
| 154 | + |
| 155 | + local output |
| 156 | + output="${tenant_id}-${base_name}-rootfs.ext4" |
| 157 | + |
| 158 | + local overlay_arg |
| 159 | + overlay_arg="$(overlay_arg_for "$tenant_id" "$base_name")" |
| 160 | + |
| 161 | + echo "Building $output from $source_image" |
| 162 | + bash ./scripts/docker-to-rootfs.sh "$source_image" "$output" "$size_mb" \ |
| 163 | + "${overlay_arg:-}" "$FC_INIT_BIN" |
| 164 | + done |
| 165 | + |
| 166 | + echo "::endgroup::" |
| 167 | + done |
| 168 | +} |
| 169 | + |
| 170 | +configure_tool_paths |
| 171 | +resolve_fc_init |
| 172 | +build_images |
0 commit comments