-
Notifications
You must be signed in to change notification settings - Fork 44
/
utils
175 lines (153 loc) · 3.79 KB
/
utils
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
#!/usr/bin/env bash
# Global vars
# Set error codes for scripts https://tecadmin.net/how-to-create-own-nagios-plugin-using-bash-shell-script/
# This should probably masked to other values to hilight issues in bash coding, etc
OCOK=0
OCINFO=1
OCERROR=2
OCSKIP=3
OCUNKNOWN=4
PARALLELJOBS="${PARALLELJOBS:=1}"
usage() {
cat <<EOF
Usage: $(basename "$0") [-h]
This script will run a minimum set of checks to an OpenShift cluster
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-l, --list Lists the available checks
-s <script>, --single <script> Executes only the provided script
--no-info Disable cluster info commands (default: enabled)
--no-checks Disable cluster check commands (default: enabled)
--no-ssh Disable ssh-based check commands (default: enabled)
--prechecks path/to/install-config.yaml Executes only prechecks (default: disabled)
--results-only Only shows pass/fail results from checks (default: disabled)
With no options, it will run all checks and info commands with no debug info
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
msg "Something went wrong"
# shellcheck disable=SC2154
if [ "${errors}" -gt 0 ]; then
msg "${RED}Total issues found: ${errors}${NOCOLOR}"
fi
die "Exiting" 9
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
export NOCOLOR='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
# shellcheck disable=2034
export NOCOLOR='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo -e "${1-}"
}
error() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
error "${msg}"
exit "${code}"
}
parse_params() {
while :; do
case "${1-}" in
-h | --help)
usage
;;
-v | --verbose)
set -x
;;
-l | --list)
# shellcheck disable=2034
LIST=1
;;
-s | --single)
# shellcheck disable=2034
SINGLE=1
# shellcheck disable=2034
SCRIPT_PROVIDED=${2}
;;
--no-color)
NO_COLOR=1
;;
--no-info)
# shellcheck disable=2034
INFO=0
;;
--no-checks)
# shellcheck disable=2034
CHECKS=0
;;
--no-ssh)
# shellcheck disable=2034
SSH=0
;;
--prechecks)
# shellcheck disable=2034
CHECKS=0
INFO=0
SSH=0
PRE=1
INSTALL_CONFIG_PATH=${2-}
;;
--results-only)
# shellcheck disable=2034
INFO=0
RESULTS_ONLY=1
;;
-?*)
die "Unknown option: $1"
;;
*)
break
;;
esac
shift
done
#args=("$@")
return 0
}
check_command() {
command -v "${1}" &>/dev/null || die "${RED}${1}${NOCOLOR} command not found"
}
kubeconfig() {
CONTEXT=$(oc whoami 2>/dev/null)
if [ -z "${CONTEXT}" ]; then
die "${RED}KUBECONFIG not set${NOCOLOR}" 1
else
if [ ${INFO} -ne 0 ]; then
msg "Using ${GREEN}${CONTEXT}${NOCOLOR} context"
fi
fi
}
oc_whoami() {
WHOAMI=$(oc whoami 2>/dev/null)
if [ -z "${WHOAMI}" ]; then
die "${RED}OpenShift user not found${NOCOLOR}" 1
else
echo "${WHOAMI}"
fi
}
is_sno() {
local SNO=$(oc get nodes -o json | jq '.items | length')
if [ ${SNO} -eq 1 ]; then
echo 1
else
echo 0
fi
}
ocdebugorwait() {
instances=$(pgrep -f 'oc debug' | wc -l)
while [ "${instances}" != "0" ]; do
# Waiting for oc debug to not be running anymore
sleep $(($RANDOM % 10))
instances=$(pgrep -f 'oc debug' | wc -l)
done
}