Skip to content

Instantly share code, notes, and snippets.

@smoser
Last active October 9, 2018 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoser/19e704627318b20d27a68636d918d08b to your computer and use it in GitHub Desktop.
Save smoser/19e704627318b20d27a68636d918d08b to your computer and use it in GitHub Desktop.
rackspace cli shell wrapper

Rackspace CLI wrapper

Figuring out how to use the openstack CLI tools ('nova', 'oscli' or 'openstack') with rackspace was quite non-obvious. Perhaps I was missing some documentation.

This is just a simple wrapper to set my creds for interaction with the openstack cli tools to interact with rackspace. I have similar wrappers for other clouds (vexxhost, server-stack, runabove, dreamcompute).

The values here have worked for me with on Rackspace public cloud with 18.04 versions of 'swift', 'openstack, and 'nova' as of 2018-04-20. In a fresh lxd container:

$ apt-get install -y python3-openstackclient python3-novaclient python3-swiftclient
$ rackspace nova server list
$ rackspace openstack server list
$ rackspace swift list

Customization

You need to edit the script to add your credentials. Since it has your credentials, you should probably: chmod go-rwx ~/bin/rackspace.

  • OS_AUTH_URL: Leave this to its setting
  • OS_USERNAME: This is your "Account Name". Mine is 'smoser'. Find it in the web UI in the top right.
  • OS_TENANT_NAME: account number. Mine is 6 digits. See in the web ui under your username. Some tools may need you to also set OS_PROJECT_NAME to this, but I could not verify that recently.
  • OS_PASSWORD: This is the password you log into the web UI with. Mine of course is 'password'.
  • OS_REGION_NAME: This controls which region is used. Some known values are: DFW, ORD, IAD
  • REGION_NAME: Set this in your environment before invoking the script or modify the default value (DFW).
  • KEYNAME: If you use 'rackspace ubuntu', this is what will be used for '--key-name' argument. You have to already have a key loaded with this name.

Usage

You can use it like:

  1. no arguments: export needed environment and invoke $SHELL:

    $ rackspace
    launching shell [/bin/bash]
    (rackspace-DFW)smoser@milhouse:~/$
    
  2. command to run. instead of running 'bash', set up environment and run that command.

    $ rackspace openstack image list | grep 16.04.*PV
    | 8b7<trim>220 | Ubuntu 16.04 LTS (Xenial Xerus) (PVHVM) | active | 
    
  3. Special arguments: Shortcuts for humans to 'nova' or 'openstack':

    • rackspace image-list -> nova image-list
    • rackspace server list -> openstack server list
    • rackspace flavor list -> oscli flavor list
  4. ubuntu: special "launch me an ubuntu" shortcut

    $ rackspace --dry-run ubuntu
    openstack server create --key-name=brickies \
        --flavor="512MB Standard Instance" \
        --image="Ubuntu 16.04 LTS (Xenial Xerus) (PVHVM)" \
        ubuntu-20180420-133330
    
