Developing software 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 192.168.33.13 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)

    switches=""
    space=$1
    case "$space" in
        "dev")
            host=openwhisk.eu-gb.bluemix.net
            key=${OW_KEY_DEV}
            ;;
        "uat")
            host=openwhisk.eu-gb.bluemix.net
            key=${OW_KEY_UAT}
            ;;
        "live")
            host=openwhisk.eu-gb.bluemix.net
            key=${OW_KEY_LIVE}
            ;;
        "local")
            host=http://${OW_LOCAL_IP}:10001
            key=`cat ${OPENWHISK_HOME}/ansible/files/auth.guest`
            ;;
        "localssl")
            switches="-i" # insecure switch required as cert is self-signed
            host=${OW_LOCAL_IP}
            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}"
            return
    esac

    if [ -z $key ] ; then
        return
    fi

    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
Host      http://192.168.33.13:10001
Namespace guest

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

$ owenv dev
Host      openwhisk.eu-gb.bluemix.net
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 OpenWhisk_CLI-mac.zip https://openwhisk.ng.bluemix.net/cli/go/download/mac/amd64/OpenWhisk_CLI-mac.zip && \
        unzip OpenWhisk_CLI-mac.zip > /dev/null && \
        mv /usr/local/bin/wsk /usr/local/bin/wsk.old
        mv wsk /usr/local/bin/wsk && \
        rm OpenWhisk_CLI-mac.zip && \
        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

Thoughts? Leave a reply

Your email address will not be published.