forked from semaphoreci/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsem-service
More file actions
executable file
·335 lines (254 loc) · 7.48 KB
/
sem-service
File metadata and controls
executable file
·335 lines (254 loc) · 7.48 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
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
#!/bin/bash
# Misc
DATE_FORMAT='%H:%M %d/%m/%Y'
service::pull_image() {
local image_name=$1
if [[ -z $image_name ]]; then
service:err "Image name can't be empty"
fi
if ! echo $image_name | grep -E '^[[:alnum:]]+(:[[:alnum:]]+.*)?$' &>/dev/null; then
service::err "Invalid image name provided '${image_name}'"
fi
service::duration service::run_cmd docker pull $image_name
}
service::start_mysql() {
service_version="${1:-5.6}"
service::pull_image "mysql:$service_version"
docker_output=$(docker run --net=host --rm -d -e MYSQL_ALLOW_EMPTY_PASSWORD=true --name mysql mysql:$service_version)
# run only if previous command exited successfully
dockerize_output=$(dockerize -wait tcp://0.0.0.0:3306 -timeout 30s 2>&1)
# if not started in 30 seconds print logs and exit
echo "done."
echo "MySQL $service_version is running at 0.0.0.0:3306"
echo "To access it use username 'root' and blank password."
exit 0
}
service::start_postgres() {
service_version="${1:-9.6}"
service::pull_image "postgres:$service_version"
docker_output=$(docker run --net=host --rm --name postgres -e POSTGRES_PASSWORD=semaphore -d postgres:$service_version)
# run only if previous command exited successfully
dockerize_output=$(dockerize -wait tcp://0.0.0.0:5432 -timeout 30s 2>&1)
# if not started in 30 seconds print logs and exit
echo "done."
echo "PostgreSQL $service_version is running at 0.0.0.0:5432"
echo "To access it use username 'postgres' and blank password."
exit 0
}
service::start_redis() {
service_version="${1:-4}"
service::pull_image "redis:$service_version"
docker_output=$(docker run --net=host --rm --name redis -d redis:$service_version)
# run only if previous command exited successfully
dockerize_output=$(dockerize -wait tcp://0.0.0.0:6379 -timeout 30s 2>&1)
# if not started in 30 seconds print logs and exit
echo "done."
echo "Redis $service_version is running at 0.0.0.0:6379"
exit 0
}
service::start_memcached() {
service_version="${1:-1.5}"
service::pull_image "memcached:$service_version"
docker_output=$(docker run --net=host --rm --name memcached -d memcached:$service_version)
# run only if previous command exited successfully
dockerize_output=$(dockerize -wait tcp://0.0.0.0:11211 -timeout 30s 2>&1)
# if not started in 30 seconds print logs and exit
echo "done."
echo "memcached $service_version is running at 0.0.0.0:11211"
exit 0
}
service::start_mongodb() {
service_version="${1:-4.1}"
service::pull_image "mongo:$service_version"
docker_output=$(docker run --net=host --rm --name mongodb -d mongo:$service_version)
# run only if previous command exited successfully
dockerize_output=$(dockerize -wait tcp://0.0.0.0:27017 -timeout 30s 2>&1)
# if not started in 30 seconds print logs and exit
echo "done."
echo "mongodb $service_version is running at 0.0.0.0:27017"
exit 0
}
service::start_elasticsearch() {
service_version="${1:-6.5.1}"
service::pull_image "elasticsearch:$service_version"
docker_output=$(docker run --net=host --rm --name elasticsearch -e "discovery.type=single-node" -d elasticsearch:$service_version)
# run only if previous command exited successfully
dockerize_output=$(dockerize -wait tcp://0.0.0.0:9200 -timeout 30s 2>&1)
# if not started in 30 seconds print logs and exit
echo "done."
echo "elasticsearch $service_version is accessible at 0.0.0.0:9200 and 0.0.0.0:9300"
exit 0
}
service::start() {
service_name=$1
service_version=$2
if [ -z "$service_name" ]; then
service::print_usage
exit 1
fi
echo -n "Starting $service_name..."
case "$service_name" in
"mysql" )
shift
service::start_mysql $service_version
;;
"postgres" )
shift
service::start_postgres $service_version
;;
"redis" )
shift
service::start_redis $service_version
;;
"memcached" )
shift
service::start_memcached $service_version
;;
"mongodb" )
shift
service::start_mongodb $service_version
;;
"elasticsearch" )
shift
service::start_elasticsearch $service_version
;;
* )
service::print_usage
exit 1
;;
esac
}
service::stop() {
local service_name
local service_check_log
service_name=$1
service_check_log=$( service::status $service_name &>/dev/null )
service_status=$?
if [[ $service_status -eq 0 ]]; then
stop_output=$( service::run_cmd docker stop $service_name )
service::log "'${service_name}' stopped."
else
service::log "No running service with name '${service_name}' was found. Skipping."
fi
return 0
}
################################################################################
# Service status
# Globals:
# none
# Arguments:
# service_name
# Returns:
# 0,1...n
################################################################################
service::status() {
local service_name
local docker_ps
service_name=$1
docker_ps=$(docker ps | grep $service_name &>/dev/null )
service_status=$?
if [[ $service_status -eq 0 ]]; then
service::log "${service_name} running."
else
service::log "'${service_name}' not running."
fi
return $service_status
}
################################################################################
# Helper for running commands with the ability to dry-run
# Globals:
# DRYRUN
# Arguments:
# command
# Returns:
# 0,1...n
################################################################################
service::run_cmd() {
local cmd=$@
local out=""
local status=0
if [[ -n $DRYRUN ]]; then
service::log "Dry-running '${cmd}'"
else
out=$($cmd 2>&1)
status=$?
fi
if ! [[ $status -eq 0 ]]; then
service::err "Failed to run command '${cmd}'\n\nReason:\n${out}"
fi
return $status
}
################################################################################
# Gets port information about the running service
# Globals:
# none
# Arguments:
# service_name
# Returns:
# port
################################################################################
service::port_info() {
local service_name
local service_check_log
service_name=$1
port_check_log=$( service::run_cmd sudo netstat -tlpn | grep $service_name 2>&1 )
port_status=$?
if [[ $service_status -eq 0 ]]; then
listens_on=$( echo ${port_check_log} | awk '{print $4}' )
echo "Listens on ${listens_on}"
else
echo "No port found for '${service_name}'."
fi
return 0
}
service::log() {
echo -e "[$(date +"${DATE_FORMAT}")]: $@" >&2
}
service::err() {
echo -e "\n! [$(date +"${DATE_FORMAT}")]: $@\n" >&2
exit 1
}
service::print_usage() {
echo -e "\nsem-service [start|stop|status] [service] [version]\n"
echo "Available services:"
echo "mysql (default: 5.6)"
echo "postgres (default: 9.6)"
echo "redis (default: 4)"
echo "memcached (default: 1.5)"
echo "mongodb (default: 4.5)"
echo "elasticsearch (default: 6.5.1)"
}
service::duration() {
local cmd="$@"
local type=$(echo $4 | tr ":." "_")
local env="prod"
local start
local end
local duration
start=$(date +%s%3N)
$cmd
end=$(date +%s%3N)
duration=$(( $end - $start ))
echo "sem_service.${env}.${type}:${duration}|ms" >> /tmp/semaphore-stats.txt
return 0
}
service::main () {
case "$1" in
"start" )
shift
service::start "$@"
;;
"stop" )
shift
service::stop "$@"
;;
"status" )
shift
service::status "$@"
;;
* )
service::print_usage
;;
esac
}
service::main "$@"