-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.sh
147 lines (122 loc) · 3.86 KB
/
lib.sh
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
# Useful functions for projects using the Ansible suitcase
warn () {
if [ -n "$1" ]; then
echo "$@" >&2
else
cat >&2
fi
}
fatal () {
warn "$@"
exit 1
}
read_interactive () {
if [ ! -t 0 ]; then return 0; fi
local prompt="$1"; shift
local var_to="$1"; shift
local default newval
case "$#" in
0) default="$(eval echo '$'"$var_to")" ;;
1) default="$1" ;;
esac
echo -n "$prompt [$default]: "
read newval
if [ -n "$newval" ]; then
eval "$var_to='$newval'"
else
eval "$var_to='$default'"
fi
case "$(echo '$'"$var_to")" in
"") fatal "No value for $var_to" ;;
*) return 0 ;;
esac
}
ensure_tkgi () {
local clustername="$1"; shift
export KUBECONFIG="$(suitcase_dir)/kubeconfig/kubeconfig"
mkdir -p "$(dirname "$KUBECONFIG")" 2>/dev/null || true
if [ "$(kubectl config current-context 2>/dev/null)" != "$clustername" ]; then
ensure_tkgi_command
do_login_tkgi "$clustername" -a "$1" --ca-cert "$2"
fi
case "$(kubectl get pods -n default 2>&1)" in
*unauthorized*) do_login_tkgi "$clustername" -a "$1" --ca-cert "$2" ;;
esac
if [ "$(kubectl config current-context)" != "$clustername" ]; then
fatal "Unable to retrieve credentials for $clustername"
fi
}
ensure_tkgi_command () {
which tkgi >/dev/null 2>&1 || \
fatal 'Please install the `tkgi` command in your PATH.'
}
do_login_tkgi () {
local clustername="$1"; shift
warn "Please log in to TKGI cluster $clustername using your GASPAR credentials"
read_interactive "GASPAR username" USERNAME "$(whoami)"
tkgi get-kubeconfig "$clustername" -u $USERNAME "$@"
# Tanzu SR 22333578705: the OIDC! It no works!!
(while IFS= read line; do
case "$line" in
*idp-certificate-authority-data*)
if (which openssl && which base64) >/dev/null 2>&1; then
case "$(echo "$line" | sed 's/.*idp-certificate-authority-data: //' | \
base64 -d | openssl x509 -noout -purpose)" in
*"SSL server CA : No"*)
;; # Skip that line
*)
echo "$line" ;;
esac
else
: # Just skip the line - Itstheonlywaytobesure.png
fi ;;
*)
echo "$line" ;;
esac
done) < "$KUBECONFIG" > "$KUBECONFIG.cleaned"
mv "$KUBECONFIG.cleaned" "$KUBECONFIG"
kubectl config use-context "$clustername"
}
ensure_oc_login () {
if ! oc projects >/dev/null 2>&1; then
echo "Please login to openshift:"
oc login
fi
}
suitcase_dir () {
# Careful not to quote $SUITCASE_DIR, as `install.sh` will substitute it
# at suitcase install time:
echo $SUITCASE_DIR
}
ensure_ansible_runtime () {
export PATH="$(suitcase_dir)/bin:$PATH"
# https://github.com/ansible/ansible/issues/32499, https://bugs.python.org/issue35219
case "$(uname -s)" in
Darwin) export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES ;;
esac
}
ensure_confined_helm () {
export HELM_CACHE_HOME="$(suitcase_dir)"/helm/cache
export HELM_CONFIG_HOME="$(suitcase_dir)"/helm/config
for dir in "$HELM_CACHE_HOME" "$HELM_CONFIG_HOME"; do
if [ ! -d "$dir" ]; then
mkdir -p "$dir"
fi
done
}
ansible_flag_set_var_git_current_branch () {
git_current_branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
case "$git_current_branch" in
"") : ;;
*) echo "-e git_current_branch=$git_current_branch" ;;
esac
}
ansible_flag_set_var_caller_pwd () {
echo "-e $1=$OLDPWD"
}
ansible_flag_set_var_homedir () {
echo "-e $1=$PWD"
}
ansible_flag_set_var_suitcase_python_interpreter () {
echo "-e $1=$(suitcase_dir)"/bin/python3-shim
}