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