annotate runXena.py @ 46:cdba6c63dce5

add help
author jingchunzhu
date Wed, 29 Jul 2015 10:26:18 -0700
parents a3fbe077a14c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
1 #!/usr/bin/env python
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
2 # To kick off the script, run the following from the python directory:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
3 # PYTHONPATH=`pwd` python testdaemon.py start
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
4
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
5 #standard python libs
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
6 import logging
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
7 import os
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
8 import signal
30
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
9 import subprocess, shlex
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
10 import sys
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
11 import time
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
12 import traceback
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
13 import xena_utils as xena
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
14
33
7ceb967147c3 start xena with no gui
jingchunzhu <jingchunzhu@gmail.com>
parents: 32
diff changeset
15 dirname = os.path.dirname(os.path.abspath(sys.argv[0]))
7ceb967147c3 start xena with no gui
jingchunzhu <jingchunzhu@gmail.com>
parents: 32
diff changeset
16
7ceb967147c3 start xena with no gui
jingchunzhu <jingchunzhu@gmail.com>
parents: 32
diff changeset
17 sys.path.insert(0, dirname + "/lockfile-0.10.2/")
7ceb967147c3 start xena with no gui
jingchunzhu <jingchunzhu@gmail.com>
parents: 32
diff changeset
18 sys.path.insert(0, dirname + "/python-daemon-2.0.5/")
7ceb967147c3 start xena with no gui
jingchunzhu <jingchunzhu@gmail.com>
parents: 32
diff changeset
19
7ceb967147c3 start xena with no gui
jingchunzhu <jingchunzhu@gmail.com>
parents: 32
diff changeset
20
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
21 #third party libs
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
22 from daemon import runner
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
23 from lockfile import LockTimeout
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
24
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
25
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
26 class App():
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
27
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
28 def __init__(self):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
29 xenaBaseDir = xena.baseDir()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
30 if not os.path.exists(xenaBaseDir):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
31 os.mkdir(xenaBaseDir)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
32 self.pidfile_path = xenaBaseDir + "/xena-daemon.pid"
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
33
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
34 self.stdin_path = '/dev/null'
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
35 self.stdout_path = '/dev/null'
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
36 self.stderr_path = '/dev/null'
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
37 self.pidfile_timeout = 5
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
38
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
39 def run(self):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
40 while True:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
41 #Main code goes here ...
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
42 #Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
33
7ceb967147c3 start xena with no gui
jingchunzhu <jingchunzhu@gmail.com>
parents: 32
diff changeset
43 xenaCmdline = "java -jar %s -r %s/files -d %s/db -t %s/tmp --no-gui --logfile %s/xena.log -p %s -H 0.0.0.0"
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
44 xenaBaseDir = xena.baseDir()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
45 xenaCmd = xenaCmdline % (xena.jarPath(), xenaBaseDir,
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
46 xenaBaseDir, xenaBaseDir,
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
47 xenaBaseDir, xena.port())
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
48 logger.debug("Invoking Xena VM with command %s" % (xenaCmd))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
49 xenaVm = subprocess.call(xenaCmd, shell=True)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
50 logger.info("Starting Xena VM")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
51 #logger.warn("Warning message")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
52 #logger.error("Error message")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
53
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
54
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
55
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
56 def processListeningOnPort(portID):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
57 cmd = "lsof -t -i :%s -sTCP:LISTEN" % portID
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
58 pid = subprocess.check_output(cmd, shell=True).rstrip()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
59 return(int(pid))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
60
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
61
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
62 fp = open(sys.argv[2], "w")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
63
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
64 app = App()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
65 logger = logging.getLogger("DaemonLog")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
66 logger.setLevel(logging.DEBUG)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
67 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
68 handler = logging.FileHandler(xena.baseDir() + "/xena-daemon.log")
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
69 handler.setFormatter(formatter)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
70 logger.addHandler(handler)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
71
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 33
diff changeset
72 hostname = xena.hostname() #subprocess.check_output("hostname -f", shell=True).rstrip()
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
73
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
74 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
75 # Check if there has been a Xena running on this system, and
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
76 # what its port number is or was.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
77 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
78 xenaPort = xena.port()
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 33
diff changeset
79
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
80 if xenaPort == None:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
81 # In this case, no Xena has been running on this sytem.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
82 xenaIsRunning = False
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
83 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
84 xenaIsRunning = xena.isRunning(xenaPort)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
85 #rint "xena running", xenaIsRunning, "port", xenaPort
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
86
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
87
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
88 if sys.argv[1] == "status":
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
89 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
90 # When checking status, if Xena is running, then report that it's running
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
91 # with hostname and port. If it's not running, then report as such.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
92 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
93 if xenaIsRunning:
32
63b1ba1e3424 server status message more accurate -- http
jingchunzhu <jingchunzhu@gmail.com>
parents: 31
diff changeset
94 fp.write("Xena VM currently running on http://%s:%s\n" % (hostname, xenaPort))
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 33
diff changeset
95 fp.write("\n")
32
63b1ba1e3424 server status message more accurate -- http
jingchunzhu <jingchunzhu@gmail.com>
parents: 31
diff changeset
96 fp.write("You can add http://%s:%s to Xena Data Hub\n" % (hostname, xenaPort))
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
97 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
98 fp.write("Xena VM is not currently running on %s\n" % (hostname))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
99
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
100 elif sys.argv[1] == "start":
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
101 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
102 # When a start command came in, allocate a new port and prepare to start
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
103 # xena if it's not already running. If it is already running, then
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
104 # report as such. In either case, close the output file before this
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
105 # process goes into daemon mode.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
106 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
107 if not xenaIsRunning:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
108 xenaPort = xena.port(testIfAvailable=True, findNewPort=True)
32
63b1ba1e3424 server status message more accurate -- http
jingchunzhu <jingchunzhu@gmail.com>
parents: 31
diff changeset
109 fp.write("Starting Xena VM on http://%s:%s\n" % (hostname, xenaPort))
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 33
diff changeset
110 fp.write("\n")
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 33
diff changeset
111 fp.write("Use Xena Administration -> Check Status tool to review status.\n")
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
112 else:
32
63b1ba1e3424 server status message more accurate -- http
jingchunzhu <jingchunzhu@gmail.com>
parents: 31
diff changeset
113 fp.write("Xena VM already running on http://%s:%s\n" % (hostname, xenaPort))
34
a3fbe077a14c replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents: 33
diff changeset
114 fp.write("\n")
31
bb84d3bc5308 make Xena VM already running on ... message more useful for adding the data hub
jingchunzhu <jingchunzhu@gmail.com>
parents: 30
diff changeset
115 fp.write("You can add http://%s:%s to Xena Data Hub\n" % (hostname, xenaPort))
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
116 fp.close()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
117
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
118 elif sys.argv[1] == "stop":
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
119 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
120 # When stopping Xena, if it's currently running, report that Xena
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
121 # is being terminated and clean up the port. If it's not running,
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
122 # report as such.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
123 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
124 if xenaIsRunning:
32
63b1ba1e3424 server status message more accurate -- http
jingchunzhu <jingchunzhu@gmail.com>
parents: 31
diff changeset
125 fp.write("Terminating Xena VM on http://%s:%s\n" % (hostname, xenaPort))
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
126 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
127 fp.write("Xena VM is not currently running on %s\n" % (hostname))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
128
30
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
129 elif sys.argv[1] == "version":
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
130 # check the current xena status
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
131 if xenaIsRunning:
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
132 xenaVersionCmd = "java -jar %s --version" % (xena.jarPath())
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
133 args =shlex.split(xenaVersionCmd)
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
134 output, error = subprocess.Popen(args, stdout = subprocess.PIPE, stderr= subprocess.PIPE).communicate()
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
135 fp.write("The current Xena server version is %s\n" % (output))
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
136 else:
77f5d8cbac77 add functionality to check the version of the current running xena server on the galaxy system
Jing Zhu <jzhu@soe.ucsc.edu>
parents: 10
diff changeset
137 fp.write("Xena Server is not currently running on %s, please start the server first, then you can check the server version.\n" % (hostname))
0
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
138 else:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
139 fp.write(("Error: Unexpected command %s" % sys.argv[1]))
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
140
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
141
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
142 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
143 # Here is where the starting and stopping of the Xena daemon takes place.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
144 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
145 if sys.argv[1] == "start" or (sys.argv[1] == "stop" and xenaIsRunning):
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
146 daemon_runner = runner.DaemonRunner(app)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
147 # This ensures that the logger file handle does not get closed during daemonization
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
148 daemon_runner.daemon_context.files_preserve=[handler.stream]
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
149 try:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
150 daemon_runner.do_action()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
151 except LockTimeout:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
152 # Xena is already running. No need to do anything special, but this
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
153 # should be separated from the other exceptions.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
154 pass
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
155 except:
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
156 exc_type, exc_value, exc_traceback = sys.exc_info()
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
157 lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
158 allLines = ''.join('!! ' + line for line in lines)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
159 fp.write("Unsuccessful: error %s\n" % allLines)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
160 if sys.argv[1] == "stop":
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
161 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
162 # If the Xena stop command has been issued, then kill the Xena
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
163 # process.
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
164 #
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
165 xenaPid = processListeningOnPort(xenaPort)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
166 logger.debug("Attempting to kill process with PID %d" % xenaPid)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
167 os.kill(xenaPid, signal.SIGTERM)
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
168 if sys.argv[1] != "start":
8bb037f88ed2 Uploaded
melissacline
parents:
diff changeset
169 fp.close()