-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.bosh
186 lines (156 loc) · 4.75 KB
/
lib.bosh
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
#!bin/bash
export BOSH_PROMPT_ENABLED=0
export BOSH_IDENTITY=""
export BOSH_NAME=""
function __bosh_update_target() {
test ${BOSH_PROMPT_ENABLED} -eq 0 && {
return 0
}
local l_color=""
local l_ident="${BOSH_CLIENT}:${BOSH_ENVIRONMENT}:${BOSH_CLIENT_SECRET}:${BOSH_ALL_PROXY}:${BOSH_CA_CERT}"
if [ "${l_ident}" != "${BOSH_IDENTITY}" ]; then
BOSH_IDENTITY="${l_ident}"
if [ "${l_ident}" != "::::" ]; then
local l_name=$(bosh environment --json | jq -r .Tables[].Rows[].name 2>/dev/null | sed 's/bosh-//g')
BOSH_NAME="${l_name}"
fi
fi
if [ -z "${BOSH_NAME}" ]; then
return 0
fi
if [[ "${l_name}" == *"prod"* ]]; then
l_color="lightmagenta"
elif [[ "${l_name}" == *"beta"* ]]; then
l_color="lightcyan"
elif [[ "${l_name}" == *"qa"* ]]; then
l_color="lightyellow"
elif [[ "${l_name}" == *"dev"* ]]; then
l_color="lightyellow"
else
l_color="lightyellow"
fi
promptcmd_add_labels "b:${BOSH_NAME}|${l_color}"
}
function bosh_enable_prompt() {
BOSH_PROMPT_ENABLED=1
promptcmd_push "__bosh_update_target"
}
function bosh_disable_prompt() {
BOSH_PROMPT_ENABLED=0
}
function __bosh_get_deployments() {
local l_env=$1; shift
bosh -e ${l_env} deployments --json | jq -r .Tables[].Rows[].name
}
function __bosh_list_deployments() {
local l_env=$1; shift
local l_cache="${HOME}/.bosh/cache-${l_env}-deployments"
if [ ! -f "${l_cache}" ]; then
__bosh_update_deployments "${l_env}" "${l_cache}"
fi
cat ${l_cache}
}
function __bosh_update_deployments() {
local l_env=$1; shift
local l_cache=$1; shift
__bosh_get_deployments ${l_env} >> "${l_cache}"
}
function __bosh_get_instances() {
local l_env=$1; shift
local l_dep=$1; shift
bosh -e ${l_env} -d ${l_dep} instances --json | jq -r .Tables[].Rows[].instance
}
function __bosh_list_instances() {
local l_env=$1; shift
local l_dep=$1; shift
local l_cache="${HOME}/.bosh/cache-${l_env}-${l_dep}-instances"
if [ ! -f "${l_cache}" ]; then
__bosh_update_instances "${l_env}" "${l_dep}" "${l_cache}"
fi
cat ${l_cache}
}
function __bosh_update_instances() {
local l_env=$1; shift
local l_dep=$1; shift
local l_cache=$1; shift
__bosh_get_instances ${l_env} ${l_dep} >> "${l_cache}"
}
function __bosh_parse_env() {
local args
args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
local cur prev words cword split
_init_completion -s || return
i=1
env=""
while [ "${i}" -le "${cword}" ]; do
if [ "${words[${i}]}" == "-e" ] || [ "${words[${i}]}" == "--environment" ]; then
n=$((i+1))
env=${words[${n}]}
fi
i=$((i+1))
done
echo ${env}
}
function __bosh_parse_deployment() {
local args
args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
local cur prev words cword split
_init_completion -s || return
i=1
env=""
while [ "${i}" -le "${cword}" ]; do
if [ "${words[${i}]}" == "-d" ] || [ "${words[${i}]}" == "--deployment" ]; then
n=$((i+1))
dep=${words[${n}]}
fi
i=$((i+1))
done
echo ${dep}
}
function __bosh_list_static_deployments() {
local l_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/
cat ${l_dir}/lib.bosh.deployments
}
function __bosh_list_static_instances() {
local l_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/
local l_deployment=$1; shift
cat ${l_dir}/lib.bosh.${l_deployment}.instances
}
function __bosh_list_envs() {
bosh environments --json | jq -r '.Tables[].Rows[].alias'
}
__bosh_complete_cli() {
# All arguments except the first one
local args
args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
# shellcheck disable=SC2034
local cur prev words cword split
_init_completion -s || return
if [ "${prev}" == "-e" ] || [ "${prev}" == "--environment" ]; then
COMPREPLY+=($(compgen -W "$(__bosh_list_envs)" -- ${cur}))
elif [ "${prev}" == "-d" ] || [ "${prev}" == "--deployment" ]; then
local env=$(__bosh_parse_env)
if [ ! -z "${env}" ]; then
COMPREPLY+=($(compgen -W "$(__bosh_list_deployments ${env})" -- ${cur}))
else
COMPREPLY+=($(compgen -W "$(__bosh_list_static_deployments ${env})" -- ${cur}))
fi
elif [ "${prev}" == "ssh" ]; then
local dep=$(__bosh_parse_deployment)
local env=$(__bosh_parse_env)
if [ ! -z "${dep}" ]; then
if [ ! -z "${env}" ]; then
COMPREPLY+=($(compgen -W "$(__bosh_list_instances ${env} ${dep})" -- ${cur}))
else
COMPREPLY+=($(compgen -W "$(__bosh_list_static_instances ${dep})" -- ${cur}))
fi
fi
else
# Only split on newlines
local IFS=$'\n'
# Call completion (note that the first element of COMP_WORDS is the executable itself
COMPREPLY+=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}"))
fi
return 0
}
complete -o default -F __bosh_complete_cli bosh