-
Notifications
You must be signed in to change notification settings - Fork 2
/
svc_ctl.sh
executable file
·183 lines (147 loc) · 3.9 KB
/
svc_ctl.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
183
#!/usr/bin/env bash
# Name: svc_ctl.sh
# Purpose: Service control for the mediumroast.io
# Copyright: Copyright 2021 and 2022 mediumroast.io. All rights reserved.
# Author(s): Michael Hay, John Goodman
###################################
###
### Environment variables
###
###################################
# Service specific variables
SERVICE_NAME="www.mediumroast.io"
HELLO_EMAIL="hello@mediumroast.io"
IMAGE_NAME="company_dns:latest"
MEMORY_LIMIT="1g"
PORT="8080"
# Colors
NC='\033[0m'
ORANGE='\033[0;33m'
RED='\033[0;31m'
BLUE='\033[0;94m'
GREEN='\033[0;92m'
# Generic variables
SERVICE="company_dns"
SERVICE_DIR="company_dns/"
APP_DIR="app/"
###################################
###
### Generic functions
###
###################################
function check_error () {
EXIT=$1
CMD=$2
if [ $EXIT -eq 0 ]; then
echo -e "${GREEN}ok${NC}"
else
echo "${RED}FAILED${NC}, ${CMD} exited with code: ${EXIT}"
exit -1
fi
}
function print_header () {
HEADER="${1}"
echo -e ">>>> ${ORANGE}BEGIN:${NC} ${BLUE}${HEADER}${NC}"
}
function print_step () {
MSG="${1}"
SEP=" ... "
echo -n -e "${ORANGE}${MSG}${NC}${SEP}"
}
function print_detail () {
DETAIL="${1}"
echo -e "${BLUE}${DETAIL}${NC}"
}
function print_footer () {
FOOTER="${1}"
echo -e ">>>> ${ORANGE}END:${NC} ${BLUE}${FOOTER}${NC}"
}
function bring_down_server () {
FUNC="Bring down service"
STEP="bring_down_server"
print_header "${FUNC}"
docker_image=`docker ps |grep " ${IMAGE_NAME} " |awk '{print $1}'`
print_step "Bring down ${IMAGE_NAME}"
docker kill ${docker_image}
print_footer $FUNC
}
function stop_server () {
FUNC="Stop ${SERVICE}"
STEP="stop_server"
print_header $FUNC
docker_image=`docker ps |grep " ${IMAGE_NAME} " |awk '{print $1}'`
print_step "Stop $IMAGE_NAME"
docker stop ${docker_image}
print_footer $FUNC
}
function start_server () {
FUNC="Start ${SERVICE} in the background"
STEP="start_server"
print_header "${FUNC}"
docker run -d -m ${MEMORY_LIMIT} -p ${PORT}:${PORT} ${SERVICE}
docker_image=`docker ps |grep " ${SERVICE} " |awk '{print $1}'`
}
function build_server () {
FUNC="Build ${SERVICE}"
print_header "${FUNC}"
docker build -t ${IMAGE_NAME} .
print_footer "${FUNC}"
}
function run_foreground () {
FUNC="Run $SERVICE in the foreground"
print_header "${FUNC}"
docker run -m ${MEMORY_LIMIT} -p 8000:8000 ${SERVICE}
print_footer "${FUNC}"
}
function tail_backend () {
FUNC="Tail logs for ${SERVICE}"
print_header "${FUNC}"
docker_image=`docker ps |grep " ${SERVICE} " |awk '{print $1}'`
echo "'${docker_image}'"
docker logs -f ${docker_image}
print_footer "${FUNC}"
}
###################################
###
### Service specific functions
###
###################################
function print_help () {
clear
echo "NAME:"
echo " $0 <sub-command>"
echo ""
echo "DESCRIPTION:"
echo " Control functions to run the ${SERVICE}"
echo ""
echo "COMMANDS:"
echo " help start stop build foreground tail"
echo ""
echo " help - call up this help text"
echo " start - start the service using docker-compose "
echo " stop - stop the docker service"
echo " build - build the docker images for the server"
echo " foreground - run the server in the foreground to watch for output"
echo " tail - tail the logs for a server running in the background"
echo ""
exit -1
}
###################################
###
### Main control shell logic
###
###################################
if [ ! $1 ] || [ $1 == "help" ]; then
print_help
elif [ $1 == "start" ]; then
start_server
elif [ $1 == "stop" ]; then
stop_server
elif [ $1 == "build" ]; then
build_server
elif [ $1 == "foreground" ]; then
run_foreground
elif [ $1 == "tail" ]; then
tail_backend
fi
exit 0