juju.plugin.zsh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # ---------------------------------------------------------- #
  2. # Aliases and functions for juju (https://juju.is) #
  3. # ---------------------------------------------------------- #
  4. # Load TAB completions
  5. # You need juju's bash completion script installed. By default bash-completion's
  6. # location will be used (i.e. pkg-config --variable=completionsdir bash-completion).
  7. completion_file="$(pkg-config --variable=completionsdir bash-completion 2>/dev/null)/juju" || \
  8. completion_file="/usr/share/bash-completion/completions/juju"
  9. [[ -f "$completion_file" ]] && source "$completion_file"
  10. unset completion_file
  11. # ---------------------------------------------------------- #
  12. # Aliases (in alphabetic order) #
  13. # #
  14. # Generally, #
  15. # - `!` means --force --no-wait -y #
  16. # - `ds` suffix means --destroy-storage #
  17. # - `jsh` prefix means juju show-* #
  18. # ---------------------------------------------------------- #
  19. alias j="juju"
  20. alias jam="juju add-model --config logging-config=\"<root>=WARNING; unit=DEBUG\"\
  21. --config update-status-hook-interval=\"60m\""
  22. alias jb='juju bootstrap'
  23. alias jbng='juju bootstrap --no-gui'
  24. alias jbl='juju bootstrap localhost'
  25. alias jblng='juju bootstrap --no-gui localhost'
  26. alias jbm='juju bootstrap microk8s'
  27. alias jbmng='juju bootstrap --no-gui microk8s'
  28. alias jc='juju config'
  29. alias jcld='juju clouds'
  30. alias jclda='juju clouds --all'
  31. alias jctl='juju controllers'
  32. alias jctlr='juju controllers --refresh'
  33. alias jdc='juju destroy-controller --destroy-all-models'
  34. alias 'jdc!'='juju destroy-controller --destroy-all-models --force --no-wait -y'
  35. alias jdcds='juju destroy-controller --destroy-all-models --destroy-storage'
  36. alias 'jdcds!'='juju destroy-controller --destroy-all-models --destroy-storage --force --no-wait -y'
  37. alias jdm='juju destroy-model'
  38. alias 'jdm!'='juju destroy-model --force --no-wait -y'
  39. alias jdmds='juju destroy-model --destroy-storage'
  40. alias 'jdmds!'='juju destroy-model --destroy-storage --force --no-wait -y'
  41. alias jde='juju deploy --channel=edge'
  42. alias jd='juju deploy'
  43. alias jdl='juju debug-log --ms'
  44. alias jdlr='juju debug-log --ms --replay'
  45. alias jcon='juju consume'
  46. alias jeb='juju export-bundle'
  47. alias jex='juju expose'
  48. alias jh='juju help'
  49. alias jkc='juju kill-controller -y -t 0'
  50. alias jm='juju models'
  51. alias jmc='juju model-config'
  52. alias jof='juju offer'
  53. alias jra='juju run-action'
  54. alias jraw='juju run-action --wait'
  55. alias jrel='juju relate'
  56. alias jrm='juju remove-application'
  57. alias 'jrm!'='juju remove-application --force --no-wait'
  58. alias jrmds='juju remove-application --destroy-storage'
  59. alias 'jrmds!'='juju remove-application --destroy-storage --force --no-wait'
  60. alias jrmrel='juju remove-relation'
  61. alias 'jrmrel!'='juju remove-relation --force'
  62. alias jrmsas='juju remove-saas'
  63. alias jrp='juju refresh --path'
  64. alias jrs='juju remove-storage'
  65. alias 'jrs!'='juju remove-storage --force'
  66. alias jsa='juju scale-application'
  67. alias jsha='juju show-application'
  68. alias jshc='juju show-controller'
  69. alias jshm='juju show-model'
  70. alias jshsl='juju show-status-log'
  71. alias jshu='juju show-unit'
  72. alias jssh='juju ssh'
  73. alias jsshc='juju ssh --container'
  74. alias jstj='juju status --format=json'
  75. alias jst='juju status --relations --color'
  76. alias jsts='juju status --relations --storage --color'
  77. alias jsw='juju switch'
  78. # ---------------------------------------------------------- #
  79. # Functions (in alphabetic order) #
  80. # ---------------------------------------------------------- #
  81. # Get app or unit address
  82. jaddr() {
  83. # $1 = app name
  84. # $2 = unit number (optional)
  85. if (( ! ${+commands[jq]} )); then
  86. echo "jq is required but could not be found." >&2
  87. return 1
  88. fi
  89. if [[ $# -eq 1 ]]; then
  90. # Get app address
  91. juju status "$1" --format=json \
  92. | jq -r ".applications.\"$1\".address"
  93. elif [[ $# -eq 2 ]]; then
  94. # Get unit address
  95. juju status "$1/$2" --format=json \
  96. | jq -r ".applications.\"$1\".units.\"$1/$2\".address"
  97. else
  98. echo "Invalid number of arguments."
  99. echo "Usage: jaddr <app-name> [<unit-number>]"
  100. echo "Example: jaddr karma"
  101. echo "Example: jaddr karma 0"
  102. return 1
  103. fi
  104. }
  105. # Destroy all controllers
  106. jclean() {
  107. if (( ! ${+commands[jq]} )); then
  108. echo "jq is required but could not be found." >&2
  109. return 1
  110. fi
  111. local controllers=$(juju controllers --format=json | jq -r '.controllers | keys[]' 2>/dev/null)
  112. if [[ -z "$controllers" ]]; then
  113. echo "No controllers registered"
  114. return 0
  115. fi
  116. echo "This will forcefully destroy all storages, models and controllers."
  117. echo "Controllers to be destroyed:"
  118. echo "$controllers"
  119. if ! read -q '?Are you sure (y/n)? '; then
  120. echo
  121. echo "Aborted."
  122. return 0
  123. fi
  124. echo
  125. for controller in ${=controllers}; do
  126. timeout 2m juju destroy-controller --destroy-all-models --destroy-storage --force --no-wait -y $controller
  127. timeout 2m juju kill-controller -y -t 0 $controller 2>/dev/null
  128. timeout 10s juju unregister $controller 2>/dev/null
  129. done
  130. }
  131. # Display app and unit relation data
  132. jreld() {
  133. # $1 = relation name
  134. # $2 = app name
  135. # $3 = unit number
  136. if [[ $# -ne 3 ]]; then
  137. echo "Invalid number of arguments."
  138. echo "Usage: jreld <relation-name> <app-name> <unit-number>"
  139. echo "Example: jreld karma-dashboard alertmanager 0"
  140. return 1
  141. fi
  142. local relid="$(juju run "relation-ids $1" --unit $2/$3)"
  143. if [[ -z "$relid" ]]; then
  144. return 1
  145. fi
  146. echo "App data:"
  147. juju run "relation-get -r $relid --app - $2" --unit $2/$3
  148. echo
  149. echo "Unit data:"
  150. juju run "relation-get -r $relid - $2" --unit $2/$3
  151. }
  152. # Return Juju current controller
  153. jcontroller() {
  154. local controller="$(awk '/current-controller/ {print $2}' ~/.local/share/juju/controllers.yaml)"
  155. if [[ -z "$controller" ]]; then
  156. return 1
  157. fi
  158. echo $controller
  159. return 0
  160. }
  161. # Return Juju current model
  162. jmodel() {
  163. local yqbin="$(whereis yq | awk '{print $2}')"
  164. if [[ -z "$yqbin" ]]; then
  165. echo "--"
  166. return 1
  167. fi
  168. local model="$(yq e ".controllers.$(jcontroller).current-model" < ~/.local/share/juju/models.yaml | cut -d/ -f2)"
  169. if [[ -z "$model" ]]; then
  170. echo "--"
  171. return 1
  172. fi
  173. echo $model
  174. return 0
  175. }
  176. # Watch juju status, with optional interval (default: 5 sec)
  177. wjst() {
  178. local interval="${1:-5}"
  179. shift $(( $# > 0 ))
  180. watch -n "$interval" --color juju status --relations --color "$@"
  181. }