Skip to content

Commit dca48a1

Browse files
committed
chore: fewer RTTs to build and deploy for testing
1 parent 844fc46 commit dca48a1

File tree

1 file changed

+113
-103
lines changed

1 file changed

+113
-103
lines changed

bin/build_and_deploy.sh

Lines changed: 113 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -40,132 +40,142 @@ fi
4040
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4141
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
4242

43-
log_info "Building Branchd binaries and web UI..."
43+
log_info "Building Branchd binaries and web UI in parallel..."
4444

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"
5349

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"
59100
exit 1
60101
fi
61102

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"
70104

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)..."
76107

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/"
81113

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
85118

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" \
89122
-o StrictHostKeyChecking=no \
90123
-o UserKnownHostsFile=/dev/null \
91124
-o LogLevel=ERROR \
92125
-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
95128
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
103132
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
106156
exit 1
107157
fi
158+
EOF
159+
)
108160

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"
163165
exit 1
164166
fi
165167

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+
166177
# Cleanup
167178
log_info "Cleaning up temporary files..."
168179
rm -rf "$BUNDLE_DIR"
169-
rm -f /tmp/branchd-server /tmp/branchd-worker
170180

171181
log_info "Done!"

0 commit comments

Comments
 (0)