Reduce the fragility of the Sanity Test by not trying to access anything outside...
[controller.git] / opendaylight / distribution / opendaylight / src / main / resources / run.sh
1 #!/bin/bash
2
3 platform='unknown'
4 unamestr=`uname`
5 if [[ "$unamestr" == 'Linux' ]]; then
6    platform='linux'
7 elif [[ "$unamestr" == 'Darwin' ]]; then
8    platform='osx'
9 fi
10
11 if [[ $platform == 'linux' ]]; then
12    fullpath=`readlink -f $0`
13
14    [[ -z ${JAVA_HOME} ]] && [[ -x "/usr/lib/jvm/java-1.7.0" ]] && export JAVA_HOME=`/usr/lib/jvm/java-1.7.0`;
15
16 elif [[ $platform == 'osx' ]]; then
17    TARGET_FILE=$0
18    cd `dirname $TARGET_FILE`
19    TARGET_FILE=`basename $TARGET_FILE`
20
21    # Iterate down a (possible) chain of symlinks
22    while [ -L "$TARGET_FILE" ]
23    do
24        TARGET_FILE=`readlink $TARGET_FILE`
25        cd `dirname $TARGET_FILE`
26        TARGET_FILE=`basename $TARGET_FILE`
27    done
28
29    # Compute the canonicalized name by finding the physical path
30    # for the directory we're in and appending the target file.
31    PHYS_DIR=`pwd -P`
32    RESULT=$PHYS_DIR/$TARGET_FILE
33    fullpath=$RESULT
34
35    [[ -z ${JAVA_HOME} ]] && [[ -x "/usr/libexec/java_home" ]] && export JAVA_HOME=`/usr/libexec/java_home -v 1.7`;
36
37 fi
38
39 [[ -z ${JAVA_HOME} ]] && echo "Need to set JAVA_HOME environment variable" && exit -1;
40 [[ ! -x ${JAVA_HOME}/bin/java ]] && echo "Cannot find an executable \
41 JVM at path ${JAVA_HOME}/bin/java check your JAVA_HOME" && exit -1;
42
43 basedir=`dirname ${fullpath}`
44
45 function usage {
46     echo "Usage: $0 [-jmx] [-jmxport <num>] [-debug] [-debugsuspend] [-debugport <num>] [-start [<console port>]] [-stop] [-status] [-console] [-help] [<other args will automatically be used for the JVM>]"
47     exit 1
48 }
49
50 if [ -z ${TMP} ]; then
51     pidfile="/tmp/opendaylight.PID"
52 else
53     pidfile="${TMP}/opendaylight.PID"
54 fi
55 debug=0
56 debugsuspend=0
57 debugport=8000
58 debugportread=""
59 startdaemon=0
60 daemonport=2400
61 daemonportread=""
62 jmxport=1088
63 jmxportread=""
64 startjmx=0
65 stopdaemon=0
66 statusdaemon=0
67 consolestart=1
68 dohelp=0
69 extraJVMOpts=""
70 unknown_option=0
71 while true ; do
72     case "$1" in
73         -debug) debug=1; shift ;;
74         -jmx) startjmx=1; shift ;;
75         -debugsuspend) debugsuspend=1; shift ;;
76         -debugport) shift; debugportread="$1"; if [[ "${debugportread}" =~ ^[0-9]+$ ]] ; then debugport=${debugportread}; shift; else echo "-debugport expects a number but was not found"; exit -1; fi;;
77         -jmxport) shift; jmxportread="$1"; if [[ "${jmxportread}" =~ ^[0-9]+$ ]] ; then jmxport=${jmxportread}; shift; else echo "-jmxport expects a number but was not found"; exit -1; fi;;
78         -start) startdaemon=1; shift; daemonportread="$1"; if [[ "${daemonportread}" =~ ^[0-9]+$ ]] ; then daemonport=${daemonportread}; shift; fi;;
79         -stop) stopdaemon=1; shift ;;
80         -status) statusdaemon=1; shift ;;
81         -console) shift ;;
82         -help) dohelp=1; shift;;
83         -D*) extraJVMOpts="${extraJVMOpts} $1"; shift;;
84         -X*) extraJVMOpts="${extraJVMOpts} $1"; shift;;
85         "") break ;;
86         *) echo "Unknown option $1"; unknown_option=1; shift ;;
87     esac
88 done
89
90 # Unknown Options and help
91 if [ "${unknown_option}" -eq 1 ]; then
92     usage
93 fi
94
95 if [ "${dohelp}" -eq 1 ]; then
96     usage
97 fi
98
99 # Validate debug port
100 if [[ "${debugport}" -lt 1024 ]] || [[ "${debugport}" -gt 65535 ]]; then
101     echo "Debug Port not in the range [1024,65535] ${debugport}"
102     exit -1
103 fi
104
105 # Validate daemon console port
106 if [[ "${daemonport}" -lt 1024 ]] || [[ "${daemonport}" -gt 65535 ]]; then
107     echo "Daemon console Port not in the range [1024,65535] value is ${daemonport}"
108     exit -1
109 fi
110
111 # Validate jmx port
112 if [[ "${jmxport}" -lt 1024 ]] || [[ "${jmxport}" -gt 65535 ]]; then
113     echo "JMX Port not in the range [1024,65535] value is ${jmxport}"
114     exit -1
115 fi
116
117 # Debug options
118 if [ "${debugsuspend}" -eq 1 ]; then
119     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${debugport}"
120 elif [ "${debug}" -eq 1 ]; then
121     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debugport}"
122 fi
123
124 # Add JMX support
125 if [ "${startjmx}" -eq 1 ]; then
126     extraJVMOpts="${extraJVMOpts} -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=${jmxport} -Dcom.sun.management.jmxremote"
127 fi
128
129 ########################################
130 # Now add to classpath the OSGi JAR
131 ########################################
132 CLASSPATH=${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
133 FWCLASSPATH=file:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
134
135 ########################################
136 # Now add the extensions
137 ########################################
138
139 # Extension 1: this is used to be able to convert all the
140 # bundleresouce: URL in file: so packages that are not OSGi ready can
141 # still work. Notably this is the case for spring classes
142 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
143 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
144
145 ########################################
146 # Now add the launcher
147 ########################################
148 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
149 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
150
151 if [ "${stopdaemon}" -eq 1 ]; then
152     if [ -e "${pidfile}" ]; then
153         daemonpid=`cat "${pidfile}"`
154         kill "${daemonpid}"
155         rm -f "${pidfile}"
156         echo "Controller with PID: ${daemonpid} -- Stopped!"
157         exit 0
158     else
159         echo "Doesn't seem any Controller daemon is currently running"
160         exit -1
161     fi
162 fi
163
164 if [ "${statusdaemon}" -eq 1 ]; then
165     if [ -e "${pidfile}" ]; then
166         daemonpid=`cat "${pidfile}"`
167         ps -p ${daemonpid} > /dev/null
168         daemonexists=$?
169         if [ "${daemonexists}" -eq 0 ]; then
170             echo "Controller with PID: ${daemonpid} -- Running!"
171             exit 0
172         else
173             echo "Controller with PID: ${daemonpid} -- Doesn't seem to exist"
174             rm -f "${pidfile}"
175             exit 0
176         fi
177     else
178         echo "Doesn't seem any Controller daemon is currently running, at least no PID file has been found"
179         exit -1
180     fi
181 fi
182
183 if [ "${startdaemon}" -eq 1 ]; then
184     if [ -e "${pidfile}" ]; then
185         echo "Another instance of controller running, check with $0 -status"
186         exit -1
187     fi
188     $JAVA_HOME/bin/java ${extraJVMOpts} \
189         -Djava.io.tmpdir=${basedir}/work/tmp \
190         -Dosgi.install.area=${basedir} \
191         -Dosgi.configuration.area=${basedir}/configuration \
192         -Dosgi.frameworkClassPath=${FWCLASSPATH} \
193         -Dosgi.framework=file:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar \
194         -classpath ${CLASSPATH} \
195         org.eclipse.equinox.launcher.Main \
196         -console ${daemonport} \
197         -consoleLog &
198     daemonpid=$!
199     echo ${daemonpid} > ${pidfile}
200 elif [ "${consolestart}" -eq 1 ]; then
201     if [ -e "${pidfile}" ]; then
202         echo "Another instance of controller running, check with $0 -status"
203         exit -1
204     fi
205     $JAVA_HOME/bin/java ${extraJVMOpts} \
206         -Djava.io.tmpdir=${basedir}/work/tmp \
207         -Dosgi.install.area=${basedir} \
208         -Dosgi.configuration.area=${basedir}/configuration \
209         -Dosgi.frameworkClassPath=${FWCLASSPATH} \
210         -Dosgi.framework=file:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar \
211         -classpath ${CLASSPATH} \
212         org.eclipse.equinox.launcher.Main \
213         -console \
214         -consoleLog
215 fi