summaryrefslogtreecommitdiff
path: root/build.sh
blob: cb9524356982122a2f084e0dc9328bc803bad06b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash

set -Eeuo pipefail
MODE="${1:-build}"
### CONFIG ###
TIMESTAMP=$(date +%Y-%m-%d)
REPO_URL="https://git.hibbian.org/hibbian/hibbian-livedisk"
WORKDIR="/opt/livebuild/work"
OUTPUT_DIR="/opt/livebuild/output"
AMD64_DIR="$OUTPUT_DIR/amd64"
ARM64_DIR="$OUTPUT_DIR/arm64"
REMOTE_TARGET="hibby@hibbian.org:/var/www/download/"
LOGFILE_BASENAME="${TIMESTAMP}-build-amd64.log"
FINAL_NAME="${TIMESTAMP}-Hibbian-amd64.iso"
LIVE_BUILD_SUCCESS_GRACE_SECONDS="${LIVE_BUILD_SUCCESS_GRACE_SECONDS:-300}"

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
LOGFILE="$SCRIPT_DIR/$LOGFILE_BASENAME"
cd "$SCRIPT_DIR"

# shellcheck source=build-helpers.sh
source "$SCRIPT_DIR/build-helpers.sh"

trap 'echo "ERROR: command failed at ${BASH_SOURCE[0]}:${LINENO}: $BASH_COMMAND" >> "$LOGFILE"' ERR

run_build() {
    mkdir -p "$WORKDIR" "$OUTPUT_DIR" "$AMD64_DIR" "$ARM64_DIR"

    if has_today_build "$AMD64_DIR" "amd64.iso"; then
        log_step "Build already exists for today; skipping build"
        generate_links_notes
        run_stats
        return 0
    fi

    log_step "No build for today; proceeding"

    ensure_git_repo_updated "$REPO_URL" "$WORKDIR/config" "$LOGFILE"

    cd "$WORKDIR/config"

	echo "Cleaning previous live-build state..." >> "$LOGFILE"
	sudo lb clean >> "$LOGFILE" 2>&1

	# Run live-build config
	echo "Running lb config..." >> "$LOGFILE"
	lb config >> "$LOGFILE" 2>&1

	# Build (must be root)
	echo "Running lb build..." >> "$LOGFILE"
	run_live_build
	log_step "lb build complete; starting post_build"
	post_build
}

run_live_build() {
	local success_marker="P: Build completed successfully"
	local grace_started=0
	local deadline=0
	local pid
	local status
	local log_start_size
	local lb_command=(sudo lb build)

	log_start_size=$(wc -c < "$LOGFILE" 2>/dev/null || echo 0)

	if command -v setsid >/dev/null 2>&1; then
		setsid "${lb_command[@]}" </dev/null >> "$LOGFILE" 2>&1 &
	else
		"${lb_command[@]}" </dev/null >> "$LOGFILE" 2>&1 &
	fi

	pid=$!

	while kill -0 "$pid" 2>/dev/null; do
		if (( grace_started == 0 )) && tail -c +"$((log_start_size + 1))" "$LOGFILE" | grep -qF "$success_marker"; then
			grace_started=1
			deadline=$(( $(date +%s) + LIVE_BUILD_SUCCESS_GRACE_SECONDS ))
			log_step "lb build reported success; waiting up to ${LIVE_BUILD_SUCCESS_GRACE_SECONDS}s for process exit"
		fi

		if (( grace_started == 1 && $(date +%s) >= deadline )); then
			log_step "lb build still running after success; terminating stuck process group"
			kill -- "-$pid" 2>/dev/null || kill "$pid" 2>/dev/null || true
			sleep 5
			kill -KILL -- "-$pid" 2>/dev/null || kill -KILL "$pid" 2>/dev/null || true
			wait "$pid" 2>/dev/null || true
			return 0
		fi

		sleep 5
	done

	if wait "$pid"; then
		status=0
	else
		status=$?
	fi

	if (( status != 0 )); then
		log_step "lb build failed with exit status $status"
		return "$status"
	fi
}

post_build() {
    log_step "post_build: preparing output directories"
    mkdir -p "$OUTPUT_DIR" "$AMD64_DIR" "$ARM64_DIR"

    log_step "post_build: entering $WORKDIR/config"
    cd "$WORKDIR/config"

    log_step "post_build: finding live-build ISO"
    if ! ISO=$(find_single_artifact "." "*.iso" "live-build ISO"); then
        exit 1
    fi

    log_step "post_build: moving $ISO to $AMD64_DIR/$FINAL_NAME"
    mv "$ISO" "$AMD64_DIR/$FINAL_NAME" >> "$LOGFILE" 2>&1

    log_step "post_build: generating checksum"
    (cd "$AMD64_DIR" && sha256sum "$FINAL_NAME" > "$FINAL_NAME.sha256") >> "$LOGFILE" 2>&1

    log_step "post_build: generating latest links and release notes"
    generate_links_notes >> "$LOGFILE" 2>&1

    log_step "post_build: pruning old builds"
    prune_old_builds >> "$LOGFILE" 2>&1

    log_step "post_build: regenerating stats and index"
    run_stats >> "$LOGFILE" 2>&1

    log_step "post_build: syncing to remote"
    run_sync >> "$LOGFILE" 2>&1

    log_step "Build complete"
}

case "$MODE" in
    build)
        run_build
        ;;
    stats)
        run_stats
        ;;
    post_build)
        post_build
        ;;
    sync)
        run_sync
        ;;
    *)
        echo "Usage: $0 [build|post_build|stats|sync]"
        exit 1
        ;;
esac