deploy-orly.sh raw
1 #!/bin/bash
2 # ORLY Deployment Script
3 # Usage: curl -sSL https://relay.orly.dev/deploy.sh | bash -s -- [options]
4 #
5 # Options:
6 # --binaries-url URL URL to download binaries tarball from
7 # --local-path PATH Local path to binaries (for scp-based deploy)
8 # --host HOST Target host (default: relay.orly.dev)
9 # --restart Restart service after deployment
10 # --rollback Rollback to previous release
11 # --list List available releases
12 # --help Show this help
13
14 set -e
15
16 # Configuration
17 DEPLOY_HOST="${DEPLOY_HOST:-relay.orly.dev}"
18 TIMESTAMP=$(date +%Y%m%d-%H%M%S)
19
20 # Binary names and their symlink mappings
21 declare -A BINARIES=(
22 ["orly"]="orly-relay"
23 ["orly-db"]="orly-db-badger"
24 ["orly-acl"]="orly-acl-follows"
25 ["orly-launcher"]="orly-launcher"
26 ["orly-sync-negentropy"]="orly-sync-negentropy"
27 )
28
29 # Colors for output
30 RED='\033[0;31m'
31 GREEN='\033[0;32m'
32 YELLOW='\033[1;33m'
33 NC='\033[0m' # No Color
34
35 log_info() {
36 echo -e "${GREEN}[INFO]${NC} $1"
37 }
38
39 log_warn() {
40 echo -e "${YELLOW}[WARN]${NC} $1"
41 }
42
43 log_error() {
44 echo -e "${RED}[ERROR]${NC} $1"
45 }
46
47 show_help() {
48 head -15 "$0" | tail -13
49 exit 0
50 }
51
52 # Parse arguments
53 BINARIES_URL=""
54 LOCAL_PATH=""
55 DO_RESTART=false
56 DO_ROLLBACK=false
57 DO_LIST=false
58
59 while [[ $# -gt 0 ]]; do
60 case $1 in
61 --binaries-url)
62 BINARIES_URL="$2"
63 shift 2
64 ;;
65 --local-path)
66 LOCAL_PATH="$2"
67 shift 2
68 ;;
69 --host)
70 DEPLOY_HOST="$2"
71 shift 2
72 ;;
73 --restart)
74 DO_RESTART=true
75 shift
76 ;;
77 --rollback)
78 DO_ROLLBACK=true
79 shift
80 ;;
81 --list)
82 DO_LIST=true
83 shift
84 ;;
85 --help|-h)
86 show_help
87 ;;
88 *)
89 log_error "Unknown option: $1"
90 show_help
91 ;;
92 esac
93 done
94
95 # List releases
96 if $DO_LIST; then
97 log_info "Available releases on $DEPLOY_HOST:"
98 ssh "$DEPLOY_HOST" 'ls -la ~/.local/bin/releases/ 2>/dev/null | tail -n +4' || log_error "Failed to list releases"
99
100 log_info "Current symlinks:"
101 ssh "$DEPLOY_HOST" 'ls -la ~/.local/bin/orly* 2>/dev/null | grep "^l"' || log_warn "No symlinks found"
102 exit 0
103 fi
104
105 # Rollback to previous release
106 if $DO_ROLLBACK; then
107 log_info "Rolling back to previous release on $DEPLOY_HOST..."
108
109 # Get the two most recent releases
110 RELEASES=$(ssh "$DEPLOY_HOST" 'ls -1t ~/.local/bin/releases/ | head -2')
111 CURRENT=$(echo "$RELEASES" | head -1)
112 PREVIOUS=$(echo "$RELEASES" | tail -1)
113
114 if [ -z "$PREVIOUS" ] || [ "$CURRENT" = "$PREVIOUS" ]; then
115 log_error "No previous release found to rollback to"
116 exit 1
117 fi
118
119 log_info "Rolling back from $CURRENT to $PREVIOUS"
120
121 # Update symlinks to previous release
122 ssh "$DEPLOY_HOST" "bash -c '
123 RELEASES_DIR=~/.local/bin/releases
124 BIN_DIR=~/.local/bin
125 PREVIOUS=\"$PREVIOUS\"
126
127 cd \$BIN_DIR
128 for link in orly-*; do
129 if [ -L \"\$link\" ]; then
130 target=\$(readlink \"\$link\")
131 binary=\$(basename \"\$target\")
132 if [ -f \"\$RELEASES_DIR/\$PREVIOUS/\$binary\" ]; then
133 ln -sf \"\$RELEASES_DIR/\$PREVIOUS/\$binary\" \"\$link\"
134 echo \"Updated \$link -> \$RELEASES_DIR/\$PREVIOUS/\$binary\"
135 fi
136 fi
137 done
138 '"
139
140 if $DO_RESTART; then
141 log_info "Restarting service..."
142 ssh "root@$DEPLOY_HOST" "systemctl restart orly" || log_warn "Failed to restart (may need root)"
143 fi
144
145 log_info "Rollback complete"
146 exit 0
147 fi
148
149 # Deploy new release
150 log_info "Deploying to $DEPLOY_HOST (release: $TIMESTAMP)"
151
152 # Create release directory
153 ssh "$DEPLOY_HOST" "mkdir -p ~/.local/bin/releases/$TIMESTAMP"
154
155 if [ -n "$LOCAL_PATH" ]; then
156 # Deploy from local path
157 log_info "Deploying from local path: $LOCAL_PATH"
158
159 for binary in "${!BINARIES[@]}"; do
160 symlink_name="${BINARIES[$binary]}"
161 if [ -f "$LOCAL_PATH/$binary" ]; then
162 log_info "Uploading $binary as $symlink_name..."
163 scp "$LOCAL_PATH/$binary" "$DEPLOY_HOST:~/.local/bin/releases/$TIMESTAMP/$symlink_name"
164 ssh "$DEPLOY_HOST" "chmod +x ~/.local/bin/releases/$TIMESTAMP/$symlink_name"
165 fi
166 done
167
168 elif [ -n "$BINARIES_URL" ]; then
169 # Deploy from URL
170 log_info "Downloading binaries from $BINARIES_URL"
171
172 ssh "$DEPLOY_HOST" "bash -c '
173 cd ~/.local/bin/releases/$TIMESTAMP
174 curl -sSL \"$BINARIES_URL\" | tar xz
175 chmod +x *
176 '"
177
178 else
179 log_error "No binaries source specified. Use --local-path or --binaries-url"
180 exit 1
181 fi
182
183 # Update symlinks
184 log_info "Updating symlinks..."
185 ssh "$DEPLOY_HOST" "bash -c '
186 RELEASES_DIR=~/.local/bin/releases
187 BIN_DIR=~/.local/bin
188 TIMESTAMP=\"$TIMESTAMP\"
189
190 cd \$BIN_DIR
191 for binary in \$RELEASES_DIR/\$TIMESTAMP/*; do
192 name=\$(basename \"\$binary\")
193 ln -sf \"\$binary\" \"\$name\"
194 echo \" \$name -> \$binary\"
195 done
196 '"
197
198 # Show deployed binaries
199 log_info "Deployed binaries:"
200 ssh "$DEPLOY_HOST" "ls -la ~/.local/bin/releases/$TIMESTAMP/"
201
202 # Restart service if requested
203 if $DO_RESTART; then
204 log_info "Restarting orly service..."
205 ssh "root@$DEPLOY_HOST" "systemctl restart orly" && log_info "Service restarted" || log_warn "Failed to restart (may need root)"
206 fi
207
208 # Cleanup old releases (keep last 5)
209 log_info "Cleaning up old releases (keeping last 5)..."
210 ssh "$DEPLOY_HOST" 'bash -c '\''
211 RELEASES_DIR=~/.local/bin/releases
212 cd "$RELEASES_DIR"
213 ls -1t | tail -n +6 | while read old; do
214 echo " Removing old release: $old"
215 rm -rf "$old"
216 done
217 '\'''
218
219 log_info "Deployment complete!"
220 log_info "To restart the service: ssh root@$DEPLOY_HOST 'systemctl restart orly'"
221