|
40 | 40 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
41 | 41 | PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
42 | 42 |
|
43 | | -log_info "Building Branchd binaries and web UI..." |
| 43 | +log_info "Building Branchd binaries and web UI in parallel..." |
44 | 44 |
|
45 | | -# Build server binary (ARM64 for t4g instance) |
46 | | -log_info "Building server binary (ARM64)..." |
47 | | -cd "$PROJECT_ROOT" |
48 | | -GOOS=linux GOARCH=arm64 go build -o /tmp/branchd-server ./cmd/server |
49 | | -if [ $? -ne 0 ]; then |
50 | | - log_error "Failed to build server binary" |
51 | | - exit 1 |
52 | | -fi |
| 45 | +# Create bundle directory structure |
| 46 | +BUNDLE_DIR="/tmp/branchd-bundle" |
| 47 | +rm -rf "$BUNDLE_DIR" |
| 48 | +mkdir -p "$BUNDLE_DIR/web" |
53 | 49 |
|
54 | | -# Build worker binary (ARM64 for t4g instance) |
55 | | -log_info "Building worker binary (ARM64)..." |
56 | | -GOOS=linux GOARCH=arm64 go build -o /tmp/branchd-worker ./cmd/worker |
57 | | -if [ $? -ne 0 ]; then |
58 | | - log_error "Failed to build worker binary" |
| 50 | +# Build all components in parallel |
| 51 | +BUILD_FAILED=0 |
| 52 | + |
| 53 | +# Server binary (ARM64) |
| 54 | +( |
| 55 | + log_info "Building server binary (ARM64)..." |
| 56 | + cd "$PROJECT_ROOT" |
| 57 | + if GOOS=linux GOARCH=arm64 go build -o "$BUNDLE_DIR/server" ./cmd/server 2>&1; then |
| 58 | + log_info "✓ Server binary built" |
| 59 | + else |
| 60 | + log_error "Failed to build server binary" |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | +) & |
| 64 | +SERVER_PID=$! |
| 65 | + |
| 66 | +# Worker binary (ARM64) |
| 67 | +( |
| 68 | + log_info "Building worker binary (ARM64)..." |
| 69 | + cd "$PROJECT_ROOT" |
| 70 | + if GOOS=linux GOARCH=arm64 go build -o "$BUNDLE_DIR/worker" ./cmd/worker 2>&1; then |
| 71 | + log_info "✓ Worker binary built" |
| 72 | + else |
| 73 | + log_error "Failed to build worker binary" |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +) & |
| 77 | +WORKER_PID=$! |
| 78 | + |
| 79 | +# Web UI |
| 80 | +( |
| 81 | + log_info "Building web UI..." |
| 82 | + cd "$PROJECT_ROOT/web" |
| 83 | + if bun run build 2>&1; then |
| 84 | + cp -r "$PROJECT_ROOT/web/dist/"* "$BUNDLE_DIR/web/" |
| 85 | + log_info "✓ Web UI built" |
| 86 | + else |
| 87 | + log_error "Failed to build web UI" |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +) & |
| 91 | +WEB_PID=$! |
| 92 | + |
| 93 | +# Wait for all builds to complete |
| 94 | +wait $SERVER_PID || BUILD_FAILED=1 |
| 95 | +wait $WORKER_PID || BUILD_FAILED=1 |
| 96 | +wait $WEB_PID || BUILD_FAILED=1 |
| 97 | + |
| 98 | +if [ $BUILD_FAILED -eq 1 ]; then |
| 99 | + log_error "One or more builds failed" |
59 | 100 | exit 1 |
60 | 101 | fi |
61 | 102 |
|
62 | | -# Build web UI |
63 | | -log_info "Building web UI..." |
64 | | -cd "$PROJECT_ROOT/web" |
65 | | -bun run build |
66 | | -if [ $? -ne 0 ]; then |
67 | | - log_error "Failed to build web UI" |
68 | | - exit 1 |
69 | | -fi |
| 103 | +log_info "All builds completed successfully" |
70 | 104 |
|
71 | | -# Create bundle directory structure |
72 | | -BUNDLE_DIR="/tmp/branchd-bundle" |
73 | | -log_info "Creating bundle directory structure..." |
74 | | -rm -rf "$BUNDLE_DIR" |
75 | | -mkdir -p "$BUNDLE_DIR/web" |
| 105 | +# Upload and deploy in a single SSH session to minimize RTTs |
| 106 | +log_info "Uploading and deploying to VM ($VM_IP)..." |
76 | 107 |
|
77 | | -# Copy binaries to bundle |
78 | | -log_info "Copying binaries to bundle..." |
79 | | -cp /tmp/branchd-server "$BUNDLE_DIR/server" |
80 | | -cp /tmp/branchd-worker "$BUNDLE_DIR/worker" |
| 108 | +# Use rsync for efficient file transfer (single RTT) |
| 109 | +rsync -az --delete \ |
| 110 | + -e "ssh -i $SSH_KEY -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o ConnectTimeout=10" \ |
| 111 | + "$BUNDLE_DIR/" \ |
| 112 | + "$SSH_USER@$VM_IP:/tmp/branchd-bundle/" |
81 | 113 |
|
82 | | -# Copy web UI to bundle |
83 | | -log_info "Copying web UI to bundle..." |
84 | | -cp -r "$PROJECT_ROOT/web/dist/"* "$BUNDLE_DIR/web/" |
| 114 | +if [ $? -ne 0 ]; then |
| 115 | + log_error "Failed to upload bundle to VM" |
| 116 | + exit 1 |
| 117 | +fi |
85 | 118 |
|
86 | | -# Upload bundle to VM |
87 | | -log_info "Uploading bundle to VM ($VM_IP)..." |
88 | | -ssh -i "$SSH_KEY" \ |
| 119 | +# Execute all remote operations in a single SSH session |
| 120 | +log_info "Installing and restarting services..." |
| 121 | +REMOTE_OUTPUT=$(ssh -i "$SSH_KEY" \ |
89 | 122 | -o StrictHostKeyChecking=no \ |
90 | 123 | -o UserKnownHostsFile=/dev/null \ |
91 | 124 | -o LogLevel=ERROR \ |
92 | 125 | -o ConnectTimeout=10 \ |
93 | | - "$SSH_USER@$VM_IP" \ |
94 | | - "mkdir -p /tmp/branchd-bundle" |
| 126 | + "$SSH_USER@$VM_IP" /bin/bash <<'EOF' |
| 127 | +set -euo pipefail |
95 | 128 |
|
96 | | -scp -i "$SSH_KEY" \ |
97 | | - -o StrictHostKeyChecking=no \ |
98 | | - -o UserKnownHostsFile=/dev/null \ |
99 | | - -o LogLevel=ERROR \ |
100 | | - -o ConnectTimeout=10 \ |
101 | | - -r "$BUNDLE_DIR/." \ |
102 | | - "$SSH_USER@$VM_IP:/tmp/branchd-bundle/" |
| 129 | +# Install binaries |
| 130 | +sudo install -m 755 /tmp/branchd-bundle/server /usr/local/bin/branchd-server |
| 131 | +sudo install -m 755 /tmp/branchd-bundle/worker /usr/local/bin/branchd-worker |
103 | 132 |
|
104 | | -if [ $? -ne 0 ]; then |
105 | | - log_error "Failed to upload bundle to VM" |
| 133 | +# Install web UI |
| 134 | +sudo rm -rf /var/www/branchd/* |
| 135 | +sudo cp -r /tmp/branchd-bundle/web/* /var/www/branchd/ |
| 136 | +sudo chown -R caddy:caddy /var/www/branchd |
| 137 | +
|
| 138 | +# Create data directory |
| 139 | +sudo mkdir -p /data |
| 140 | +sudo chmod 755 /data |
| 141 | +
|
| 142 | +# Restart services |
| 143 | +sudo systemctl restart caddy |
| 144 | +sudo systemctl daemon-reload |
| 145 | +sudo systemctl restart branchd-server branchd-worker |
| 146 | +sleep 1 |
| 147 | +
|
| 148 | +# Verify services and output status |
| 149 | +SERVER_STATUS=$(systemctl is-active branchd-server || echo "failed") |
| 150 | +WORKER_STATUS=$(systemctl is-active branchd-worker || echo "failed") |
| 151 | +
|
| 152 | +echo "SERVER_STATUS=$SERVER_STATUS" |
| 153 | +echo "WORKER_STATUS=$WORKER_STATUS" |
| 154 | +
|
| 155 | +if [ "$SERVER_STATUS" != "active" ] || [ "$WORKER_STATUS" != "active" ]; then |
106 | 156 | exit 1 |
107 | 157 | fi |
| 158 | +EOF |
| 159 | +) |
108 | 160 |
|
109 | | -# Helper function to run SSH commands |
110 | | -ssh_exec() { |
111 | | - ssh -i "$SSH_KEY" \ |
112 | | - -o StrictHostKeyChecking=no \ |
113 | | - -o UserKnownHostsFile=/dev/null \ |
114 | | - -o LogLevel=ERROR \ |
115 | | - -o ConnectTimeout=10 \ |
116 | | - "$SSH_USER@$VM_IP" \ |
117 | | - "$1" |
118 | | -} |
119 | | - |
120 | | -# Install binaries on VM |
121 | | -log_info "Installing binaries on VM..." |
122 | | -ssh_exec "sudo install -m 755 /tmp/branchd-bundle/server /usr/local/bin/branchd-server" |
123 | | -ssh_exec "sudo install -m 755 /tmp/branchd-bundle/worker /usr/local/bin/branchd-worker" |
124 | | - |
125 | | -# Install web UI on VM |
126 | | -log_info "Installing web UI on VM..." |
127 | | -ssh_exec "sudo rm -rf /var/www/branchd/*" |
128 | | -ssh_exec "sudo cp -r /tmp/branchd-bundle/web/* /var/www/branchd/" |
129 | | -ssh_exec "sudo chown -R caddy:caddy /var/www/branchd" |
130 | | - |
131 | | -# Create data directory if it doesn't exist |
132 | | -log_info "Creating data directory..." |
133 | | -ssh_exec "sudo mkdir -p /data" |
134 | | -ssh_exec "sudo chmod 755 /data" |
135 | | - |
136 | | -# Restart Caddy (web UI is now deployed) |
137 | | -log_info "Restarting Caddy web server..." |
138 | | -ssh_exec "sudo systemctl restart caddy" |
139 | | - |
140 | | -# Restart Branchd services |
141 | | -log_info "Restarting Branchd services..." |
142 | | -ssh_exec "sudo systemctl daemon-reload" |
143 | | -ssh_exec "sudo systemctl restart branchd-server branchd-worker" |
144 | | - |
145 | | -# Wait a moment for services to start |
146 | | -log_info "Waiting for services to start..." |
147 | | -sleep 5 |
148 | | - |
149 | | -# Verify services are running |
150 | | -log_info "Verifying services..." |
151 | | -SERVER_STATUS=$(ssh_exec "systemctl is-active branchd-server" || echo "failed") |
152 | | -WORKER_STATUS=$(ssh_exec "systemctl is-active branchd-worker" || echo "failed") |
153 | | - |
154 | | -if [ "$SERVER_STATUS" = "active" ] && [ "$WORKER_STATUS" = "active" ]; then |
155 | | - log_info "✓ Branchd deployment complete!" |
156 | | - log_info " Server status: $SERVER_STATUS" |
157 | | - log_info " Worker status: $WORKER_STATUS" |
158 | | - log_info " Web UI: https://$VM_IP" |
159 | | -else |
160 | | - log_error "Service verification failed" |
161 | | - log_error " Server status: $SERVER_STATUS" |
162 | | - log_error " Worker status: $WORKER_STATUS" |
| 161 | +# Check if remote commands succeeded |
| 162 | +if [ $? -ne 0 ]; then |
| 163 | + log_error "Deployment failed on VM" |
| 164 | + log_error "$REMOTE_OUTPUT" |
163 | 165 | exit 1 |
164 | 166 | fi |
165 | 167 |
|
| 168 | +# Parse status from output |
| 169 | +SERVER_STATUS=$(echo "$REMOTE_OUTPUT" | grep "SERVER_STATUS=" | cut -d= -f2) |
| 170 | +WORKER_STATUS=$(echo "$REMOTE_OUTPUT" | grep "WORKER_STATUS=" | cut -d= -f2) |
| 171 | + |
| 172 | +log_info "✓ Branchd deployment complete!" |
| 173 | +log_info " Server status: $SERVER_STATUS" |
| 174 | +log_info " Worker status: $WORKER_STATUS" |
| 175 | +log_info " Web UI: https://$VM_IP" |
| 176 | + |
166 | 177 | # Cleanup |
167 | 178 | log_info "Cleaning up temporary files..." |
168 | 179 | rm -rf "$BUNDLE_DIR" |
169 | | -rm -f /tmp/branchd-server /tmp/branchd-worker |
170 | 180 |
|
171 | 181 | log_info "Done!" |
0 commit comments