From 54783f66b8188297448bc725c578fdc7647e9fa0 Mon Sep 17 00:00:00 2001 From: Leon <82407168+sed-i@users.noreply.github.com> Date: Wed, 29 Sep 2021 12:09:02 -0400 Subject: [PATCH] feat(juju): add plugin for Juju (juju.is) (#10075) Co-authored-by: Jon Seager --- plugins/juju/README.md | 117 ++++++++++++++++++++++++++++++++ plugins/juju/juju.plugin.zsh | 127 +++++++++++++++++++++++++++++++++++ 2 files changed, 244 insertions(+) create mode 100644 plugins/juju/README.md create mode 100644 plugins/juju/juju.plugin.zsh diff --git a/plugins/juju/README.md b/plugins/juju/README.md new file mode 100644 index 000000000..49f8b0d47 --- /dev/null +++ b/plugins/juju/README.md @@ -0,0 +1,117 @@ +# juju plugin + +This plugin provides useful aliases and functions for [juju](https://juju.is/) (for TAB completion, +refer to the [official repo](https://github.com/juju/juju/blob/develop/etc/bash_completion.d/juju)). + +To use this plugin, add `juju` to the plugins array in your zshrc file. + +```zsh +plugins=(... juju) +``` + +## Aliases + +Naming convention: + +- `!` suffix: `--force --no-wait -y`. +- `ds` suffix: `--destroy-storage`. + +### General + +| Alias | Command | Description | +|--------|---------------------------------------------|--------------------------------------------------------| +| `jdl` | `juju debug-log --ms` | Display log, with millisecond resolution | +| `jdlr` | `juju debug-log --ms --replay` | Replay entire log | +| `jh` | `juju help` | Show help on a command or other topic | +| `jssl` | `juju juju show-status-log` | Output past statuses for the specified entity | +| `jstj` | `juju status --format=json` | Show status in json format (more detailed) | +| `jst` | `juju status --relations --storage --color` | Show status, including relations and storage, in color | + +### Bootstrap + +| Alias | Command | Description | +|-------|---------------------------|-------------------------------------------| +| `jb` | `juju bootstrap` | Initializing a Juju cloud environment | +| `jbm` | `juju bootstrap microk8s` | Initializing a MicroK8s cloud environment | + +### Controller + +| Alias | Command | Description | +|----------|---------------------------------------------------------------------------------------|-------------------------------------------------------------------| +| `jdc` | `juju destroy-controller --destroy-all-models` | Destroy a controller | +| `jdc!` | `juju destroy-controller --destroy-all-models --force --no-wait -y` | Destroy a controller | +| `jdcds` | `juju destroy-controller --destroy-all-models --destroy-storage` | Destroy a controller and associated storage | +| `jdcds!` | `juju destroy-controller --destroy-all-models --destroy-storage --force --no-wait -y` | Destroy a controller and associated storage | +| `jkc` | `juju kill-controller -y -t 0` | Forcibly terminate all associated resources for a Juju controller | +| `jsw` | `juju switch` | Select or identify the current controller and model | + +### Model + +| Alias | Command | Description | +|----------|-------------------------------------------------------------|-------------------------------------------------------| +| `jam` | `juju add-model` | Add a hosted model | +| `jdm` | `juju destroy-model` | Non-recoverable, complete removal of a model | +| `jdm!` | `juju destroy-model --force --no-wait -y` | Non-recoverable, complete removal of a model | +| `jdmds` | `juju destroy-model --destroy-storage` | Non-recoverable, complete removal of a model | +| `jdmds!` | `juju destroy-model --destroy-storage --force --no-wait -y` | Non-recoverable, complete removal of a model | +| `jmc` | `juju model-config` | Display or set configuration values on a model | +| `jm` | `juju models` | List models a user can access on a controller | +| `jshm` | `juju show-model` | Show information about the current or specified model | +| `jsw` | `juju switch` | Select or identify the current controller and model | + +### Application / unit + +| Alias | Command | Description | +|----------|---------------------------------------------------------------|---------------------------------------------------------------------------| +| `jc` | `juju config` | Get, set, or reset configuration for a deployed application | +| `jde` | `juju deploy --channel=edge` | Deploy a new application or bundle from the edge channel | +| `jd` | `juju deploy` | Deploy a new application or bundle | +| `jra` | `juju run-action` | Queue an action for execution | +| `jraw` | `juju run-action --wait` | Queue an action for execution and wait for results, with optional timeout | +| `jrm` | `juju remove-application` | Remove application | +| `jrm!` | `juju remove-application --force --no-wait` | Remove application forcefully | +| `jrmds` | `juju remove-application --destroy-storage` | Remove application and destroy attached storage | +| `jrmds!` | `juju remove-application --destroy-storage --force --no-wait` | Remove application forcefully, destroying attached storage | +| `jrp` | `juju refresh --path` | Upgrade charm from local charm file | +| `jsa` | `juju scale-application` | Set the desired number of application units | +| `jsh` | `juju ssh` | Initiate an SSH session or execute a command on a Juju target | +| `jshc` | `juju ssh --container` | Initiate an SSH session or execute a command on a given container | +| `jsu` | `juju show-unit` | Displays information about a unit | + +### Storage + +| Alias | Command | Description | +|---------|-------------------------------|-------------------------------------------------| +| `jrs` | `juju remove-storage` | Remove storage | +| `jrs!` | `juju remove-storage --force` | Remove storage even if it is currently attached | + +### Relation + +| Alias | Command | Description | +|-----------|--------------------------------|-------------------------------------------------------------------| +| `jrel` | `juju relate` | Relate two applications | +| `jrmrel` | `juju remove-relation` | Remove an existing relation between two applications. | +| `jrmrel!` | `juju remove-relation --force` | Remove an existing relation between two applications, forcefully. | + +### Cross-model relation (CMR) + +| Alias | Command | Description | +|----------|--------------------|----------------------------------------------------------------| +| `jex` | `juju expose` | Make an application publicly available over the network | +| `jof` | `juju offer` | Offer application endpoints for use in other models | +| `jcon` | `juju consume` | Add a remote offer to the model | +| `jrmsas` | `juju remove-saas` | Remove consumed applications (SAAS) from the model | +| `junex` | `juju unexpose` | Remove public availability over the network for an application | + +### Bundle + +| Alias | Command | Description | +|-------|----------------------|-------------------------------------------------------------| +| `jeb` | `juju export-bundle` | Export the current model configuration as a reusable bundle | + +## Functions + +- `jaddr [unit_num]`: display app or unit IP address. +- `jreld `: display app and unit relation data. +- `wjst [interval_secs] [args_for_watch]`: watch juju status, with optional interval + (default: 5s); you may pass additional arguments to `watch`. diff --git a/plugins/juju/juju.plugin.zsh b/plugins/juju/juju.plugin.zsh new file mode 100644 index 000000000..07f15b392 --- /dev/null +++ b/plugins/juju/juju.plugin.zsh @@ -0,0 +1,127 @@ +# ---------------------------------------------------------- # +# Aliases and functions for juju (https://juju.is) # +# ---------------------------------------------------------- # + +# Load TAB completions +# You need juju's bash completion script installed. By default bash-completion's +# location will be used (i.e. pkg-config --variable=completionsdir bash-completion). +completion_file="$(pkg-config --variable=completionsdir bash-completion 2>/dev/null)/juju" || \ + completion_file="/usr/share/bash-completion/completions/juju" +[[ -f "$completion_file" ]] && source "$completion_file" +unset completion_file + +# ---------------------------------------------------------- # +# Aliases (in alphabetic order) # +# # +# Generally, # +# - `!` means --force --no-wait -y # +# - `ds` suffix means --destroy-storage # +# ---------------------------------------------------------- # +alias jam="juju add-model --config logging-config=\"=WARNING; unit=DEBUG\"\ + --config update-status-hook-interval=\"60m\"" +alias jb='juju bootstrap' +alias jbm='juju bootstrap microk8s' +alias jc='juju config' +alias jdc='juju destroy-controller --destroy-all-models' +alias 'jdc!'='juju destroy-controller --destroy-all-models --force --no-wait -y' +alias jdcds='juju destroy-controller --destroy-all-models --destroy-storage' +alias 'jdcds!'='juju destroy-controller --destroy-all-models --destroy-storage --force --no-wait -y' +alias jdm='juju destroy-model' +alias 'jdm!'='juju destroy-model --force --no-wait -y' +alias jdmds='juju destroy-model --destroy-storage' +alias 'jdmds!'='juju destroy-model --destroy-storage --force --no-wait -y' +alias jde='juju deploy --channel=edge' +alias jd='juju deploy' +alias jdl='juju debug-log --ms' +alias jdlr='juju debug-log --ms --replay' +alias jcon='juju consume' +alias jeb='juju export-bundle' +alias jex='juju expose' +alias jh='juju help' +alias jkc='juju kill-controller -y -t 0' +alias jm='juju models' +alias jmc='juju model-config' +alias jof='juju offer' +alias jra='juju run-action' +alias jraw='juju run-action --wait' +alias jrel='juju relate' +alias jrm='juju remove-application' +alias 'jrm!'='juju remove-application --force --no-wait' +alias jrmds='juju remove-application --destroy-storage' +alias 'jrmds!'='juju remove-application --destroy-storage --force --no-wait' +alias jrmrel='juju remove-relation' +alias 'jrmrel!'='juju remove-relation --force' +alias jrmsas='juju remove-saas' +alias jrp='juju refresh --path' +alias jrs='juju remove-storage' +alias 'jrs!'='juju remove-storage --force' +alias jsa='juju scale-application' +alias jsh='juju ssh' +alias jshc='juju ssh --container' +alias jshm='juju show-model' +alias jssl='juju show-status-log' +alias jstj='juju status --format=json' +alias jst='juju status --relations --storage --color' +alias jsu='juju show-unit' +alias jsw='juju switch' + +# ---------------------------------------------------------- # +# Functions (in alphabetic order) # +# ---------------------------------------------------------- # + +# Get app or unit address +jaddr() { + # $1 = app name + # $2 = unit number (optional) + if (( ! ${+commands[jq]} )); then + echo "jq is required but could not be found." >&2 + return 1 + fi + + if [[ $# -eq 1 ]]; then + # Get app address + juju status "$1" --format=json \ + | jq -r ".applications.\"$1\".address" + elif [[ $# -eq 2 ]]; then + # Get unit address + juju status "$1/$2" --format=json \ + | jq -r ".applications.\"$1\".units.\"$1/$2\".address" + else + echo "Invalid number of arguments." + echo "Usage: jaddr []" + echo "Example: jaddr karma" + echo "Example: jaddr karma 0" + return 1 + fi +} + +# Display app and unit relation data +jreld() { + # $1 = relation name + # $2 = app name + # $3 = unit number + if [[ $# -ne 3 ]]; then + echo "Invalid number of arguments." + echo "Usage: jreld " + echo "Example: jreld karma-dashboard alertmanager 0" + return 1 + fi + + local relid="$(juju run "relation-ids $1" --unit $2/$3)" + if [[ -z "$relid" ]]; then + return 1 + fi + + echo "App data:" + juju run "relation-get -r $relid --app - $2" --unit $2/$3 + echo + echo "Unit data:" + juju run "relation-get -r $relid - $2" --unit $2/$3 +} + +# Watch juju status, with optional interval (default: 5 sec) +wjst() { + local interval="${1:-5}" + shift $(( $# > 0 )) + watch -n "$interval" --color juju status --relations --storage --color "$@" +}