Skip to main content

max / makenotwork

5.0 KB · 161 lines History Blame Raw
1 #!/bin/bash
2 # Multithreaded Deployment Script
3 # Builds natively on astra (aarch64), deploys to /opt/multithreaded.
4 # Run from the multithreaded project root.
5 #
6 # Usage:
7 # ./deploy/deploy.sh # Build + deploy + restart
8 # ./deploy/deploy.sh --setup # First-time: create user, dirs, db, build, install, seed
9
10 set -e
11
12 # Configuration
13 SERVER="max@100.106.221.39"
14 REMOTE_DIR="/opt/multithreaded"
15 SRC_DIR="src/multithreaded"
16 BINARY_NAME="multithreaded"
17 DEPLOY_DIR="deploy"
18
19 # Check we're in the right directory
20 if [ ! -f "Cargo.toml" ] || ! grep -q 'name = "multithreaded"' Cargo.toml; then
21 echo "Error: Run this script from the MNW/multithreaded/ directory"
22 exit 1
23 fi
24
25 rsync_source() {
26 echo "[rsync] Uploading source to $SERVER:~/$SRC_DIR/..."
27 ssh $SERVER "mkdir -p ~/$SRC_DIR"
28 # node_modules is reinstalled on the build host by build.rs (npm ci) and is
29 # large, platform-specific, and regenerable. static/dist is NOT excluded:
30 # it is emitted there by the same build script during cargo build.
31 rsync -az --delete \
32 --exclude target/ \
33 --exclude .env \
34 --exclude .git/ \
35 --exclude frontend/node_modules/ \
36 ./ $SERVER:~/$SRC_DIR/
37
38 # Sync shared dependencies (Cargo.toml references ../shared/ from ~/src/multithreaded/)
39 local shared_dir
40 shared_dir="$(cd ../shared && pwd)"
41 for dep in docengine tagtree s3-storage livechat; do
42 local dep_dir="$shared_dir/$dep"
43 if [ -d "$dep_dir" ]; then
44 echo "[rsync] Syncing shared dep: $dep"
45 ssh $SERVER "mkdir -p ~/src/shared/$dep"
46 rsync -az --delete --exclude target --exclude .git \
47 "$dep_dir/" "$SERVER:~/src/shared/$dep/"
48 fi
49 done
50 echo "[rsync] Done"
51 }
52
53 build_remote() {
54 echo "[build] Building release on astra..."
55 ssh $SERVER "cd ~/$SRC_DIR && ~/.cargo/bin/cargo build --release 2>&1"
56 echo "[build] Done"
57 }
58
59 deploy_files() {
60 echo "[deploy] Stopping service + copying binary + assets to $REMOTE_DIR..."
61 ssh $SERVER "
62 sudo systemctl stop multithreaded || true
63 sudo cp ~/$SRC_DIR/target/release/$BINARY_NAME $REMOTE_DIR/$BINARY_NAME
64 sudo chmod +x $REMOTE_DIR/$BINARY_NAME
65 sudo rsync -a --delete ~/$SRC_DIR/static/ $REMOTE_DIR/static/
66 sudo rsync -a --delete ~/$SRC_DIR/migrations/ $REMOTE_DIR/migrations/
67 sudo chown -R multithreaded:multithreaded $REMOTE_DIR
68 "
69 echo "[deploy] Done"
70 }
71
72 restart_app() {
73 echo "[restart] Restarting multithreaded..."
74 ssh $SERVER "sudo systemctl restart multithreaded"
75 sleep 2
76 echo ""
77 ssh $SERVER "sudo systemctl status multithreaded --no-pager"
78 echo ""
79 echo "[health] Checking..."
80 ssh $SERVER "curl -s -o /dev/null -w 'HTTP %{http_code}\n' http://127.0.0.1:3400/"
81 }
82
83 first_time_setup() {
84 echo "=== First-Time Setup ==="
85
86 echo "[setup] Creating system user..."
87 ssh $SERVER "
88 sudo useradd --system --shell /usr/sbin/nologin --home-dir $REMOTE_DIR multithreaded 2>/dev/null || echo 'User already exists'
89 "
90
91 echo "[setup] Creating directories..."
92 ssh $SERVER "
93 sudo mkdir -p $REMOTE_DIR
94 sudo chown multithreaded:multithreaded $REMOTE_DIR
95 "
96
97 echo "[setup] Creating database role + database..."
98 ssh $SERVER "
99 sudo -u postgres createuser multithreaded 2>/dev/null || echo 'Role already exists'
100 createdb multithreaded 2>/dev/null || echo 'Database already exists'
101 sudo -u postgres psql -c 'GRANT ALL PRIVILEGES ON DATABASE multithreaded TO multithreaded;' 2>/dev/null
102 sudo -u postgres psql -d multithreaded -c 'GRANT ALL ON SCHEMA public TO multithreaded;' 2>/dev/null
103 "
104
105 rsync_source
106 build_remote
107
108 echo "[setup] Installing files..."
109 deploy_files
110
111 echo "[setup] Installing env file..."
112 ssh $SERVER "
113 sudo cp ~/$SRC_DIR/$DEPLOY_DIR/env.production $REMOTE_DIR/.env
114 sudo chmod 600 $REMOTE_DIR/.env
115 sudo chown multithreaded:multithreaded $REMOTE_DIR/.env
116 "
117
118 echo "[setup] Installing systemd service..."
119 ssh $SERVER "
120 sudo cp ~/$SRC_DIR/$DEPLOY_DIR/multithreaded.service /etc/systemd/system/multithreaded.service
121 sudo systemctl daemon-reload
122 sudo systemctl enable multithreaded
123 "
124
125 echo "[setup] Starting service..."
126 ssh $SERVER "sudo systemctl start multithreaded"
127 sleep 2
128
129 echo "[setup] Running seed..."
130 ssh $SERVER "
131 cd $REMOTE_DIR
132 sudo -u multithreaded $REMOTE_DIR/$BINARY_NAME --seed
133 "
134
135 echo ""
136 ssh $SERVER "sudo systemctl status multithreaded --no-pager"
137 echo ""
138 echo "[health] Checking..."
139 ssh $SERVER "curl -s -o /dev/null -w 'HTTP %{http_code}\n' http://127.0.0.1:3400/"
140 }
141
142 case "${1:-deploy}" in
143 --setup)
144 first_time_setup
145 ;;
146 deploy|"")
147 echo "=== Deploy ==="
148 rsync_source
149 build_remote
150 deploy_files
151 restart_app
152 ;;
153 *)
154 echo "Usage: $0 [--setup]"
155 exit 1
156 ;;
157 esac
158
159 echo ""
160 echo "=== Done ==="
161