Handle python binary look for scripts

We have some scripts that we deploy via tripleo that use inline python.
For this we need to be able to find an available python on the system in
order for it to work. This change adds a lookup function to the scripts
to find a working version of python as /usr/bin/python may not exist.

Change-Id: Ida7a7cbd064ebdb923f38c9102eb4b0771f9b273
Related-Blueprint: python3-support
This commit is contained in:
Alex Schultz 2018-11-21 17:42:06 -07:00
parent 07241f33d1
commit 1e5ccb4c7d
4 changed files with 24 additions and 5 deletions

View File

@ -14,8 +14,13 @@ SSH_HOSTKEY_OPTIONS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
SHORT_TERM_KEY_COMMENT="TripleO split stack short term key" SHORT_TERM_KEY_COMMENT="TripleO split stack short term key"
SLEEP_TIME=5 SLEEP_TIME=5
# needed to handle where python lives
function get_python() {
command -v python3 || command -v python2 || command -v python || exit 1
}
function overcloud_ssh_hosts_json { function overcloud_ssh_hosts_json {
echo "$OVERCLOUD_HOSTS" | python -c ' echo "$OVERCLOUD_HOSTS" | $(get_python) -c '
from __future__ import print_function from __future__ import print_function
import json, re, sys import json, re, sys
print(json.dumps(re.split("\s+", sys.stdin.read().strip())))' print(json.dumps(re.split("\s+", sys.stdin.read().strip())))'
@ -24,7 +29,7 @@ print(json.dumps(re.split("\s+", sys.stdin.read().strip())))'
function overcloud_ssh_key_json { function overcloud_ssh_key_json {
# we pass the contents to Mistral instead of just path, otherwise # we pass the contents to Mistral instead of just path, otherwise
# the key file would have to be readable for the mistral user # the key file would have to be readable for the mistral user
cat "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))' cat "$1" | $(get_python) -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
} }
function workflow_finished { function workflow_finished {

View File

@ -61,10 +61,15 @@ resources:
eth_addr=$(cat /sys/class/net/*/address | tr '\n' ',') eth_addr=$(cat /sys/class/net/*/address | tr '\n' ',')
mkdir -p /etc/os-net-config mkdir -p /etc/os-net-config
# needed to handle where python lives
function get_python() {
command -v python3 || command -v python2 || command -v python || exit 1
}
# Create an os-net-config mapping file, note this defaults to # Create an os-net-config mapping file, note this defaults to
# /etc/os-net-config/mapping.yaml, so we use that name despite # /etc/os-net-config/mapping.yaml, so we use that name despite
# rendering the result as json # rendering the result as json
echo '$node_lookup' | python -c " echo '$node_lookup' | $(get_python) -c "
import json import json
import sys import sys
import copy import copy

View File

@ -31,10 +31,14 @@ resources:
#!/bin/sh #!/bin/sh
node_id=$(dmidecode --s system-uuid | awk 'match($0, /[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}/) { print substr($0, RSTART, RLENGTH) }') node_id=$(dmidecode --s system-uuid | awk 'match($0, /[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}/) { print substr($0, RSTART, RLENGTH) }')
# needed to handle where python lives
function get_python() {
command -v python3 || command -v python2 || command -v python || exit 1
}
# Create a /etc/puppet/hieradata/UUID.json file to provide # Create a /etc/puppet/hieradata/UUID.json file to provide
# the data of the NodeDataLookup parameter that matches the # the data of the NodeDataLookup parameter that matches the
# system UUID # system UUID
echo $node_lookup | python -c " echo $node_lookup | $(get_python) -c "
import json import json
import sys import sys
input = sys.stdin.readline() or '{}' input = sys.stdin.readline() or '{}'

View File

@ -1,6 +1,11 @@
#!/bin/bash #!/bin/bash
set -e set -e
# needed to handle where python lives
function get_python() {
command -v python3 || command -v python2 || command -v python || exit 1
}
function ping_retry() { function ping_retry() {
local IP_ADDR=$1 local IP_ADDR=$1
local TIMES=${2:-'10'} local TIMES=${2:-'10'}
@ -33,7 +38,7 @@ function ping_controller_ips() {
networks=$(ip r | grep -v default | cut -d " " -f 1) networks=$(ip r | grep -v default | cut -d " " -f 1)
fi fi
for LOCAL_NETWORK in $networks; do for LOCAL_NETWORK in $networks; do
in_network=$(python -c "import ipaddress; net=ipaddress.ip_network(u'$LOCAL_NETWORK'); addr=ipaddress.ip_address(u'$REMOTE_IP'); print(addr in net)") in_network=$($(get_python) -c "import ipaddress; net=ipaddress.ip_network(u'$LOCAL_NETWORK'); addr=ipaddress.ip_address(u'$REMOTE_IP'); print(addr in net)")
if [[ $in_network == "True" ]]; then if [[ $in_network == "True" ]]; then
echo "Trying to ping $REMOTE_IP for local network ${LOCAL_NETWORK}." echo "Trying to ping $REMOTE_IP for local network ${LOCAL_NETWORK}."
set +e set +e