annotate runXena.py @ 31:bb84d3bc5308

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