blob: 17255ca8df3128b84dae1b0519bcbdeeeac636dd [file] [log] [blame]
Luigi Santivetti512fb232020-05-18 00:57:16 +01001#!/bin/bash
2#
3# generate a new docker-compose.yml
4#
5# Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com>
6
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13
14# The above copyright notice and this permission notice (including the next
15# paragraph) shall be included in all copies or substantial portions of the
16# Software.
17
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21# ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25if [ "${_APACHE_HAS_MEDIA_}" -eq 1 ]; then
26 declare -r compose_has_mount_media="\
27 - ${_APACHE_HTML_PHOTOS_D_}:/var/www/html/photos
28 - ${_APACHE_HTML_VIDEOS_D_}:/var/www/html/videos"
29else
30 declare -r compose_has_mount_media=""
31fi
32
33if [ "${_GERRIT_HAS_HTTPS_}" -eq 1 ]; then
34 declare -r compose_has_secure_store="\
35 - ${_GERRIT_DKRC_KEYSTORE_F_}:${_GERRIT_KEYSTORE_}:ro
36 - ${_GERRIT_DKRC_SSLIB_F_}:${_GERRIT_SSLIB_}"
37else
38 declare -r compose_has_secure_store=""
39fi
40
41declare -r docker_compose_t="\
42version: '${_COMPOSE_API_VERSION_}'
43networks:
44 frontend:
45 driver: bridge
46 ipam:
47 config:
48 - subnet: ${_COMPOSE_SUBNET_FRONTEND_IP_}/${_COMPOSE_SUBNET_BIT_}
49 backend:
50 driver: bridge
51 ipam:
52 config:
53 - subnet: ${_COMPOSE_SUBNET_BACKEND_IP_}/${_COMPOSE_SUBNET_BIT_}
54services:
55 ${_GERRIT_DKRC_SERVICE_}:
56 image: ${_GERRIT_DKRC_IMAGE_}
57 build:
58 context: ${_GERRIT_DKRC_CONTEXT_}
59 dockerfile: ${_GERRIT_DKRC_DOCKERFILE_}
60 container_name: ${_GERRIT_DKRC_CONTAINER_}
61 expose:
62 - \"${_GERRIT_PROXY_PORT_}\"
63 - \"${_GERRIT_SSH_PORT_}\"
64 ports:
65 - \"${_COMPOSE_HOST_SSH_PORT_}:${_GERRIT_SSH_PORT_}\"
66 volumes:
67 - ${_GERRIT_DKRC_ROOTFS_}/index:/var/gerrit/index
68 - ${_GERRIT_DKRC_ROOTFS_}/cache:/var/gerrit/cache
69 - ${_GERRIT_DKRC_ROOTFS_}/git:/var/gerrit/git
Luigi Santivetti5ed1dae2020-10-31 11:14:37 +000070 - ${_GERRIT_DKRC_HOOKS_D_}:${_GERRIT_HOOKS_CON_D_}
Luigi Santivetti512fb232020-05-18 00:57:16 +010071 - ${_GERRIT_DKRC_ETC_D_}:/var/gerrit/etc
Luigi Santivetti1b31e502020-10-28 18:07:39 +000072 - ${_GERRIT_LOGS_D_}:/var/gerrit/logs
Luigi Santivetti512fb232020-05-18 00:57:16 +010073${compose_has_secure_store}
74 environment:
75 - CANONICAL_WEB_URL=${_GERRIT_CANON_URL_}
76 - LISTEN_URL=${_GERRIT_LISTEN_URL_}
Luigi Santivetti5ed1dae2020-10-31 11:14:37 +000077 - HOOKS_REMOTE_ALIAS=${_GERRIT_HOOKS_REMOTE_NAME_}
78 - HOOKS_REMOTE_HOST=${_APACHE_DKRC_FRONTEND_IP_}
Luigi Santivetti79506762020-10-21 20:56:00 +000079 - HOOKS_REMOTE_RSAID=${_GERRIT_SSH_RSA_ID_F_}
Luigi Santivetti5ed1dae2020-10-31 11:14:37 +000080 - HOOKS_LOG_DIR=${_GERRIT_HOOKS_LOG_CON_D_}
Luigi Santivetti512fb232020-05-18 00:57:16 +010081 depends_on:
82 - ${_APACHE_DKRC_SERVICE_}
83 networks:
84 frontend:
85 ipv4_address: ${_GERRIT_DKRC_FRONTEND_IP_}
86 entrypoint: /bin/bash -c \"/${_GERRIT_DKRC_ENTRYPOINT_} \${GERRIT_ENTRYPOINT_ARGS}\"
87 ${_APACHE_DKRC_SERVICE_}:
88 environment:
89 - HOST_PEPPER=\${_PASSWD_APACHE_HASH_HMAC_PEPPER_}
90 - MYSQL_PASSWORD=\${_PASSWD_MYSQL_DB_PASSWORD_}
91 - MYSQL_USER=\${_PASSWD_MYSQL_DB_USERNAME_}
92 env_file:
93 - ${_COMPOSE_ENVIRONMENT_}
94 image: ${_APACHE_DKRC_IMAGE_}
95 build:
96 context: ${_APACHE_DKRC_CONTEXT_}
97 dockerfile: ${_APACHE_DKRC_DOCKERFILE_}
98 container_name: ${_APACHE_DKRC_CONTAINER_}
99 ports:
100 - \"${_COMPOSE_HOST_HTTP_PORT_}:${_APACHE_HTTP_PORT_}\"
101 - \"${_COMPOSE_HOST_HTTPS_PORT_}:${_APACHE_HTTPS_PORT_}\"
102 volumes:
103 - ${_APACHE_DKRC_PORTS_F_}:/etc/apache2/ports.conf:ro
104 - ${_APACHE_DKRC_CONF_F_}:/etc/apache2/apache2.conf:ro
105 - ${_APACHE_DKRC_AVAILABLE_D_}:/etc/apache2/sites-available:ro
106 - ${_APACHE_DKRC_ENABLED_D_}:/etc/apache2/sites-enabled:ro
107 - ${_MYSQL_ROOTFS_SSL_D_}:${_APACHE_CON_SSL_MYSQL_D_}:ro
108 - ${_APACHE_DKRC_CERT_L_}:${_APACHE_CON_SSL_D_}:ro
109 - ${_APACHE_EXT_AUTH_F_}:${_APACHE_CON_EXT_AUTH_F_}:ro
110 - ${_APACHE_DKRC_LOG_D_}:/var/log/apache2
111 - ${_APACHE_DKRC_WWW_D_}:/var/www
112${compose_has_mount_media}
113 depends_on:
114 - ${_MYSQL_DKRC_SERVICE_}
115 networks:
116 frontend:
117 ipv4_address: ${_APACHE_DKRC_FRONTEND_IP_}
118 backend:
119 ipv4_address: ${_APACHE_DKRC_BACKEND_IP_}
120 ${_MYSQL_DKRC_SERVICE_}:
121 environment:
122 - MYSQL_ROOT_PASSWORD=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_}
123 - MYSQL_PASSWORD=\${_PASSWD_MYSQL_DB_PASSWORD_}
124 - MYSQL_USER=\${_PASSWD_MYSQL_DB_USERNAME_}
125 env_file:
126 - ${_COMPOSE_ENVIRONMENT_}
127 image: ${_MYSQL_DKRC_IMAGE_}
128 build:
129 context: ${_MYSQL_DKRC_CONTEXT_}
130 dockerfile: ${_MYSQL_DKRC_DOCKERFILE_}
131 container_name: ${_MYSQL_DKRC_CONTAINER_}
132 restart: always
133 networks:
134 backend:
135 ipv4_address: ${_MYSQL_DKRC_BACKEND_IP_}
136 expose:
137 - \"${_MYSQL_DB_PORT_}\"
138 volumes:
139 - ${_MYSQL_ROOTFS_SSL_D_}:${_MYSQL_CON_SSL_D_}
140 - ${_MYSQL_INITDB_F_}:/docker-entrypoint-initdb.d/initdb.sql
141 - ${_MYSQL_MYCONF_F_}:/etc/mysql/my.cnf:ro
142 - ${_MYSQL_MYSQL_D_}:/var/lib/mysql
143 - ${_MYSQL_LOG_D_}:${_MYSQL_CON_LOG_D_}"
144
145declare -r environment_t="\
146MYSQL_DATABASE=${_MYSQL_DB_NAME_}
147MYSQL_HOSTNAME=${_MYSQL_DKRC_BACKEND_IP_}
148MYSQL_SSL_CAPATH=${_APACHE_CON_SSL_MYSQL_D_}
149MYSQL_SSL_CERT=${_APACHE_CON_SSL_MYSQL_CERT_F_}
150MYSQL_SSL_KEY=${_APACHE_CON_SSL_MYSQL_KEY_F_}
151MYSQL_SSL_CA=${_APACHE_CON_SSL_MYSQL_CA_F_}
152HOST_NAME=${_APACHE_SERVER_NAME_}"
153
154declare -r compose_cli_bang_t="\
155#!/bin/bash
156
157if (return 0 2>/dev/null); then
158 echo \"You must run this script\" >&2
159 return 1
160fi
161
162function __help
163{
164 cat <<EOF; exit 0
165`printf \"\\033[1m%s\\033[0m\\n\" \"NAME\"`
166
167 \${BASH_SOURCE[0]//.\//} - docker-compose CLI for ${host_name}
168
169`printf \"\\033[1m%s\\033[0m\\n\" \"USAGE\"`
170
171 \$ \${BASH_SOURCE[0]} [ OPTION ] [ ARGS ... ]
172
173`printf \"\\033[1m%s\\033[0m\\n\" \"ENVIRONMENT\"`
174
175 PASSWD_F path to file containing runtime credentials
176
177`printf \"\\033[1m%s\\033[0m\\n\" \"OPTION\"`
178
179 --start [init] start instance. If \\\`init\\\` is passed,
180 then do entrypoint.sh initialization steps
181 and run in the foreground not as a daemon
182 --stop stop instance
183 -pc|--prune-containers delete all services container
184 -pn|--prune-networks delete all services network
185 -ps|--prune-system delete all images, containers, cache,
186 networks and volumes
187 -bs|--build-services build all services and networks
188 -h|--help show this help
189
190`printf \"\\033[1m%s\\033[0m\\n\" \"END\"`
191EOF
192}
193
194function source_passwd_file
195{
196 if [ ! -f \"\${PASSWD_F}\" ]; then
197 echo \"error: PASSWD file not found\" >&2
198 exit 1
199 fi
200
201 if ! source \"\${PASSWD_F}\"; then
202 echo \"error: PASSWD file not sourced\" >&2
203 exit 1
204 fi
205}
206
207function __dkrc_start
208{
209 local -i error
210
211 source_passwd_file
212 pushd \"${instance_d}\" >/dev/null || exit 1
213
214 if sudo systemctl is-active --quiet service apache2.service; then
215 echo \"warning: shutting down apache2.service\" >&2
216 sudo systemctl stop apache2.service
217 fi
218
219 case \"\$1\" in
220 init )
221 GERRIT_ENTRYPOINT_ARGS=init \\
222 _PASSWD_APACHE_HASH_HMAC_PEPPER_=\${_PASSWD_APACHE_HASH_HMAC_PEPPER_} \\
223 _PASSWD_MYSQL_DB_USERNAME_=\${_PASSWD_MYSQL_DB_USERNAME_} \\
224 _PASSWD_MYSQL_DB_PASSWORD_=\${_PASSWD_MYSQL_DB_PASSWORD_} \\
225 _PASSWD_MYSQL_DB_PASSWORD_ROOT_=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_} \\
226 sudo -E docker-compose up ;;
227 * )
228 [ -z \"\$1\" ] || echo \"warning: input ignored: \$1\" >&2
229
230 _PASSWD_APACHE_HASH_HMAC_PEPPER_=\${_PASSWD_APACHE_HASH_HMAC_PEPPER_} \\
231 _PASSWD_MYSQL_DB_USERNAME_=\${_PASSWD_MYSQL_DB_USERNAME_} \\
232 _PASSWD_MYSQL_DB_PASSWORD_=\${_PASSWD_MYSQL_DB_PASSWORD_} \\
233 _PASSWD_MYSQL_DB_PASSWORD_ROOT_=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_} \\
234 sudo -E docker-compose up -d ;;
235 esac
236
237 error=\"\$?\"
238 popd >/dev/null
239
240 return \$error
241}
242
243function __dkrc_stop
244{
245 sudo docker stop \$(sudo docker ps -a -q)
246}
247
248function __dkrc_prune_containers
249{
250 sudo docker rm \$(sudo docker ps -a -q)
251}
252
253function __dkrc_prune_networks
254{
255 sudo docker network prune -f
256}
257
258function __dkrc_prune_system
259{
260 sudo docker system prune --all
261}
262
263function __dkrc_build_services
264{
265 local -i error
266
267 source_passwd_file
268 pushd \"${instance_d}\" >/dev/null || exit 1
269
270 _PASSWD_APACHE_HASH_HMAC_PEPPER_=\${_PASSWD_APACHE_HASH_HMAC_PEPPER_} \\
271 _PASSWD_MYSQL_DB_USERNAME_=\${_PASSWD_MYSQL_DB_USERNAME_} \\
272 _PASSWD_MYSQL_DB_PASSWORD_=\${_PASSWD_MYSQL_DB_PASSWORD_} \\
273 _PASSWD_MYSQL_DB_PASSWORD_ROOT_=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_} \\
274 sudo -E docker-compose build --force-rm --no-cache \$@
275 error=\"\$?\"
276 popd >/dev/null
277
278 return \$error
279}
280
281case \"\$1\" in
Luigi Santivetti1dbf2e72020-10-24 14:42:47 +0000282 --start ) __dkrc_start \"\$2\" ;;
283 --stop ) __dkrc_stop ;;
284 -pc|--prune-containers ) __dkrc_prune_containers ;;
285 -pn|--prune-networks ) __dkrc_prune_networks ;;
286 -ps|--prune-system ) __dkrc_prune_system ;;
287 -bs|--build-services ) __dkrc_build_services \"\${@:2}\" ;;
288 -h|--help ) __help ;;
Luigi Santivetti512fb232020-05-18 00:57:16 +0100289esac"