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