-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgek8s-common.sh
215 lines (185 loc) · 5.17 KB
/
gek8s-common.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
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
#!/bin/bash
# set the current version number
VERSION="0.1"
# initialize the bash environment if needed
# . /Archive/Software/Modules/3.2.10/init/bash
# portable version of abspath
function abspath() {
local path="${*}"
if [[ -d "${path}" ]]; then
echo "$( cd "${path}" >/dev/null && pwd )"
else
echo "$( cd "$( dirname "${path}" )" >/dev/null && pwd )/$(basename "${path}")"
fi
}
function script_dir() {
echo "$(dirname $(abspath ${BASH_SOURCE[0]}))"
}
function print_version() {
echo "${VERSION}"
}
function log() {
echo -e "${@}" >&2
}
function debug_log() {
if [[ -n "${DEBUG:-}" ]]; then
echo -e "DEBUG: ${@}" >&2
fi
}
function log_allowed_environment_config_properties() {
if [[ -n ${DEBUG:-} ]]; then
debug_log "\nAllowed config properties..."
for param in ${gek8s_allowed_config_properties}; do
debug_log "${param} => ${!param}"
done
debug_log "DONE"
fi
}
function log_config_properities(){
if [[ -n ${DEBUG:-} ]]; then
debug_log "Config properties..."
for v in "${config_properties[@]}"; do
debug_log "${v}"
done
debug_log "DONE"
fi
}
function error_log() {
echo -e "ERROR: ${@}" >&2
}
function error_trap() {
error_log "Error at line ${BASH_LINENO[1]} running the following command:\n\n\t${BASH_COMMAND}\n\n"
error_log "Stack trace:"
for (( i=1; i < ${#BASH_SOURCE[@]}; ++i)); do
error_log "$(printf "%$((4*$i))s %s:%s\n" " " "${BASH_SOURCE[$i]}" "${BASH_LINENO[$i]}")"
done
exit 2
}
trap error_trap ERR
function usage_error() {
if [[ $# > 0 ]]; then
echo -e "ERROR: ${@}" >&2
fi
help
exit 2
}
# load global settings
set -o allexport
source "$(script_dir)/gek8s-settings.sh"
set +o allexport
#
host_list=""
# Collect arguments to be passed on to the next program in an array, rather than
# a simple string. This choice lets us deal with arguments that contain spaces.
positional_parameters=()
# configuration properties from cmd options
config_properties=()
# parse arguments
while [ -n "${1-}" ]; do
# Copy so we can modify it (can't modify $1)
OPT="$1"
# Detect argument termination
if [ x"$OPT" = x"--" ]; then
shift
for OPT ; do
# append to array
positional_parameters+=("$OPT")
done
break
fi
# Parse current opt
while [ x"$OPT" != x"-" ] ; do
case "$OPT" in
-h )
help
exit 0
;;
-f )
gek8s_config_file="${OPT#*=}"
shift
;;
--hosts )
host_list="${2}"
shift
;;
--hosts-file )
hosts_file="${OPT#*=}"
shift
;;
-v )
config_properties+=("${2}")
shift
;;
* )
# append to array
positional_parameters+=("$OPT")
break
;;
esac
# Check for multiple short options
# NOTICE: be sure to update this pattern to match valid options
NEXTOPT="${OPT#-[cfr]}" # try removing single short opt
if [ x"$OPT" != x"$NEXTOPT" ] ; then
OPT="-$NEXTOPT" # multiple short opts, keep going
else
break # long form, exit inner loop
fi
done
# move to the next param
shift
done
# Only for debugging
log_config_properities
# check whether the config-file exists
if [[ ! -f "${gek8s_config_file}" ]]; then
error_log "Config file ${gek8s_config_file} doen't exist!"
exit 1
fi
# Save current environment variables
debug_log "Saving environment config properties..."
environment_config_properties=""
for v in ${gek8s_allowed_config_properties}; do
if [[ -n "${!v}" ]]; then
cp="${v}"="${!v}"
environment_config_properties="${environment_config_properties} ${cp}"
debug_log "${cp}"
fi
done
debug_log "DONE"
# load configuration from file
set -o allexport
source "${gek8s_config_file}"
set +o allexport
# override properties on configuration file with the existing environment
debug_log "Restore existing environment variables..."
for v in ${environment_config_properties}; do
debug_log "${v%=*}"="${v#*=}"
export "${v%=*}"="${v#*=}"
done
debug_log "DONE"
# Only for debugging
log_allowed_environment_config_properties
# override default configuration by setting environment parameters
for v in "${config_properties[@]}"; do
export "${v%=*}"="${v#*=}"
done
# Only for debugging
log_allowed_environment_config_properties
# write node config file
debug_log "Writing node configuration parameters..."
parameters=""
for param in ${gek8s_allowed_config_properties}; do
debug_log "Adding ${param}=${!param}"
parameters="${parameters} ${param}=${!param}"
done
debug_log "DONE"
# set host list
if [[ -f ${hosts_file} ]]; then
for h in $(cat ${hosts_file}); do
host_list="${hosts_file}${h},"
done
fi
export host_list
export config_properties
export positional_parameters
export environment_config_properties