Skip to content

Commit 78ae80f

Browse files
author
Slyke
committed
Added in Python, pip and PyYaml version checking
1 parent 186c2c0 commit 78ae80f

File tree

3 files changed

+133
-34
lines changed

3 files changed

+133
-34
lines changed

compose-override.yml

Whitespace-only changes.

menu.sh

+89-14
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ TMP_DOCKER_COMPOSE_YML=./.tmp/docker-compose.tmp.yml
88
DOCKER_COMPOSE_YML=./docker-compose.yml
99
DOCKER_COMPOSE_OVERRIDE_YML=./compose-override.yml
1010

11+
# Minimum Software Versions
1112
COMPOSE_VERSION="3.6"
12-
DOCKER_VERSION_MAJOR=18
13-
DOCKER_VERSION_MINOR=2
14-
DOCKER_VERSION_BUILD=0
13+
REQ_DOCKER_VERSION_MAJOR=18
14+
REQ_DOCKER_VERSION_MINOR=2
15+
REQ_DOCKER_VERSION_BUILD=0
16+
17+
REQ_PYTHON_VERSION_MAJOR=3
18+
REQ_PYTHON_VERSION_MINOR=6
19+
REQ_PYTHON_VERSION_BUILD=9
20+
21+
REQ_PYYAML_VERSION_MAJOR=5
22+
REQ_PYYAML_VERSION_MINOR=3
23+
REQ_PYYAML_VERSION_BUILD=1
1524

