-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·279 lines (235 loc) · 7.54 KB
/
deploy.sh
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env bash
# Author: Jesse Goerzen
# License: MIT
# Description:
# This script will deploy a full guacamole stack over docker.
###############################################################################
## Setup ##
###############################################################################
cd "$(dirname "$0")"
sudo systemctl stop docker-guacamole.service
sudo rm -r gd-drive gd-record pg-data pg-init
## Functions ##
###############################################################################
# Displays detailed description of this script.
Help() {
cat <<EOF
This script will deploy the guacamole stack over docker.
Syntax: ./deploy.sh [-h/-u/-g/-p/-d]
options:
h Prints a detailed description of this script.
u Select the user that will run the docker instance.
g Select the group of the user that will run the docker instance.
p Select the password for the guacamole database user.
d Use default options. Don't prompt the user for input. Doesn't override other arguments.
EOF
}
## Variables ##
###############################################################################
serviceUser=$(whoami)
serviceUserOverwritten=0
serviceGroup=$(groups | awk '{print $1}')
serviceGroupOverwritten=0
dbPass=$(cat /dev/urandom | tr -cd '[:alnum:]' | head -c 32)
dbPassOverwritten=0
useDefaults=0
## Handle command line arguments ##
###############################################################################
while getopts ":h:u:g:p:d" option; do
case $option in
h) # display Help
Help
exit;;
u) # Set service user
serviceUser=$OPTARG
serviceUserOverwritten=1;;
g) # Set service group
serviceGroup=$OPTARG
serviceGroupOverwritten=1;;
p) # Set database password
dbPass=$OPTARG
dbPassOverwritten=1;;
d) # Use defaults
useDefaults=1;;
\?) # incorrect option
echo 'Error: Invalid option'
echo 'Use -h for help.'
echo
Help
exit 1;;
esac
done
## Install Dependencies ##
###############################################################################
if [ -x "$(command -v docker)" ]; then
read -p "Docker is already installed. Would you like to update it? (y/[n]): " updateDocker
if [ "${updateDocker,,}" = "y" ] || [ "${updateDocker,,}" = "yes" ]; then
# Remove old docker packages
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
sudo apt-get remove $pkg;
done
# Install docker
curl -fsSL https://get.docker.com | sudo sh
fi
else
# Remove old docker packages
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
sudo apt-get remove $pkg;
done
# Install docker
curl -fsSL https://get.docker.com | sudo sh
fi
if [ -f /.dockerenv ]; then
sudo service docker start
else
sudo systemctl enable docker.service
sudo systemctl daemon-reload
sudo systemctl start docker
fi
## Handle User Input ##
###############################################################################
## Generate Files ##
###############################################################################
# Create .env file if missing or if template is newer than instance
if [ ! -f './.env' ] || [ './.env' -ot './deploy.sh' ]; then
if [ $useDefaults -eq 0 -a $dbPassOverwritten -eq 0 ]; then
read -s -p "Enter a custom password for the guacamole database user? [random]: " dbPassInput
if [ ! -z "$dbPassInput" ]; then
dbPass=$dbPassInput
fi
echo ''
fi
cat <<EOF | sed "s/REPLACEMEWITHDBPASS/$( echo $dbPass | sed 's/\\/\\\\/g' | sed 's/[]['"'"']/\\&/g' | sed 's/[][`~!@#$%^&*()-_=+{}|;:",<.>/?]/\\&/g' )/g" > ./.env
# Author: Jesse Goerzen
# License: MIT
GUAC_PG_DB=guacamole
GUAC_PG_USER=guacamole
GUAC_PG_PASS='REPLACEMEWITHDBPASS'
EOF
sudo chmod 640 ./.env
fi
# Create docker-compose.yaml if missing
if [ ! -f './docker-compose.yaml' ] || [ './docker-compose.yaml' -ot './deploy.sh' ]; then
cat <<EOF > ./docker-compose.yaml
version: '3'
networks:
guac:
driver: bridge
services:
# PostgreSQL database
pg:
image: postgres:16.1-alpine
container_name: postgres
restart: always
environment:
PGDATA: /var/lib/postgresql/data
POSTGRES_DB: \${GUAC_PG_DB}
POSTGRES_USER: \${GUAC_PG_USER}
POSTGRES_PASSWORD: \${GUAC_PG_PASS}
networks:
- guac
volumes:
- ./pg-init:/docker-entrypoint-initdb.d:rwz
- ./pg-data:/var/lib/postgresql/data:rw
# Guacamole server
guacd:
image: guacamole/guacd:1.5.4
container_name: guacd
restart: always
networks:
- guac
volumes:
- ./gd-drive:/drive:rw
- ./gd-record:/record:rw
# Guacamole web client
guac:
image: guacamole/guacamole:1.5.4
container_name: guacamole
restart: always
depends_on:
- guacd
- pg
links:
- guacd
environment:
GUACD_HOSTNAME: guacd
POSTGRESQL_HOSTNAME: pg
POSTGRESQL_DATABASE: \${GUAC_PG_DB}
POSTGRESQL_USER: \${GUAC_PG_USER}
POSTGRESQL_PASSWORD: \${GUAC_PG_PASS}
networks:
- guac
ports:
- 8080:8080/tcp
EOF
sudo chmod 750 ./docker-compose.yaml
fi
# Create docker-guacamole.service if missing
if [ ! -f './docker-guacamole.service' ] || [ './docker-guacamole.service' -ot './deploy.sh' ]; then
if [ $useDefaults -eq 0 -a $serviceUserOverwritten -eq 0 ]; then
read -p "Enter the user that will run the docker instance [$serviceUser]: " serviceUserInput
if [ ! -z "$serviceUserInput" ]; then
serviceUser=$serviceUserInput
fi
fi
if [ $useDefaults -eq 0 -a $serviceGroupOverwritten -eq 0 ]; then
read -p "Enter the group of the user that will run the docker instance. [$serviceGroup]: " serviceGroupInput
if [ ! -z "$serviceGroupInput" ]; then
serviceGroup=$serviceGroupInput
fi
fi
cat <<EOF | sed "s/REPLACEMEWITHUSER/$serviceUser/g; s/REPLACEMEWITHGROUP/$serviceGroup/g; s/REPLACEMEWITHPWD/$( echo $PWD | sed 's/[][`~!@#$%^&*()-_=+{}\|;:",<.>/?'"'"']/\\&/g' )/g" > ./docker-guacamole.service
[Unit]
Description=Apache Guacamole server docker compose group
After=docker.service
Requires=docker.service
[Service]
User=REPLACEMEWITHUSER
Group=REPLACEMEWITHGROUP
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=REPLACEMEWITHPWD/.env
ExecStart=/bin/bash -c "docker compose -f REPLACEMEWITHPWD/docker-compose.yaml up --detach"
ExecStop=/bin/bash -c "docker compose -f REPLACEMEWITHPWD/docker-compose.yaml stop"
[Install]
WantedBy=multi-user.target
EOF
sudo chmod 755 ./docker-guacamole.service
fi
## Create Required Directories ##
###############################################################################
#if [ ! -d ./gd-drive ]; then
# mkdir ./gd-drive
# sudo chmod 750 ./gd-drive
#fi
#if [ ! -d ./gd-record ]; then
# mkdir ./gd-record
# sudo chmod 750 ./gd-record
#fi
if [ ! -d ./pg-init ]; then
mkdir ./pg-init
sudo chmod 755 ./pg-init
fi
#if [ ! -d ./pg-data ]; then
# mkdir ./pg-data
# sudo chmod 750 ./pg-data
#fi
## Build init script ##
###############################################################################
docker run --rm guacamole/guacamole /opt/guacamole/bin/initdb.sh --postgresql > ./pg-init/initdb.sql
sudo chmod 755 ./pg-init/initdb.sql
## Deploy Service File ##
###############################################################################
sudo cp ./docker-guacamole.service /etc/systemd/system/docker-guacamole.service
if [ -f /.dockerenv ]; then
sudo service docker-guacamole start
else
sudo systemctl enable docker-guacamole.service
sudo systemctl daemon-reload
sudo systemctl start docker-guacamole
fi
## Cleanup ##
###############################################################################
## Exit Cleanly ##
###############################################################################
exit 0