-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-ec2.sh
More file actions
executable file
·115 lines (97 loc) · 3.48 KB
/
Copy pathbootstrap-ec2.sh
File metadata and controls
executable file
·115 lines (97 loc) · 3.48 KB
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
#!/usr/bin/env bash
# Bootstrap an EC2 instance for CodeStream API + worker (frontend on Vercel).
# Run as root or with sudo after cloning the repo to /opt/codestream-src (or set REPO_DIR).
set -euo pipefail
REPO_DIR="${REPO_DIR:-/opt/codestream-src}"
INSTALL_DIR="/opt/codestream"
ENV_FILE="/etc/codestream/env"
SERVICE_USER="codestream"
log() { echo "[bootstrap] $*"; }
if [[ $EUID -ne 0 ]]; then
echo "Run with sudo." >&2
exit 1
fi
detect_os() {
if [[ -f /etc/os-release ]]; then
# shellcheck source=/dev/null
. /etc/os-release
echo "${ID:-unknown}"
else
echo "unknown"
fi
}
install_packages() {
local os
os="$(detect_os)"
log "Detected OS: $os"
case "$os" in
amzn)
dnf update -y
dnf install -y java-17-amazon-corretto-devel maven docker nginx git
systemctl enable --now docker
;;
ubuntu|debian)
apt-get update
apt-get install -y openjdk-17-jdk maven docker.io nginx git
systemctl enable --now docker
;;
*)
echo "Unsupported OS. Install Java 17, Maven, Docker, and nginx manually." >&2
exit 1
;;
esac
}
create_service_user() {
if ! id "$SERVICE_USER" &>/dev/null; then
useradd --system --create-home --home-dir "$INSTALL_DIR" --shell /sbin/nologin "$SERVICE_USER"
fi
usermod -aG docker "$SERVICE_USER"
}
build_app() {
if [[ ! -d "$REPO_DIR" ]]; then
echo "Repo not found at $REPO_DIR. Clone the project first." >&2
exit 1
fi
log "Building Java services..."
cd "$REPO_DIR"
mvn clean install -DskipTests
log "Building python-runner Docker image..."
docker build -t codestream-python-runner:latest "$REPO_DIR/worker-service/docker/python-runner"
log "Installing artifacts to $INSTALL_DIR..."
install -d -o "$SERVICE_USER" -g "$SERVICE_USER" "$INSTALL_DIR"
cp "$REPO_DIR/api-service/target/api-service-"*.jar "$INSTALL_DIR/api-service.jar"
cp "$REPO_DIR/worker-service/target/worker-service-"*.jar "$INSTALL_DIR/worker-service.jar"
if ! unzip -p "$INSTALL_DIR/api-service.jar" META-INF/MANIFEST.MF | grep -q "Main-Class:"; then
echo "api-service.jar is not a runnable Spring Boot fat JAR. Check Maven spring-boot-maven-plugin repackage." >&2
exit 1
fi
if ! unzip -p "$INSTALL_DIR/worker-service.jar" META-INF/MANIFEST.MF | grep -q "Main-Class:"; then
echo "worker-service.jar is not a runnable Spring Boot fat JAR. Check Maven spring-boot-maven-plugin repackage." >&2
exit 1
fi
chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR"
}
install_env_and_systemd() {
install -d -m 700 /etc/codestream
if [[ ! -f "$ENV_FILE" ]]; then
cp "$REPO_DIR/deploy/env.production.example" "$ENV_FILE"
chmod 600 "$ENV_FILE"
log "Created $ENV_FILE — edit it before starting services."
else
log "$ENV_FILE already exists; skipping."
fi
cp "$REPO_DIR/deploy/systemd/codestream-api.service" /etc/systemd/system/
cp "$REPO_DIR/deploy/systemd/codestream-worker.service" /etc/systemd/system/
systemctl daemon-reload
log "Systemd units installed. Enable after configuring env:"
log " systemctl enable --now codestream-api codestream-worker"
}
install_packages
create_service_user
build_app
install_env_and_systemd
log "Done. Next steps:"
log " 1. Edit $ENV_FILE (DATABASE_URL, SQS URLs, CORS for Vercel domain)"
log " 2. Configure nginx for api.codestream.npsolver.io: see deploy/EC2-DEPLOY.md"
log " 3. systemctl enable --now codestream-api codestream-worker"
log " 4. Set API_SERVICE_URL on Vercel (see deploy/env.vercel.example) and redeploy"