-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdc
executable file
·125 lines (98 loc) · 3.93 KB
/
mdc
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
#!/usr/bin/env bash
set -e
shopt -s dotglob # recursive copy of dot files too
die() { echo >&2 "${*}"; exit 1; }
vecho() { [ "${VERBOSE}" ] && echo >&2 "${*}" || true; }
realpath() { [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"; }
[ "${*}" ] || die "Usage: ${0} MODES [DOCKER-COMPOSE-ARGS]"
VERBOSE="${VERBOSE:-}"
MODES_DIR="${MODES_DIR:-./modes}"
ENV_FILE="${ENV_FILE:-.env}"
MDC_FILES_DIR="${MDC_FILES_DIR:-./.files}"
LS=$(which ls)
RESOLVE_DEPS="${RESOLVE_DEPS-./node_modules/@lonocloud/resolve-deps/resolve-deps.py}"
DOCKER_COMPOSE="${DOCKER_COMPOSE:-docker-compose}"
which ${RESOLVE_DEPS} >/dev/null 2>/dev/null \
|| die "Missing ${RESOLVE_DEPS}. Perhaps 'npm install'?"
# Resolve mode directory paths
MDC_CLI="${0} ${*}"
MODE_SPEC="${1}"; shift
RESOLVED_MODES="$(${RESOLVE_DEPS} --path "${MODES_DIR}" --format=paths ${MODE_SPEC})"
# Create base (empty) compose file to anchor mode relative paths
# to the same root directory. Create files dir.
COMPOSE_FILE=./.compose-empty.yaml
echo "### mdc command line: ${MDC_CLI}" > ${ENV_FILE}-mdc-tmp
echo -e "services: {}" > ./.compose-empty.yaml
vecho "Removing ${MDC_FILES_DIR}"
case "$(basename ${MDC_FILES_DIR})" in
.|/) die "MDC_FILES_DIR must not be '.' or '/'";;
esac
[ -d "${MDC_FILES_DIR}" ] && rm -r${VERBOSE:+v} ${MDC_FILES_DIR}/
mkdir -p "${MDC_FILES_DIR}"
# Incorporate modes' compose config, env, and files
declare -A FINISHED
COMPOSE_PROFILES=
MDC_MODE_NAMES=
MDC_MODE_DIRS=
for resolved in ${RESOLVED_MODES}; do
mode=${resolved%=*}
path=${resolved#*=}
# Only process each mode once
[ "${FINISHED[${mode}]}" ] && continue
FINISHED["${mode}"]=1
MDC_MODE_NAMES="${MDC_MODE_NAMES} ${mode}"
MDC_MODE_DIRS="${MDC_MODE_DIRS},${path}"
# mode can refer to a compose file in multiple ways
cfiles="${path}/compose.yaml ${path}/compose.yml ${path}/docker-compose.yaml ${path}/docker-compose.yml"
for cfile in ${cfiles}; do
if [ -e "${cfile}" ]; then
COMPOSE_FILE="${COMPOSE_FILE}:${cfile}"
break
fi
done
# Add MODE_ prefixed compose profile for each mode
COMPOSE_PROFILES="${COMPOSE_PROFILES},MODE_${mode}"
# if there is a mode specific env file then include it
efiles="${path}/env ${path}/.env"
for efile in ${efiles}; do
if [ -e "${efile}" ]; then
echo "### mdc begin mode ${mode} (${efile})" >> ${ENV_FILE}-mdc-tmp
# Add MDC_MODE_ prefixed environment variable for each mode
echo "MDC_MODE_${mode//[^a-zA-Z]/_}=enabled" >> ${ENV_FILE}-mdc-tmp
vecho "cat ${efile} >> ${ENV_FILE}-mdc-tmp"
cat ${efile} >> ${ENV_FILE}-mdc-tmp
echo >> ${ENV_FILE}-mdc-tmp
echo "### mdc end mode ${mode} (${efile})" >> ${ENV_FILE}-mdc-tmp
fi
done
# if there are mode specific files then copy them to MDC_FILES_DIR
if [ -d "${path}" ]; then
for vfd in $(cd ${path} && $LS -d */files 2>/dev/null || true); do
dest=${MDC_FILES_DIR}/${vfd%/files}
mkdir -p ${dest}
vecho cp -a ${path}/${vfd}/* ${dest}
cp -a ${path}/${vfd}/* ${dest}
done
fi
done
vecho
# Summarize and set env
echo >&2 "MODES: ${MDC_MODE_NAMES}"
vecho "ENV_FILE: ${ENV_FILE}"
COMPOSE_FILE="${COMPOSE_FILE#:}"
vecho "COMPOSE_FILE: ${COMPOSE_FILE}"
echo "COMPOSE_FILE=${COMPOSE_FILE}" >> ${ENV_FILE}-mdc-tmp
echo "COMPOSE_DIR=$(realpath $(dirname ${ENV_FILE}))" >> ${ENV_FILE}-mdc-tmp
COMPOSE_PROFILES="${COMPOSE_PROFILES#,}"
vecho "COMPOSE_PROFILES: ${COMPOSE_PROFILES}"
echo "COMPOSE_PROFILES=${COMPOSE_PROFILES}" >> ${ENV_FILE}-mdc-tmp
MDC_MODE_DIRS="${MDC_MODE_DIRS#,}"
vecho "MDC_MODE_DIRS: ${MDC_MODE_DIRS}"
echo "MDC_MODE_DIRS=\"${MDC_MODE_DIRS}\"" >> ${ENV_FILE}-mdc-tmp
vecho
vecho mv ${ENV_FILE}-mdc-tmp ${ENV_FILE}
mv ${ENV_FILE}-mdc-tmp ${ENV_FILE}
if [ "${*}" ]; then
vecho "Running: ${DOCKER_COMPOSE} --env-file "${ENV_FILE}" ${@}"
exec ${DOCKER_COMPOSE} --env-file "${ENV_FILE}" "${@}"
fi