-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
213 lines (180 loc) · 6.73 KB
/
setup.sh
File metadata and controls
213 lines (180 loc) · 6.73 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
set -e
# Animated ASCII art: reveal line by line
ascii_art=(
""
" APOLLO BY SYNEHQ"
""
" /\\"
" / \\"
" /----\\"
" / \\"
" / \\"
" /----------\\"
" / \\"
" / \\"
" / \\"
" /------------------\\"
""
" . . . . . . . . . . . . ."
" . . . . . . . . . . . . . ."
" . . . . . . . . . . . . . ."
" . . . . . . . . . . . . ."
)
for line in "${ascii_art[@]}"; do
echo "$line"
sleep 0.02
done
# 1. Install Docker if not present
if ! command -v docker &> /dev/null
then
echo "Docker not found. Installing Docker..."
# For Ubuntu/Debian
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
rm get-docker.sh
sudo usermod -aG docker $USER
echo "Docker installed. Please log out and log back in for group changes to take effect."
else
echo "Docker is already installed."
fi
# 2. Enable and start Docker
sudo systemctl enable docker
sudo systemctl start docker
# 3. Get the advertising IP for Docker Swarm
# Try to auto-detect the primary IP address
ADVERTISE_ADDR="${SWARM_ADVERTISE_ADDR:-$(hostname -I | awk '{print $1}')}"
echo "Using $ADVERTISE_ADDR as the Docker Swarm advertise address."
# 4. Initialize Docker Swarm if not already initialized
if ! docker info | grep -q "Swarm: active"; then
echo "Initializing Docker Swarm with advertise address $ADVERTISE_ADDR..."
docker swarm init --advertise-addr "$ADVERTISE_ADDR"
else
echo "Docker Swarm already initialized."
fi
echo ""
echo "Which deployment do you want to set up?"
echo "1) Cloud Run (cloudrun image)"
echo "2) Local Docker (localrun image)"
read -p "Enter 1 for Cloud Run or 2 for Localrun [2]: " DEPLOY_CHOICE
ENABLE_DOCKER_SOCK_MOUNT="0"
DEPLOY_CHOICE="${DEPLOY_CHOICE:-2}"
if [[ "$DEPLOY_CHOICE" == "1" ]]; then
IMAGE_NAME="synehq/apollo-cloudrun"
SERVICE_NAME="apollo-cloudrun"
echo "You have selected Cloud Run setup. Using image: $IMAGE_NAME"
else
IMAGE_NAME="synehq/apollo-localrun"
SERVICE_NAME="apollo"
ENABLE_DOCKER_SOCK_MOUNT="1"
echo "You have selected Localrun setup. Using image: $IMAGE_NAME"
fi
# 5. Set variables for your image
REGISTRY="ghcr.io"
TAG="sudo" # or set to a specific tag
read -p "Which port do you want to expose the service on? [6910]: " PORT_TO_EXPOSE
PORT_TO_EXPOSE="${PORT_TO_EXPOSE:-6910}"
echo "Port to expose: $PORT_TO_EXPOSE"
PORT_TO_EXPOSE="${PORT_TO_EXPOSE:-6910}"
# 6a. Load environment variables from a file (optional)
ENV_FILE=${ENV_FILE:-.env}
ENV_CREATE_FLAGS=(--env "ENVIRONMENT=development" --env "PORT=$PORT_TO_EXPOSE")
ENV_UPDATE_FLAGS=(--env-add "ENVIRONMENT=development" --env "PORT=$PORT_TO_EXPOSE")
if [ -f "$ENV_FILE" ]; then
echo "Loading environment variables from $ENV_FILE"
while IFS= read -r line || [ -n "$line" ]; do
# Skip comments and empty lines
case "$line" in
''|\#*) continue ;;
esac
ENV_CREATE_FLAGS+=("--env" "$line")
ENV_UPDATE_FLAGS+=("--env-add" "$line")
done < "$ENV_FILE"
fi
# 6. Login to GHCR (GitHub Container Registry)
echo "Logging in to GHCR..."
read -p "Do you want to log in to GHCR (GitHub Container Registry)? [y/N]: " GHCR_LOGIN_CHOICE
GHCR_LOGIN_CHOICE=${GHCR_LOGIN_CHOICE,,} # to lower case
if [[ "$GHCR_LOGIN_CHOICE" == "y" || "$GHCR_LOGIN_CHOICE" == "yes" ]]; then
if [ -z "$GHCR_TOKEN" ]; then
echo "Enter your GitHub Personal Access Token (with 'read:packages' scope):"
read -s GHCR_TOKEN
fi
if [ -z "$GHCR_USERNAME" ]; then
echo "Enter your GitHub Username:"
read -r GHCR_USERNAME
fi
if ! echo "$GHCR_TOKEN" | docker login ghcr.io -u "$GHCR_USERNAME" --password-stdin; then
echo "GHCR login failed. Please check your token and try again."
exit 1
fi
else
echo "Skipping GHCR login. Make sure you are already logged in if you want to pull private images."
fi
# ask path of jobs config
read -p "Enter the path to the jobs config file: (Optional)" JOBS_CONFIG_PATH
JOBS_CONFIG_PATH="${JOBS_CONFIG_PATH}"
echo "Jobs config path: $JOBS_CONFIG_PATH"
# 7. Deploy or update the Docker Swarm service
# Expose port 6910:6910 using Docker Swarm's --publish mode
# Remove the service if it exists, to ensure port publishing is correct
if docker service ls | grep -q "$SERVICE_NAME"; then
echo "Removing existing service to ensure port publishing is correct..."
docker service rm $SERVICE_NAME
# Wait for the service to be fully removed
while docker service ls | grep -q "$SERVICE_NAME"; do
sleep 1
done
fi
echo "Creating new service with correct port publishing..."
DOCKER_MOUNT_FLAG=()
if [ "$ENABLE_DOCKER_SOCK_MOUNT" = "1" ] || [ "$ENABLE_DOCKER_SOCK_MOUNT" = "true" ]; then
DOCKER_MOUNT_FLAG+=(--mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock)
fi
if [ -f "$JOBS_CONFIG_PATH" ]; then
DOCKER_MOUNT_FLAG+=(--mount type=bind,src=$JOBS_CONFIG_PATH,dst=/app/jobs.yml)
fi
if [ ${#ENV_CREATE_FLAGS[@]} -gt 0 ]; then
docker service create \
--name $SERVICE_NAME \
--replicas 1 \
--with-registry-auth \
--publish mode=host,target=$PORT_TO_EXPOSE,published=$PORT_TO_EXPOSE,protocol=tcp \
"${DOCKER_MOUNT_FLAG[@]}" \
"${ENV_CREATE_FLAGS[@]}" \
$REGISTRY/$IMAGE_NAME:$TAG
else
docker service create \
--name $SERVICE_NAME \
--replicas 1 \
--with-registry-auth \
--publish mode=host,target=$PORT_TO_EXPOSE,published=$PORT_TO_EXPOSE,protocol=tcp \
"${DOCKER_MOUNT_FLAG[@]}" \
$REGISTRY/$IMAGE_NAME:$TAG
fi
# 8. Set up Watchtower for automatic image updates
WATCHTOWER_SERVICE="watchtower"
if docker service ls | grep -q "$WATCHTOWER_SERVICE"; then
echo "Watchtower service already running."
else
echo "Deploying Watchtower for automatic image updates..."
docker service create \
--name $WATCHTOWER_SERVICE \
--restart-condition any \
--mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
-e WATCHTOWER_CLEANUP=true \
-e WATCHTOWER_POLL_INTERVAL=60 \
-e WATCHTOWER_TRACE=true \
-e WATCHTOWER_INCLUDE_STOPPED=true \
-e WATCHTOWER_ROLLING_RESTART=true \
containrrr/watchtower \
$SERVICE_NAME
fi
# 9. Set up firewall for $PORT_TO_EXPOSE port
if ufw status | grep -q "$PORT_TO_EXPOSE/tcp"; then
echo "Firewall rule for $PORT_TO_EXPOSE/tcp already exists."
else
echo "Adding firewall rule for $PORT_TO_EXPOSE/tcp..."
ufw allow $PORT_TO_EXPOSE/tcp
fi
echo "Setup complete. Your service will be automatically updated when a new image is pushed to GHCR."