Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

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 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() {
    switches=""
    space=$1
    case "$space" in
        "dev")
            host=openwhisk.ng.bluemix.net
            key="aaa-bbb-ccc-ddd-eee:fff"
            ;;
        "uat")
            host=openwhisk.ng.bluemix.net
            key="bbb-ccc-ddd-eee-fff:ggg"
            ;;
        "live")
            host=openwhisk.ng.bluemix.net
            key="ccc-ddd-eee-fff-ggg:hhh"
            ;;
        "local")
            host=http://192.168.33.13:10001
            key=`cat ~/Projects/openwhisk/openwhisk/ansible/files/auth.guest`
            ;;
        "localssl")
            switches="-i" # insecure switch required as cert is self-signed
            host=192.168.33.13
            key=`cat ~/Projects/openwhisk/openwhisk/ansible/files/auth.guest`
            ;;
        *)
            echo "Error. No space set: choose dev, uat, live, local or localssl"
            return
    esac

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

Take a look at the Indigo.Design sample applications to learn more about how apps are created with design to code software.

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

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}