Pragmatism in the real world

Switching OpenWhisk environments

When developing with OpenWhisk, it’s useful to use separate environments for working locally or on the cloud for development, staging and production of your application. In OpenWhisk terms, this means setting the host and the API key for your wsk command line application.

$ wsk property set --apihost $host --auth $key

(Of course, for live and staging, ideally, you will be using a build server!)

For a Vagrant install of OpenWhisk, the host is and the key can be found inside the ansible provisioning files. On Bluemix, the host is found on the Download OpenWhisk CLI page, buried in a command that you can copy. Separate environments is most easily done using separate “namespaces” as each space has its own key.

To avoid having to keep looking up the correct keys, I wrote a simple Bash function in my .bash_profile file:

function owenv() {
    bold=$(tput bold)
    normal=$(tput sgr0)

    case "$space" in
            key=`cat ${OPENWHISK_HOME}/ansible/files/auth.guest`
            switches="-i" # insecure switch required as cert is self-signed
            key=`cat ${OPENWHISK_HOME}/ansible/files/auth.guest`
            apihost=`wsk -i property get --apihost | rev | cut -f1 | rev`
            namespace=`wsk -i namespace list | tail -n1`
            echo "Host      ${bold}${apihost}${normal}"
            echo "Namespace ${bold}${namespace}${normal}"
            echo "Change using: owenv {dev|uat|live|local|localssl}"

    if [ -z $key ] ; then

    wsk $switches property set --apihost $host --auth $key > /dev/null && \
    wsk $switches property unset --namespace > /dev/null

    # display some information
    apihost=`wsk $switches property get --apihost | rev | cut -f1 | rev`
    namespace=`wsk $switches namespace list | tail -n1`
    echo "Host      ${bold}${apihost}${normal}"
    echo "Namespace ${bold}${namespace}${normal}"

(Actual keys are stored separately.)

This code uses a case statement to set up the right host and key to use. I’m lazy, so just hardcode my Bluemix keys via environment variables. There’s probably a better way to that though. For the local Vagrant instance, I get the key directly from the file used by Ansible for provisioning. Again, due to laziness, I’ve hardcoded the Vagrant VM’s IP address.

Lastly I set display the current host and namespace – tput is my new favourite bash command!

Switching environments

I can then use the function to switch to my Vagrant installation like this:

$ owenv local
Namespace guest

Or, I can switch to my cloud development environment using:

$ owenv dev
Namespace 19FT_dev

Updating the CLI tool

I also have a script to update the CLI tool:

function owupdatewsk() {
        echo "Current version: `wsk property get --cliversion | rev | cut -f1 | rev`"
        pushd /tmp > /dev/null
        curl -s -o && \
        unzip > /dev/null && \
        mv /usr/local/bin/wsk /usr/local/bin/wsk.old
        mv wsk /usr/local/bin/wsk && \
        rm && \
        echo "New version:     `wsk property get --cliversion | rev | cut -f1 | rev`"
        popd > /dev/null

This is a quick way to collect the latest version of the Bluemix version of the wsk app.

One thought on “Switching OpenWhisk environments

Comments are closed.