summaryrefslogtreecommitdiff
path: root/ot-tools-user.sh
blob: e93f9c4d7d4e7a848c5708161777f0c50ad8432c (plain)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Some useful scripts to put in your ~/.bashrc in case you are using bash (assuming OT_PREFIX is '~/opentox-ruby')

# Load server config
otconfig() {
  . $HOME/.opentox/config/install/config.sh
  . $OT_PREFIX/install/utils.sh
}

# configure tab completion for ottools
_ottoolcompletion()
{
  word="${COMP_WORDS[COMP_CWORD]}"
  COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '
    all
    algorithm
    compound
    dataset
    feature
    model
    task
    validation
    4store
    ' "$word")) ;
}
complete -o default -F _ottoolcompletion otstart
#complete -o default -F _ottoolcompletion otreload
complete -o default -F _ottoolcompletion otkill
complete -o default -F _ottoolcompletion otcheck

# Display log
alias otless='less $HOME/.opentox/log/development.log'

# Tail log
alias ottail='tail -f $HOME/.opentox/log/development.log'

# Start unicorn
# @param1 [service_name]
# @param2 integer Port
# @example start_unicorn algorithm 8081
start_unicorn() {
  cd $HOME/opentox-ruby/$1
  nice bash -c "nohup unicorn -p $2 >/dev/null 2>&1 &"
#  sleep 0.5
#  check_service $1 online
}

# Start 4Store
# @param1 [backend_name]
# @param2 integer Port
# @example start_4s opentox 9088
start_4s() {
  nice bash -c "nohup $OT_PREFIX/4S/bin/4s-backend $1 >/dev/null 2>&1 &";
  sleep 4;
  nice bash -c "nohup $OT_PREFIX/4S/bin/4s-httpd -p $2 -s -1 $1 >/dev/null 2>&1 &"; #-D for testing
#  sleep 1;
#  check_service four_store online
}

# Start the server
otstart() {
  if [ $# != 1 ]; then
    echo "One argument required: [service_name] or 'all'"
    echo "usage: otstart [all|algorithm|compound|dataset|feature|model|task|validation|4store]"
    return 1
  fi

  otconfig
  otkill $1
  DIR=`pwd`
  case "$1" in
    "algorithm")  otkill compound;
                  otkill feature;
                  otstart compound;
                  otstart feature;
                  start_unicorn $1 8081;;
    "compound")   start_unicorn $1 8082;;
    "dataset")    start_unicorn $1 8083;;
    "feature")    start_unicorn $1 8084;;
    "model")      start_unicorn $1 8085;;
    "task")       start_unicorn $1 8086;;
    "validation") start_unicorn $1 8087;
                  nice bash -c "nohup redis-server $OT_PREFIX/validation/redis-*/redis.conf >/dev/null 2>&1 &";;
    "4store")     start_4s opentox 9088;
                  if ! pgrep -u $USER 4s-backend>/dev/null 2>&1; then echo "Failed to start 4s-backend."; fi
                  if ! pgrep -u $USER 4s-httpd>/dev/null 2>&1; then echo "Failed to start 4s-httpd."; fi;;
                  #otcheck 4store;;
    "all")        otstart 4store;
                  otstart dataset;
                  otstart model;
                  otstart task;
                  otstart algorithm; #compound and feature is included
                  otstart validation;;
                  #sleep 1;
                  #otcheck all;;
    *)            echo "One argument required: [service_name] or 'all'";
                  echo "usage: otstart [all|algorithm|compound|dataset|feature|model|task|validation|4store]";
                  return 1;;
  esac
  cd $DIR
}

# reload unicorn
# @param1 integer Port
# @example reload_unicorn 8081
#reload_unicorn() {
#  for p in `ps x | grep 'unicorn master' | grep $1 | grep -v grep | awk '{print $1}'`; do
#    kill -12 $p
#  done
#  sleep 0.5
#  for p in `ps x | grep 'unicorn master (old)' | grep $1 | grep -v grep | awk '{print $1}'`; do
#    kill -28 $p
#  done
#  sleep 0.5
#  # ToDo: Check if new master is working properly.
#  for p in `ps x | grep 'unicorn master (old)' | grep $1 | grep -v grep | awk '{print $1}'`; do
#    kill -3 $p
#  done
#}

# Reload the server
#otreload() {
#  if [ $# != 1 ]; then
#    echo "One argument required: [service_name] or 'all'"
#    echo "usage: otreload [all|algorithm|compound|dataset|feature|model|task|validation|4store]"
#    return 1
#  fi
#
#  otconfig
#  case "$1" in
#    "algorithm")  reload_unicorn 8081;
#                  check_service "algorithm";;
#    "compound")   reload_unicorn 8082;
#                  check_service "compound";;
#    "dataset")    reload_unicorn 8083;
#                  check_service "dataset";;
#    "feature")    reload_unicorn 8084;
#                  check_service "feature";;
#    "model")      reload_unicorn 8085;
#                  check_service "model";;
#    "task")       reload_unicorn 8086;
#                  check_service "task";;
#    "validation") reload_unicorn 8087;
#                  check_service "validation";;
#    "4store")     #killall 4s-httpd >/dev/null 2>&1;
#                  #killall 4s-backend >/dev/null 2>&1;
#                  #check_service "four_store";;
#                  echo "$1 reload not available yet.";;
#    "all")        otreload algorithm;
#                  otreload compound;
#                  otreload dataset;
#                  otreload feature;
#                  otreload model;
#                  otreload task;
#                  otrelaod validation;;
#                  #otreload 4store;;
#    *)            echo "One argument required: [service_name] or 'all'";
#                  echo "usage: otreload [all|algorithm|compound|dataset|feature|model|task|validation|4store]";
#                  return 1;;
#  esac
#}

# kill unicorn
# @param1 integer Port
# @example kill_unicorn 8081
kill_unicorn() {
  for p in `ps x | grep 'unicorn' | grep $1 | grep -v grep | awk '{print $1}'`; do kill -3 $p; done;
  sleep 0.5
  for p in `ps x | grep 'unicorn' | grep $1 | grep -v grep | awk '{print $1}'`; do echo "Process with PID $p is still running and belongs to port $1. Please wait and run otkill again."; done;
}

# Kill the server
otkill() {
  if [ $# != 1 ]; then
    echo "One argument required: [service_name] or 'all'"
    echo "usage: otkill [all|algorithm|compound|dataset|feature|model|task|validation|4store]"
    return 1
  fi

  otconfig
  case "$1" in
    "algorithm")  kill_unicorn 8081;;
    "compound")   kill_unicorn 8082;;
    "dataset")    kill_unicorn 8083;;
    "feature")    kill_unicorn 8084;;
    "model")      kill_unicorn 8085;;
    "task")       kill_unicorn 8086;;
    "validation") kill_unicorn 8087;
                  bash -c "redis-cli -p $OHM_PORT shutdown >/dev/null 2>&1";;
    "4store")     killall 4s-httpd >/dev/null 2>&1;
                  killall 4s-backend >/dev/null 2>&1;;
    "all")        otkill algorithm;
                  otkill compound;
                  otkill dataset;
                  otkill feature;
                  otkill model;
                  otkill task;
                  otkill validation;
                  otkill 4store;;
    *)            echo "One argument required: [service_name] or 'all'";
                  echo "usage: otkill [all|algorithm|compound|dataset|feature|model|task|validation|4store]";
                  return 1;;
  esac
}

# get service uri
# sets $SERVICE_URI if found in config files
# @param1 string [service_name]
# @example get_service_uri algorithm
get_service_uri() {
  if [ $# != 1 ]; then
    echo "One argument required: [service_name]"
    echo "usage: get_service_uri [algorithm|compound|dataset|feature|model|task|validation|four_store]"
    return 1
  fi
  SERVICE_URI=""

  if [ -f $HOME/.opentox/config/$1.rb ]; then
    SERVICE_URI=`cat $HOME/.opentox/config/$1.rb | grep $1 | grep "uri" | awk -F":uri => " '{print $2}' | awk -F" " '{print $1}' | awk -F"," '{print $1}' |  sed "s/'//g" | sed 's/"//g'`
  fi

  if [ -f $HOME/.opentox/config/default.rb ]; then
    [ -n "$SERVICE_URI" ] || SERVICE_URI=`cat $HOME/.opentox/config/default.rb | grep $1 | grep "uri" | awk -F":uri => " '{print $2}' | awk -F" " '{print $1}' | awk -F"," '{print $1}' |  sed "s/'//g" | sed 's/"//g'`
  fi

  if [ -z "$SERVICE_URI" ]; then
    echo "Cannot find service uri for $1 in config files."
    return 1
  else
    return 0
  fi
}

# Check service
# @param1 string [service_name]
# @param2 (optinal) string ["online"] ; output for running services
# @example check_service algorithm online
check_service() {
  check_utils "curl"
  if [ $# != 1 ] && [ $# != 2 ]; then
    echo "One argument required: [service_name]. Second argument optional."
    echo "usage: check_service [algorithm|compound|dataset|feature|model|task|validation|four_store] [online](optional)"
    return 1
  fi
  get_service_uri $1
  MY_SERVICE="IST service"
  if [[ $1 == "four_store" ]]; then
    SERVICE_URI="$SERVICE_URI""/status/"
    MY_SERVICE="HTTP service"
  fi
  if [ -n "`$CURL -i -k -X HEAD $SERVICE_URI 2>&1 | grep '^HTTP' |grep ' 200'`" ]; then
    if [[ $2 == "online" ]]; then
      echo "$MY_SERVICE $1 is running at $SERVICE_URI."
      return 1
    fi
    return 0
  else
    echo "$MY_SERVICE $1 is NOT available at $SERVICE_URI."
    return 1
  fi
}

#check_all_service_offline() {
#  CHECK_RESULT=`otcheck all`
#  if [ -z "$CHECK_RESULT" ]; then
#    echo "IST service(s) is(are) running."
#    otcheck all
#    return 1
#  else
#    echo "IST services are offline."
#  fi
#}


# Check response of web service
otcheck() {
  if [ $# != 1 ]; then
    echo "One argument required: [service_name] or 'all'"
    echo "usage: otcheck [all|algorithm|compound|dataset|feature|model|task|validation|4store]"
    return 1
  fi

  otconfig
  case "$1" in
    "algorithm")  check_service "algorithm";;
    "compound")   check_service "compound";;
    "dataset")    check_service "dataset";;
    "feature")    check_service "feature";;
    "model")      check_service "model";;
    "task")       check_service "task";;
    "validation") check_service "validation";;
    "4store")     check_service "four_store";;
    "all")        otcheck "algorithm";
                  otcheck "compound";
                  otcheck "dataset";
                  otcheck "feature";
                  otcheck "model";
                  otcheck "task";
                  otcheck "validation";
                  otcheck 4store;;
    *)            echo "One argument required: [service_name] or 'all'";
                  echo "usage: otcheck [all|algorithm|compound|dataset|feature|model|task|validation|4store]";
                  return 1;;
  esac
}

# pull repository
# @param1  service
# @example pull_repo algorithm
pull_repo() {
  otconfig
  cd $OT_PREFIX/$1
  if OUTPUT=`git pull 2>&1`; then echo "Pulled $1."; else echo -e "Pulling $1 failed.\nMessage: $OUTPUT\n";  fi
}

# pulls recent git repoitory of the service
otupdate_repo() {
  if [ $# != 1 ]; then
    echo "One argument required: [service_name] or 'all'"
    echo "usage: otupdate_repo [all|algorithm|compound|dataset|feature|model|opentox-client|opentox-server|opentox-test|task|validation]"
    return 1
  fi
  DIR=`pwd`
  otconfig
  case "$1" in
    "algorithm")       pull_repo algorithm;;
    "compound")        pull_repo compound;;
    "dataset")         pull_repo dataset;;
    "feature")         pull_repo feature;;
    "model")           pull_repo model;;
    "opentox-client")  pull_repo opentox-client;;
    "opentox-server")  pull_repo opentox-server;;
    "opentox-test")    pull_repo opentox-test;;
    "task")            pull_repo task;;
    "validation")      pull_repo validation;;
    "all")             otupdate_repo algorithm;
                       otupdate_repo compound;
                       otupdate_repo dataset;
                       otupdate_repo feature;
                       otupdate_repo model;
                       otupdate_repo opentox-client;
                       otupdate_repo opentox-server;
                       otupdate_repo opentox-test;
                       otupdate_repo task;
                       otupdate_repo validation;;
    *)                 echo "One argument required: [service_name] or 'all'";
                       echo "usage: otupdate_repo [all|algorithm|compound|dataset|feature|model|opentox-client|opentox-server|opentox-test|task|validation]";
                       return 1;;
  esac
  cd $DIR
}