Mercurial > repos > melissacline > ucsc_xena_platform
annotate runXena.py @ 53:09fdd4d23a3a
version 17
author | jingchunzhu |
---|---|
date | Mon, 21 Sep 2015 13:25:05 -0700 |
parents | a3fbe077a14c |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 # To kick off the script, run the following from the python directory: | |
3 # PYTHONPATH=`pwd` python testdaemon.py start | |
4 | |
5 #standard python libs | |
6 import logging | |
7 import os | |
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 | 10 import sys |
11 import time | |
12 import traceback | |
13 import xena_utils as xena | |
14 | |
33 | 15 dirname = os.path.dirname(os.path.abspath(sys.argv[0])) |
16 | |
17 sys.path.insert(0, dirname + "/lockfile-0.10.2/") | |
18 sys.path.insert(0, dirname + "/python-daemon-2.0.5/") | |
19 | |
20 | |
0 | 21 #third party libs |
22 from daemon import runner | |
23 from lockfile import LockTimeout | |
24 | |
25 | |
26 class App(): | |
27 | |
28 def __init__(self): | |
29 xenaBaseDir = xena.baseDir() | |
30 if not os.path.exists(xenaBaseDir): | |
31 os.mkdir(xenaBaseDir) | |
32 self.pidfile_path = xenaBaseDir + "/xena-daemon.pid" | |
33 | |
34 self.stdin_path = '/dev/null' | |
35 self.stdout_path = '/dev/null' | |
36 self.stderr_path = '/dev/null' | |
37 self.pidfile_timeout = 5 | |
38 | |
39 def run(self): | |
40 while True: | |
41 #Main code goes here ... | |
42 #Note that logger level needs to be set to logging.DEBUG before this shows up in the logs | |
33 | 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 | 44 xenaBaseDir = xena.baseDir() |
45 xenaCmd = xenaCmdline % (xena.jarPath(), xenaBaseDir, | |
46 xenaBaseDir, xenaBaseDir, | |
47 xenaBaseDir, xena.port()) | |
48 logger.debug("Invoking Xena VM with command %s" % (xenaCmd)) | |
49 xenaVm = subprocess.call(xenaCmd, shell=True) | |
50 logger.info("Starting Xena VM") | |
51 #logger.warn("Warning message") | |
52 #logger.error("Error message") | |
53 | |
54 | |
55 | |
56 def processListeningOnPort(portID): | |
57 cmd = "lsof -t -i :%s -sTCP:LISTEN" % portID | |
58 pid = subprocess.check_output(cmd, shell=True).rstrip() | |
59 return(int(pid)) | |
60 | |
61 | |
62 fp = open(sys.argv[2], "w") | |
63 | |
64 app = App() | |
65 logger = logging.getLogger("DaemonLog") | |
66 logger.setLevel(logging.DEBUG) | |
67 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | |
68 handler = logging.FileHandler(xena.baseDir() + "/xena-daemon.log") | |
69 handler.setFormatter(formatter) | |
70 logger.addHandler(handler) | |
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 | 73 |
74 # | |
75 # Check if there has been a Xena running on this system, and | |
76 # what its port number is or was. | |
77 # | |
78 xenaPort = xena.port() | |
34
a3fbe077a14c
replace wget with python method
jingchunzhu <jingchunzhu@gmail.com>
parents:
33
diff
changeset
|
79 |
0 | 80 if xenaPort == None: |
81 # In this case, no Xena has been running on this sytem. | |
82 xenaIsRunning = False | |
83 else: | |
84 xenaIsRunning = xena.isRunning(xenaPort) | |
85 #rint "xena running", xenaIsRunning, "port", xenaPort | |
86 | |
87 | |
88 if sys.argv[1] == "status": | |
89 # | |
90 # When checking status, if Xena is running, then report that it's running | |
91 # with hostname and port. If it's not running, then report as such. | |
92 # | |
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 | 97 else: |
98 fp.write("Xena VM is not currently running on %s\n" % (hostname)) | |
99 | |
100 elif sys.argv[1] == "start": | |
101 # | |
102 # When a start command came in, allocate a new port and prepare to start | |
103 # xena if it's not already running. If it is already running, then | |
104 # report as such. In either case, close the output file before this | |
105 # process goes into daemon mode. | |
106 # | |
107 if not xenaIsRunning: | |
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 | 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 | 116 fp.close() |
117 | |
118 elif sys.argv[1] == "stop": | |
119 # | |
120 # When stopping Xena, if it's currently running, report that Xena | |
121 # is being terminated and clean up the port. If it's not running, | |
122 # report as such. | |
123 # | |
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 | 126 else: |
127 fp.write("Xena VM is not currently running on %s\n" % (hostname)) | |
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 | 138 else: |
139 fp.write(("Error: Unexpected command %s" % sys.argv[1])) | |
140 | |
141 | |
142 # | |
143 # Here is where the starting and stopping of the Xena daemon takes place. | |
144 # | |
145 if sys.argv[1] == "start" or (sys.argv[1] == "stop" and xenaIsRunning): | |
146 daemon_runner = runner.DaemonRunner(app) | |
147 # This ensures that the logger file handle does not get closed during daemonization | |
148 daemon_runner.daemon_context.files_preserve=[handler.stream] | |
149 try: | |
150 daemon_runner.do_action() | |
151 except LockTimeout: | |
152 # Xena is already running. No need to do anything special, but this | |
153 # should be separated from the other exceptions. | |
154 pass | |
155 except: | |
156 exc_type, exc_value, exc_traceback = sys.exc_info() | |
157 lines = traceback.format_exception(exc_type, exc_value, exc_traceback) | |
158 allLines = ''.join('!! ' + line for line in lines) | |
159 fp.write("Unsuccessful: error %s\n" % allLines) | |
160 if sys.argv[1] == "stop": | |
161 # | |
162 # If the Xena stop command has been issued, then kill the Xena | |
163 # process. | |
164 # | |
165 xenaPid = processListeningOnPort(xenaPort) | |
166 logger.debug("Attempting to kill process with PID %d" % xenaPid) | |
167 os.kill(xenaPid, signal.SIGTERM) | |
168 if sys.argv[1] != "start": | |
169 fp.close() |