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