forked from RHsyseng/openshift-checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils
133 lines (116 loc) · 2.78 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
#!/usr/bin/env bash
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)
--prechecks path/to/install-config.yaml Executes only prechecks (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
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
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
;;
--prechecks)
# shellcheck disable=2034
CHECKS=0
INFO=0
PRE=1
INSTALL_CONFIG_PATH=${2-}
;;
-?*)
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
}