python snippets should work on both old and new python versions

This commit is contained in:
Davanum Srinivas 2020-01-31 21:51:52 -05:00
parent 7e8f31b313
commit f20e17e9dd
No known key found for this signature in database
GPG Key ID: 80D83A796103BF59
2 changed files with 40 additions and 7 deletions

View File

@ -2713,13 +2713,24 @@ function main() {
KUBE_HOME="/home/kubernetes"
KUBE_BIN=${KUBE_HOME}/bin
PYTHON="python"
CONTAINERIZED_MOUNTER_HOME="${KUBE_HOME}/containerized_mounter"
PV_RECYCLER_OVERRIDE_TEMPLATE="${KUBE_HOME}/kube-manifests/kubernetes/pv-recycler-template.yaml"
if [[ "$(python -V)" =~ "Python 3" ]]; then
if [[ "$(python -V 2>&1)" =~ "Python 2" ]]; then
# found python2, just use that
PYTHON="python"
elif [[ -f "/usr/bin/python2.7" ]]; then
# System python not defaulted to python 2 but using 2.7 during migration
PYTHON="/usr/bin/python2.7"
else
# No python2 either by default, let's see if we can find python3
PYTHON="python3"
if ! command -v ${PYTHON} >/dev/null 2>&1; then
echo "ERROR Python not found. Aborting."
exit 2
fi
fi
echo "Version : " $(${PYTHON} -V 2>&1)
if [[ ! -e "${KUBE_HOME}/kube-env" ]]; then
echo "The ${KUBE_HOME}/kube-env file does not exist!! Terminate cluster initialization."

View File

@ -66,8 +66,13 @@ function download-kube-env {
# Convert the yaml format file into a shell-style file.
eval $(${PYTHON} -c '''
import pipes,sys,yaml
for k,v in yaml.load(sys.stdin).iteritems():
print("readonly {var}={value}".format(var = k, value = pipes.quote(str(v))))
# check version of python and call methods appropriate for that version
if sys.version_info[0] < 3:
items = yaml.load(sys.stdin).iteritems()
else:
items = yaml.load(sys.stdin, Loader=yaml.BaseLoader).items()
for k, v in items:
print("readonly {var}={value}".format(var=k, value=pipes.quote(str(v))))
''' < "${tmp_kube_env}" > "${KUBE_HOME}/kube-env")
rm -f "${tmp_kube_env}"
)
@ -105,8 +110,13 @@ function download-kube-master-certs {
# Convert the yaml format file into a shell-style file.
eval $(${PYTHON} -c '''
import pipes,sys,yaml
for k,v in yaml.load(sys.stdin).iteritems():
print("readonly {var}={value}".format(var = k, value = pipes.quote(str(v))))
# check version of python and call methods appropriate for that version
if sys.version_info[0] < 3:
items = yaml.load(sys.stdin).iteritems()
else:
items = yaml.load(sys.stdin, Loader=yaml.BaseLoader).items()
for k, v in items:
print("readonly {var}={value}".format(var=k, value=pipes.quote(str(v))))
''' < "${tmp_kube_master_certs}" > "${KUBE_HOME}/kube-master-certs")
rm -f "${tmp_kube_master_certs}"
)
@ -530,9 +540,21 @@ KUBE_HOME="/home/kubernetes"
KUBE_BIN="${KUBE_HOME}/bin"
PYTHON="python"
if [[ "$(python -V)" =~ "Python 3" ]]; then
if [[ "$(python -V 2>&1)" =~ "Python 2" ]]; then
# found python2, just use that
PYTHON="python"
elif [[ -f "/usr/bin/python2.7" ]]; then
# System python not defaulted to python 2 but using 2.7 during migration
PYTHON="/usr/bin/python2.7"
else
# No python2 either by default, let's see if we can find python3
PYTHON="python3"
if ! command -v ${PYTHON} >/dev/null 2>&1; then
echo "ERROR Python not found. Aborting."
exit 2
fi
fi
echo "Version : " $(${PYTHON} -V 2>&1)
# download and source kube-env
download-kube-env