aboutsummaryrefslogtreecommitdiff
path: root/scripts/build-release
blob: 030a88b0a87a5ff036416f0101daf47fafbf0f05 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/bin/bash
# build-release - Build and distribute the archzfs ISO
#
# Usage:
#   ./scripts/build-release              # Full build, test, and distribute
#   ./scripts/build-release --skip-build # Skip build, just distribute existing ISO
#   ./scripts/build-release --skip-test  # Skip sanity test
#
# Distribution targets:
#   - ~/Downloads/isos (always)
#   - truenas.local:/mnt/vault/isos (if reachable)
#   - USB drive with ARCHZFS label (if present, writes via dd)
#   - Ventoy USB drive (if detected, copies ISO file)

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
LOCAL_ISO_DIR="$REAL_HOME/Downloads/isos"
TRUENAS_HOST="truenas.local"
TRUENAS_PATH="/mnt/vault/isos"

# 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
while [[ $# -gt 0 ]]; do
    case $1 in
        --skip-build) SKIP_BUILD=true; shift ;;
        --skip-test)  SKIP_TEST=true; shift ;;
        -h|--help)
            echo "Usage: $0 [--skip-build] [--skip-test]"
            echo ""
            echo "Options:"
            echo "  --skip-build  Skip ISO build, distribute existing ISO"
            echo "  --skip-test   Skip QEMU sanity test"
            exit 0
            ;;
        *) error "Unknown option: $1" ;;
    esac
done

# Distribution status tracking
TRUENAS_SUCCESS=false

# Check root for build
check_root() {
    if [[ $EUID -ne 0 ]]; then
        error "This script must be run as root (for build and dd 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!"
}

# Find ARCHZFS labeled USB drive
find_archzfs_drive() {
    ARCHZFS_DEV=$(blkid -L ARCHZFS 2>/dev/null | head -1)
    if [[ -n "$ARCHZFS_DEV" ]]; then
        # Get the parent device (e.g., /dev/sda from /dev/sda1)
        ARCHZFS_DISK=$(lsblk -no PKNAME "$ARCHZFS_DEV" 2>/dev/null | head -1)
        if [[ -n "$ARCHZFS_DISK" ]]; then
            ARCHZFS_DISK="/dev/$ARCHZFS_DISK"
            # Verify it's removable (USB)
            REMOVABLE=$(lsblk -no RM "$ARCHZFS_DISK" 2>/dev/null | head -1)
            if [[ "$REMOVABLE" == "1" ]]; then
                info "Found ARCHZFS USB drive: $ARCHZFS_DISK"
                return 0
            else
                warn "ARCHZFS label found on $ARCHZFS_DISK but it's not removable, skipping"
            fi
        fi
    fi
    ARCHZFS_DISK=""
    return 1
}

# Find Ventoy USB drive
find_ventoy_drive() {
    VENTOY_PART=""
    VENTOY_MOUNT=""

    # Method 1: Look for partition labeled "Ventoy"
    local ventoy_by_label=$(blkid -L Ventoy 2>/dev/null | head -1)
    if [[ -n "$ventoy_by_label" ]]; then
        VENTOY_PART="$ventoy_by_label"
        info "Found Ventoy drive by label: $VENTOY_PART"
        return 0
    fi

    # Method 2: Check mounted or mountable partitions for ventoy/ directory
    # Look for removable drives with exfat/ntfs
    while read -r dev fstype rm; do
        [[ "$rm" != "1" ]] && continue  # Skip non-removable
        [[ ! "$fstype" =~ ^(exfat|ntfs|vfat)$ ]] && continue  # Skip wrong fs types

        local mount_point=$(lsblk -no MOUNTPOINT "/dev/$dev" 2>/dev/null)
        local needs_unmount=false

        if [[ -z "$mount_point" ]]; then
            # Try mounting temporarily
            mount_point=$(mktemp -d)
            if mount -o ro "/dev/$dev" "$mount_point" 2>/dev/null; then
                needs_unmount=true
            else
                rmdir "$mount_point"
                continue
            fi
        fi

        # Check for ventoy directory
        if [[ -d "$mount_point/ventoy" ]] || [[ -f "$mount_point/ventoy/ventoy.json" ]]; then
            VENTOY_PART="/dev/$dev"
            info "Found Ventoy drive by content: $VENTOY_PART"
            if $needs_unmount; then
                umount "$mount_point"
                rmdir "$mount_point"
            fi
            return 0
        fi

        if $needs_unmount; then
            umount "$mount_point"
            rmdir "$mount_point"
        fi
    done < <(lsblk -rno NAME,FSTYPE,RM 2>/dev/null | grep -v "^loop")

    return 1
}

