aboutsummaryrefslogtreecommitdiff
path: root/scripts/build-release
blob: 2fbf0046b15b070f1492e614df54381936895e61 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env bash
# build-release - Build and distribute the archangel ISO
#
# Usage:
#   ./scripts/build-release              # Full build, sanity test, and distribute
#   ./scripts/build-release --full-test  # Full build with comprehensive install tests
#   ./scripts/build-release --skip-build # Skip build, just distribute existing ISO
#   ./scripts/build-release --skip-test  # Skip all testing
#
# Distribution targets (configurable via environment variables):
#   - $LOCAL_ISO_DIR (default: ~/archangel-isos)
#   - $ARCHSETUP_INBOX (notification for test VM rebuild, if set)
#   - $DIST_REMOTE_HOST:$DIST_REMOTE_PATH (if set and reachable)

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

# Get actual user (not root when running with sudo)
if [[ -n "$SUDO_USER" ]]; then
    REAL_USER="$SUDO_USER"
    REAL_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6)
else
    REAL_USER="$USER"
    REAL_HOME="$HOME"
fi

# Distribution targets (override via environment variables)
DIST_REMOTE_HOST="${DIST_REMOTE_HOST:-}"
DIST_REMOTE_PATH="${DIST_REMOTE_PATH:-/mnt/isos}"
LOCAL_ISO_DIR="${LOCAL_ISO_DIR:-$REAL_HOME/archangel-isos}"
ARCHSETUP_INBOX="${ARCHSETUP_INBOX:-}"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

info()  { echo -e "${GREEN}[INFO]${NC} $1"; }
warn()  { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
step()  { echo -e "\n${CYAN}=== $1 ===${NC}\n"; }

# Parse arguments
SKIP_BUILD=false
SKIP_TEST=false
FULL_TEST=false
while [[ $# -gt 0 ]]; do
    case $1 in
        --skip-build) SKIP_BUILD=true; shift ;;
        --skip-test)  SKIP_TEST=true; shift ;;
        --full-test)  FULL_TEST=true; shift ;;
        -h|--help)
            echo "Usage: $0 [--skip-build] [--skip-test] [--full-test]"
            echo ""
            echo "Options:"
            echo "  --skip-build  Skip ISO build, distribute existing ISO"
            echo "  --skip-test   Skip QEMU sanity test"
            echo "  --full-test   Run comprehensive install tests (single, mirror, raidz1)"
            exit 0
            ;;
        *) error "Unknown option: $1" ;;
    esac
done

# Distribution status tracking
DIST_REMOTE_SUCCESS=false
LOCAL_SUCCESS=false

# Check root for build
check_root() {
    if [[ $EUID -ne 0 ]]; then
        error "This script must be run as root (for build operations)"
    fi
}

# Find the latest ISO
find_iso() {
    ISO_FILE=$(ls -t "$PROJECT_DIR/out/"*.iso 2>/dev/null | head -1)
    if [[ -z "$ISO_FILE" ]]; then
        error "No ISO found in $PROJECT_DIR/out/"
    fi
    ISO_NAME=$(basename "$ISO_FILE")
    info "Found ISO: $ISO_NAME"
    info "Size: $(du -h "$ISO_FILE" | cut -f1)"
}

# Build the ISO
build_iso() {
    step "Building ISO"
    cd "$PROJECT_DIR"
    ./build.sh
    find_iso
}

# Run sanity test in QEMU (automated)
sanity_test() {
    step "Sanity Test (Automated)"

    if ! "$SCRIPT_DIR/sanity-test.sh"; then
        error "Sanity test failed!"
        exit 1
    fi

    info "Sanity test passed!"
}

# Run full installation tests (automated)
full_test() {
    step "Full Installation Tests"

    if ! "$SCRIPT_DIR/full-test.sh"; then
        error "Full installation test failed!"
        exit 1
    fi

    info "All installation tests passed!"
}

# Distribute to remote host via SCP
distribute_remote() {
    step "Distributing to Remote Host"

    # Skip if no host configured
    if [[ -z "$DIST_REMOTE_HOST" ]]; then
        info "DIST_REMOTE_HOST not set, skipping"
        return 0
    fi

    # Check if reachable
    if ! ping -c 1 -W 2 "$DIST_REMOTE_HOST" &>/dev/null; then
        warn "Remote host ($DIST_REMOTE_HOST) not reachable, skipping"
        return 0
    fi

    info "Remote host is reachable"

    # Run SCP as the real user (not root) to use their SSH keys
    local scp_cmd="scp"
    if [[ -n "$SUDO_USER" ]]; then
        scp_cmd="sudo -u $SUDO_USER scp"
    fi

    info "Copying to $DIST_REMOTE_HOST:$DIST_REMOTE_PATH/"
    if $scp_cmd "$ISO_FILE" "$REAL_USER@$DIST_REMOTE_HOST:$DIST_REMOTE_PATH/"; then
        info "Done: $DIST_REMOTE_HOST:$DIST_REMOTE_PATH/$ISO_NAME"
        DIST_REMOTE_SUCCESS=true
    else
        warn "Failed to copy to remote host (SSH key issue?), skipping"
        return 0
    fi
}

# Distribute locally
distribute_local() {
    step "Distributing Locally"

    mkdir -p "$LOCAL_ISO_DIR"
    info "Copying to $LOCAL_ISO_DIR/"
    cp "$ISO_FILE" "$LOCAL_ISO_DIR/"
    info "Done: $LOCAL_ISO_DIR/$ISO_NAME"
    LOCAL_SUCCESS=true

    # Notify archsetup project that a new ISO is available
    if [[ -n "$ARCHSETUP_INBOX" && -d "$ARCHSETUP_INBOX" ]]; then
        cat > "$ARCHSETUP_INBOX/rebuild-test-vm.txt" << EOF
New archangel ISO available: $ISO_NAME
Location: $LOCAL_ISO_DIR/
Rebuild the base test VM from this ISO to pick up the new kernel.
EOF
        info "Notified archsetup inbox"
    fi
}

# Summary
show_summary() {
    step "Distribution Complete"
    echo "ISO: $ISO_NAME"
    echo ""
    echo "Distributed to:"
    $LOCAL_SUCCESS && echo "  - $LOCAL_ISO_DIR/"
    $DIST_REMOTE_SUCCESS && echo "  - $DIST_REMOTE_HOST:$DIST_REMOTE_PATH/"
    $DIST_REMOTE_SUCCESS || [[ -z "$DIST_REMOTE_HOST" ]] || echo "  - Remote ($DIST_REMOTE_HOST) not reachable, skipped"
}

# Main
main() {
    check_root

    if $SKIP_BUILD; then
        step "Skipping Build"
        find_iso
    else
        build_iso
    fi

    if $FULL_TEST; then
        full_test
    elif ! $SKIP_TEST; then
        sanity_test
    else
        step "Skipping Tests"
    fi

    distribute_local
    distribute_remote

    show_summary
}

main "$@"