-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_module
executable file
·210 lines (183 loc) · 8.44 KB
/
add_module
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
projectname=`basename "$PWD"`
source ./lib/singlenv
source ./lib/colorful
source ./lib/env
blueecho "1. perform some checks & normalizations ..."
source ./lib/validation
compileCompose() {
blueecho "Add module '$modulename' to city '$city'"
if ! [ -f "$moduleDir/docker-compose.yml" ]; then
echo " doing this makes no sense as the template file '$moduleDir/docker-compose.yml' does not exist!"
return
fi
# 1. Generate a docker-compose for the city (if not already existing)
ymlFile="$moduleDir/$city.yml"
ymlFileInactive="$ymlFile.inactive"
if ! [ -f "$ymlFileInactive" ] && ! [ -f "$ymlFile" ]; then
orangeecho " building & adding in directory '$moduleDir' ) ..."
(export `cat ./$cityfile | xargs`; export projectname="$projectname" ; export city_normalize="${city,,}"
envsubst < $moduleDir/docker-compose.yml > "$ymlFile" )
if ! [ -f "$ymlFile" ]; then
redecho " Creating & adding operation failed! Do I have write access to '$moduleDir'?">&2
exit 1
fi
if [ -d "$moduleDir/data_template" ]; then
cp -R "$moduleDir/data_template" "$moduleDir/data_$city"
fi
elif [ -f "$ymlFileInactive" ]; then
orangeecho " adding ..."
mv "$ymlFileInactive" $ymlFile --verbose
if ! [ -f "$ymlFile" ]; then
redecho " Adding failed! Do I have write access to '$ymlFileInactive'?">&2
exit 1
fi
else
greenecho " already added. No need to do it again :)"
fi
}
interwebConfiguration() {
# 2. Prepare interweb configuration
blueecho "Add interweb nginx configuration"
orangeecho " adding nginx configuration for '$modulename' in city '$city' ..."
# This folder to be created somewhere under './data' is necessary to be able to store all nginx reverse proxy configurations for '$city' e.g. 'Germany-Hamburg'
nginxcityfolder="./data/nginx/interweb/$city" # e.g. './data/nginx/interweb/Germany-Hamburg'
# This file to be created will contain the altered nginx server configuration for '$city' e.g. 'Germany-Hamburg'
nginxcityconf="$nginxcityfolder.conf" # e.g. './data/nginx/interweb/Germany-Hamburg.conf'
# This file to be created will contain the altered nginx reverse proxy configuration for the module '$modulename' e.g. 'tileserver'
nginxcitymoduleconf="$nginxcityfolder/$modulename.conf" # e.g. './data/nginx/interweb/Germany-Hamburg/tileserver.conf'
# This file ensures that traffic redirection to that module works
modulenginxconf="$moduleDir/nginx.conf" # e.g. './modules/tileserver/nginx.conf'
# if a nginx reverse proxy configuration exists for this module
if [ -f "$modulenginxconf" ]; then # read it into memory (to be able to alter it later)
modulenginxconf_content=`cat "$modulenginxconf"`
modulenginxconf_content=`
export \`cat $cityfile | xargs\`
export projectname="$projectname"
export modulename="$modulename"
export city_normalize="${city,,}"
echo "$modulenginxconf_content" | envsubst`
else
# if no module nginx reverse configuration exists then abbort
return
fi
# If that folder does not exist
if ! [ -d "$nginxcityfolder" ]; then # create it
orangeecho " creating nginx server configuration folder for city '$city' ..."
mkdir "$nginxcityfolder"
fi
# 3. Generating nginx server configuration
orangeecho " completing nginx server configuration for city '$city' ..."
# if in real domains mode and if the file '$nginxcityconf' does not exist already
if [ "$curmode" = "$MODE_REALDOMAINS" ] && ! [ -f "$nginxcityconf" ]; then # take the template file at $nginxcityconf_template_interweb and make modifications
# - to include all *.conf files in '$nginxcityfolder'
# - and to just be applied for the domain assigned to '$city'
sed "s/# modules/# real domain include location blocks for city '$city'\ninclude \/etc\/nginx\/interweb\/$city\/\*.conf\;/" "$nginxcityconf_template_interweb" | sed "s/example.org/$domain/g" > "$nginxcityconf"
elif ! [ -f "$nginxcityconf" ]; then # or if in virtual domain mode then just create the file '$nginxcityconf' containing the include statement to include all *.conf files in '$nginxcityfolder'
echo "include /etc/nginx/interweb/$city/*.conf;" > "$nginxcityconf"
else
greenecho " already done. No need to do it again :)"
fi
# 4. Generating nginx module configuration
orangeecho " copying & altering nginx module configuration for module '$modulename' to nginx server configuration for city '$city' ..."
if ! [ -f "$nginxcitymoduleconf" ]; then
# search for pattern 'http://otp:' or 'http://otp;' or 'http://otp/'
# and replace it with 'http://otp-$city:' or 'http://otp-$city;' or 'http://otp-$city/'
# e.g. 'http://otp-germany-hamburg:' or 'http://otp-germany-hamburg;' or 'http://otp-germany-hamburg/' (lower case because of '${city,,}')
modulenginxconf_content=`echo "import re
txt = \"\"\"$modulenginxconf_content\"\"\"
print(re.sub(\"http\:/\/(.*?)(:|;|\/)\",\"http://${modulename}-\\\\\\1-${city,,}\\\\\\2\", txt, re.S))
" | python3`
# if in virtual domains mode
if [ "$curmode" = "$MODE_VIRTUALDOMAINS" ]; then # do the following additional modifications to all location blocks to
# change their urls e.g. from '/tileserver' to '/Germany-Hamburg/tileserver' (no lower case here)
modulenginxconf_content=`echo "$modulenginxconf_content" | sed -E "s/location \/(.+?)/location \/$city\/\1/g" | sed -E "s/return (.+?) \/(.*?)/return \1 \/$city\/\2/g"`
fi
echo "$modulenginxconf_content" > "$nginxcitymoduleconf"
else
greenecho " already generated. No need to do it again :)"
fi
}
prepareLogging() {
# 5. Logging
orangeecho " creating logging structure ..."
if ! [ -d "./data/logs/nginx/$domain" ]; then
mkdir -p "./data/logs/nginx/$domain" --verbose
else
greenecho " not necessary as already existing :)"
fi
}
intrawebConfiguration() {
# 6. Intraweb configuration
if [ "$intraweb" = "yes" ]; then
blueecho "Add intraweb nginx configuration ..."
if ! [ -f "$moduleDir/docker-compose.yml" ]; then
echo " doing this makes no sense as the template file '$moduleDir/docker-compose.yml' does not exist!"
return
fi
orangeecho " adding nginx configuration for '$modulename' in city '$city' ..."
nginxcityfolder="./data/nginx/intraweb/$city"
nginxcityconf="$nginxcityfolder.conf"
orangeecho " creating nginx server configuration folder for city '$city' ..."
if ! [ -d "$nginxcityfolder" ]; then
mkdir "$nginxcityfolder"
fi
orangeecho " completing nginx configuration for city '$city' ..."
if ! [ -f "$nginxcityconf" ]; then
echo "include /etc/nginx/intraweb/$city/*.conf ;" > "$nginxcityconf"
else
greenecho " not necessary as already completed :)"
fi
orangeecho " adding intraweb server for module '$modulename' in city '$city'"
cd modules/$modulename
allServices=`sudo docker-compose -p "$projectname" -f "${city}.yml" ps --services`
allServices=( $allServices )
cd ../../
# iterate through names of all services of that city in the module
for servicename in "${allServices[@]}"; do
orangeecho " - $servicename ..."
nginxcitymoduleconf="$nginxcityfolder/$servicename.conf"
if ! [ -f "$nginxcitymoduleconf" ]; then
# take content from template in file '$nginxcityconf_template_intraweb' and modify it to
# - be just applied to a specified autogenerated intraweb domain
# - and to replace all occurrence of 'modulename-city' with the name of the container e.g. to 'tileserver-tileserver-germany-hamburg'
# Syntax: <modulename>-<servicename>-<city>
sed -E "s/localhost/$servicename.localhost/" "$nginxcityconf_template_intraweb" | sed "s/modulename-city/$servicename/" > "$nginxcitymoduleconf"
else
greenecho " not necessary as already existing :)"
fi
done
fi
}
addModule() {
modulename="$1"
compileCompose
interwebConfiguration
prepareLogging
intrawebConfiguration
source "lib/plugin"
invokeAllPluginsOf "add_module"
blueecho "Wiring up module '$modulename' for city '$city'"
./server "${city}" up "$modulename"
greenecho "Added trufi module '$modulename' to city '$city'!"
}
moduleNotFound=""
for module in "${args[@]}"; do
moduleDir="./modules/$module"
#if $modulename is empty or $moduleDir does not exist
if [ -z "$module" ]; then
continue
fi
if ! [ -d "$moduleDir" ]; then
moduleNotFound="true"
continue
fi
echo
addModule "$module"
done
if ! [ -z "$moduleNotFound" ]; then
redecho "Error: You specified an invalid name of a module to add to city '$city'">&2
echo "A list of modules available:"
ls -p ./modules | grep -v /
exit 1
fi