# Distribute to local directory
distribute_local() {
    step "Distributing to Local Directory"
    mkdir -p "$LOCAL_ISO_DIR"

    # Remove old archzfs ISOs
    local old_isos=$(ls "$LOCAL_ISO_DIR"/archzfs-*.iso 2>/dev/null || true)
    if [[ -n "$old_isos" ]]; then
        info "Removing old ISOs..."
        rm -f "$LOCAL_ISO_DIR"/archzfs-*.iso
    fi

    info "Copying to $LOCAL_ISO_DIR/"
    cp "$ISO_FILE" "$LOCAL_ISO_DIR/"
    info "Done: $LOCAL_ISO_DIR/$ISO_NAME"
}

# Distribute to TrueNAS
distribute_truenas() {
    step "Distributing to TrueNAS"

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

    info "TrueNAS is reachable"

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

    # Remove old ISOs and copy new one
    info "Removing old ISOs from TrueNAS..."
    $ssh_cmd "root@$TRUENAS_HOST" "rm -f $TRUENAS_PATH/archzfs-*.iso" 2>/dev/null || true

    info "Copying to $TRUENAS_HOST:$TRUENAS_PATH/"
    if $scp_cmd "$ISO_FILE" "root@$TRUENAS_HOST:$TRUENAS_PATH/"; then
        info "Done: $TRUENAS_HOST:$TRUENAS_PATH/$ISO_NAME"
        TRUENAS_SUCCESS=true
    else
        warn "Failed to copy to TrueNAS (SSH key issue?), skipping"
        return 0
    fi
}

# Write to ARCHZFS USB drive
distribute_archzfs_usb() {
    step "Writing to ARCHZFS USB Drive"

    if ! find_archzfs_drive; then
        warn "No ARCHZFS USB drive found, skipping"
        return 0
    fi

    # Confirm before writing
    echo ""
    warn "About to write ISO to $ARCHZFS_DISK"
    warn "This will ERASE ALL DATA on the drive!"
    lsblk "$ARCHZFS_DISK"
    echo ""
    read -p "Type 'yes' to confirm: " confirm

    if [[ "$confirm" != "yes" ]]; then
        warn "Skipping USB write"
        return 0
    fi

    # Unmount any mounted partitions
    for part in $(lsblk -rno NAME "$ARCHZFS_DISK" | tail -n +2); do
        umount "/dev/$part" 2>/dev/null || true
    done

    info "Writing ISO to $ARCHZFS_DISK..."
    dd if="$ISO_FILE" of="$ARCHZFS_DISK" bs=4M status=progress oflag=sync
    sync
    info "Done writing to $ARCHZFS_DISK"
}

# Copy to Ventoy USB drive
distribute_ventoy() {
    step "Copying to Ventoy USB Drive"

    if ! find_ventoy_drive; then
        warn "No Ventoy USB drive found, skipping"
        return 0
    fi

    # Mount if needed
    local mount_point=$(lsblk -no MOUNTPOINT "$VENTOY_PART" 2>/dev/null)
    local needs_unmount=false

    if [[ -z "$mount_point" ]]; then
        mount_point=$(mktemp -d)
        info "Mounting $VENTOY_PART to $mount_point"
        mount "$VENTOY_PART" "$mount_point"
        needs_unmount=true
    fi

    # Remove old archzfs ISOs
    local old_isos=$(ls "$mount_point"/archzfs-*.iso 2>/dev/null || true)
    if [[ -n "$old_isos" ]]; then
        info "Removing old ISOs from Ventoy..."
        rm -f "$mount_point"/archzfs-*.iso
    fi

    info "Copying ISO to Ventoy drive..."
    cp "$ISO_FILE" "$mount_point/"
    sync
    info "Done: $mount_point/$ISO_NAME"

    if $needs_unmount; then
        info "Unmounting Ventoy drive..."
        umount "$mount_point"
        rmdir "$mount_point"
    fi
}

# Summary
show_summary() {
    step "Distribution Complete"
    echo "ISO: $ISO_NAME"
    echo ""
    echo "Distributed to:"
    echo "  ✓ $LOCAL_ISO_DIR/"

    if $TRUENAS_SUCCESS; then
        echo "  ✓ $TRUENAS_HOST:$TRUENAS_PATH/"
    else
        echo "  ✗ $TRUENAS_HOST (failed or skipped)"
    fi

    if [[ -n "$ARCHZFS_DISK" ]]; then
        echo "  ✓ $ARCHZFS_DISK (USB boot drive)"
    else
        echo "  - ARCHZFS USB drive (not found)"
    fi

    if [[ -n "$VENTOY_PART" ]]; then
        echo "  ✓ $VENTOY_PART (Ventoy)"
    else
        echo "  - Ventoy drive (not found)"
    fi
}

# Main
main() {
    check_root

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

    if ! $SKIP_TEST; then
        sanity_test
    else
        step "Skipping Sanity Test"
    fi

    distribute_local
    distribute_truenas
    distribute_archzfs_usb
    distribute_ventoy

    show_summary
}

main "$@"