1625
declare -A cont_array=(
1726
[portainer]="Portainer"
@@ -109,9 +118,68 @@ password_dialog() {
109118
#test=$( password_dialog )
110119

111120
function command_exists() {
112-
command -v "$@" >/dev/null 2>&1
121+
command -v "$@" > /dev/null 2>&1
122+
}
123+
124+
function install_python3_and_deps() {
125+
if (whiptail --title "Python 3 and Dependencies" --yesno "Python 3 (v3.6.9) or later, PyYaml 5.3.1 and pip3 is required for compose-overrides.yml file to merge into the docker-compose.yml file. Install now?" 20 78); then
126+
sudo apt install -y python3-pip python3-dev
127+
if [ $? -eq 0 ]; then
128+
PYTHON_VERSION_GOOD="true"
129+
else
130+
echo "Failed to install Python"
131+
exit 1
132+
fi
133+
pip3 install -U pyyaml==5.3.1
134+
if [ $? -eq 0 ]; then
135+
PYYAML_VERSION_GOOD="true"
136+
else
137+
echo "Failed to install Python"
138+
exit 1
139+
fi
140+
fi
141+
}
142+
143+
function do_python3_pip() {
144+
PYTHON_VERSION_GOOD="false"
145+
PYYAML_VERSION_GOOD="false"
146+
147+
if command_exists python3 && command_exists pip3; then
148+
PYTHON_VERSION=$(python3 --version)
149+
echo "Python Version: $PYTHON_VERSION"
150+
PYTHON_VERSION_MAJOR=$(echo "$PYTHON_VERSION"| cut -d' ' -f 2 | cut -d'.' -f 1)
151+
PYTHON_VERSION_MINOR=$(echo "$PYTHON_VERSION"| cut -d'.' -f 2)
152+
PYTHON_VERSION_BUILD=$(echo "$PYTHON_VERSION"| cut -d'.' -f 3)
153+
154+
if [ "${PYTHON_VERSION_MAJOR}" -eq $REQ_PYTHON_VERSION_MAJOR ] && \
155+
[ "${PYTHON_VERSION_MINOR}" -eq $REQ_PYTHON_VERSION_MINOR ] && \
156+
[ "${PYTHON_VERSION_BUILD}" -ge $REQ_PYTHON_VERSION_BUILD ]; then
157+
PYTHON_VERSION_GOOD="true"
158+
else
159+
echo "Python is outdated."
160+
install_python3_and_deps
161+
fi
162+
163+
PYYAML_VERSION=$(python3 ./scripts/yaml_merge.py --pyyaml-version)
164+
echo "PyYaml Version: $PYYAML_VERSION"
165+
PYYAML_VERSION_MAJOR=$(echo "$PYYAML_VERSION"| cut -d' ' -f 2 | cut -d'.' -f 1)
166+
PYYAML_VERSION_MINOR=$(echo "$PYYAML_VERSION"| cut -d'.' -f 2)
167+
PYYAML_VERSION_BUILD=$(echo "$PYYAML_VERSION"| cut -d'.' -f 3)
168+
169+
if [ "${PYYAML_VERSION_MAJOR}" -ge $REQ_PYYAML_VERSION_MAJOR ] && \
170+
[ "${PYYAML_VERSION_MINOR}" -ge $REQ_PYYAML_VERSION_MINOR ] && \
171+
[ "${PYYAML_VERSION_BUILD}" -ge $REQ_PYYAML_VERSION_BUILD ]; then
172+
PYYAML_VERSION_GOOD="true"
173+
else
174+
echo "PyYaml is outdated."
175+
install_python3_and_deps
176+
fi
177+
else
178+
install_python3_and_deps
179+
fi
113180
}
114181

182+
115183
#function copies the template yml file to the local service folder and appends to the docker-compose.yml file
116184
function yml_builder() {
117185

@@ -205,9 +273,9 @@ SERVER_VERSION_MAJOR=$(echo "$SERVER_VERSION"| cut -d'.' -f 1)
205273
SERVER_VERSION_MINOR=$(echo "$SERVER_VERSION"| cut -d'.' -f 2)
206274
SERVER_VERSION_BUILD=$(echo "$SERVER_VERSION"| cut -d'.' -f 3)
207275

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
276+
if [ "${SERVER_VERSION_MAJOR}" -ge $REQ_DOCKER_VERSION_MAJOR ] && \
277+
[ "${SERVER_VERSION_MINOR}" -ge $REQ_DOCKER_VERSION_MINOR ] && \
278+
[ "${SERVER_VERSION_BUILD}" -ge $REQ_DOCKER_VERSION_BUILD ]; then
211279
echo "Docker version >= 18.2.0. You are good to go."
212280
else
213281
echo ""
@@ -291,11 +359,11 @@ case $mainmenu_selection in
291359
#if no container is selected then dont overwrite the docker-compose.yml file
292360
if [ -n "$container_selection" ]; then
293361
touch $TMP_DOCKER_COMPOSE_YML
294-
echo "services:" > $TMP_DOCKER_COMPOSE_YML
362+
# echo "services:" > $TMP_DOCKER_COMPOSE_YML
295363

296364
# 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
365+
echo "version: '$COMPOSE_VERSION'" > $TMP_DOCKER_COMPOSE_YML
366+
echo "services:" >> $TMP_DOCKER_COMPOSE_YML
299367

300368
#set the ACL for the stack
301369
#docker_setfacl
@@ -313,15 +381,22 @@ case $mainmenu_selection in
313381
done
314382

315383
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
384+
do_python3_pip
385+
386+
if [ "$PYTHON_VERSION_GOOD" == "true" ] && [ "$PYYAML_VERSION_GOOD" == "true" ]; then
387+
echo "merging docker overrides with docker-compose.yml"
388+
python3 ./scripts/yaml_merge.py $TMP_DOCKER_COMPOSE_YML $DOCKER_COMPOSE_OVERRIDE_YML $DOCKER_COMPOSE_YML
389+
else
390+
echo "incorrect python or dependency versions, aborting override and using docker-compose.yml"
391+
cp $TMP_DOCKER_COMPOSE_YML $DOCKER_COMPOSE_YML
392+
fi
318393
else
319-
echo "no override found, using docker-compose.yaml"
394+
echo "no override found, using docker-compose.yml"
320395
cp $TMP_DOCKER_COMPOSE_YML $DOCKER_COMPOSE_YML
321396
fi
322397

323398
# 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.
399+
# echo -e "version: '$COMPOSE_VERSION'\n$(cat $DOCKER_COMPOSE_YML)" > $DOCKER_COMPOSE_YML # Remove once sort_keys works in Python->yaml.
325400

326401
echo "docker-compose successfully created"
327402
echo "run 'docker-compose up -d' to start the stack"

scripts/yaml_merge.py

+44-20
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,60 @@
11
import sys
2+
import traceback
23
import yaml
34

5+
if sys.argv[1] == "--pyyaml-version":
6+
try:
7+
print("pyyaml", yaml.__version__)
8+
sys.exit(0)
9+
except SystemExit:
10+
sys.exit(0)
11+
except:
12+
print("could not get pyyaml version")
13+
sys.exit(3)
14+
415
if len(sys.argv) < 4:
516
print("Error: Not enough args")
617
print("Usage:")
718
print(" yaml_merge.py [inputFile] [mergeFile] [outputFile]")
819
print("")
920
print("Example:")
1021
print(" yaml_merge.py ./.tmp/docker-compose.tmp.yml ./compose-override.yml ./docker-compose.yml")
11-
sys.exit(1)
22+
sys.exit(4)
23+
24+
try:
25+
pathTempDockerCompose = sys.argv[1]
26+
pathOverride = sys.argv[2]
27+
pathOutput = sys.argv[3]
1228

13-
pathTempDockerCompose = sys.argv[1]
14-
pathOverride = sys.argv[2]
15-
pathOutput = sys.argv[3]
29+
def mergeYaml(priorityYaml, defaultYaml):
30+
if isinstance(priorityYaml, dict) and isinstance(defaultYaml, dict):
31+
for k, v in defaultYaml.iteritems():
32+
if k not in priorityYaml:
33+
priorityYaml[k] = v
34+
else:
35+
priorityYaml[k] = mergeYaml(priorityYaml[k], v)
36+
return defaultYaml
1637

17-
def mergeYaml(priorityYaml, extensionYaml):
18-
if isinstance(priorityYaml,dict) and isinstance(extensionYaml,dict):
19-
for k,v in extensionYaml.iteritems():
20-
if k not in priorityYaml:
21-
priorityYaml[k] = v
22-
else:
23-
priorityYaml[k] = mergeYaml(priorityYaml[k],v)
24-
return priorityYaml
38+
with open(r'%s' % pathTempDockerCompose) as fileTempDockerCompose:
39+
yamlTempDockerCompose = yaml.load(fileTempDockerCompose, Loader=yaml.SafeLoader)
2540

26-
with open(r'%s' % pathTempDockerCompose) as fileTempDockerCompose:
27-
yamlTempDockerCompose = yaml.load(fileTempDockerCompose)
41+
with open(r'%s' % pathOverride) as fileOverride:
42+
yamlOverride = yaml.load(fileOverride, Loader=yaml.SafeLoader)
2843

29-
with open(r'%s' % pathOverride) as fileOverride:
30-
yamlOverride = yaml.load(fileOverride)
44+
mergedYaml = mergeYaml(yamlOverride, yamlTempDockerCompose)
3145

32-
mergedYaml = mergeYaml(yamlOverride, yamlTempDockerCompose)
46+
with open(r'%s' % pathOutput, 'w') as outputFile:
47+
yaml.dump(mergedYaml, outputFile, default_flow_style=False, sort_keys=False)
3348

34-
with open(r'%s' % pathOutput, 'w') as outputFile:
35-
# yaml.dump(mergedYaml, outputFile, Loader=yaml.FullLoader, default_flow_style=False, sort_keys=False) # TODO: 'sort_keys' not available in this version of Python/yaml
36-
yaml.dump(mergedYaml, outputFile, Loader=yaml.FullLoader, default_flow_style=False)
49+
sys.exit(0)
50+
except SystemExit:
51+
sys.exit(0)
52+
except:
53+
print("Something went wrong: ")
54+
print(sys.exc_info())
55+
print(traceback.print_exc())
56+
print("")
57+
print("")
58+
print("PyYaml Version: ", yaml.__version__)
59+
print("")
60+
sys.exit(2)

0 commit comments

Comments
 (0)