-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-runner.sh
executable file
·327 lines (288 loc) · 7.4 KB
/
service-runner.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
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
#!/bin/bash
CMD=$1
PROJECT=$2
DEPENDS_OFF=$3
CURRENT_PATH=$(pwd)
function red() {
echo -e "\033[0;31m$*\033[0m"
}
function green() {
echo -e "\033[0;32m$*\033[0m"
}
function parse_yaml {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @ | tr @ '\034')
sed -ne "s|,$s\]$s\$|]|" \
-e "s|^\($s\)\($w\)$s:$s\[$s\(.*\)$s\]|\1\2:\n\1 - \3|;p" "$1" |
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)-$s\(.*\)$s\$|\1$fs$fs\2|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" |
awk -F"$fs" '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]; idx[i]=0}}
if(length($2)== 0){ vname[indent]= ++idx[indent] };
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) { vn=(vn)(vname[i])("_")}
printf("%s%s%s=\%s\n", "'"$prefix"'",vn, vname[indent], $3);
}
}'
}
CONFIG=()
UPDATE_COMMANDS=()
PROJECTS_DEPENDENCY_MAP=()
OIFS="$IFS"
IFS=$'\n'
for ctx in $(parse_yaml config.yaml); do
IFS='='
exp=($ctx)
IFS=$'\n'
key="${exp[0]}"
value="${exp[1]}"
if [[ $key =~ "dependency_map" ]]; then
value=${value//\"/}
PROJECTS_DEPENDENCY_MAP+=($value)
elif [[ $key =~ "update_project_cmd" ]]; then
value=${value//\"/}
UPDATE_COMMANDS+=($value)
else
CONFIG+=("$key::$value")
fi
done
function get_config() {
find_key=$1
for config in "${CONFIG[@]}"; do
IFS='::'
exp=($config)
unset IFS
key="${exp[0]}"
value="${exp[2]}"
if [[ $find_key == "$key" ]]; then
echo "$value"
fi
done
}
IFS="$OIFS"
LOG_FOLDER=$(get_config log_folder)
PROJECTS_PATH=$(get_config project_path)
mkdir -p "$LOG_FOLDER"
function _help() {
echo -e "Usages;\n"
echo -e "project-manager.sh start // start projects"
echo -e "project-manager.sh start <project_name> // start project with dependencies"
echo -e "project-manager.sh start <project_name> depends_off // start project\n"
echo -e "project-manager.sh stop // stops projects."
echo -e "project-manager.sh stop <project_name> // stops project\n"
echo -e "project-manager.sh restart // restart projects"
echo -e "project-manager.sh restart <project_name> // restart project\n"
echo -e "project-manager.sh update // update projects"
echo -e "project-manager.sh update <project_name> // update project\n"
echo -e "project-manager.sh show // lists running projects\n"
echo -e "project-manager.sh watch // show projects logs"
echo -e "project-manager.sh watch <project_namee> // show project logs\n"
echo -e "project-manager.sh remove-logs // remove projects logs file"
echo -e "project-manager.sh remove-logs <project_name> // remove project logs file"
}
function update-project() {
project="$1"
red "[**] ${project}"
check_diff=$(git diff)
if [ "$check_diff" ]; then
red "There are uncommitted changes in the ${project} project"
while true; do
read -r -p "Should I continue anyway? yes/no -> " yn
case $yn in
[Yy]*)
uncommitted_cmd=$(get_config uncommitted_cmd | tr -d '"')
uncommitted_cmd_msg=$(get_config uncommitted_cmd_msg | tr -d '"')
eval "$uncommitted_cmd"
green "$uncommitted_cmd_msg"
break
;;
[Nn]*) return ;;
*) echo "Please answer yes or no." ;;
esac
done
fi
for cmd in "${UPDATE_COMMANDS[@]}"; do
eval "$cmd"
done
}
function update-projects() {
for project_path in "${PROJECTS_PATH}"/*; do
cd "$project_path" || return
project_name=$(basename "$project_path")
update-project "$project_name"
cd "$CURRENT_PATH" || return
done
}
function stop-project() {
project=$1
is_running=$(screen -ls | grep "$project")
if [ "$project" == "" ]; then
red "enter the project you want to stop"
return
elif [ "$is_running" == "" ]; then
red "${project} is not running"
return
fi
green "[*] ${project} is stopping."
screen -S "$project" -X quit >/dev/null
screen -wipe >/dev/null
pgrep -f "${PROJECTS_PATH}/${project}" | xargs kill
}
function stop-projects() {
for dependency_map in "${PROJECTS_DEPENDENCY_MAP[@]}"; do
project=$(echo "$dependency_map" | cut -d ":" -f 1)
stop-project "$project"
done
}
function watch-projects-logs() {
project=$1
if [[ "$project" ]]; then
tail -f "$LOG_FOLDER/$project.log"
else
tail -f "$LOG_FOLDER/"*
fi
}
function remove-projects-logs() {
project=$1
if [[ "$project" ]]; then
rm -rf "$LOG_FOLDER/$project.log"
else
rm -rf "$LOG_FOLDER/"*
fi
}
function start-project() {
project_path=$1
project=$2
is_already_running=$(screen -ls | grep "$project")
if [[ "$is_already_running" && "$DEPENDS_OFF" == "" ]]; then
return
elif [ "$is_already_running" ]; then
red "${project} already running!"
while true; do
read -r -p "Do you want it restarted? yes/no -> " yn
case $yn in
[Yy]*)
stop-project "$project"
break
;;
[Nn]*) return ;;
*) echo "Please answer yes or no." ;;
esac
done
fi
prefix_cmd=$(get_config start_project_cmd)
suffix_cmd=$(get_config start_project_suffix_cmd)
cmd="${prefix_cmd} ${suffix_cmd}"
cmd=${cmd//\"/}
cmd=$(echo "${cmd/\$project_path/$project_path}")
cmd=$(echo "${cmd/\$project/$project}")
cmd=$(echo "${cmd/\$LOG_FOLDER/$LOG_FOLDER}")
sleep 1
screen -SU "$project" -d -m bash -c "${cmd}"
green "[*] ${project} is running."
}
function get-dependency-map() {
project=$1
for dependency_map in "${PROJECTS_DEPENDENCY_MAP[@]}"; do
depend_projects=$(echo "$dependency_map" | cut -d ':' -f 1)
main_project=${depend_projects[0]}
if [[ "$main_project" == "$project" ]]; then
echo "$dependency_map"
fi
done
}
function prepare-start-project() {
project=$1
start_dependency=$2
main_project=$project
if [ "$start_dependency" != "depends_off" ]; then
dependency_map=$(get-dependency-map "$project")
dependency_projects=$(echo "$dependency_map" | tr ":" "\n")
counter=0
for project_name in $dependency_projects:; do
counter=$(($counter + 1))
if [[ $counter -eq 1 ]]; then
continue
fi
project_name=$(echo "${project_name//:/}")
start-project "${PROJECTS_PATH}/$project_name" "$project_name"
done
fi
start-project "${PROJECTS_PATH}/$main_project" "$main_project"
}
function start-projects() {
for dependency_map in "${PROJECTS_DEPENDENCY_MAP[@]}"; do
project=$(echo "$dependency_map" | cut -d ":" -f 1)
prepare-start-project "$project"
done
}
function show-projects() {
for dependency_map in "${PROJECTS_DEPENDENCY_MAP[@]}"; do
project=$(echo "$dependency_map" | cut -d ":" -f 1)
check=$(screen -ls | grep "$project")
if [[ "$check" ]]; then
green "$project running."
fi
done
}
case $CMD in
"start")
if [ "$PROJECT" ]; then
prepare-start-project "$PROJECT" "$DEPENDS_OFF"
else
start-projects
fi
;;
"stop")
if [ "$PROJECT" ]; then
stop-project "$PROJECT"
else
stop-projects
fi
;;
"restart")
if [ "$PROJECT" ]; then
stop-project "$PROJECT"
prepare-start-project "$PROJECT"
else
stop-projects
start-projects
fi
;;
"update")
if [ "$PROJECT" ]; then
project_path="${PROJECTS_PATH}/${PROJECT}"
cd "$project_path" || return
update-project "$PROJECT"
cd "$CURRENT_PATH" || exit
else
update-projects
fi
;;
"watch")
if [ "$PROJECT" ]; then
watch-projects-logs "$PROJECT"
else
watch-projects-logs
fi
;;
"show")
show-projects
;;
"help")
_help
;;
"remove-logs")
if [ "$PROJECT" ]; then
remove-projects-logs "$PROJECT"
else
remove-projects-logs
fi
;;
*)
_help
;;
esac