-
Notifications
You must be signed in to change notification settings - Fork 4
/
run
executable file
·169 lines (160 loc) · 3.42 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
#!/bin/bash
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
# error
#
# show an error message and exit
#
# params:
# 1: l_msg - the message to display
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error: $l_msg" 1>&2
exit 1
}
#
# show the usage
#
usage() {
echo "usage: $0 [-d <debugServer>] [-dp <remotePath> <localPath>] [-s] [-h] "
echo " -h: show this usage"
echo " -d: allow remote debugging with the given debugServer"
echo " -dp: allow remote debugging with the given debugPath mapping from remotePath to localPath"
echo " -s: start as server only"
}
export PYTHONPATH=""
#
# open the given url waiting for the given number of seconds
#
# param #1: the url to open
# param #2: the number of loops to wait
# param #3: the sleep time per loop
openUrl() {
local l_url="$1"
local l_loops="$2"
local l_sleep="$3"
local l_count=1
local l_done=0
until [ $l_done -eq 1 ]
do
l_count=$((l_count+1))
if [ "$l_count" -ge "$l_loops" ]
then
echo "giving up to wait for $l_url"
l_done=1
fi
status=$(curl -Is $l_url | head -1)
echo "waiting $l_count/$l_loops for $l_url: $status"
case $status in
*200*OK*) open $l_url
l_done="1" ;;
esac
sleep $l_sleep
done
}
#
# kill the given process by name if it is running
#
# param #1: l_name: the name to search for
killifrunning() {
local l_name="$1"
pgrep -fl "$l_name"
if [ $? -eq 0 ]
then
color_msg $blue "killing running $l_name server"
sudo pkill -f "$l_name"
fi
}
#
# start the server
#
startServer() {
local l_logdir=/var/log/diagrams
local l_logfile=diagrams.log
local l_debugServer="$1"
local l_debugPort="$2"
local l_remotePath="$3"
local l_localPath="$4"
color_msg $blue "starting server only"
if [ ! -d $l_logdir ]
then
sudo mkdir -p $l_logdir
sudo chmod 770 $l_logdir
fi
sudo chown $USER $l_logdir
sudo chgrp $(id -gn) $l_logdir
export PYTHONPATH="."
poptions=""
if [ "$debugServer" != "" ]
then
poptions="--debugServer $l_debugServer --debugPort $l_debugPort"
fi
if [ "$remotePath" != "" ]
then
poptions="$poptions --debugPath $l_remotePath $l_localPath"
fi
echo "setting python options to $poptions"
nohup python3 $pyapp $poptions > $l_logdir/$l_logfile 2>&1 &
color_msg $green "log is at $l_logdir/$l_logfile"
}
pyapp=dgs/webserver.py
killifrunning $pyapp
port=5003
debugServer=""
debugPort=5678
remotePath=""
localPath=""
#commandline option
while [ "$1" != "" ]
do
option=$1
shift
case $option in
-h|--help)
usage
exit 0;;
-d|--debug)
if [ $# -lt 1 ]
then
usage
fi
debugServer="$1"
shift
;;
-dp|--debugPath)
if [ $# -lt 2 ]
then
usage
fi
remotePath="$1"
shift
localPath="$1"
;;
-s)
startServer "$debugServer" "$debugPort" "$remotePath" "$localPath"
exit 0
;;
*)
;;
esac
done
color_msg $blue "starting in client mode"
openUrl "http://localhost:$port" 60 0.5&
export PYTHONPATH="."
sudo python3 $pyapp