b032469b7f1403d9a6f9bdcfc172a982ada2ac41
[controller.git] / opendaylight / distribution / opendaylight / src / main / resources / run.sh
1 #!/bin/bash
2
3 ##HELP
4 # For more information on a specific command, type -help command-name.
5 #
6 #   jmx              [-jmx]
7 #   jmxport          [-jmxport <num>] - DEFAULT is 1088
8 #   debug            [-debug]
9 #   debugsuspend     [-debugsuspend]
10 #   debugport        [-debugport <num>] - DEFAULT is 8000
11 #   start            [-start [<console port>]] - DEFAULT port is 2400
12 #   stop             [-stop]
13 #   status           [-status]
14 #   console          [-console]
15 #   agentpath        [-agentpath:<path to lib>]
16 ##
17
18 platform='unknown'
19 unamestr=`uname`
20 if [[ "$unamestr" == 'Linux' ]]; then
21    platform='linux'
22 elif [[ "$unamestr" == 'Darwin' ]]; then
23    platform='osx'
24 fi
25
26 if [[ $platform == 'linux' ]]; then
27    fullpath=`readlink -f $0`
28
29    if [[ -z ${JAVA_HOME} ]]; then
30       # Find the actual location of the Java launcher:
31       java_launcher=`command -v java`
32       java_launcher=`readlink -f "${java_launcher}"`
33
34       # Compute the Java home from the location of the Java launcher:
35       export JAVA_HOME="${java_launcher%/bin/java}"
36     fi
37 elif [[ $platform == 'osx' ]]; then
38    TARGET_FILE=$0
39    cd `dirname "$TARGET_FILE"`
40    TARGET_FILE=`basename $TARGET_FILE`
41
42    # Iterate down a (possible) chain of symlinks
43    while [ -L "$TARGET_FILE" ]
44    do
45        TARGET_FILE=`readlink "$TARGET_FILE"`
46        cd `dirname "$TARGET_FILE"`
47        TARGET_FILE=`basename "$TARGET_FILE"`
48    done
49
50    # Compute the canonicalized name by finding the physical path
51    # for the directory we're in and appending the target file.
52    PHYS_DIR=`pwd -P`
53    RESULT=$PHYS_DIR/$TARGET_FILE
54    fullpath=$RESULT
55
56    [[ -z ${JAVA_HOME} ]] && [[ -x "/usr/libexec/java_home" ]] && export JAVA_HOME=`/usr/libexec/java_home -v 1.7`;
57
58 fi
59
60 [[ -z ${JAVA_HOME} ]] && echo "Need to set JAVA_HOME environment variable" && exit -1;
61 [[ ! -x ${JAVA_HOME}/bin/java ]] && echo "Cannot find an executable \
62 JVM at path ${JAVA_HOME}/bin/java check your JAVA_HOME" && exit -1;
63
64 if [ -z ${ODL_BASEDIR} ]; then
65     basedir=`dirname "${fullpath}"`
66 else
67     basedir=${ODL_BASEDIR}
68 fi
69
70 if [ -z ${ODL_DATADIR} ]; then
71     datadir=`dirname "${fullpath}"`
72 else
73     datadir=${ODL_DATADIR}
74 fi
75
76 if [ -z ${TMP} ]; then
77     pidfile="/tmp/opendaylight.PID"
78 else
79     pidfile="${TMP}/opendaylight.PID"
80 fi
81 debug=0
82 debugsuspend=0
83 debugport=8000
84 debugportread=""
85 startdaemon=0
86 daemonport=2400
87 daemonportread=""
88 jmxport=1088
89 jmxportread=""
90 startjmx=0
91 stopdaemon=0
92 statusdaemon=0
93 consolestart=1
94 dohelp=0
95 jvmMaxMemory="-Xmx1G"
96 extraJVMOpts=""
97 agentPath=""
98 unknown_option=0
99 helper=""
100 while true ; do
101     case "$1" in
102         -debug) debug=1; shift ;;
103         -help) dohelp=1; shift;  helper=$1; break ;;
104         -jmx) startjmx=1; shift ;;
105         -debugsuspend) debugsuspend=1; shift ;;
106         -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;;
107         -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;;
108         -start) startdaemon=1; shift; daemonportread="$1"; if [[ "${daemonportread}" =~ ^[0-9]+$ ]] ; then daemonport=${daemonportread}; shift; fi;;
109         -stop) stopdaemon=1; shift ;;
110         -status) statusdaemon=1; shift ;;
111         -console) shift ;;
112         -Xmx*) jvmMaxMemory="$1"; shift;;
113         -D*) extraJVMOpts="${extraJVMOpts} $1"; shift;;
114         -X*) extraJVMOpts="${extraJVMOpts} $1"; shift;;
115         -J*) extraJVMOpts="${extraJVMOpts} -$(echo "$1" | cut -d'J' -f2)"; shift;;
116         -agentpath:*) agentPath="$1"; shift;;
117         "") break ;;
118         *) echo "Unknown option $1"; unknown_option=1; break ;;
119     esac
120 done
121
122
123
124 if [ "${unknown_option}" -eq 1 ]; then
125     echo "Use -help for more information."
126     exit 1
127 fi
128
129
130 if [ "${dohelp}" -eq 1 ]; then
131     . ${basedir}/functions.sh
132     harvestHelp ${helper}
133     echo -e '\nFor other information type -help.\n'
134     exit 1
135 fi
136
137 extraJVMOpts="${extraJVMOpts} ${jvmMaxMemory}"
138
139 ##debugport
140 #debugport        [-debugport <num>] - DEFAULT is 8000
141 #
142 #    Set address for settings in runjdwp in extra JVM options.
143 #    The address is transport address for the connection.
144 #    The address has to be in the range [1024,65535]. If this option was not call, port will be set to default value.
145 ##
146 # Validate debug port
147 if [[ "${debugport}" -lt 1024 ]] || [[ "${debugport}" -gt 65535 ]]; then
148     echo "Debug Port not in the range [1024,65535] ${debugport}"
149     exit -1
150 fi
151
152 # Validate daemon console port
153 if [[ "${daemonport}" -lt 1024 ]] || [[ "${daemonport}" -gt 65535 ]]; then
154     echo "Daemon console Port not in the range [1024,65535] value is ${daemonport}"
155     exit -1
156 fi
157
158 ##jmxport
159 #jmxport          [-jmxport <num>] - DEFAULT is 1088
160 #
161 #    Set jmx port for com.sun.management.jmxremote.port in JMX support. Port has to be in the range [1024,65535]. If this option was not call, port will be set to default value.
162 ##
163 # Validate jmx port
164 if [[ "${jmxport}" -lt 1024 ]] || [[ "${jmxport}" -gt 65535 ]]; then
165     echo "JMX Port not in the range [1024,65535] value is ${jmxport}"
166     exit -1
167 fi
168 ##debug
169 #debug            [-debug]
170 #
171 #Run ODL controller with -Xdebug and -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=\${debugport}
172 #-Xdebug enables debugging capabilities in the JVM which are used by the Java Virtual Machine Tools Interface (JVMTI). JVMTI is a low-level debugging interface used by debuggers and profiling tools.
173 #-Xrunjdwp option loads the JPDA reference implementation of JDWP. This library resides in the target VM and uses JVMDI and JNI to interact with it. It uses a transport and the JDWP protocol to
174 #communicate with a separate debugger application.
175 #settings for -Xrunjdwp:
176 #            transport -  name of the transport to use in connecting to debugger application
177 #            server    -  if “y”, listen for a debugger application to attach; otherwise, attach to the debugger application at the specified address
178 #                      -  if “y” and no address is specified, choose a transport address at which to listen for a debugger application, and print the address to the standard output stream
179 #            suspend   -  if “y”, VMStartEvent has a suspend Policy of SUSPEND_ALL
180 #                      -  if “n”, VMStartEvent has a suspend policy of SUSPEND_NONE
181 #            address   -  transport address for the connection
182 #                      -  if server=n, attempt to attach to debugger application at this address
183 #          -  if server=y, listen for a connection at this address
184 ##
185
186 ##debugsuspend
187 #debugsuspend     [-debugsuspend]
188 #
189 #This command sets suspend on true in runjdwp in extra JVM options. If its true, VMStartEvent has a suspendPolicy of SUSPEND_ALL. If its false, VMStartEvent has a suspendPolicy of SUSPEND_NONE.
190 ##
191 # Debug options
192 if [ "${debugsuspend}" -eq 1 ]; then
193     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${debugport}"
194 elif [ "${debug}" -eq 1 ]; then
195     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debugport}"
196 fi
197 ##jmx
198 #jmx              [-jmx]
199 #
200 #Add JMX support. With settings for extra JVM options: -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=\${jmxport} -Dcom.sun.management.jmxremote
201 #jmxport can by set with command -jmxport <num>. Default num for the option is 1088.
202 ##
203 # Add JMX support
204 if [ "${startjmx}" -eq 1 ]; then
205     extraJVMOpts="${extraJVMOpts} -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=${jmxport} -Dcom.sun.management.jmxremote"
206 fi
207
208 ########################################
209 # Now add to classpath the OSGi JAR
210 ########################################
211 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
212 FWCLASSPATH=file:"${basedir}"/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
213
214 ########################################
215 # Now add the extensions
216 ########################################
217
218 # Extension 1: this is used to be able to convert all the
219 # bundleresouce: URL in file: so packages that are not OSGi ready can
220 # still work. Notably this is the case for spring classes
221 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
222 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
223
224 ########################################
225 # Now add the launcher
226 ########################################
227 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
228 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
229
230 cd $basedir
231
232 ##stop
233 #stop             [-stop]
234 #
235 #If a controller is running, the command stop controller. Pid will be clean.
236 ##
237 if [ "${stopdaemon}" -eq 1 ]; then
238     if [ -e "${pidfile}" ]; then
239         daemonpid=`cat "${pidfile}"`
240         kill "${daemonpid}"
241         rm -f "${pidfile}"
242         echo "Controller with PID: ${daemonpid} -- Stopped!"
243         exit 0
244     else
245         echo "Doesn't seem any Controller daemon is currently running"
246         exit -1
247     fi
248 fi
249
250 ##status
251 #status           [-status]
252 #
253 #Find out whether a controller is running and print it.
254 ##
255 if [ "${statusdaemon}" -eq 1 ]; then
256     if [ -e "${pidfile}" ]; then
257         daemonpid=`cat "${pidfile}"`
258         ps -p ${daemonpid} > /dev/null
259         daemonexists=$?
260         if [ "${daemonexists}" -eq 0 ]; then
261             echo "Controller with PID: ${daemonpid} -- Running!"
262             exit 0
263         else
264             echo "Controller with PID: ${daemonpid} -- Doesn't seem to exist"
265             rm -f "${pidfile}"
266             exit 1
267         fi
268     else
269         echo "Doesn't seem any Controller daemon is currently running, at least no PID file has been found"
270         exit -1
271     fi
272 fi
273
274 iotmpdir=`echo "${datadir}" | sed 's/ /\\ /g'`
275 bdir=`echo "${basedir}" | sed 's/ /\\ /g'`
276 confarea=`echo "${datadir}" | sed 's/ /\\ /g'`
277 fwclasspath=`echo "${FWCLASSPATH}" | sed 's/ /\\ /g'`
278
279 ##start
280 #start            [-start [<console port>]]
281 #
282 #    If controller is not running, the command with argument(for set port, where controller has start) will start new controller on a port. The port has to be in the range [1024,65535]. If this option was not call, port will be set to default value. Pid will be create.
283 ##
284 ##console
285 #console          [-console]
286 #
287 #    Default option.
288 ##
289 ##agentpath
290 #agentpath        [-agentpath:<path to lib>]
291 #
292 #   Agentpath option passes path to agent to jvm in order to load native agent library, e.g. yourkit profiler agent.
293 ##
294 echo "JVM maximum memory was set to ${jvmMaxMemory}."
295 if [ "${startdaemon}" -eq 1 ]; then
296     if [ -e "${pidfile}" ]; then
297         echo "Another instance of controller running, check with $0 -status"
298         exit -1
299     fi
300     $JAVA_HOME/bin/java ${extraJVMOpts} \
301         ${agentPath} \
302         -Djava.io.tmpdir="${iotmpdir}/work/tmp" \
303         -Dosgi.install.area="${bdir}" \
304         -Dosgi.configuration.area="${confarea}/configuration" \
305         -Dosgi.frameworkClassPath="${fwclasspath}" \
306         -Dosgi.framework=file:"${bdir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar" \
307         -Djava.awt.headless=true \
308         -classpath "${CLASSPATH}" \
309         org.eclipse.equinox.launcher.Main \
310         -console ${daemonport} \
311         -consoleLog &
312     daemonpid=$!
313     echo ${daemonpid} > ${pidfile}
314 elif [ "${consolestart}" -eq 1 ]; then
315     if [ -e "${pidfile}" ]; then
316         echo "Another instance of controller running, check with $0 -status"
317         exit -1
318     fi
319     $JAVA_HOME/bin/java ${extraJVMOpts} \
320         ${agentPath} \
321         -Djava.io.tmpdir="${iotmpdir}/work/tmp" \
322         -Dosgi.install.area="${bdir}" \
323         -Dosgi.configuration.area="${confarea}/configuration" \
324         -Dosgi.frameworkClassPath="${fwclasspath}" \
325         -Dosgi.framework=file:"${bdir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar" \
326         -Djava.awt.headless=true \
327         -classpath "${CLASSPATH}" \
328         org.eclipse.equinox.launcher.Main \
329         -console \
330         -consoleLog
331 fi