-
Notifications
You must be signed in to change notification settings - Fork 3
/
zoe
executable file
·217 lines (196 loc) · 4.1 KB
/
zoe
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
#!/bin/bash
function err() {
echo "--- Error ---"
echo $@
echo
exit 1
}
which java > /dev/null || err "Please install java 1.7 or later"
which python3 > /dev/null || err "Please install python3"
. etc/environment.sh
export ZOE_HOME=$(pwd)
export ZOE_LOGS=${ZOE_HOME}/logs
export ZOE_VAR=${ZOE_HOME}/var
export ZOE_DOMAIN=private
export PYTHONUNBUFFERED=1
export PERL5LIB=${ZOE_HOME}/lib/perl:${PERL5LIB}
#
# Starts the server
# Returns its PID
#
function launch_server() {
cd ${ZOE_HOME}/server
java -jar gul-zoe-server-assembly-1.0.jar \
-p ${ZOE_SERVER_PORT} \
-d ${ZOE_DOMAIN} \
-g ${ZOE_DOMAIN} \
-c ${ZOE_HOME}/etc/zoe.conf > ${ZOE_LOGS}/server.log 2>&1 &
echo $!
}
#
# Starts an agent
# launch_agent [name]
# Writes the PID into 'var/agent.pid' file
#
function launch_agent() {
name="$1"
AGENTDIR=${ZOE_HOME}/agents/$name
pushd $AGENTDIR > /dev/null 2>&1
# export PYTHONPATH=${ZOE_HOME}/lib/python-dependencies:${ZOE_HOME}/lib/python:${AGENTDIR}/lib:${PYTHONPATH}
if [[ -f "pip-requirements.txt" ]] && [[ ! -f ".pip-requirements-installed" ]]
then
echo "Installing pip requirements for $name"
mkdir -p lib
pip3 install -t lib -r pip-requirements.txt && touch .pip-requirements-installed && echo "Done!"
fi
for script in *
do
if [[ -f "$script" ]] && [[ -x "$script" ]]
then
echo "Launching agent $name ($script)..."
PYTHONPATH=${ZOE_HOME}/lib/python-dependencies:${ZOE_HOME}/lib/python:${AGENTDIR}/lib:${PYTHONPATH} \
./${script} > ${ZOE_LOGS}/$name.log 2>&1 &
sleep 1
fi
done
popd > /dev/null 2>&1
echo "$!" > ${ZOE_VAR}/$name.pid
}
#
# Restarts an agent
# restart_agent [name]
#
function restart_agent() {
name="$1"
stop_agent $name
launch_agent $name
}
#
# Starts the server
#
function server() {
echo "Starting server..."
launch_server > ${ZOE_VAR}/server.pid
sleep 5
}
#
# Starts your beautiful Zoe agents
#
function start() {
echo "Starting agents..."
pushd ${ZOE_HOME}/agents > /dev/null 2>&1
for f in *
do
if [[ -d "$f" ]]
then
popd >/dev/null 2>&1
launch_agent $f
pushd ${ZOE_HOME}/agents > /dev/null 2>&1
fi
done
popd >/dev/null 2>&1
}
#
# Stops your ZOE instance
#
function stop() {
for f in ${ZOE_VAR}/*.pid
do
if [[ -f "$f" ]]
then
pid=$(cat $f)
echo "Stopping process $pid ($f)"
kill "$pid"
rm "$f"
fi
done
}
#
# Stops a single Zoe agent
# stop_agent [name]
#
function stop_agent() {
name="$1"
f="${ZOE_VAR}/$name.pid"
if [[ -f "$f" ]]
then
pid=$(cat $f)
echo "Stopping process $pid ($f)"
kill "$pid"
rm "$f"
fi
}
#
# Shows the zoe running processes
#
function status() {
for f in ${ZOE_VAR}/*.pid
do
if [[ -f "$f" ]]
then
name=$(basename $f)
name=${name%%.pid}
pid=$(cat $f)
found=$(ps -p $pid)
r=$?
if [[ "$r" == "0" ]]
then
echo "ALIVE $name (pid $pid)"
else
echo "DEAD! $name"
fi
fi
done
}
#
# Launches a python shell with the zoe libs and their dependencies loaded
#
function launch_python_shell() {
TMP=/tmp/zoe_shell.py
echo "
import zoe
import os
import sys
import pprint
" > $TMP
echo "Launching python3 interactive shell with path $PYTHONPATH"
PYTHONSTARTUP=$TMP python3
}
#
# Magic starts here
# ./zoe.sh [start | stop]
#
case "$1" in
"server" )
server
;;
"start" )
server
start
;;
"stop" )
stop
;;
"status" )
status
;;
"restart" )
stop
start
;;
"launch-agent" )
launch_agent "$2"
;;
"stop-agent" )
stop_agent "$2"
;;
"restart-agent" )
restart_agent "$2"
;;
"python" )
launch_python_shell
;;
* )
echo "usage: ./zoe.sh server | start | stop | status | restart | launch-agent <name> | stop-agent <name> | restart-agent <name> | python"
;;
esac