-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps-tools
executable file
·390 lines (354 loc) · 13.1 KB
/
maps-tools
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env bash
#needs to increment on every change!!!
#version: 36
#initialize variables
readonly DEFAULT_DOCKER_IMAGE_REGISTRY="ghcr.io/spring-media"
readonly DEFAULT_DOCKER_IMAGE_NAME=maps-tools
readonly DEFAULT_DOCKER_IMAGE_TAG=latest
readonly DEFAULT_DOCKER_IMAGE_TAG_TESTING=testing
readonly DEFAULT_DOCKER_IMAGE="${DEFAULT_DOCKER_IMAGE_REGISTRY}/${DEFAULT_DOCKER_IMAGE_NAME}:${DEFAULT_DOCKER_IMAGE_TAG}"
readonly DEFAULT_DOCKER_IMAGE_TESTING="${DEFAULT_DOCKER_IMAGE_REGISTRY}/${DEFAULT_DOCKER_IMAGE_NAME}:${DEFAULT_DOCKER_IMAGE_TAG_TESTING}"
readonly DEFAULT_DOCKER_USERNAME=k8s
USE_DOCKER_IMAGE=false
DEFAULT_STAGE=$AWS_PROFILE
USE_AWS_VOLUME=true
CREATE_DAEMON_CONTAINER=false
CONTAINER_ATTACH=false
UPDATE=false
CLEANUP_CONFIGS=false
SESSION_KUBECONFIG=false
K8S_DASHBOARD=false
RELEASE=
USER_NAME=${HOME##*/}
DOCKER_STARTED=false
declare -a ADDITIONAL_CONTAINER_ARGS
volumes[0]="-v $HOME/.aws:/home/$DEFAULT_DOCKER_USERNAME/.aws"
volumes[1]="-v $HOME:/home/$USER_NAME"
volumes[2]="-v $HOME/.ssh:/home/$DEFAULT_DOCKER_USERNAME/.ssh"
volumes[3]="-v $HOME/.helm:/home/$DEFAULT_DOCKER_USERNAME/.helm"
volumes[4]="-v $HOME/.kube:/home/$DEFAULT_DOCKER_USERNAME/.kube"
volumes[5]="-v $HOME/.spacelift:/home/$DEFAULT_DOCKER_USERNAME/.spacelift"
volumes[6]="-v $HOME/.terraformrc:/home/$DEFAULT_DOCKER_USERNAME/.terraformrc"
declare -a ports
# select 5 random ports that are not in use from range 8000-8999
for i in {8000..8999}
do
if nc -z 127.0.0.1 ${i} -w2 2>/dev/null ; then
if [ "${i}" == "8999" ] ; then
echo "all local ports 8000-8999 are in use"
exit
else
continue
fi
else
ports+=("${i}")
fi
if [ "${#ports[@]}" == "5" ]; then
break
fi
done
[ ! -f ~/.terraformrc ] && touch ~/.terraformrc
[ ! -d ~/.spacelift ] && mkdir ~/.spacelift
function print_usage() {
echo
echo "Usage: $0 [-e] [-v] [-h] [-i] [-d] [-a] [-c] [-o] [-u] [-x] [-s] [-r]"
echo '-e: Environment example (AWS_PROFILE NAME: mediasites-stage.dev|mediasites-prod.admin)'
echo '-v: DO NOT USE a Docker volume mount for local .aws, .ssh, .helm and .kube configs inside the container.'
echo '-h: Print this help text'
echo '-i: Ask for a local Docker image to use'
echo '-d: create daemon container that runs in the background, so you can use docker exec to connect to'
echo '-a: attach to daemon container that previously created with -d option'
echo '-c: cleanup/delete your actual .aws/config, .aws/credentials and .kube/config-* files, backup of old files will be created .aws/config-$DATE, e.g. .aws/config-20220504123050'
echo '-o: add aditional cmd parameter to docker run/create command, IMPORTANT: use double quotes! you can use this option more than one time, e.g. -o "-p 2222:22" -o "-e DISPLAY=host.docker.internal:0"'
echo '-u: update maps-tools and docker image (force pull), to get testing versions use "-u testing"'
echo '-x: start with kubernetes dashboard http://0.0.0.0:7777/'
echo '-s: start a container with individual kube context'
echo
exit 0
}
function create_docker_run_command() {
[ $# -ne 1 ] && echo ❌ No image name given to function \"create_docker_run_command\" && return
local docker_string=""
local image_name="$1"
local start_path=""
if [ "${#ports[@]}" == "0" ]; then
ports_string=""
else
for value in "${ports[@]}"; do
ports_string+="-p ${value}:${value} "
port_variable+="${value} "
done
fi
if [[ "${K8S_DASHBOARD}" == true ]]; then
dashboard_string=" -p 7777:7777 -e OCTANT_LISTENER_ADDR=0.0.0.0:7777 -e K8S_DASHBOARD=true "
else
dashboard_string=" -e K8S_DASHBOARD=false "
fi
start_path=$PWD
if [[ $start_path =~ "$HOME" ]]; then
start_path="${start_path//Users/home}"
else
start_path="/home/$USER"
fi
case true in
"$CREATE_DAEMON_CONTAINER")
if [[ $(docker ps -a --format "{{.Names}}" --filter "name=maps-tools") == "maps-tools" ]]
then
docker rm -f maps-tools 2>&1 >/dev/null
fi
if [[ "${USE_AWS_VOLUME}" == true ]]
then
docker_string="docker create --name maps-tools ${volumes[*]} ${ports_string} ${dashboard_string} -e \"PORT_VARIABLE=$port_variable\" -e \"AWS_PROFILE=$DEFAULT_STAGE\" -e \"DEFAULT_SESSION_KUBECONFIG=$SESSION_KUBECONFIG\" -e \"START_PATH=$start_path\" ${ADDITIONAL_CONTAINER_ARGS[@]} --entrypoint \"/init/init_daemon.sh\" $image_name"
else
docker_string="docker create --name maps-tools ${volumes[1]} ${ports_string} ${dashboard_string} -e \"PORT_VARIABLE=$port_variable\" -e \"AWS_PROFILE=$DEFAULT_STAGE\" -e \"DEFAULT_SESSION_KUBECONFIG=$SESSION_KUBECONFIG\" -e \"START_PATH=$start_path\" ${ADDITIONAL_CONTAINER_ARGS[@]} --entrypoint \"/init/init_daemon.sh\" $image_name"
fi
;;
"$CONTAINER_ATTACH")
if [[ $(docker ps --format "{{.Names}}" --filter "name=maps-tools") == "maps-tools" ]]
then
docker_string="docker exec -ti -e \"START_PATH=$start_path\" -e \"AWS_PROFILE=$DEFAULT_STAGE\" -e \"DEFAULT_SESSION_KUBECONFIG=$SESSION_KUBECONFIG\" ${ADDITIONAL_CONTAINER_ARGS[@]} maps-tools /usr/bin/bash --login"
elif [[ $(docker ps -a --format "{{.Names}}" --filter "name=maps-tools") == "maps-tools" ]]
then
docker_string="docker start maps-tools; docker exec -ti -e \"START_PATH=$start_path\" -e \"AWS_PROFILE=$DEFAULT_STAGE\" -e \"DEFAULT_SESSION_KUBECONFIG=$SESSION_KUBECONFIG\" ${ADDITIONAL_CONTAINER_ARGS[@]} maps-tools /init/init.sh"
fi
;;
*)
if [[ "${USE_AWS_VOLUME}" == true ]]
then
docker_string="docker run -it --rm ${volumes[*]} ${ports_string} ${dashboard_string} -e \"PORT_VARIABLE=$port_variable\" -e \"START_PATH=$start_path\" -e \"AWS_PROFILE=$DEFAULT_STAGE\" -e \"DEFAULT_SESSION_KUBECONFIG=$SESSION_KUBECONFIG\" ${ADDITIONAL_CONTAINER_ARGS[@]} $image_name"
else
docker_string="docker run -it --rm ${volumes[1]} ${ports_string} ${dashboard_string} -e \"PORT_VARIABLE=$port_variable\" -e \"START_PATH=$start_path\" -e \"AWS_PROFILE=$DEFAULT_STAGE\" -e \"DEFAULT_SESSION_KUBECONFIG=$SESSION_KUBECONFIG\" ${ADDITIONAL_CONTAINER_ARGS[@]} $image_name"
fi
;;
esac
echo "$docker_string"
}
function run_docker_image() {
local image=${DEFAULT_DOCKER_IMAGE}
if [[ "${CREATE_DAEMON_CONTAINER}" == true && "${CONTAINER_ATTACH}" == true ]]
then
echo "you cannot use -d and -a Option simultaneously"
exit 1
fi
case true in
"$USE_DOCKER_IMAGE")
declare -a docker_images=( $(docker images |grep ${DEFAULT_DOCKER_IMAGE_NAME} |awk '{ print $1 ":" $2 }') )
if [ ${#docker_images[@]} -eq 0 ]; then echo ❌ Image not found. Run \"make build\" or download image from \"GitHub Package Repository\" !; exit 1; fi
PS3="Choose the image to run: "
select docker_image_name in "${docker_images[@]}"; do
if (( $REPLY >= 1 && $REPLY < (( "${#docker_images[@]}" + 1)) )); then
local image="${docker_images[(($REPLY - 1))]}"
local docker_run_command="$(create_docker_run_command "$image")"
eval $docker_run_command
else
echo ❌ Invalid choice && exit 1
fi
break
done
;;
*)
local docker_run_command="$(create_docker_run_command "$image")"
eval $docker_run_command
;;
esac
}
function get_script() {
if [ -f ./maps-tools ]; then
cp -f ./maps-tools /tmp/maps-tools
else
curl -s https://raw.githubusercontent.com/spring-media/maps-tools-install/main/maps-tools${RELEASE} >/tmp/maps-tools
fi
retVal=$?
if [ $retVal -ne 0 ]; then
echo "Error getting version of maps-tools"
exit $retVal
fi
}
function install_script() {
if [ -f /tmp/maps-tools ]; then
echo "If ask, enter your AD password to install maps-tools into /usr/local/bin/maps-tools"
cp -f /tmp/maps-tools /usr/local/bin/maps-tools 1>&- 2>&- || sudo cp -f /tmp/maps-tools /usr/local/bin/maps-tools
chmod 755 /usr/local/bin/maps-tools 1>&- 2>&- || sudo chmod 755 /usr/local/bin/maps-tools
else
echo "no version of maps-tools found for installation"
exit 1
fi
}
function check_for_updates() {
if [ -f /tmp/maps-tools ]; then
local version=$(grep -m 1 "#version:" /usr/local/bin/maps-tools | awk '{print $2}')
local new_version=$(grep -m 1 "#version:" /tmp/maps-tools | awk '{print $2}')
if [ ${version} -lt ${new_version} ]; then
echo "upgrading maps-tools to version ${new_version}"
install_script
else
echo "no upgrade available for maps-tools"
fi
else
echo "no version of maps-tools found for installation"
exit 1
fi
}
function cleanup_tmp() {
if [ -f /tmp/maps-tools ]; then
rm -f /tmp/maps-tools
fi
}
function docker_pull() {
if [[ -z $RELEASE ]]; then
docker pull ${DEFAULT_DOCKER_IMAGE}
elif [[ -n $RELEASE ]]; then
docker pull ${DEFAULT_DOCKER_IMAGE_TESTING}
fi
}
function getopts_get_optional_argument() {
eval next_token=\${$OPTIND}
if [[ -n $next_token && $next_token != -* ]]; then
OPTIND=$((OPTIND + 1))
OPTARG=$next_token
else
OPTARG=""
fi
}
function cleanup_configs() {
[[ -s "${HOME}/.aws/config" ]] && \
{ echo "safe old ${HOME}/.aws/config to ${HOME}/.aws/config-$(date +%Y%m%d%H%M%S)"; mv -f ${HOME}/.aws/config ${HOME}/.aws/config-$(date +%Y%m%d%H%M%S); }
[[ -s "${HOME}/.aws/credentials" ]] && \
{ echo "safe old ${HOME}/.aws/credentials to ${HOME}/.aws/credentials-$(date +%Y%m%d%H%M%S)"; mv -f ${HOME}/.aws/credentials ${HOME}/.aws/credentials-$(date +%Y%m%d%H%M%S); }
[[ -s "${HOME}/.aws/awsctx" ]] && \
{ echo "safe old ${HOME}/.aws/awsctx to ${HOME}/.aws/awsctx-$(date +%Y%m%d%H%M%S)"; mv -f ${HOME}/.aws/awsctx ${HOME}/.aws/awsctx-$(date +%Y%m%d%H%M%S); }
[[ -s "${HOME}/.kube/config" ]] && \
{ echo "safe old ${HOME}/.kube/config to ${HOME}/.kube/config-$(date +%Y%m%d%H%M%S)"; mv -f ${HOME}/.kube/config ${HOME}/.kube/config-$(date +%Y%m%d%H%M%S); }
for i in ${HOME}/.kube/*config-*; do
[[ -s "${i}" ]] && \
{ echo "safe old ${i} to ${i%/*}/backup-${i#*/*config-}-$(date +%Y%m%d%H%M%S)"; mv -f ${i} ${i%/*}/backup-${i#*/*config-}-$(date +%Y%m%d%H%M%S); }
done
}
# Parse flags
while getopts "b:o:he:ivdaucsx" argument; do
case "${argument}" in
b)
# Will be eventually used for additional BUILD ARGs later. Currently, the only BUILD ARG is directly
# handled in "create_docker_run_command"
echo 🛠️ TODO !
;;
e)
DEFAULT_STAGE="${OPTARG}"
;;
v)
USE_AWS_VOLUME=false
;;
i)
USE_DOCKER_IMAGE=true
;;
d)
CREATE_DAEMON_CONTAINER=true
;;
a)
CONTAINER_ATTACH=true
;;
c)
CLEANUP_CONFIGS=true;
;;
s)
SESSION_KUBECONFIG=true;
;;
u)
getopts_get_optional_argument $@
if [[ "${OPTARG}" == "testing" ]]; then
RELEASE="-testing"
fi
UPDATE=true
;;
o)
ADDITIONAL_CONTAINER_ARGS+=("$OPTARG")
;;
x)
K8S_DASHBOARD=true
;;
h | ? | *)
print_usage
;;
esac
done
shift $((OPTIND - 1))
#self installation or upgrade
if [ ! -x /usr/local/bin/maps-tools ]; then
echo "installing maps-tools to /usr/local/bin"
echo ""
get_script
install_script
cleanup_tmp
echo 'add /usr/local/bin to your $PATH environment variable'
echo 'for example:'
echo ''
echo 'bash:'
echo 'echo export PATH="/usr/local/bin:\$PATH" >> ${HOME}/.bashrc && source ${HOME}/.bashrc'
echo 'zsh:'
echo 'echo export PATH="/usr/local/bin:\$PATH" >> ${HOME}/.zshrc && source ${HOME}/.zshrc'
echo ''
exit 0
elif [ -x /usr/local/bin/maps-tools ] && [ "$UPDATE" = true ]; then
get_script
check_for_updates
cleanup_tmp
docker_pull
exit 0
elif [ -x /usr/local/bin/maps-tools ] && [ "${0##*/}" != "maps-tools" ]; then
get_script
check_for_updates
cleanup_tmp
docker_pull
exit 0
fi
#cleanup aws and kube config
if [[ "$CLEANUP_CONFIGS" == "true" ]]; then
while true; do
read -p "Are you really sure to cleanup your .aws and .kube config files?" yn
case $yn in
[Yy]* ) cleanup_configs; break;;
* ) echo "nothing clean up"; break;;
esac
done
exit 0
fi
#check docker is installed
if [ ! -d "/Applications/Rancher Desktop.app" ] && [ ! -d "/Applications/Docker.app" ]; then
echo "No Rancher Desktop or Docker Desktop found, only these are official supported"
fi
if [ `lsof -U | grep docker.sock | wc -l` -gt 0 ]; then
echo "Docker is running"
DOCKER_STARTED=true
else
if [ -e "/Applications/Rancher Desktop.app" ]; then
echo "Starting Rancher Desktop"
open /Applications/Rancher\ Desktop.app
echo "please use the dockerd runtime with Rancher Desktop - containerd is not supported yet"
elif [ -e "/Applications/Docker.app" ]; then
echo "Starting Docker Desktop"
open /Applications/Docker.app
fi
START_CNT=0
STEP_CNT=5
MAX_CNT=$((240 / STEP_CNT))
while [ $START_CNT -lt $MAX_CNT ]; do
sleep $STEP_CNT
START_CNT=$((START_CNT++))
echo -n "."
if [ `lsof -U | grep docker.sock | wc -l` -gt 0 ]; then
echo
echo "Docker was started"
START_CNT=$((++MAX_CNT))
DOCKER_STARTED=true
sleep 5
fi
done
echo
if [ !$DOCKER_STARTED ]; then
echo "Docker couldn't be started"
fi
fi
# Call function to obtain the proper call to docker run
if [ $DOCKER_STARTED ]; then
run_docker_image
fi