Skip to content

Commit 9e094ab

Browse files
committed
Refractor the script to work in both python2 and python3
Signed-off-by: Roy Yang <[email protected]>
1 parent bc4c381 commit 9e094ab

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

contrib/gce/configure.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ set -o errexit
1919
set -o nounset
2020
set -o pipefail
2121

22+
if [[ "$(python -V 2>&1)" =~ "Python 2" ]]; then
23+
# found python2, just use that
24+
PYTHON="python"
25+
elif [[ -f "/usr/bin/python2.7" ]]; then
26+
# System python not defaulted to python 2 but using 2.7 during migration
27+
PYTHON="/usr/bin/python2.7"
28+
else
29+
# No python2 either by default, let's see if we can find python3
30+
PYTHON="python3"
31+
if ! command -v ${PYTHON} >/dev/null 2>&1; then
32+
echo "ERROR Python not found. Aborting."
33+
exit 2
34+
fi
35+
fi
36+
echo "Version : " $(${PYTHON} -V 2>&1)
37+
2238
# CONTAINERD_HOME is the directory for containerd.
2339
CONTAINERD_HOME="/home/containerd"
2440
cd "${CONTAINERD_HOME}"
@@ -53,9 +69,14 @@ fetch_env() {
5369
fi
5470
echo "${tmp_env_content}" > "${tmp_env_file}"
5571
# Convert the yaml format file into a shell-style file.
56-
eval $(python -c '''
72+
eval $(${PYTHON} -c '''
5773
import pipes,sys,yaml
58-
for k,v in yaml.load(sys.stdin).iteritems():
74+
# check version of python and call methods appropriate for that version
75+
if sys.version_info[0] < 3:
76+
items = yaml.load(sys.stdin).iteritems()
77+
else:
78+
items = yaml.load(sys.stdin, Loader=yaml.BaseLoader).items()
79+
for k,v in items:
5980
print("readonly {var}={value}".format(var = k, value = pipes.quote(str(v))))
6081
''' < "${tmp_env_file}" > "${CONTAINERD_HOME}/${env_file_name}")
6182
rm -f "${tmp_env_file}"

0 commit comments

Comments
 (0)