-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmcp
executable file
·223 lines (192 loc) · 6.13 KB
/
tmcp
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
#!/usr/bin/env bash
# @file tmcp
# Simple switch TMC default provisioners
# @author Alister Lewis-Bowen <alister@lewis-bowen.org>
# Adapted for TMC from kubectx
# @ref https://github.com/ahmetb/kubectx
[[ -n $DEBUG ]] && set -x
set -eou pipefail
IFS=$'\n\t'
SELF_CMD="$0"
TMCP_DIR="$HOME/.config/tmcp"
usage() {
cat <<EOF
USAGE:
tmcp : list the provisioners in the current management cluster
tmcp <NAME> : change the active provisioner of current management cluster
tmcp - : switch to the previous provisioner in this management cluster
tmcp -c, --current : show the current provisioner
tmcp -h,--help : show this message
EOF
}
exit_err() {
echo >&2 "${1}"
exit 1
}
current_context() {
# Current context direct from tmc command...
# !! Need to parse yaml, e.g. tmc system context current | yq e '.full_name.name' -
grep name "$TMC_CONFIG_DIR/current-context" 2>/dev/null | \
cut -d':' -f2 | \
sed -e 's/^[ ]*//'
}
current_mgmt_cluster() {
local cur_ctx mc
cur_ctx="$(current_context)" || exit_err "error getting current context"
# !! Only returns yaml so need to jump through hoops to get default values
mc="$(tmc system context get "${cur_ctx}" | grep MANAGEMENT_CLUSTER_NAME | cut -d':' -f2 | sed -e 's/^[ ]*//')" \
|| exit_err "error getting current management cluster"
if [[ -z "${mc}" ]]; then
echo
else
echo "${mc}"
fi
}
current_provisioner() {
local cur_ctx p
cur_ctx="$(current_context)" || exit_err "error getting current context"
# !! Only returns yaml so need to jump through hoops to get default values
p="$(tmc system context get "${cur_ctx}" | grep PROVISIONER_NAME | cut -d':' -f2 | sed -e 's/^[ ]*//')" \
|| exit_err "error getting current provisioner"
if [[ -z "${p}" ]]; then
echo
else
echo "${p}"
fi
}
get_provisioners() {
# !! Provisioner suncommand not shown in management cluster command help
tmc mc prv list -m "${1}" | grep -v NAME | awk '{print $1}' | sort -n
}
escape_mgmt_cluster_name() {
echo "${1//\//-}"
}
provisioner_file() {
local mc
mc="$(escape_mgmt_cluster_name "${1}")"
echo "${TMCP_DIR}/${mc}"
}
read_provisioner() {
local f
f="$(provisioner_file "${1}")"
[[ -f "${f}" ]] && cat "${f}"
return 0
}
save_provisioner() {
mkdir -p "${TMCP_DIR}"
local f saved
f="$(provisioner_file "${1}")"
saved="$(read_provisioner "${1}")"
if [[ "${saved}" != "${2}" ]]; then
printf %s "${2}" > "${f}"
fi
}
switch_provisioner() {
local mc="${1}"
# !! Unable to set defaults independenty from each other
tmc configure -m "${mc}" -p "${2}"
echo "Active provisioner is \"${2}\".">&2
}
choose_provisioner_interactive() {
# directly calling tmcmc via fzf might fail with a cryptic error like
# "$FZF_DEFAULT_COMMAND failed", so try to see if we can list
# provisioners locally first
local p_list
p_list="$(list_provisioners)"
if [[ -z "$p_list" ]]; then
echo >&2 "error: could not list provisioners"
exit 1
elif [[ "$(echo "${p_list}" | wc -l)" -eq 1 ]]; then
set_provisioner "${p_list}"
return 0
fi
local choice
choice="$(_KUBECTX_FORCE_COLOR=1 \
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
fzf --height=40% --layout=reverse --info=inline --border --ansi --no-preview || true)"
if [[ -z "${choice}" ]]; then
echo 2>&1 "error: you did not choose any of the options"
exit 1
else
set_provisioner "${choice}"
fi
}
set_provisioner() {
local mc prev
mc="$(current_mgmt_cluster)" || exit_err "error getting current management cluster"
prev="$(current_provisioner)" || exit_error "error getting current provisioner"
if grep -q ^"${1}"\$ <(get_provisioners "${mc}"); then
switch_provisioner "${mc}" "${1}"
if [[ "${prev}" != "${1}" ]]; then
save_provisioner "${mc}" "${prev}"
fi
else
echo "error: no provisioner exists with name \"${1}\".">&2
exit 1
fi
}
list_provisioners() {
local yellow darkbg normal
yellow=$(tput setaf 3 || true)
darkbg=$(tput setab 0 || true)
normal=$(tput sgr0 || true)
local cur_ctx_fg cur_ctx_bg
cur_ctx_fg=${TMCTX_CURRENT_FGCOLOR:-$yellow}
cur_ctx_bg=${TMCTX_CURRENT_BGCOLOR:-$darkbg}
local cur p_list
cur="$(current_mgmt_cluster)" || exit_err "error getting current provisioner"
p_list=$(get_provisioners "${cur}") || exit_err "error getting management cluster list"
for c in $p_list; do
if [[ -n "${_TMCTX_FORCE_COLOR:-}" || \
-t 1 && -z "${NO_COLOR:-}" ]]; then
# colored output mode
if [[ "${c}" = "${cur}" ]]; then
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
else
echo "${c}"
fi
else
echo "${c}"
fi
done
}
swap_provisioner() {
local mc p
mc="$(current_mgmt_cluster)" || exit_err "error getting current management cluster"
p="$(read_provisioner "${mc}")"
if [[ -z "${p}" ]]; then
echo "error: No previous provisioner found for current management cluster." >&2
exit 1
fi
set_provisioner "${p}"
}
main() {
if [[ "$#" -eq 0 ]]; then
if [[ -t 1 && -z ${TMCTX_IGNORE_FZF:-} && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
choose_provisioner_interactive
else
list_provisioners
fi
elif [[ "$#" -eq 1 ]]; then
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage
elif [[ "${1}" == "-" ]]; then
swap_provisioner
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
current_provisioner
elif [[ "${1}" =~ ^-(.*) ]]; then
echo "error: unrecognized flag \"${1}\"" >&2
usage
exit 1
elif [[ "${1}" =~ (.+)=(.+) ]]; then
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
else
set_provisioner "${1}"
fi
else
echo "error: too many flags" >&2
usage
exit 1
fi
}
main "$@"