-
Notifications
You must be signed in to change notification settings - Fork 0
/
dc-ws-apache.sh
executable file
·191 lines (159 loc) · 4.73 KB
/
dc-ws-apache.sh
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
#
# by TS, May 2019
#
# ----------------------------------------------------------
# update Docker Image from remote repository? [true|false]
LCFG_UPDATE_REMOTE_IMAGE=true
# ----------------------------------------------------------
# @param string $1 Path
# @param int $2 Recursion level
#
# @return string Absolute path
function realpath_osx() {
local TMP_RP_OSX_RES=
[[ $1 = /* ]] && TMP_RP_OSX_RES="$1" || TMP_RP_OSX_RES="$PWD/${1#./}"
if [ -h "$TMP_RP_OSX_RES" ]; then
TMP_RP_OSX_RES="$(readlink "$TMP_RP_OSX_RES")"
# possible infinite loop...
local TMP_RP_OSX_RECLEV=$2
[ -z "$TMP_RP_OSX_RECLEV" ] && TMP_RP_OSX_RECLEV=0
TMP_RP_OSX_RECLEV=$(( TMP_RP_OSX_RECLEV + 1 ))
if [ $TMP_RP_OSX_RECLEV -gt 20 ]; then
# too much recursion
TMP_RP_OSX_RES="--error--"
else
TMP_RP_OSX_RES="$(realpath_osx "$TMP_RP_OSX_RES" $TMP_RP_OSX_RECLEV)"
fi
fi
echo "$TMP_RP_OSX_RES"
}
# @param string $1 Path
#
# @return string Absolute path
function realpath_poly() {
case "$OSTYPE" in
linux*) realpath "$1" ;;
darwin*) realpath_osx "$1" ;;
*) echo "$VAR_MYNAME: Error: Unknown OSTYPE '$OSTYPE'" >/dev/stderr; echo -n "$1" ;;
esac
}
VAR_MYNAME="$(basename "$0")"
VAR_MYDIR="$(realpath_poly "$0")"
VAR_MYDIR="$(dirname "$VAR_MYDIR")"
# ----------------------------------------------------------
# Outputs CPU architecture string
#
# @param string $1 debian_rootfs|debian_dist
#
# @return int EXITCODE
function _getCpuArch() {
case "$(uname -m)" in
x86_64*)
echo -n "amd64"
;;
i686*)
echo -n "i386"
;;
aarch64*)
echo -n "arm64"
;;
armv7*)
echo -n "armhf"
;;
*)
echo "$VAR_MYNAME: Error: Unknown CPU architecture '$(uname -m)'" >/dev/stderr
return 1
;;
esac
return 0
}
_getCpuArch debian_dist >/dev/null || exit 1
LVAR_DEBIAN_DIST="$(_getCpuArch debian_dist)"
# ----------------------------------------------------------
VAR_DC_SERVICE_DEF="apache"
function printUsageAndExit() {
echo "Usage: $VAR_MYNAME <COMMAND> ..." >/dev/stderr
echo "Examples: $VAR_MYNAME up" >/dev/stderr
echo " $VAR_MYNAME start" >/dev/stderr
echo " $VAR_MYNAME start $VAR_DC_SERVICE_DEF" >/dev/stderr
echo " $VAR_MYNAME stop" >/dev/stderr
echo " $VAR_MYNAME stop $VAR_DC_SERVICE_DEF" >/dev/stderr
echo " $VAR_MYNAME restart $VAR_DC_SERVICE_DEF" >/dev/stderr
echo " $VAR_MYNAME ps" >/dev/stderr
echo " $VAR_MYNAME rm" >/dev/stderr
echo " $VAR_MYNAME down" >/dev/stderr
echo " $VAR_MYNAME logs -f" >/dev/stderr
echo " $VAR_MYNAME logs -f $VAR_DC_SERVICE_DEF" >/dev/stderr
echo " $VAR_MYNAME exec $VAR_DC_SERVICE_DEF bash" >/dev/stderr
echo " or simply" >/dev/stderr
echo " $VAR_MYNAME bash $VAR_DC_SERVICE_DEF" >/dev/stderr
exit 1
}
cd "$VAR_MYDIR" || exit 1
if [ $# -lt 1 ]; then
printUsageAndExit
fi
OPT_CMD="$1"
shift
# ----------------------------------------------------------
function _createDockComp() {
local TMP_UID="$(id -u)"
local TMP_GID="$(id -g)"
[ $TMP_UID -lt 500 ] && TMP_UID=1000
[ $TMP_GID -lt 101 ] && TMP_GID=1000
#
cp "docker-compose-SAMPLE.yaml" "$VAR_DCY_INP" || return 1
#
sed \
-i.bck \
-e "s/<CPU_ARCH>/$LVAR_DEBIAN_DIST/g" \
-e "s/<YOUR_UID>/$TMP_UID/g" \
-e "s/<YOUR_GID>/$TMP_GID/g" \
"$VAR_DCY_INP" || return 1
rm "${VAR_DCY_INP}.bck" || return 1
return 0
}
VAR_DCY_INP="docker-compose.yaml"
VAR_PROJNAME="apaphp74mdb103example"
[ ! -f "$VAR_DCY_INP" -a -f "docker-compose-SAMPLE.yaml" ] && {
_createDockComp || exit 1
}
[ ! -f "$VAR_DCY_INP" ] && {
echo "$VAR_MYNAME: Error: File '$VAR_DCY_INP' not found. Aborting." >/dev/stderr
exit 1
}
# ----------------------------------------------------------
function _updateRemoteImages() {
local TMP_IMGLIST="$(grep "image:" "$VAR_DCY_INP" | grep -v -E ".*#.*image:" | grep "/" | awk '{print $2}' | tr -d \"\')"
[ -z "$TMP_IMGLIST" ] && return 0
local TMP_IMGENTRY
for TMP_IMGENTRY in $TMP_IMGLIST; do
echo "$VAR_MYNAME: Updating image '${TMP_IMGENTRY}'..."
docker pull ${TMP_IMGENTRY} || return 1
echo
done
return 0
}
TMP_ADDITIONAL_OPT=
if [ "$OPT_CMD" = "up" ]; then
# prevent Docker Compose from running in foreground
TMP_ADDITIONAL_OPT="--no-start"
#
if [ "$LCFG_UPDATE_REMOTE_IMAGE" = "true" ]; then
_updateRemoteImages || exit 1
fi
fi
if [ "$OPT_CMD" = "bash" ]; then
OPT_SERVICE="$1"
shift
[ -z "$OPT_SERVICE" ] && OPT_SERVICE="$VAR_DC_SERVICE_DEF"
docker-compose -p "$VAR_PROJNAME" -f "$VAR_DCY_INP" exec "$OPT_SERVICE" "$OPT_CMD" $@ || exit 1
else
docker-compose -p "$VAR_PROJNAME" -f "$VAR_DCY_INP" "$OPT_CMD" $TMP_ADDITIONAL_OPT $@ || exit 1
fi
if [ "$OPT_CMD" = "up" ]; then
# now start services in background
docker-compose -p "$VAR_PROJNAME" -f "$VAR_DCY_INP" start || exit 1
fi
exit 0