automate/docker_compose-writer.sh

56 lines
1.9 KiB
Bash

#!/usr/bin/env bash
set -x
NGINX_NETWORK="$(docker network ls | grep nginx | awk '{print $2}')"
JSON_FILE="docker-compose2.json"
YAML_FILE="docker-compose2.yml"
WORKDIR="/tmp"
declare -A DOCKER_OPT
DOCKER_OPT[7]='.["version"] = '2''
# redis
DOCKER_OPT[6]='.["services"] += { "redis":{ "restart": "always", "image": "sameersbn/redis:4.0.9-2", "command": [ "--loglevel warning" ], "volumes": [ "redis-data:/var/lib/redis:Z" ]}}'
# postgresql
DOCKER_OPT[5]='.["services"] += { "postgresql": { "restart": "always", "image": "sameersbn/postgresql:10-2", "volumes": [ "postgresql-data:/var/lib/postgresql:Z" ], "environment": [ "DB_USER=gitlab", "DB_PASS=mlpfinsonik", "DB_NAME=gitlabhq_production", "DB_EXTENSION=pg_trgm" ] }}'
# gitlab
DOCKER_OPT[4]='.["services"] += { "gitlab": { "restart": "always", "image": "sameersbn/gitlab:12.7.6", "depends_on": [ "redis", "postgresql" ], "ports": [ "10080:80", "10022:22", "10443:443" ], "volumes": [ "gitlab-data:/home/git/data:Z" ]}}'
DOCKER_OPT[3]='.["services"] += { "gitlab": { "environment": }}'
# volumes
DOCKER_OPT[2]='.["volumes"] += { "redis-data": null, "postgresql-data": null, "gitlab-data": null }'
#networks
DOCKER_OPT[1]='.["networks"] += { "default": { "external": { "name": "'${NGINX_NETWORK}'" }}}'
if [ ! -f ${WORKDIR}/${JSON_FILE} ]
then
echo "{}" > ${WORKDIR}/${JSON_FILE}
else
echo "${WORKDIR}/${JSON_FILE} exists."
fi
# Count through the number of array elements, using ${#
# looking for one missing.
for ((i = 1; i < ${#DOCKER_OPT[@]}; ++i)); do
if [ -z "${DOCKER_OPT[$i]}" ]; then
echo Error: $i missing.
exit 1
fi
done
# Work through each array element, using ${!.
# regardless how it's spelled.
for i in "${!DOCKER_OPT[@]}"; do
OPTION="${DOCKER_OPT[$i]}"
jq "${OPTION}" ${WORKDIR}/${JSON_FILE} > ${WORKDIR}/${JSON_FILE}.new && \
mv -b ${WORKDIR}/${JSON_FILE}.new ${WORKDIR}/${JSON_FILE}
yq -y . ${WORKDIR}/${JSON_FILE} > ${WORKDIR}/${YAML_FILE}
done
cat ${WORKDIR}/${JSON_FILE} | jq