-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjuju-helpers
More file actions
390 lines (358 loc) · 11.1 KB
/
juju-helpers
File metadata and controls
390 lines (358 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash
unalias jst 2> /dev/null
unset -f jst
function jst() {
if [[ $(juju version) == "2."* ]]; then
juju status --color "$@"
else
juju status --format=tabular
fi
}
unalias jpt 2> /dev/null
unset -f jpt
function jpt() {
if [[ $(juju version) == "2."* ]]; then
watch -tcn0.5 -- juju status --color "$@"
else
watch -tcn0.5 -- juju status --format=tabular
fi
}
unalias jptt 2> /dev/null
unset -f jptt
function jptt() {
watch -cn0.5 'juju status --color -m $(juju-test-model-name $1)'
}
unalias jr-a 2> /dev/null
unalias jr-s 2> /dev/null
unset -f jr-a
function jr-a() {
if [[ $(juju version) == "2."* ]]; then
juju remove-application "$@"
else
juju remove-service "$@"
fi
}
alias jr-s='jr-a'
_PROXY=http://squid.internal:3128
_NO_PROXY="localhost,127.0.0.1,0.0.0.0,ppa.launchpad.net,launchpad.net,10.246.156.0/24,10.152.183.0/24,10.1.0.0/16"
alias dhx='juju dhx'
alias jr-m='juju remove-machine'
alias ja-r='juju add-relation'
alias jr-r='juju remove-relation'
alias ja-u='juju add-unit'
alias jr-u='juju remove-unit'
alias jam='juju add-model --config default-series=focal --config resource-tags=owner=johnsca --config test-mode=true --config automatically-retry-hooks=false --config logging-config="<root>=DEBUG; unit=DEBUG" --config update-status-hook-interval="1m"'
alias jscp='juju scp'
# shellcheck disable=SC2139
alias jproxy="juju model-config juju-http-proxy='$_PROXY' juju-https-proxy='$_PROXY' juju-no-proxy='$_NO_PROXY'"
unset -f jproxyd
function jproxyd() {
juju model-defaults "$1" juju-http-proxy="$_PROXY" juju-https-proxy="$_PROXY" juju-no-proxy="$_NO_PROXY"
}
unset -f jbs
function jbs() {
proxy_opts="--model-default juju-http-proxy='$_PROXY' --model-default juju-https-proxy='$_PROXY' --model-default juju-no-proxy='$_NO_PROXY'"
name="$1"; shift
if [[ -z "$1" || "$1" == --* ]]; then
cloud="$name"
else
cloud="$1"; shift
fi
case "$cloud" in
vsphere)
extra_opts="--model-default datastore=vsanDatastore --model-default primary-network=VLAN_2766 $proxy_opts" # --config caas-image-repo=rocks.canonical.com/cdk/jujusolutions"
;;
google)
extra_opts="--model-default container-networking-method=local --model-default fan-config="
;;
*)
extra_opts=""
;;
esac
cloud_type="$(juju show-cloud --client "$cloud" | yq e '.type' -)"
bootstrap_constraints="cores=2 mem=4G"
if [[ "$cloud_type" == "k8s" ]]; then
bootstrap_constraints=""
fi
#--model-default image-stream=daily \
juju bootstrap "$cloud" "$name" \
--bootstrap-constraints "$bootstrap_constraints" \
--config test-mode=true \
--config mongo-memory-profile=low \
--model-default default-series=focal \
--model-default resource-tags=owner=johnsca \
--model-default test-mode=true \
--model-default automatically-retry-hooks=false \
--model-default logging-config="<root>=DEBUG; unit=DEBUG" \
--model-default update-status-hook-interval="1m" \
$extra_opts \
"$@"
}
function _jdm() {
juju show-model "$1" | grep -q 'cloud: localhost'
lxd=$?
juju destroy-model --destroy-storage -y "$1"
if (( lxd )); then
lxc profile delete juju-"$1"
fi
}
unset -f jdm
function jdm() {
if [[ "$*" == "" ]]; then
_jdm "$(juju models --format=json | jq -r '."current-model"')"
else
for model in "$@"; do
_jdm "$model"
done
fi
}
unset -f unit
function _unit() {
unit=$1
shift
if [[ -z "$unit" ]]; then
# nothing specified, take first errored unit, or no filter
unit="$(juju status | grep '/.*error' | cut -f1 -d' ' | tr -d '*' | head -n1)"
elif ! echo "$unit" | grep -q '[-/]'; then
# application name specified; take first errored unit or first unit
units="$(juju status "$unit" | grep '/[0-9]')"
unit="$(echo "$units" | grep 'error')"
if [[ -z "$unit" ]]; then
unit="$(echo "$units" | head -n1)"
fi
unit="$(echo "$unit" | cut -f1 -d' ' | tr -d '*')"
fi
echo -n "$unit"
}
unset -f _filter
function _filter() {
unit="$(_unit "$1")"
if [[ "$unit" == */* ]]; then
unit="$(echo "unit-$unit" | tr '/' '-')"
fi
if [[ -n "$unit" ]]; then
echo -n "-i $unit"
fi
}
unalias jssh 2> /dev/null
unset -f jssh
function jssh {
unit="$(resolve_leader "$1")"
shift
juju ssh "$unit" -o IdentitiesOnly=yes "$@"
}
unset -f jl
function jl() {
if [[ -n "$1" && $1 != "-"* ]]; then
set -- "-i" "$1" "${@:2}"
fi
if [[ $(juju version) == "2."* ]]; then
color="--color"
else
color=""
fi
juju debug-log $color "$@"
}
unset -f jll
function jll() {
if [[ -n "$1" && $1 != "-"* ]]; then
set -- "-i" "$1" "${@:2}"
fi
if [[ $(juju version) == "2."* ]]; then
color="--color"
else
color=""
fi
juju debug-log $color --replay "$@" | less -R +G -F
}
function jd() {
if [[ -z "$CHARM_BUILD_DIR" ]]; then
>&2 echo "CHARM_BUILD_DIR is not set"
return 1
elif [[ -z "$CBD" ]]; then
CBD="$CHARM_BUILD_DIR"
fi
name=""
charm="$1"
shift
if [[ "$charm" == "-n" ]]; then
charm=""
name="$1"
shift
fi
if [[ -e layer.yaml ]]; then
if [[ -z "$charm" ]]; then
charm=$(yq e .name metadata.yaml)
fi
if [[ "$charm" != *: && -e "$CBD/$charm" ]]; then
charm_path="./$(realpath --relative-to . "$CBD/$charm")"
>&2 echo "Deploying from $charm_path"
name="$charm"
charm="$charm_path"
fi
else
>&2 echo "Deploying from ."
charm=.
fi
juju deploy "$charm" "$name" "$@"
}
function ju() {
if [[ -z "$CHARM_BUILD_DIR" ]]; then
>&2 echo "CHARM_BUILD_DIR is not set"
return 1
elif [[ -z "$JR" ]]; then
JR="$(dirname "$CHARM_BUILD_DIR")"
fi
charm="$1"
shift
if [[ -z "$charm" ]]; then
charm=$(yq e .name metadata.yaml)
fi
if [[ -e "$CBD/$charm" ]]; then
charm_path="./$(realpath --relative-to . "$CBD/$charm")"
>&2 echo "Upgrading from $charm_path"
else
>&2 echo "Unable to determine path for $charm"
return
fi
errored_units="$(juju status --format=json | jq -r '.applications["'"$charm"'"].units | to_entries | map(select(.value["workload-status"].current == "error")) | map(.key) | .[]')"
if [[ -n "$errored_units" ]]; then
juju upgrade-charm --force-units "$charm" --path "$charm_path" "$@"
for unit in $errored_units; do
echo "Retrying hook on $unit"
juju resolved "$unit"
done
else
juju upgrade-charm "$charm" --path "$charm_path" "$@"
fi
}
function js() {
juju switch "$@"
~/.devhelpers/juju-machine-count > ~/.cache/juju-machine-count
}
function jcc() {
mkdir -p "$HOME/juju/envs/$1/"{charms,layers,interfaces,bundles,builds}
jcs~ "$@"
}
function jcs() {
export JR="$HOME/juju/envs/$1"
export CHARM_LAYERS_DIR="$JR/layers"
export CLD="$CHARM_LAYERS_DIR"
export CHARM_INTERFACES_DIR="$JR/interfaces"
export CID="$CHARM_INTERFACES_DIR"
export CHARM_BUILD_DIR="$JR/builds"
export CBD="$CHARM_BUILD_DIR"
export CHARM_CHARMS_DIR="$JR/charms"
export CCD="$CHARM_CHARMS_DIR"
export CHARM_OVERLAYS_DIR="$JR/overlays"
export COD="$CHARM_OVERLAYS_DIR"
export CHARM_BUNDLES_DIR="$JR/bundles"
export CBND="$CHARM_BUNDLES_DIR"
export CHARM_RESOURCES_DIR="$JR/resources"
export CRD="$CHARM_RESOURCES_DIR"
}
unset -f juju-current-model
function juju-current-model {
if [[ -n "$JUJU_MODEL" ]]; then
echo "$JUJU_MODEL"
else
controller="$(juju controllers --format=json | jq -r '.["current-controller"]')"
if [[ -z "$controller" ]]; then
echo "-:-"
else
model="$(juju controllers --format=json | jq -r ".controllers[\"$controller\"][\"current-model\"]" | sed -e 's:^.*/::')"
if [[ "$model" == "null" ]]; then
model="-"
# machines="?"
# units="?"
#else
# status="$(timeout 0.5 juju models | grep '\*')"
# if [[ -n "$status" ]]; then
# machines="$(echo "$status" | awk '{print $5}')"
# units="$(echo "$status" | awk '{print $6}')"
# else
# machines="?"
# units="?"
# fi
fi
echo "$controller:$model"
fi
fi
}
unset -f jcd
function jcd() {
case "$1" in
-i) prefix="$JR"/interfaces; shift;;
-b) prefix="$JR"/bundles; shift;;
-l) prefix="$JR"/layers; shift;;
-o) prefix="$JR"/overlays; shift;;
-c) prefix="$JR"/charms; shift;;
*) prefix="$JR"/charms;;
esac
cd "$prefix/$1" || return
}
unset -f jdest
function jdest() {
if [[ $(juju version) == "2."* ]]; then
controller=${1:-$(juju controllers --format=json | jq -r '."current-controller"')}
juju destroy-controller --destroy-storage --destroy-all-models "$controller"
else
jenv=${1-$(juju switch)}
juju destroy-environment -y "$jenv" "$@"
~/.devhelpers/juju-machine-count > ~/.juju/.machine-count &
fi
}
function render-bundle() {
bundle="${1:-bundle.yaml}"
output="${2:-${bundle//.yaml/.png/}}"
curl -d "$(cat "$bundle")" http://svg.juju.solutions/?output=png > "$output"
}
unset -f jdo
function jdo {
juju run-action --wait "$@"
}
function jrun {
if [[ $# == 1 ]]; then
selector='--all'
elif [[ "$1" == *'/'* ]]; then
selector='--unit'
else
if [[ $(juju version) == "2."* ]]; then
selector='--application'
else
selector='--service'
fi
fi
juju run $selector "$@"
}
alias juju-secret='grep admin-secret ~/.juju/environments/`juju switch`.jenv | sed -e "s/ admin-secret: //"'
function pull-src {
output=$(charm pull-source "$@")
echo "$output"
cd "$(echo "$output" | cut -d' ' -f4)" || return
if git remote -v | grep -Eq 'github.com/juju(-solutions)?'; then
git remote set-url origin "$(git remote -v | head -n1 | awk '{print $2}' | sed -e 's=https://github.com/=git@github.com:=')"
fi
}
# compare two charm store revisions to see if they're the same entity
function comp-rev {
if [[ "$(charm show "$1" hash)" == "$(charm show "$2" hash)" ]]; then
echo Match
fi
}
unset -f resolve_leader
function resolve_leader {
unit="$1"
if [[ "$unit" != */* || "$unit" == *"/leader" ]]; then
app="${unit%/leader}"
unit="$(juju status --format=json | jq -r '.applications["'"$app"'"].units | to_entries | map(select(.value.leader)) | map(.key) | .[]')"
fi
echo "$unit"
}
unset -f jdh
function jdh {
unit="$(resolve_leader "$1")"
shift
juju ssh "$unit" 'echo "export TERM=xterm-256color; stty sane; reset" | sudo tee -a /root/.bashrc >> ~/.bashrc'
juju debug-hooks "$unit" "$@"
}