33# get path of menu correct
44pushd ~ /IOTstack
55
6+ # Consts/vars
7+ TMP_DOCKER_COMPOSE_YML=./.tmp/docker-compose.tmp.yml
8+ DOCKER_COMPOSE_YML=./docker-compose.yml
9+ DOCKER_COMPOSE_OVERRIDE_YML=./compose-override.yml
10+
11+ COMPOSE_VERSION=" 3.6"
12+ DOCKER_VERSION_MAJOR=18
13+ DOCKER_VERSION_MINOR=2
14+ DOCKER_VERSION_BUILD=0
15+
616declare -A cont_array=(
717 [portainer]=" Portainer"
818 [nodered]=" Node-RED"
@@ -79,6 +89,7 @@ docker_setfacl() {
7989 [ -d ./services ] || mkdir ./services
8090 [ -d ./volumes ] || mkdir ./volumes
8191 [ -d ./backups ] || mkdir ./backups
92+ [ -d ./tmp ] || mkdir ./tmp
8293
8394 # give current user rwx on the volumes and backups
8495 [ $( getfacl ./volumes | grep -c " default:user:$USER " ) -eq 0 ] && sudo setfacl -Rdm u:$USER :rwx ./volumes
@@ -108,47 +119,47 @@ function yml_builder() {
108119
109120 [ -d ./services/ ] || mkdir ./services/
110121
111- if [ -d ./services/$1 ]; then
112- # directory already exists prompt user to overwrite
113- sevice_overwrite=$( whiptail --radiolist --title " Overwrite Option" --notags \
114- " $1 service directory has been detected, use [SPACEBAR] to select you overwrite option" 20 78 12 \
115- " none" " Do not overwrite" " ON" \
116- " env" " Preserve Environment and Config files" " OFF" \
117- " full" " Pull full service from template" " OFF" \
118- 3>&1 1>&2 2>&3 )
119-
120- case $sevice_overwrite in
121-
122- " full" )
123- echo " ...pulled full $1 from template"
124- rsync -a -q .templates/$1 / services/$1 / --exclude ' build.sh'
125- ;;
126- " env" )
127- echo " ...pulled $1 excluding env file"
128- rsync -a -q .templates/$1 / services/$1 / --exclude ' build.sh' --exclude ' $1.env' --exclude ' *.conf'
129- ;;
130- " none" )
131- echo " ...$1 service not overwritten"
132- ;;
133-
134- esac
122+ if [ -d ./services/$1 ]; then
123+ # directory already exists prompt user to overwrite
124+ sevice_overwrite=$( whiptail --radiolist --title " Overwrite Option" --notags \
125+ " $1 service directory has been detected, use [SPACEBAR] to select you overwrite option" 20 78 12 \
126+ " none" " Do not overwrite" " ON" \
127+ " env" " Preserve Environment and Config files" " OFF" \
128+ " full" " Pull full service from template" " OFF" \
129+ 3>&1 1>&2 2>&3 )
135130
136- else
137- mkdir ./services/$1
131+ case $sevice_overwrite in
132+
133+ " full" )
138134 echo " ...pulled full $1 from template"
139135 rsync -a -q .templates/$1 / services/$1 / --exclude ' build.sh'
140- fi
136+ ;;
137+ " env" )
138+ echo " ...pulled $1 excluding env file"
139+ rsync -a -q .templates/$1 / services/$1 / --exclude ' build.sh' --exclude ' $1.env' --exclude ' *.conf'
140+ ;;
141+ " none" )
142+ echo " ...$1 service not overwritten"
143+ ;;
144+
145+ esac
146+
147+ else
148+ mkdir ./services/$1
149+ echo " ...pulled full $1 from template"
150+ rsync -a -q .templates/$1 / services/$1 / --exclude ' build.sh'
151+ fi
141152
142153
143154 # if an env file exists check for timezone
144155 [ -f " ./services/$1 /$1 .env" ] && timezones ./services/$1 /$1 .env
145156
146- # if a volumes.yml exists, append to overall volumes.yml file
147- [ -f " ./services/$1 /volumes.yml" ] && cat " ./services/$1 /volumes.yml" >> docker-volumes.yml
157+ # if a volumes.yml exists, append to overall volumes.yml file
158+ [ -f " ./services/$1 /volumes.yml" ] && cat " ./services/$1 /volumes.yml" >> docker-volumes.yml
148159
149160 # add new line then append service
150- echo " " >> docker-compose.yml
151- cat $service >> docker-compose.yml
161+ echo " " >> $TMP_DOCKER_COMPOSE_YML
162+ cat $service >> $TMP_DOCKER_COMPOSE_YML
152163
153164 # test for post build
154165 if [ -f ./.templates/$1 /build.sh ]; then
@@ -194,9 +205,9 @@ SERVER_VERSION_MAJOR=$(echo "$SERVER_VERSION"| cut -d'.' -f 1)
194205SERVER_VERSION_MINOR=$( echo " $SERVER_VERSION " | cut -d' .' -f 2)
195206SERVER_VERSION_BUILD=$( echo " $SERVER_VERSION " | cut -d' .' -f 3)
196207
197- if [ " ${SERVER_VERSION_MAJOR} " -ge 18 ] && \
198- [ " ${SERVER_VERSION_MINOR} " -ge 2 ] && \
199- [ " ${SERVER_VERSION_BUILD} " -ge 0 ]; then
208+ if [ " ${SERVER_VERSION_MAJOR} " -ge $DOCKER_VERSION_MAJOR ] && \
209+ [ " ${SERVER_VERSION_MINOR} " -ge $DOCKER_VERSION_MINOR ] && \
210+ [ " ${SERVER_VERSION_BUILD} " -ge $DOCKER_VERSION_BUILD ]; then
200211 echo " Docker version >= 18.2.0. You are good to go."
201212else
202213 echo " "
@@ -279,9 +290,12 @@ case $mainmenu_selection in
279290
280291 # if no container is selected then dont overwrite the docker-compose.yml file
281292 if [ -n " $container_selection " ]; then
282- touch docker-compose.yml
283- echo " version: '3.6'" > docker-compose.yml
284- echo " services:" >> docker-compose.yml
293+ touch $TMP_DOCKER_COMPOSE_YML
294+ echo " services:" > $TMP_DOCKER_COMPOSE_YML
295+
296+ # Uncomment once sort_keys is available in Pyton->yaml
297+ # echo "version: '$COMPOSE_VERSION'" > $TMP_DOCKER_COMPOSE_YML
298+ # echo "services:" >> $TMP_DOCKER_COMPOSE_YML
285299
286300 # set the ACL for the stack
287301 # docker_setfacl
@@ -298,25 +312,17 @@ case $mainmenu_selection in
298312 echo " $container " >> ./services/selection.txt
299313 done
300314
301- # add custom containers
302- if [ -f ./services/custom.txt ]; then
303- if (whiptail --title " Custom Container detected" --yesno " custom.txt has been detected do you want to add these containers to the stack?" 20 78); then
304- mapfile -t containers <<< $( cat ./services/custom.txt)
305- for container in " ${containers[@]} " ; do
306- echo " Adding $container container"
307- yml_builder " $container "
308- done
309- fi
310- fi
311-
312- # if a container needs volume, put it at the end of docker-compose
313- if [ -f docker-volumes.yml ]; then
314- echo " " >> docker-compose.yml
315- echo " volumes:" >> docker-compose.yml
316- cat docker-volumes.yml >> docker-compose.yml
317- rm docker-volumes.yml
315+ if [ -f " $DOCKER_COMPOSE_OVERRIDE_YML " ]; then
316+ echo " merging docker overrides with docker-compose.yaml"
317+ python ./scripts/yaml_merge.py $TMP_DOCKER_COMPOSE_YML $DOCKER_COMPOSE_OVERRIDE_YML $DOCKER_COMPOSE_YML
318+ else
319+ echo " no override found, using docker-compose.yaml"
320+ cp $TMP_DOCKER_COMPOSE_YML $DOCKER_COMPOSE_YML
318321 fi
319322
323+ # Prepend compose version after merging/copying, so that merging doesn't move it to the bottom (alphabetically ordered).
324+ echo -e " version: '$COMPOSE_VERSION '\n$( cat $DOCKER_COMPOSE_YML ) " > $DOCKER_COMPOSE_YML # Remove once sort_keys works in Python->yaml.
325+
320326 echo " docker-compose successfully created"
321327 echo " run 'docker-compose up -d' to start the stack"
322328 else
@@ -496,4 +502,4 @@ case $mainmenu_selection in
496502
497503esac
498504
499- popd
505+ popd > /dev/null 2>&1
0 commit comments