Skip to content

Instantly share code, notes, and snippets.

@imperialwicket
Last active December 14, 2015 22:19
Show Gist options
  • Save imperialwicket/5157946 to your computer and use it in GitHub Desktop.
Save imperialwicket/5157946 to your computer and use it in GitHub Desktop.
Bash script for installing the various openstack python clients and populating a file with appropriate environment variables.
#!/bin/bash
#####################################################################
#
# openstack-python-packages.sh
#
# https://gist.github.com/imperialWicket
#
# This scripts automates much of getting started process for
# openstack cli installation (python clients). Pip is required for
# this script to work, and also Python 2.x (x > 6). Pip presence is
# validated, the Python version is NOT checked.
#
#
# version 1.0.0
#
# http://mit-license.org/
#
#####################################################################
# openstack-python-packages.sh usage
usage()
{
cat << EOF
openstack-python-packages.sh
This script loops through the available clients and installs (default),
upgrades, or uninstalls. Sudo privileges are required.
It also optionally adds your username and api key to a file.
This script will exit with an error if pip is not available.
USAGE: openstack-python-packages.sh [-r|-u][-s][-c 'client list'][-f file][-k
apikey][-n username][-p password][-t tenant][-a auth_url]
OPTIONS:
-h Show this message
-u Run upgrades to all clients
-r Remove all clients (via pip uninstall)
-c Clients to install (quote multiple clients separated by spaces or
use multiple -c client flags)
-f Add environment variables to a specific file
-k API key
-n Username
-p Password
-t Tenant name
-a Authentication URL
-z Region name (rackspace only)
-s Skip environment variables addition
-x Use Rackspace (uses openstack without)
-q Do not confirm environment variables, just write with empty vars.
If file is not specified with -f file, vars are sent to stdout.
EXAMPLES:
./openstack-python-packages.sh
Install the default clients (nova, quantum, keystone, glance, swift,
cinder) and interactively populate a file with environment variables.
./openstack-python-packages.sh -suc "nova keystone"
./openstack-python-packages.sh -su -c nova -c keystone
Run upgrades to the nova and keystone clients and skip any environment
variable writing.
./openstack-python-packages.sh -q -n username -k apikey -a auth_url -f file
Install default clients and populate environment variables in the provided
file without checking for empty values.
EOF
}
environment()
{
if [ $QUIET -eq 0 ]; then
if [ $RACKSPACE -eq 1 ]; then
echo 'Rackspace uses the username value for both username and tenant'
echo 'name, requires the region value, and places the API key value'
echo 'in the password environment variable.'
echo ''
fi
if [ -z $USERNAME ]; then
echo 'Please provide credentials to add to your environment,'
echo 'or provide an empty username to exit. '
echo ''
read -p "Username: " USERNAME
if [ -z $USERNAME ]; then
echo 'Exiting, update your environment variables manually or re-run with the -u flag.'
exit 1
fi
fi
if [ -z $PASSWORD ]; then
read -p "Password: " PASSWORD
fi
if [ -z $TENANT ]; then
read -p "Tenant name: " TENANT
fi
if [ -z $APIKEY ]; then
read -p "API Key: " APIKEY
fi
if [ -z $AUTH_URL ]; then
read -p "Authentication endpoint: " AUTH_URL
fi
if [ $RACKSPACE -eq 1 ]; then
if [ -z $REGION ]; then
read -p "Rackspace region: " REGION
fi
fi
if [ -z $ENV_FILE ]; then
read -p "Environment variable file: " ENV_FILE
fi
fi
# Silly indentation here
if [ $RACKSPACE -eq 0 ]; then
ENV_VARS="
############
# OPENSTACK
############
export OS_USERNAME=$USERNAME
export OS_PASSWORD=$PASSWORD
export OS_TENANT_NAME=$TENANT
export OS_AUTH_URL=$AUTH_URL
export NOVACLIENT_DEBUG=$NOVACLIENT_DEBUG
export NOVA_VERSION=$NOVA_VERSION"
else
ENV_VARS="
############
# RACKSPACE
############
export OS_USERNAME=$USERNAME
export OS_TENANT_NAME=$USERNAME
export OS_AUTH_SYSTEM=rackspace
export OS_PASSWORD=$APIKEY
export OS_AUTH_URL=$AUTH_URL
export OS_REGION_NAME=$REGION
export OS_NO_CACHE=1"
fi
if [ -w $ENV_FILE ]; then
echo -e "$ENV_VARS" >> $ENV_FILE
echo "Run 'source $ENV_FILE'; Then try 'nova credentials' to validate
configuration success."
else
echo "$ENV_FILE is not writable, these are the environment variables:"
echo -e "$ENV_VARS"
fi
}
# Set default values
CLIENTS="nova quantum keystone glance swift cinder"
ENV_FILE=''
APIKEY=''
USERNAME=''
TENANT=''
PASSWORD=''
AUTH_URL=''
UPDATE=0
REMOVE=0
DO_ENV=1
QUIET=0
RACKSPACE=0
REGION=''
SET='set'
NOVA_VERSION=2
NOVACLIENT_DEBUG=0
# handle args
while getopts ":uhxsqrc:f:a:k:t:p:z:n:" flag
do
case "$flag" in
h)
usage
exit 0
;;
u)
UPDATE=1
;;
r)
REMOVE=1
;;
f)
ENV_FILE=$OPTARG
;;
c)
CLIENTS=$OPTARG
;;
k)
APIKEY=$OPTARG
;;
a)
AUTH_URL=$OPTARG
;;
n)
USERNAME=$OPTARG
;;
t)
TENNANT=$OPTARG
;;
p)
PASSWORD=$OPTARG
;;
z)
REGION=$OPTARG
;;
s)
DO_ENV=0
;;
q)
QUIET=1
;;
x)
RACKSPACE=1
# Rackspace currently only uses an extended nova client
CLIENTS="nova"
;;
?)
usage
exit 1
;;
esac
done
# pip?
PIP=`which pip`
if [ -z $PIP ]; then
echo 'Install pip.'
exit 1
fi
# rackspace?
if [ $RACKSPACE -eq 1 ]; then
PREFIX='rackspace'
else
PREFIX='python'
fi
# Install, upgrade, or remove
if [ $((UPDATE + REMOVE)) -eq 0 ]; then
CMD="sudo pip install $PREFIX-"
elif [ $((UPDATE + REMOVE)) -eq 2 ]; then
echo -e '\n################\nUpdate and remove are mutually exclusive, please provide -r (remove),
-u (upgrade), or neither (install).\n################\n'
usage
exit 1
elif [ $UPDATE -eq 1 ]; then
CMD="sudo pip install --upgrade $PREFIX-"
else
CMD="sudo pip uninstall $PREFIX-"
SET='remove'
echo 'This script does not remove your environment variables!!'
fi
# Perform the CMD on the CLIENTS
for proj in $CLIENTS
do
$CMD${proj}client
done
# Don't mess with environment if we removed the packages.
if [ $REMOVE -eq 1 ]; then
DO_ENV=0
fi
# Set environment vars?
if [ $DO_ENV -eq 1 ]; then
echo ''
environment
else
echo "Don't forget to $SET your environment variables!"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment