blob: 1310cf5c6df5916d6e4f4273a9b4061f5376ad51 [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_}
79 - HOOKS_LOG_DIR=${_GERRIT_HOOKS_LOG_CON_D_}
Luigi Santivetti512fb232020-05-18 00:57:16 +010080 depends_on:
81 - ${_APACHE_DKRC_SERVICE_}
82 networks:
83 frontend:
84 ipv4_address: ${_GERRIT_DKRC_FRONTEND_IP_}
85 entrypoint: /bin/bash -c \"/${_GERRIT_DKRC_ENTRYPOINT_} \${GERRIT_ENTRYPOINT_ARGS}\"
86 ${_APACHE_DKRC_SERVICE_}:
87 environment:
88 - HOST_PEPPER=\${_PASSWD_APACHE_HASH_HMAC_PEPPER_}
89 - MYSQL_PASSWORD=\${_PASSWD_MYSQL_DB_PASSWORD_}
90 - MYSQL_USER=\${_PASSWD_MYSQL_DB_USERNAME_}
91 env_file:
92 - ${_COMPOSE_ENVIRONMENT_}
93 image: ${_APACHE_DKRC_IMAGE_}
94 build:
95 context: ${_APACHE_DKRC_CONTEXT_}
96 dockerfile: ${_APACHE_DKRC_DOCKERFILE_}
97 container_name: ${_APACHE_DKRC_CONTAINER_}
98 ports:
99 - \"${_COMPOSE_HOST_HTTP_PORT_}:${_APACHE_HTTP_PORT_}\"
100 - \"${_COMPOSE_HOST_HTTPS_PORT_}:${_APACHE_HTTPS_PORT_}\"
101 volumes:
102 - ${_APACHE_DKRC_PORTS_F_}:/etc/apache2/ports.conf:ro
103 - ${_APACHE_DKRC_CONF_F_}:/etc/apache2/apache2.conf:ro
104 - ${_APACHE_DKRC_AVAILABLE_D_}:/etc/apache2/sites-available:ro
105 - ${_APACHE_DKRC_ENABLED_D_}:/etc/apache2/sites-enabled:ro
106 - ${_MYSQL_ROOTFS_SSL_D_}:${_APACHE_CON_SSL_MYSQL_D_}:ro
107 - ${_APACHE_DKRC_CERT_L_}:${_APACHE_CON_SSL_D_}:ro
108 - ${_APACHE_EXT_AUTH_F_}:${_APACHE_CON_EXT_AUTH_F_}:ro
109 - ${_APACHE_DKRC_LOG_D_}:/var/log/apache2
110 - ${_APACHE_DKRC_WWW_D_}:/var/www
111${compose_has_mount_media}
112 depends_on:
113 - ${_MYSQL_DKRC_SERVICE_}
114 networks:
115 frontend:
116 ipv4_address: ${_APACHE_DKRC_FRONTEND_IP_}
117 backend:
118 ipv4_address: ${_APACHE_DKRC_BACKEND_IP_}
119 ${_MYSQL_DKRC_SERVICE_}:
120 environment:
121 - MYSQL_ROOT_PASSWORD=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_}
122 - MYSQL_PASSWORD=\${_PASSWD_MYSQL_DB_PASSWORD_}
123 - MYSQL_USER=\${_PASSWD_MYSQL_DB_USERNAME_}
124 env_file:
125 - ${_COMPOSE_ENVIRONMENT_}
126 image: ${_MYSQL_DKRC_IMAGE_}
127 build:
128 context: ${_MYSQL_DKRC_CONTEXT_}
129 dockerfile: ${_MYSQL_DKRC_DOCKERFILE_}
130 container_name: ${_MYSQL_DKRC_CONTAINER_}
131 restart: always
132 networks:
133 backend:
134 ipv4_address: ${_MYSQL_DKRC_BACKEND_IP_}
135 expose:
136 - \"${_MYSQL_DB_PORT_}\"
137 volumes:
138 - ${_MYSQL_ROOTFS_SSL_D_}:${_MYSQL_CON_SSL_D_}
139 - ${_MYSQL_INITDB_F_}:/docker-entrypoint-initdb.d/initdb.sql
140 - ${_MYSQL_MYCONF_F_}:/etc/mysql/my.cnf:ro
141 - ${_MYSQL_MYSQL_D_}:/var/lib/mysql
142 - ${_MYSQL_LOG_D_}:${_MYSQL_CON_LOG_D_}"
143
144declare -r environment_t="\
145MYSQL_DATABASE=${_MYSQL_DB_NAME_}
146MYSQL_HOSTNAME=${_MYSQL_DKRC_BACKEND_IP_}
147MYSQL_SSL_CAPATH=${_APACHE_CON_SSL_MYSQL_D_}
148MYSQL_SSL_CERT=${_APACHE_CON_SSL_MYSQL_CERT_F_}
149MYSQL_SSL_KEY=${_APACHE_CON_SSL_MYSQL_KEY_F_}
150MYSQL_SSL_CA=${_APACHE_CON_SSL_MYSQL_CA_F_}
151HOST_NAME=${_APACHE_SERVER_NAME_}"
152
153declare -r compose_cli_bang_t="\
154#!/bin/bash
155
156if (return 0 2>/dev/null); then
157 echo \"You must run this script\" >&2
158 return 1
159fi
160
161function __help
162{
163 cat <<EOF; exit 0
164`printf \"\\033[1m%s\\033[0m\\n\" \"NAME\"`
165
166 \${BASH_SOURCE[0]//.\//} - docker-compose CLI for ${host_name}
167
168`printf \"\\033[1m%s\\033[0m\\n\" \"USAGE\"`
169
170 \$ \${BASH_SOURCE[0]} [ OPTION ] [ ARGS ... ]
171
172`printf \"\\033[1m%s\\033[0m\\n\" \"ENVIRONMENT\"`
173
174 PASSWD_F path to file containing runtime credentials
175
176`printf \"\\033[1m%s\\033[0m\\n\" \"OPTION\"`
177
178 --start [init] start instance. If \\\`init\\\` is passed,
179 then do entrypoint.sh initialization steps
180 and run in the foreground not as a daemon
181 --stop stop instance
182 -pc|--prune-containers delete all services container
183 -pn|--prune-networks delete all services network
184 -ps|--prune-system delete all images, containers, cache,
185 networks and volumes
186 -bs|--build-services build all services and networks
187 -h|--help show this help
188
189`printf \"\\033[1m%s\\033[0m\\n\" \"END\"`
190EOF
191}
192
193function source_passwd_file
194{
195 if [ ! -f \"\${PASSWD_F}\" ]; then
196 echo \"error: PASSWD file not found\" >&2
197 exit 1
198 fi
199
200 if ! source \"\${PASSWD_F}\"; then
201 echo \"error: PASSWD file not sourced\" >&2
202 exit 1
203 fi
204}
205
206function __dkrc_start
207{
208 local -i error
209
210 source_passwd_file
211 pushd \"${instance_d}\" >/dev/null || exit 1
212
213 if sudo systemctl is-active --quiet service apache2.service; then
214 echo \"warning: shutting down apache2.service\" >&2
215 sudo systemctl stop apache2.service
216 fi
217
218 case \"\$1\" in
219 init )
220 GERRIT_ENTRYPOINT_ARGS=init \\
221 _PASSWD_APACHE_HASH_HMAC_PEPPER_=\${_PASSWD_APACHE_HASH_HMAC_PEPPER_} \\
222 _PASSWD_MYSQL_DB_USERNAME_=\${_PASSWD_MYSQL_DB_USERNAME_} \\
223 _PASSWD_MYSQL_DB_PASSWORD_=\${_PASSWD_MYSQL_DB_PASSWORD_} \\
224 _PASSWD_MYSQL_DB_PASSWORD_ROOT_=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_} \\
225 sudo -E docker-compose up ;;
226 * )
227 [ -z \"\$1\" ] || echo \"warning: input ignored: \$1\" >&2
228
229 _PASSWD_APACHE_HASH_HMAC_PEPPER_=\${_PASSWD_APACHE_HASH_HMAC_PEPPER_} \\
230 _PASSWD_MYSQL_DB_USERNAME_=\${_PASSWD_MYSQL_DB_USERNAME_} \\
231 _PASSWD_MYSQL_DB_PASSWORD_=\${_PASSWD_MYSQL_DB_PASSWORD_} \\
232 _PASSWD_MYSQL_DB_PASSWORD_ROOT_=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_} \\
233 sudo -E docker-compose up -d ;;
234 esac
235
236 error=\"\$?\"
237 popd >/dev/null
238
239 return \$error
240}
241
242function __dkrc_stop
243{
244 sudo docker stop \$(sudo docker ps -a -q)
245}
246
247function __dkrc_prune_containers
248{
249 sudo docker rm \$(sudo docker ps -a -q)
250}
251
252function __dkrc_prune_networks
253{
254 sudo docker network prune -f
255}
256
257function __dkrc_prune_system
258{
259 sudo docker system prune --all
260}
261
262function __dkrc_build_services
263{
264 local -i error
265
266 source_passwd_file
267 pushd \"${instance_d}\" >/dev/null || exit 1
268
269 _PASSWD_APACHE_HASH_HMAC_PEPPER_=\${_PASSWD_APACHE_HASH_HMAC_PEPPER_} \\
270 _PASSWD_MYSQL_DB_USERNAME_=\${_PASSWD_MYSQL_DB_USERNAME_} \\
271 _PASSWD_MYSQL_DB_PASSWORD_=\${_PASSWD_MYSQL_DB_PASSWORD_} \\
272 _PASSWD_MYSQL_DB_PASSWORD_ROOT_=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_} \\
273 sudo -E docker-compose build --force-rm --no-cache \$@
274 error=\"\$?\"
275 popd >/dev/null
276
277 return \$error
278}
279
280case \"\$1\" in
Luigi Santivetti1dbf2e72020-10-24 14:42:47 +0000281 --start ) __dkrc_start \"\$2\" ;;
282 --stop ) __dkrc_stop ;;
283 -pc|--prune-containers ) __dkrc_prune_containers ;;
284 -pn|--prune-networks ) __dkrc_prune_networks ;;
285 -ps|--prune-system ) __dkrc_prune_system ;;
286 -bs|--build-services ) __dkrc_build_services \"\${@:2}\" ;;
287 -h|--help ) __help ;;
Luigi Santivetti512fb232020-05-18 00:57:16 +0100288esac"