f045810697d423d108617ccd48bd7a414cb58734

Author
sed-i <82407168+sed-i@users.noreply.github.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

feat(juju): add `jclean` function to destroy all registered controllers (#10426)

Closes #10426

Diff

 1diff --git a/plugins/juju/README.md b/plugins/juju/README.md
 2index 49f8b0d47fe19638f2ca679d414c40dfda5573d8..2417c466902d0d2a7f26aec1582cb57f67bae36b 100644
 3--- a/plugins/juju/README.md
 4+++ b/plugins/juju/README.md
 5@@ -113,5 +113,6 @@ Naming convention:
 6 
 7 - `jaddr <app_name> [unit_num]`: display app or unit IP address.
 8 - `jreld <relation_name> <app_name> <unit_num>`: display app and unit relation data.
 9+- `jclean`: destroy all controllers
10 - `wjst [interval_secs] [args_for_watch]`: watch juju status, with optional interval
11   (default: 5s); you may pass additional arguments to `watch`.
12diff --git a/plugins/juju/juju.plugin.zsh b/plugins/juju/juju.plugin.zsh
13index 07f15b3925f4156bbaffe1cb9e7ecfcdd7db1116..cf89418c674ca3aa5847bec96b997003755ffc82 100644
14--- a/plugins/juju/juju.plugin.zsh
15+++ b/plugins/juju/juju.plugin.zsh
16@@ -95,6 +95,37 @@ jaddr() {
17   fi
18 }
19 
20+# Destroy all controllers
21+jclean() {
22+  if (( ! ${+commands[jq]} )); then
23+    echo "jq is required but could not be found." >&2
24+    return 1
25+  fi
26+
27+  local controllers=$(juju controllers --format=json | jq -r '.controllers | keys[]' 2>/dev/null)
28+  if [[ -z "$controllers" ]]; then
29+    echo "No controllers registered"
30+    return 0
31+  fi
32+
33+  echo "This will forcefully destroy all storages, models and controllers."
34+  echo "Controllers to be destroyed:"
35+  echo "$controllers"
36+
37+  if ! read -q '?Are you sure (y/n)? '; then
38+    echo
39+    echo "Aborted."
40+    return 0
41+  fi
42+
43+  echo
44+  for controller in ${=controllers}; do
45+    timeout 2m juju destroy-controller --destroy-all-models --destroy-storage --force --no-wait -y $controller
46+    timeout 2m juju kill-controller -y -t 0 $controller 2>/dev/null
47+    timeout 10s juju unregister $controller 2>/dev/null
48+  done
49+}
50+
51 # Display app and unit relation data
52 jreld() {
53   # $1 = relation name
54@@ -125,3 +156,4 @@ wjst() {
55   shift $(( $# > 0 ))
56   watch -n "$interval" --color juju status --relations --storage --color "$@"
57 }
58+