#!/bin/bash
# https://gist.github.com/smoser/19e704627318b20d27a68636d918d08b
# This is my 'rackspace' script. It has credentials in it so I set
# perms on it like: chmod 700 rackspace.
#
# Customize it with your creds inline below.
#
# You can use it like:
# 1. no arguments: export needed environment and invoke $SHELL:
#
# $ rackspace
# launching shell [/bin/bash]
# (rackspace-DFW)smoser@milhouse:~/$
#
# 2. command to run. instead of running 'bash', set up environment
# and run that command.
#
# $ rackspace openstack image list | grep 16.04.*PV
# | 8b7<trim>220 | Ubuntu 16.04 LTS (Xenial Xerus) (PVHVM) | active |
#
# 3. Special arguments: Shortcuts for humans to 'nova' or 'openstack'
# rackspace image-list -> nova image-list
# rackspace server list -> openstack server list
# rackspace console-log -> nova console-log
#
# 4. ubuntu: special "launch me an ubuntu" shortcut
# $ rackspace --dry-run ubuntu
# openstack server create --key-name=brickies \
# --flavor="512MB Standard Instance" \
# --image="Ubuntu 16.04 LTS (Xenial Xerus) (PVHVM)" \
# ubuntu-20180420-133330
#
# It picks REGION_NAME of DFW by default, but will read that from env.
# update variables below, or put them in CREDS_PATH
CREDS_PATH="${CREDS_PATH:-$HOME/creds/rackspace}"
OS_AUTH_URL=https://identity.api.rackspacecloud.com/v2.0/
# OS_USERNAME == account name (in web ui top right)
OS_USERNAME=smoser
# OS_TENANT_NAME == account number (see in the web ui under your username)
# (it may be needed for some tools to also set OS_PROJECT_NAME to this value)
OS_TENANT_NAME="012345"
# OS_PASSWORD == login to web ui with this.
OS_PASSWORD="passw0rd"
# regions are DFW,ORD,IAD
OS_REGION_NAME=${REGION_NAME:-DFW}
KEYNAME="${KEYNAME:-brickies}" # used for 'rackspace ubuntu as --key-name='
if [ -f "${CREDS_PATH}" ]; then
. "${CREDS_PATH}" || { echo "failed '. \"$CREDS_PATH\"'" 1>&2; exit 1; }
fi
export OS_AUTH_URL OS_USERNAME OS_TENANT_NAME OS_PASSWORD OS_REGION_NAME
# Entries in OSCLI_SUBCOMMANDS and NOVA_SUBCOMMANDS pre-pended with
# 'openstack' and 'nova'.
OSCLI_SUBCOMMANDS="
address aggregate availability catalog command complete compute configuration
console container ec2 endpoint extension flavor floating help host hypervisor
image ip keypair limits module network object port project quota role router
security server service snapshot subnet token usage user volume "
NOVA_SUBCOMMANDS=" list delete console-log image-list "
if [ "$0" = "$BASH_SOURCE" ]; then
if [ $# -eq 0 ]; then
set -- "${SHELL:-/bin/bash}"
# abuse debian_chroot to get a this in the prompt of the shell.
export debian_chroot="rackspace-$OS_REGION_NAME"
echo "launching shell [$1]" 1>&2
fi
dry_echo=""
if [ "$1" = "--dry-run" ]; then
dry_echo="echo"
shift
fi
if [ "${NOVA_SUBCOMMANDS#* ${1} }" != "${NOVA_SUBCOMMANDS}" ]; then
set -- nova "$@"
elif [ "${OSCLI_SUBCOMMANDS#* ${1} }" != "${OSCLI_SUBCOMMANDS}" ]; then
set -- openstack "$@"
fi
if [ "$1" = "ubuntu" -o "$1" = "ubuntu-onmetal" ]; then
echo "here: $*"
case "$1" in
ubuntu-onmetal)
flavor="onmetal-general2-small"
img="OnMetal - Ubuntu 18.04 LTS (Bionic Beaver)";;
ubuntu)
flavor="512MB Standard Instance"
img="Ubuntu 18.04 LTS (Bionic Beaver) (PVHVM)";;
esac
shift
if [ "${1%%=*}" = "--name" ]; then
name="${1#*=}"
shift
elif [ -z "$name" ]; then
name=$(date --utc "+ubuntu-%Y%m%d-%H%M%S")
fi
# --config-drive=1
set -- openstack server create --key-name=$KEYNAME \
"--flavor=$flavor" "--image=$img" "$@" "$name"
[ -n "$dry_echo" ] || echo "$@" 1>&2
fi
maps="n:nova o:openstack k:keystone g:glance net:neutron s:swift"
for i in $maps; do
[ "$1" = "${i%:*}" ] && { shift; set -- "${i#*:}" "$@"; break; }
done
exec $dry_echo "$@"
fi
# vi: ts=4 expandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment