Bug 51 fixedgit status
[osm/UI.git] / skyquake / scripts / launch_ui.sh
1 #!/bin/bash
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 usage() {
20 echo "usage: launch_ui.sh [--enable-https --keyfile-path=<keyfile_path> --certfile-path=<certfile-path>]"
21 }
22
23 function handle_received_signal() {
24 forever stopall
25 echo "Stopped Skyquake and API servers started with forever"
26 exit
27 }
28
29
30 start_servers() {
31 cd $THIS_DIR
32 echo "Stopping any previous instances of Skyquake and API servers started with forever"
33 forever stopall
34
35
36 echo "Running Node.js Skyquake server. HTTPS Enabled: ${ENABLE_HTTPS}"
37 cd ..
38 if [ ! -z "${ENABLE_HTTPS}" ]; then
39 forever start -a -l /dev/null -o out.log -e err.log skyquake.js --enable-https --keyfile-path="${KEYFILE_PATH}" --certfile-path="${CERTFILE_PATH}"
40 else
41 forever start -a -l /dev/null -o out.log -e err.log skyquake.js
42 fi
43 }
44
45 function extract_node_modules() {
46 tar xf node_modules.tar
47 touch timestamp.txt
48 }
49
50 function handle_plugin_node_modules() {
51 cd $THIS_DIR
52 echo "Handling plugin node modules"
53
54 cd ../plugins
55 for dir in */; do
56 echo "Checking plugin "${dir}" for newer node_modules"
57 cd ${dir}
58 if [ ! -f timestamp.txt ]; then
59 echo "timestamp file not found ... node_modules need to be expanded and timestamp needs to be touched"
60 extract_node_modules
61 else
62 echo "Checking if node_modules.tar has a newer timestamp than timestamp.txt"
63 if [[ node_modules.tar -nt timestamp.txt ]]; then
64 echo "node_modules.tar is newer than timestamp ... node modules need to be expanded and timestamp needs to be touched"
65 extract_node_modules
66 else
67 echo "node_modules.tar is older than timestamp ... nothing needs to be done"
68 fi
69 fi
70 cd ..
71 echo "Checking plugin "${dir}" for newer node_modules ...done"
72 done
73 }
74
75
76 # Begin work
77 for i in "$@"
78 do
79 case $i in
80 -k=*|--keyfile-path=*)
81 KEYFILE_PATH="${i#*=}"
82 shift # past argument=value
83 ;;
84 -c=*|--certfile-path=*)
85 CERTFILE_PATH="${i#*=}"
86 shift # past argument=value
87 ;;
88 -h|--help)
89 usage
90 exit
91 ;;
92 -e|--enable-https)
93 ENABLE_HTTPS=YES
94 shift # past argument=value
95 ;;
96 *)
97 # unknown option
98 ;;
99 esac
100 done
101
102 if [[ ! -z "${ENABLE_HTTPS}" ]]; then
103 if [ -z "${KEYFILE_PATH}" ] || [ -z "${CERTFILE_PATH}" ]; then
104 usage
105 exit
106 fi
107 fi
108
109
110 # change to the directory of this script
111 THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
112
113 # Call function to handle tarred node_modules as cpack+RPM cannot handle a lot of files
114 handle_plugin_node_modules
115
116 # Call function to start web and API servers
117 start_servers
118
119
120 # Ensure that the forever script is stopped when this script exits
121 trap "echo \"Received EXIT\"; handle_received_signal" EXIT
122 trap "echo \"Received SIGINT\"; handle_received_signal" SIGINT
123 trap "echo \"Received SIGKILL\"; handle_received_signal" SIGKILL
124 trap "echo \"Received SIGABRT\"; handle_received_signal" SIGABRT
125 trap "echo \"Received SIGQUIT\"; handle_received_signal" SIGQUIT
126 trap "echo \"Received SIGSTOP\"; handle_received_signal" SIGSTOP
127 trap "echo \"Received SIGTERM\"; handle_received_signal" SIGTERM
128 trap "echo \"Received SIGTRAP\"; handle_received_signal" SIGTRAP
129
130 # Keep this script in the foreground so the system doesn't think that the
131 # server crashed.
132 while true; do
133 sleep 5
134 done