Merge "Re-added config.version to config-module-archetype."
[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         -agentpath:*) agentPath="$1"; shift;;
116         "") break ;;
117         *) echo "Unknown option $1"; unknown_option=1; break ;;
118     esac
119 done
120
121
122
123 if [ "${unknown_option}" -eq 1 ]; then
124     echo "Use -help for more information."
125     exit 1
126 fi
127
128
129 if [ "${dohelp}" -eq 1 ]; then
130     . ${basedir}/functions.sh
131     harvestHelp ${helper}
132     echo -e '\nFor other information type -help.\n'
133     exit 1
134 fi
135
136 extraJVMOpts="${extraJVMOpts} ${jvmMaxMemory}"
137
138 ##debugport
139 #debugport        [-debugport <num>] - DEFAULT is 8000
140 #
141 #    Set address for settings in runjdwp in extra JVM options.
142 #    The address is transport address for the connection.
143 #    The address has to be in the range [1024,65535]. If this option was not call, port will be set to default value.
144 ##
145 # Validate debug port
146 if [[ "${debugport}" -lt 1024 ]] || [[ "${debugport}" -gt 65535 ]]; then
147     echo "Debug Port not in the range [1024,65535] ${debugport}"
148     exit -1
149 fi
150
151 # Validate daemon console port
152 if [[ "${daemonport}" -lt 1024 ]] || [[ "${daemonport}" -gt 65535 ]]; then
153     echo "Daemon console Port not in the range [1024,65535] value is ${daemonport}"
154     exit -1
155 fi
156
157 ##jmxport
158 #jmxport          [-jmxport <num>] - DEFAULT is 1088
159 #
160 #    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.
161 ##
162 # Validate jmx port
163 if [[ "${jmxport}" -lt 1024 ]] || [[ "${jmxport}" -gt 65535 ]]; then
164     echo "JMX Port not in the range [1024,65535] value is ${jmxport}"
165     exit -1
166 fi
167 ##debug
168 #debug            [-debug]
169 #
170 #Run ODL controller with -Xdebug and -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=\${debugport}
171 #-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.
172 #-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
173 #communicate with a separate debugger application.
174 #settings for -Xrunjdwp:
175 #            transport -  name of the transport to use in connecting to debugger application
176 #            server    -  if “y”, listen for a debugger application to attach; otherwise, attach to the debugger application at the specified address
177 #                      -  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
178 #            suspend   -  if “y”, VMStartEvent has a suspend Policy of SUSPEND_ALL
179 #                      -  if “n”, VMStartEvent has a suspend policy of SUSPEND_NONE
180 #            address   -  transport address for the connection
181 #                      -  if server=n, attempt to attach to debugger application at this address
182 #          -  if server=y, listen for a connection at this address
183 ##
184
185 ##debugsuspend
186 #debugsuspend     [-debugsuspend]
187 #
188 #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.
189 ##
190 # Debug options
191 if [ "${debugsuspend}" -eq 1 ]; then
192     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${debugport}"
193 elif [ "${debug}" -eq 1 ]; then
194     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debugport}"
195 fi
196 ##jmx
197 #jmx              [-jmx]
198 #
199 #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
200 #jmxport can by set with command -jmxport <num>. Default num for the option is 1088.
201 ##
202 # Add JMX support
203 if [ "${startjmx}" -eq 1 ]; then
204     extraJVMOpts="${extraJVMOpts} -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=${jmxport} -Dcom.sun.management.jmxremote"
205 fi
206
207 ########################################
208 # Now add to classpath the OSGi JAR
209 ########################################
210 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
211 FWCLASSPATH=file:"${basedir}"/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
212
213 ########################################
214 # Now add the extensions
215 ########################################
216
217 # Extension 1: this is used to be able to convert all the
218 # bundleresouce: URL in file: so packages that are not OSGi ready can
219 # still work. Notably this is the case for spring classes
220 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
221 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
222
223 ########################################
224 # Now add the launcher
225 ########################################
226 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
227 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
228
229 cd $basedir
230
231 ##stop
232 #stop             [-stop]
233 #
234 #If a controller is running, the command stop controller. Pid will be clean.
235 ##
236 if [ "${stopdaemon}" -eq 1 ]; then
237     if [ -e "${pidfile}" ]; then
238         daemonpid=`cat "${pidfile}"`
239         kill "${daemonpid}"
240         rm -f "${pidfile}"
241         echo "Controller with PID: ${daemonpid} -- Stopped!"
242         exit 0
243     else
244         echo "Doesn't seem any Controller daemon is currently running"
245         exit -1
246     fi
247 fi
248
249 ##status
250 #status           [-status]
251 #
252 #Find out whether a controller is running and print it.
253 ##
254 if [ "${statusdaemon}" -eq 1 ]; then
255     if [ -e "${pidfile}" ]; then
256         daemonpid=`cat "${pidfile}"`
257         ps -p ${daemonpid} > /dev/null
258         daemonexists=$?
259         if [ "${daemonexists}" -eq 0 ]; then
260             echo "Controller with PID: ${daemonpid} -- Running!"
261             exit 0
262         else
263             echo "Controller with PID: ${daemonpid} -- Doesn't seem to exist"
264             rm -f "${pidfile}"
265             exit 1
266         fi
267     else
268         echo "Doesn't seem any Controller daemon is currently running, at least no PID file has been found"
269         exit -1
270     fi
271 fi
272
273 iotmpdir=`echo "${datadir}" | sed 's/ /\\ /g'`
274 bdir=`echo "${basedir}" | sed 's/ /\\ /g'`
275 confarea=`echo "${datadir}" | sed 's/ /\\ /g'`
276 fwclasspath=`echo "${FWCLASSPATH}" | sed 's/ /\\ /g'`
277
278 ##start
279 #start            [-start [<console port>]]
280 #
281 #    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.
282 ##
283 ##console
284 #console          [-console]
285 #
286 #    Default option.
287 ##
288 ##agentpath
289 #agentpath        [-agentpath:<path to lib>]
290 #
291 #   Agentpath option passes path to agent to jvm in order to load native agent library, e.g. yourkit profiler agent.
292 ##
293 echo "JVM maximum memory was set to ${jvmMaxMemory}."
294 if [ "${startdaemon}" -eq 1 ]; then
295     if [ -e "${pidfile}" ]; then
296         echo "Another instance of controller running, check with $0 -status"
297         exit -1
298     fi
299     $JAVA_HOME/bin/java ${extraJVMOpts} \
300         ${agentPath} \
301         -Djava.io.tmpdir="${iotmpdir}/work/tmp" \
302         -Dosgi.install.area="${bdir}" \
303         -Dosgi.configuration.area="${confarea}/configuration" \
304         -Dosgi.frameworkClassPath="${fwclasspath}" \
305         -Dosgi.framework=file:"${bdir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar" \
306         -Djava.awt.headless=true \
307         -classpath "${CLASSPATH}" \
308         org.eclipse.equinox.launcher.Main \
309         -console ${daemonport} \
310         -consoleLog &
311     daemonpid=$!
312     echo ${daemonpid} > ${pidfile}
313 elif [ "${consolestart}" -eq 1 ]; then
314     if [ -e "${pidfile}" ]; then
315         echo "Another instance of controller running, check with $0 -status"
316         exit -1
317     fi
318     $JAVA_HOME/bin/java ${extraJVMOpts} \
319         ${agentPath} \
320         -Djava.io.tmpdir="${iotmpdir}/work/tmp" \
321         -Dosgi.install.area="${bdir}" \
322         -Dosgi.configuration.area="${confarea}/configuration" \
323         -Dosgi.frameworkClassPath="${fwclasspath}" \
324         -Dosgi.framework=file:"${bdir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar" \
325         -Djava.awt.headless=true \
326         -classpath "${CLASSPATH}" \
327         org.eclipse.equinox.launcher.Main \
328         -console \
329         -consoleLog
330 fi