Merge "Integration of MDSAL into distribution"
[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 [-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 [ -v "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 stopdaemon=0
60 statusdaemon=0
61 consolestart=1
62 dohelp=0
63 extraJVMOpts=""
64 unknown_option=0
65 while true ; do
66     case "$1" in
67         -debug) debug=1; shift ;;
68         -debugsuspend) debugsuspend=1; shift ;;
69         -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;;
70         -start) startdaemon=1; shift; daemonportread="$1"; if [[ "${daemonportread}" =~ ^[0-9]+$ ]] ; then daemonport=${daemonportread}; shift; fi;;
71         -stop) stopdaemon=1; shift ;;
72         -status) statusdaemon=1; shift ;;
73         -console) shift ;;
74         -help) dohelp=1; shift;;
75         -D*) extraJVMOpts="${extraJVMOpts} $1"; shift;;
76         -X*) extraJVMOpts="${extraJVMOpts} $1"; shift;;
77         "") break ;;
78         *) echo "Unknown option $1"; unknown_option=1; shift ;;
79     esac
80 done
81
82 # Unknown Options and help
83 if [ "${unknown_option}" -eq 1 ]; then
84     usage
85 fi
86
87 if [ "${dohelp}" -eq 1 ]; then
88     usage
89 fi
90
91 # Validate debug port
92 if [[ "${debugport}" -lt 1024 ]] || [[ "${debugport}" -gt 65535 ]]; then
93     echo "Debug Port not in the range [1024,65535] ${debugport}"
94     exit -1
95 fi
96
97 # Validate daemon console port
98 if [[ "${daemonport}" -lt 1024 ]] || [[ "${daemonport}" -gt 65535 ]]; then
99     echo "Daemon console Port not in the range [1024,65535] value is ${daemonport}"
100     exit -1
101 fi
102
103 # Debug options
104 if [ "${debugsuspend}" -eq 1 ]; then
105     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${debugport}"
106 elif [ "${debug}" -eq 1 ]; then
107     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debugport}"
108 fi
109
110 ########################################
111 # Now add to classpath the OSGi JAR
112 ########################################
113 CLASSPATH=${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
114 FWCLASSPATH=file:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
115
116 ########################################
117 # Now add the extensions
118 ########################################
119
120 # Extension 1: this is used to be able to convert all the
121 # bundleresouce: URL in file: so packages that are not OSGi ready can
122 # still work. Notably this is the case for spring classes
123 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
124 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
125
126 ########################################
127 # Now add the launcher
128 ########################################
129 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
130 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
131
132 if [ "${stopdaemon}" -eq 1 ]; then
133     if [ -e "${pidfile}" ]; then
134         daemonpid=`cat "${pidfile}"`
135         kill "${daemonpid}"
136         rm -f "${pidfile}"
137         echo "Controller with PID: ${daemonpid} -- Stopped!"
138         exit 0
139     else
140         echo "Doesn't seem any Controller daemon is currently running"
141         exit -1
142     fi
143 fi
144
145 if [ "${statusdaemon}" -eq 1 ]; then
146     if [ -e "${pidfile}" ]; then
147         daemonpid=`cat "${pidfile}"`
148         ps -p ${daemonpid} > /dev/null
149         daemonexists=$?
150         if [ "${daemonexists}" -eq 0 ]; then
151             echo "Controller with PID: ${daemonpid} -- Running!"
152             exit 0
153         else
154             echo "Controller with PID: ${daemonpid} -- Doesn't seem to exist"
155             rm -f "${pidfile}"
156             exit 0
157         fi
158     else
159         echo "Doesn't seem any Controller daemon is currently running, at least no PID file has been found"
160         exit -1
161     fi
162 fi
163
164 if [ "${startdaemon}" -eq 1 ]; then
165     if [ -e "${pidfile}" ]; then
166         echo "Another instance of controller running, check with $0 -status"
167         exit -1
168     fi
169     $JAVA_HOME/bin/java ${extraJVMOpts} \
170         -Djava.io.tmpdir=${basedir}/work/tmp \
171         -Dosgi.install.area=${basedir} \
172         -Dosgi.configuration.area=${basedir}/configuration \
173         -Dosgi.frameworkClassPath=${FWCLASSPATH} \
174         -Dosgi.framework=file:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar \
175         -classpath ${CLASSPATH} \
176         org.eclipse.equinox.launcher.Main \
177         -console ${daemonport} \
178         -consoleLog &
179     daemonpid=$!
180     echo ${daemonpid} > ${pidfile}
181 elif [ "${consolestart}" -eq 1 ]; then
182     if [ -e "${pidfile}" ]; then
183         echo "Another instance of controller running, check with $0 -status"
184         exit -1
185     fi
186     $JAVA_HOME/bin/java ${extraJVMOpts} \
187         -Djava.io.tmpdir=${basedir}/work/tmp \
188         -Dosgi.install.area=${basedir} \
189         -Dosgi.configuration.area=${basedir}/configuration \
190         -Dosgi.frameworkClassPath=${FWCLASSPATH} \
191         -Dosgi.framework=file:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar \
192         -classpath ${CLASSPATH} \
193         org.eclipse.equinox.launcher.Main \
194         -console \
195         -consoleLog
196 fi