-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.sh
182 lines (159 loc) · 4.19 KB
/
functions.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
#!/bin/bash
selectWord(){
local words=${1:?"undefined 'words'"};shift
local sep=${1:?"undefined 'sep'"};shift
local kw=${1:?"undefined 'kw'"};shift
perl -e "print (join qq{\\t}, (grep /^${kw}/, (split qq/${sep}/, qq/${words}/)))"
}
kill2docker(){
local p=${1:?"undefined 'p'"};shift
local kw=${1:?"undefined 'p'"};shift
docker kill $p
}
killAll(){
local comp=$(docker ps -a --format "{{.Names}}"|perl -lne 'chomp;push @a,$_}{print join ":", @a')
local kw=${1:?"missing 'keyword'"};shift
for p in $(selectWord ${comp} ":" ${kw});do
echo kill2docker $p ${kw}
kill2docker $p ${kw}
done
}
isContainerRunning(){
local kw=${1:?"missing 'keyword'"}
names=$(docker ps -f status=running -f name=${kw} --format={{.Names}})
[ -n "${names}" ]
}
selectOption(){
test $# -gt 0
select opt in $*;do
echo ${opt}
break;
done
}
selectHostList(){
local dir=${1:?"missing 'dir'"};shift
dir=${dir%%/}
local oldshopt=$(set +o)
set -e -o pipefail
test -d ${dir}
local n=$(ls ${dir}|wc -l)
test ${n} -gt 0
local selectList=$(ls ${dir}|xargs -i{} basename '{}')
local chosed=""
select opt in ${selectList};do
chosed=${opt}
break;
done
set +vx;eval "${oldshopt}"
echo ${dir}/${chosed}
}
confirm(){
echo -n "Are your sure[yes/no]: "
while : ; do
read input
input=$(perl -e "print qq/\L${input}\E/")
case ${input} in
y|ye|yes)
break
;;
n|no)
echo "operation is cancelled!!!"
exit 0
;;
*)
echo -n "invalid choice, choose again!!! [yes|no]: "
;;
esac
done
}
checkArgument(){
local name=${1:?"missing 'name'"};shift
local arg=${1:?"missing 'arg'"};shift
local alternatives=${1:?"missing 'alternatives'"};shift
if [ -z ${alternatives} ];then
echo "ERROR: empty alternatives for '${name}', value='${arg}'" >&2
exit 1
fi
if test x$(perl -e "print qq/${alternatives}/=~/^\w+(?:\|\w+)*$/")x != x1x;then
echo "ERROR: alternatives must be in format word1|word2|word3..., name='${name}', value='${arg}', alternatives='${alternatives}" >&2
exit 2
fi
if test x$(perl -e "print qq/$arg/=~/^(?:${alternatives})$/")x != x1x; then
echo "ERROR: unmatched argument, name='${name}', value='${arg}', alternatives='${alternatives}'" >&2
exit 1
fi
}
isIn(){
local arg=${1:?"missing 'arg'"};shift
local alternatives=${1:?"missing 'alternatives'"};shift
if [ -z ${alternatives} ];then
echo "ERROR: empty alternatives, value=${arg}" >&2
exit 1
fi
if test x$(perl -e "print qq/${alternatives}/=~/^\w+(?:\|\w+)*$/")x != x1x;then
echo "ERROR: alternatives must be in format word1|word2|word3..., value='${arg}', alternatives='${alternatives}" >&2
exit 2
fi
if test x$(perl -e "print qq/$arg/=~/^(?:${alternatives})$/")x != x1x; then
return 1
else
return 0
fi
}
startsWith(){
local arg=${1:?"missing 'arg'"};shift
local prefix=${1:?"missing 'prefix'"};shift
if [ "x${arg##${prefix}}x" = "x${arg}x" ];then
return 1
else
return 0
fi
}
endsWith(){
local arg=${1:?"missing 'arg'"};shift
local suffix=${1:?"missing 'prefix'"};shift
if [ "x${arg%%${suffix}}x" = "x${arg}x" ];then
return 1
else
return 0
fi
}
green_print(){
echo -e "\e[32;40;1m$*\e[m"
}
red_print(){
echo -e "\e[31;40;1m$*\e[m"
}
yellow_print(){
echo -e "\e[33;100;1m$*\e[m"
}
replace(){
local s=${1:?"undefind 's'"};shift
local a=${1:?"undefined 'a'"};shift
local b=${1:?"undefined 'b'"};shift
perl -e "print qq/${s}/ =~ y/${a}/${b}/r"
}
ensureNumber(){
local num=${1:?"missing arg:'num'"};shift
local isNum=$(perl -e "print qq/ok/ if qq(${num}) =~ /^\\d+$/")
if [ -z ${isNum} ];then
echo "Illegal number: '${num}'" >&2
exit 1
fi
echo ${num}
}
kill_docker_nodes(){
local pattern=${1:?"missing 'pattern'"};shift
set +e +o pipefail
for node in $(docker ps --format {{.Names}} --filter name=${pattern});do
echo docker kill ${node}
docker kill ${node}
done
for node in $(docker ps --format {{.Names}} --filter name=${pattern});do
docker rm ${node}
done
set -e -o pipefail
}
count_docker_nodes(){
docker ps --format {{.Names}} --filter name=${1:?"missing 'pattern'"}|wc -l
}