-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run
executable file
·303 lines (267 loc) · 8.01 KB
/
run
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
#!/usr/bin/env bash
#
# v0.3.2
set -e
set -o errexit
set -o nounset
set -o pipefail
# set -o xtrace # for debug proupose
#
#############################################################
# Parsing arguments
#############################################################
#
arg0="${0:-}"
arg1="${1:-}"
arg2="${2:-}"
arg3="${3:-}"
#
#############################################################
# Application Name & Docker Hub Configuration Section
#############################################################
#
# APP_NAME contains it is formed by [hub registry / application name : tag]
__APP_NAME="kvstok"
# for docker registry fill information below
__HUB_REGISTRY="waldirborbajr"
__TAG_LABEL=":latest"
#
#############################################################
# Constants Section
#############################################################
#
# foreground colors
__RED=$(tput setaf 1)
__GREEN=$(tput setaf 2)
__YELLOW=$(tput setaf 3)
__BLUE=$(tput setaf 4)
__MAGENTA=$(tput setaf 5)
__CYAN=$(tput setaf 6)
__WHITE=$(tput setaf 7)
#
# Set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__root="$(cd "$(dirname "${__dir}")" && pwd)" # <-- change this as it depends on your app
#
__DOCKER=${__HUB_REGISTRY}+"/"+${__APP_NAME}+":"+${__TAG_LABEL}
#
__BUILD_START="starting build for"
__BUILD_DONE="${__GREEN}.. build [ok] .. ${__WHITE}"
__BUILD_FAIL="${__RED}.. build [fail] .. ${__WHITE}"
#
#############################################################
# Certificate Functions
#############################################################
#
function gen:cert {
openssl req \
-x510 \
-newkey rsa:4097 \
-keyout ${__APP_NAME}-private.key \
-out ${__APP_NAME}.pem \
-days 366 \
-subj "/C=BR/ST=Paraná/L=Curitiba/O=Pessoal/OU=TI/CN=Desenvolvimento" \
-nodes
}
#
#############################################################
# Stress Test Functions
#############################################################
#
function wrk:request {
if [[ -z "${arg2}" ]]; then
echo -e "Usage:"
echo -e " run ${arg1} host+port+path"
echo -e ""
echo -e "Example:"
echo -e " run ${arg1} 127.0.0.1:1010/ping"
echo -e ""
exit -1
fi
# warmup
reset
echo -e "\n${__RED} warmup of 3s ${__WHITE}\n"
env wrk -t 6 -c 1000 -d 3s http://${arg2}
echo -e "\n${__RED} collecting data 6 threads for 60s ${__WHITE}\n"
# let's go
env wrk -t 6 -c 1000 -d 60s http://${arg2}
echo -e "${__GREEN} [done] ${__WHITE}"
}
function wrk:latency {
if [[ -z ${arg2} ]]; then
echo -e "Usage:"
echo -e " run ${arg1} host+port+path"
echo -e ""
echo -e "Example:"
echo -e " run ${arg1} 127.0.0.1:1010/ping"
echo -e ""
exit -1
fi
# warmup
reset
echo -e "\n${__RED} warmup of 3s ${__WHITE}\n"
env wrk --latency -t 6 -c 1000 -d 3s http://${arg2}
echo -e "\n${__RED} collecting data 6 threads for 60s ${__WHITE}\n"
# let's go
env wrk --latency -t 6 -c 1000 -d 60s http://${arg2}
echo -e "${__GREEN} [done] ${__WHITE}"
}
#
#############################################################
# Podman / Docker Functions
#############################################################
#
function pod:deploy {
echo -e "${__GREEN} -[ Clean <none> images ]- ${__WHITE}"
podman rmi $(podman images | grep "<none>" | awk '{print $4}') --force
echo -e "${__BLUE} [ pull ] ${__WHITE}"
podman pull waldirborbajr/techbot:latest
podman-compose stop ${__DOCKER}
podman-compose rm --force ${__DOCKER}
podman-compose up -d ${__DOCKER}
podman-compose ps
echo -e "${__YELLOW}Generated Run podman-compose ${__WHITE}\n"
}
function pod:run {
# podman run --rm -it "${2:-}" "${2:-}"
podman run --detach --rm --name ${__DOCKER} --publish 9091:9090 ${__DOCKER}
}
function pod:kill {
podman kill ${__DOCKER}
}
function pod:validate {
if [[ -z ${arg2} ]]; then
echo -e "Usage:"
echo -e " run ${arg1} host+port"
echo -e ""
echo -e "Example:"
echo -e " run ${arg1} 127.0.0.1:1010"
echo -e ""
exit -1
fi
curl -v ${arg2}
}
function pod:build {
echo -e "${__BLUE} starting... ${__WHITE}"
podman build -f ./podman/podmanfile -t ${__DOCKER} .
echo -e "${__GREEN} [done] ${__WHITE}"
}
function pod:prune {
echo -e "${__BLUE} starting... ${__WHITE}"
podman system prune -af --volumes
podman ps -a -q | xargs podman rm
podman images -a -q | xargs podman rmi -fa
echo -e "${__GREEN} [done] ${__WHITE}"
}
function pod:dang {
echo -e "${__BLUE} starting... ${__WHITE}"
podman rmi -f $(podman images -q -f dangling=true)
echo -e "${__GREEN} [done] ${__WHITE}"
}
function pod:remove {
echo -e "${__BLUE} starting... ${__WHITE}"
podman rmi -f $(podman ps -a -q)
echo -e "${__GREEN} [done] ${__WHITE}"
}
function podcompose:ps {
podman-compose ps
}
#
#############################################################
# Build Functions
#############################################################
#
function build:windows {
echo -e "${__BLUE} .:: ${__BUILD_START} Windows ::. ${__WHITE}"
CGO_ENABLED=1 GOARCH=amd64 GOOS=windows go build -a -installsuffix cgo -ldflags '-s -w -extldflags "-static"' -trimpath -o ./bin/windows/${__APP_NAME}.exe cmd/main.go
status=$?
if test $status -eq 0
then
echo -e "${__BUILD_DONE} "
else
echo -e "${__BUILD_FAIL} "
fi
}
function build:macos {
echo -e "${__BLUE} .:: ${__BUILD_START} MacOS ::. ${__WHITE}"
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -a -installsuffix cgo -ldflags '-s -w -extldflags "-static"' -trimpath -o ./bin/macos/${__APP_NAME} cmd/cli/main.go
status=$?
if test $status -eq 0
then
echo -e "${__BUILD_DONE} "
else
echo -e "${__BUILD_FAIL} "
fi
}
function build:raspi {
echo -e "${__BLUE} .:: ${__BUILD_START} RaspiberryPi 3/4 ::. ${__WHITE}"
CGO_ENABLED=0 GOARCH=arm GOARM=7 GOOS=linux go build -a -installsuffix cgo -ldflags '-s -w -extldflags "-static"' -trimpath -o ./bin/raspi/${__APP_NAME} cmd/main.go
status=$?
if test $status -eq 0
then
echo -e "${__BUILD_DONE} "
else
echo -e "${__BUILD_FAIL} "
fi
}
function build:linux {
echo -e "${__BLUE} .:: ${__BUILD_START} Linux ::. ${__WHITE}"
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -a -installsuffix cgo -ldflags '-s -w -extldflags "-static"' -trimpath -o ./bin/linux/${__APP_NAME} cmd/cli/main.go
status=$?
if test $status -eq 0
then
echo -e "${__BUILD_DONE} "
else
echo -e "${__BUILD_FAIL} "
fi
}
#
################################################################################
# Install / Uninstall Functions
################################################################################
#
function make:uninstall {
target=$(uname -s)
case ${target} in
Linux*) sudo rm -rf /usr/local/bin/${__APP_NAME};;
Darwin*) sudo rm -rf /usr/local/bin/${__APP_NAME};;
esac
}
function make:install {
clear
target=$(uname -s)
echo -e "${__BLUE}Installing KVStoK for" ${__YELLOW} ${target} ${__WHITE}
case ${target} in
Linux*) cp bin/linux/${__APP_NAME} ~/.local/bin/;;
Darwin*) cp bin/macos/${__APP_NAME} ~/.local/bin/;;
esac
echo -e "${__GREEN} [done] ${__WHITE}"
}
function make:clean {
rm -rf bin/
}
#
################################################################################
# Git Version Functions
################################################################################
#
#
################################################################################
# Help Functions
################################################################################
#
function help {
printf "%s <task> [args]\n\nTasks:\n" "${0}"
compgen -A function | grep -v "^_" | cat -n
printf "\nExtended help:\n Each task has comments for general usage\n"
}
#
################################################################################
# Timestamp on finish
################################################################################
#
# This idea is heavily inspired by: https://github.com/adriancooney/Taskfile
TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"