Skip to content

Instantly share code, notes, and snippets.

@dosaboy
Last active October 12, 2020 22:21
Show Gist options
  • Save dosaboy/e7b5d142fd56540b6600dc428edf5656 to your computer and use it in GitHub Desktop.
Save dosaboy/e7b5d142fd56540b6600dc428edf5656 to your computer and use it in GitHub Desktop.
#!/bin/bash -eu
#
# Origin: https://gist.github.com/dosaboy/e7b5d142fd56540b6600dc428edf5656
#
# Authors:
# - edward.hope-morley@canonical.com
# - opentastic@gmail.com
#
# Query Nova Placement API
RESULTS_DIR=`mktemp -d`
cleanup ()
{
rm -rf $RESULTS_DIR
}
trap cleanup KILL EXIT
which python &>/dev/null && PY_VERSION="" || PY_VERSION="3"
[[ PY_VERSION==3 ]] && which python3 &>/dev/null || PY_VERSION=""
install_deps ()
{
declare -a dependencies=(
python${PY_VERSION}-simplejson
jq
curl
)
declare -a missing_dependencies=()
for dep in ${dependencies[@]}; do
dpkg -s $dep &>/dev/null || missing_dependencies+=( $dep )
done
((${#missing_dependencies[@]})) || return 0
local missing="${missing_dependencies[@]}"
read -p "INFO: install required dependencies - $missing? [Yn] " answer
[ "${answer,,}" = "y" ] || [ -z "$answer" ] || return 0
sudo apt install ${missing_dependencies[@]} -y
}
json_key_exists ()
{
key=$1; shift
echo $@| python${PY_VERSION} -c "
import sys, json;
data=json.load(sys.stdin);
sys.exit(0) if '$key' in data else sys.exit(1);"
}
get_placement_endpoint ()
{
svcs=`curl -s -H "X-Auth-Token: $TOKEN" "${AUTH_URL}/services"`
if json_key_exists error $svcs; then
svcs=`curl -s -H "X-Auth-Token: $TOKEN" "${AUTH_URL}/OS-KSADM/services"`
fi
n_svc=`echo $svcs| python${PY_VERSION} -c "
import sys, json;
ss = json.load(sys.stdin);
ss = ss.get('services') or ss['OS-KSADM:services'];
print([s['id'] for s in ss if s['name'] == 'placement'][0])"`
curl -s -H "X-Auth-Token: $TOKEN" "${AUTH_URL}/endpoints"| \
python${PY_VERSION} -c "
import sys, json;
ss = json.load(sys.stdin)['endpoints'];
print([s.get('url') or s.get('adminurl') \
for s in ss \
if s['service_id'] == '$n_svc' and \
s.get('interface', 'admin') == 'admin'][0])"
}
TOKEN=`openstack token issue| grep ' id '| awk '{print $4}'`
AUTH_URL=`echo $OS_AUTH_URL| sed 's/5000/35357/g'`
placement_ep=`get_placement_endpoint`
curl -H "X-Auth-Token: $TOKEN" -H "OpenStack-API-Version: placement 1.1" $placement_ep/resource_providers > $RESULTS_DIR/resource_providers
for rp in `jq -r '.resource_providers[].uuid' $RESULTS_DIR/resource_providers`; do
echo -e "\n================================================================="
name=`jq -r ".resource_providers[]| select(.uuid==\"$rp\") | .name" $RESULTS_DIR/resource_providers`
echo -e "Resource provider\n - name: $name\n - uuid: $rp"
echo "Inventory:"
curl -s -H "X-Auth-Token: $TOKEN" -H 'OpenStack-API-Version: placement 1.1' $placement_ep/resource_providers/$rp/inventories| jq
echo "Allocations:"
curl -s -H "X-Auth-Token: $TOKEN" -H 'OpenStack-API-Version: placement 1.1' $placement_ep/resource_providers/$rp/allocations| jq
done
echo -e "\nDone."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment