-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprobe-cli
More file actions
executable file
·99 lines (90 loc) · 2.13 KB
/
probe-cli
File metadata and controls
executable file
·99 lines (90 loc) · 2.13 KB
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
#!/bin/bash
if [[ $0 =~ ^(.*)/([^/]+)$ ]]; then
WORKDIR="${BASH_REMATCH[1]}"
CALLED="${BASH_REMATCH[2]}"
fi
## container name
CNAME="probe"
CLIENTRY="cli" # symlink in container
## list local shell env variables to be passed to container here
LENV=()
LENV+=("PROBE_SERVER_PORT")
## return specific running container env value
CENV=()
cget() {
for ITEM in ${CENV[@]}; do
if [[ ${ITEM} =~ ([_0-9a-zA-Z]+)=(.*)\" ]]; then
if [[ "${BASH_REMATCH[1]}" == "${1}" ]]; then
printf "${BASH_REMATCH[2]}"
fi
fi
done
}
## compare defined local values against running container
lget() {
local PASS=1
for ITEM in ${LENV[@]}; do
local LVAL=${!ITEM}
local CVAL=$(cget ${ITEM})
if [[ -n ${LVAL} && ${LVAL} != ${CVAL} ]]; then
PASS=0
fi
done
printf ${PASS}
}
## force stop and clear any dangling containers
cstop() {
docker rm -f ${CNAME} 2>/dev/null
docker rm -v $(docker ps -qa -f name=${CNAME} -f status=exited) 2>/dev/null
}
## start new instance of container with fresh env vars
cstart() {
## build env string
local DSTRING
for ITEM in ${LENV[@]}; do
local LVAL=${!ITEM}
if [[ -n ${LVAL} ]]; then
DSTRING+=" -e ${ITEM}"
fi
done
#DSTRING+=" -v ${PWD}:/files"
docker rm -v $(docker ps -qa -f name=${CNAME} -f status=exited) 2>/dev/null
docker run -d -P --net host \
--name ${CNAME} \
${DSTRING} \
"apnex/${CNAME}"
}
## handle autocomplete - don't attach psuedo-tty
cexec() {
docker exec ${CNAME} ${CLIENTRY} "${@}"
}
## exec normal user commands
texec() {
docker exec -t ${CNAME} ${CLIENTRY} "${@}"
}
# main
case "${1}" in
'stop') ## force stop
printf "[apnex/${CNAME}] stopping\n" 1>&2
cstop
;;
*) # launch + persist (run + exec)
RUNNING=$(docker ps -q -f name="${CNAME}")
if [[ -n "$RUNNING" ]]; then
CENV=$(docker inspect -f "{{json .Config.Env }}" ${CNAME} | tr "," "\n")
if [[ $(lget) == 0 ]]; then
# env match failed, restart container with new env
cstop
cstart
fi
else
printf "[apnex/${CALLED}] not running - now starting\n" 1>&2
cstart
fi
if [[ "${1}" = "ac" ]]; then
cexec "${@:2}" ## called from bash.complete
else
texec "${@}" ## called from shell
fi
;;
esac