How to manage plugins

See also: Plugin

Juju can be easily extended by using plugins. There are many third-party plugins available to simplify certain tasks. However, you can easily create your own custom plugins for your specific needs. This tutorial will show you how to do this.

Another way to integrate Juju into your own tooling is to use the Python and JavaScript client libraries.

Contents:

Create a plugin

Let’s say we want to get the IP address for a machine. To do this, we can run juju status, look at the table of units, find the row for the desired machine and read off the IP address. However:

  • we cannot easily do this inside a Bash script
  • this might get tedious if we have to do it repeatedly.

To solve this, we can write a plugin that, given the ID for a machine, will return the IP address. We want to be able to run it like this:

$ juju ip 0
10.50.10.50

Let’s get started. Create a file called juju-ip with the following contents:

#!/bin/bash

# First arg is machine ID
MACHINE=$1
# The jq query we will use to find the IP address
QUERY=$(printf '.machines."%d"."dns-name"' "$MACHINE")
# Call juju status, use jq to filter output
juju status --format=json | jq -r "$QUERY"

This is a simple Bash script that calls juju status using JSON output, so that we can use jq to filter the output and get the IP address. (You’ll need to install jq for this to work.)

We’ve used Bash for this plugin, but you can use any language which allows command-line execution. Feel free to use Python, JS, Go, etc - whatever you are most familiar with.

Install and run a plugin

To run this plugin, we just need to ensure that the juju-ip file is placed somewhere on our $PATH. In a terminal, run

echo $PATH | tr ':' '\n'

This will output all the directories on your $PATH. Put the juju-ip file in any one of these directories.

We also need to ensure our plugin is an executable file. In your terminal, run

sudo chmod +x [path/to/juju-ip]

replacing [path/to/juju-ip] with the new location of the juju-ip file.

Now, we are ready to run the plugin! Switch to a model with some machines deployed, and run

juju ip 0

When you run

juju [command] [args]

and [command] is not a built-in Juju command, Juju will automatically search your $PATH for a file named juju-[command], and attempt to execute it with the given [args]. This mechanism is what makes plugins possible.

Distribute a plugin

If you’ve written something that you think other Juju users might find useful, feel free to distribute it. We suggest:

  • creating a GitHub repo to host your plugin
  • making a post about on the Juju Discourse forum
  • adding your plugin to our list of third-party Juju plugins.

Contributors: @barrettj12, @timclicks, @tmihoc

4 Likes