Friday, 10 May 2013

juju switch

The switch command has recently landed in trunk, and will be included in the next juju-core release.

juju switch is another way to specify the current working environment. Current precedence for environment lookup still holds, but this now sits between the JUJU_ENV environment variable and the default value in environments.yaml.

If you have multiple environments defined, there are several different ways to tell juju which environment you mean when executing commands.

Prior to switch, there were three ways to specify the environment.

The first and default way to specify the environment is to use the default value in the environments.yaml file.  This was always the fallback position if one of the other ways was not specified.

Another way was to be explicit for some commands, and use the -e or --environment command line argument.


$ juju bootstrap -e hpcloud



There is also an environment variable that can be set which will override the default specified in the environments.yaml file.


$ export JUJU_ENV=hpcloud
$ juju bootstrap          # now bootstraps hpcloud
$ juju deploy wordpress   # deploys to hpcloud



The switch option effectively overrides what the default is for the environments.yaml file without actually changing the environments.yaml file. This means that -e and the JUJU_ENV options still override the environment defined by switch.


$ juju help switch
usage: juju switch [options] [environment name]
purpose: show or change the default juju environment name
 
options:
-l, --list  (= false)
    list the environment names
 
Show or change the default juju environment name.
 
If no command line parameters are passed, switch will output the current
environment as defined by the file $JUJU_HOME/current-environment.
 
If a command line parameter is passed in, that value will is stored in the
current environment file if it represents a valid environment name as
specified in the environments.yaml file.
 
aliases: env



It works something like this:


$ juju env
Current environment: "amazon-ap"
$ juju switch
Current environment: "amazon-ap"
$ juju switch -l
Current environment: "amazon-ap"

Environments:
        amazon
        amazon-ap
        hpcloud
        openstack
$ juju switch amazon
Changed default environment from "amazon-ap" to "amazon"
$ juju switch amazon
Current environment: "amazon"
$ juju switch
Current environment: "amazon"



If you have JUJU_ENV set, then you get told that the current environment is defined by this.  Also if you try to use switch to change the current environment when the environment is defined by JUJU_ENV, you will get an error.


$ export JUJU_ENV="amazon-ap"
$ juju switch
Current environment: "amazon-ap" (from JUJU_ENV)
$ juju switch amazon
error: Cannot switch when JUJU_ENV is overriding the environment (set to "amazon-ap")


 

3 comments:

Unknown said...

Nice. Reminds me of bazaar colo branches, I wonder where you got the inspiration :)

bigjools said...

Gah, Google ID leaves me as "unknown", let's try this.

Gavin Panella said...

This seems fairly complex. Are some of these 4 ways of specifying the environment going to be deprecated or removed?

For example, JUJU_ENV could be removed, and the default could be the first environment defined in environments.yaml. That would leave just `juju switch` and `juju -e $env`.