forked from appuio/nagios-plugins-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write-config
executable file
·108 lines (87 loc) · 2.1 KB
/
write-config
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
#!/bin/bash
set -e -u -o pipefail
. /usr/lib/nagios-plugins-openshift/utils
usage() {
echo "Usage: $0 -f <path> -s <server>:<port> -t <token>"
echo
echo 'Write a client configuration file for OpenShift monitoring.'
echo
echo 'Options:'
echo ' -f Config file path'
echo ' -s Address and port of the OpenShift API server'
echo ' -t Bearer token for authentication'
echo ' -C Check whether configuration differs from file given as'\
'destination; exit status 0 indicates no changes; any other status'\
'indicates either a non-existing destination file or trouble'
}
opt_cfgfile=
opt_server=
opt_token=
opt_check=
while getopts 'hf:s:t:C' opt; do
case "$opt" in
h)
usage
exit 0
;;
f) opt_cfgfile="$OPTARG" ;;
s) opt_server="$OPTARG" ;;
t) opt_token="$OPTARG" ;;
C) opt_check=yes ;;
*)
usage >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [[ "$#" -gt 0 ]]; then
usage >&2
exit 1
fi
if [[ -z "$opt_cfgfile" ]]; then
usage >&2
exit 1
fi
if [[ -z "$opt_server" || "$opt_server" != *:* ]]; then
usage >&2
exit 1
fi
if [[ -z "$opt_token" || "${#opt_token}" -lt 10 ]]; then
usage >&2
exit 1
fi
tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"' EXIT
make_nickname() {
# Convert to lowercase
local lc=${1,,*}
# Strip unwanted characters
echo "${lc//[^0-9a-zA-Z]/}"
}
user_nickname=cred-$(make_nickname "$opt_server")
cluster_nickname=cluster-$(make_nickname "$opt_server")
context_nickname="ctx-$cluster_nickname-$user_nickname"
run_oc "$tmpfile" config set-credentials \
"$user_nickname" \
--token="$opt_token" \
--username= \
--password=
run_oc "$tmpfile" config set-cluster \
"$cluster_nickname" \
--server="$opt_server" \
--insecure-skip-tls-verify=false
run_oc "$tmpfile" config set-context \
"$context_nickname" \
--cluster="$cluster_nickname" \
--user="$user_nickname" \
--namespace=
run_oc "$tmpfile" config use-context \
"$context_nickname"
if [[ -n "$opt_check" ]]; then
/usr/bin/cmp --silent "$tmpfile" "$opt_cfgfile"
else
cp "$tmpfile" "$opt_cfgfile"
fi
exit 0
# vim: set sw=2 sts=2 et :