Switching OpenWhisk Environments

DZone 's Guide to

Switching OpenWhisk Environments

In this quick tutorial, we go over a few easy ways to switch between various environments while developing in OpenWhisk.

· Web Dev Zone ·
Free Resource

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 always openwhisk.ng.bluemix.net and separate environments are most easily done using separate "spaces" 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() {
    case "$space" in
            key=`cat ~/Projects/openwhisk/openwhisk/ansible/files/auth.guest`
            switches="-i" # insecure switch required as cert is self-signed
            key=`cat ~/Projects/openwhisk/openwhisk/ansible/files/auth.guest`
            echo "Error. No space set: choose dev, uat, live, local or localssl"

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

    # display some information
    bold=$(tput bold)
    normal=$(tput sgr0)
    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 redacted!)

This code uses a case statement to set up the right host and key to use. I'm lazy, so I just hardcoded my Bluemix keys. There's probably a better way to do 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 it to display the current host and namespace – tput is my new favorite 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 the following:

$ owenv dev
Host      openwhisk.ng.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 wsk /usr/local/bin/wsk && \
    rm OpenWhisk_CLI-mac.zip && \
    popd > /dev/null  && \
    echo "New version:     `wsk property get --cliversion | rev | cut -f1 | rev`"

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

cli, cloud environments, development environment, openwhisk, web dev

Published at DZone with permission of Rob Allen , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}