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