1 |
|
#!/usr/bin/env python |
2 |
|
from Framework import BonsaiServer |
3 |
|
from Framework import Context |
4 |
+ |
from Framework.Logger import Logger |
5 |
+ |
from Framework.Logger import g_Logger |
6 |
|
from optparse import OptionParser |
7 |
|
from Framework import CmdLineArgs |
8 |
+ |
import sys |
9 |
+ |
import os |
10 |
+ |
import getpass |
11 |
+ |
from os.path import abspath |
12 |
+ |
import errno |
13 |
|
|
14 |
< |
if __name__ == '__main__': |
15 |
< |
context = Context () |
16 |
< |
context.addService (OptionParser ()) |
17 |
< |
context.OptionParser ().add_option ("--profile", |
18 |
< |
help="start server in profiler mode", |
19 |
< |
default=False, |
20 |
< |
action="store_true", |
21 |
< |
dest="profile") |
14 |
> |
class Cfg: |
15 |
> |
def __init__ (self): |
16 |
> |
# TODO: make it a property. |
17 |
> |
self.installRoot = __file__.rsplit ("/", 1)[0] |
18 |
> |
|
19 |
> |
class CommandFactory (object): |
20 |
> |
def __init__ (self, context, opts, args): |
21 |
> |
self.context = context |
22 |
> |
self.registry = {"start": StartCommand, |
23 |
> |
"status": StatusCommand, |
24 |
> |
"stop": StopCommand} |
25 |
> |
self.opts, self.args = opts, args |
26 |
> |
|
27 |
> |
def createByName (self, name): |
28 |
> |
try: |
29 |
> |
obj = self.registry[name] () |
30 |
> |
except KeyError: |
31 |
> |
return None |
32 |
> |
obj.context = self.context |
33 |
> |
obj.opts = self.opts |
34 |
> |
obj.args = self.args |
35 |
> |
return obj |
36 |
> |
|
37 |
> |
class Command (object): |
38 |
> |
def __init__ (self): |
39 |
> |
self.context = None |
40 |
> |
self.opts = None |
41 |
> |
self.args = None |
42 |
> |
|
43 |
> |
def finish (self): |
44 |
> |
pass |
45 |
> |
|
46 |
> |
def checkIfPidRunning (pid): |
47 |
> |
try: |
48 |
> |
os.kill(pid, 0) |
49 |
> |
return True |
50 |
> |
except OSError, err: |
51 |
> |
return err.errno == errno.EPERM |
52 |
> |
|
53 |
> |
def getPidFromFile (filename): |
54 |
> |
try: |
55 |
> |
return int (open (filename).read ().strip ()) |
56 |
> |
except ValueError: |
57 |
> |
print "Invalid lockfile format for %s." % filename |
58 |
> |
print "Cannot detect status." |
59 |
> |
sys.exit (2) |
60 |
> |
|
61 |
> |
class StatusCommand (Command): |
62 |
> |
def run (self): |
63 |
> |
filename = abspath (self.opts.pidFile) |
64 |
> |
try: |
65 |
> |
pid = getPidFromFile (filename) |
66 |
> |
if checkIfPidRunning (pid): |
67 |
> |
print "cmsWeb is running as pid %s" % pid |
68 |
> |
sys.exit (0) |
69 |
> |
print "cmsWeb not running." |
70 |
> |
except IOError: |
71 |
> |
print "File %s does not exists." % opts.pidFile |
72 |
> |
print "Cannot detect status." |
73 |
> |
sys.exit (2) |
74 |
> |
|
75 |
> |
class StopCommand (Command): |
76 |
> |
def run (self): |
77 |
> |
filename = abspath (self.opts.pidFile) |
78 |
> |
username = getpass.getuser () |
79 |
> |
|
80 |
> |
try: |
81 |
> |
pid = getPidFromFile (filename) |
82 |
> |
if self.opts.forceKill == True: |
83 |
> |
print "Sending SIGKILL to pid %s" % pid |
84 |
> |
os.kill (pid, 9) |
85 |
> |
sys.exit (0) |
86 |
> |
print "Sending SIGTERM to pid %s" % pid |
87 |
> |
os.kill (pid, 15) |
88 |
> |
except OSError, err: |
89 |
> |
if err.errno == errno.EPERM: |
90 |
> |
print "Pid %s is not owned by user %s. Cannot stop it." % (pid, username) |
91 |
> |
sys.exit (2) |
92 |
> |
else: |
93 |
> |
print "Pid %s does not exists. Please remove the lock file %s." % (pid, filename) |
94 |
> |
except IOError: |
95 |
> |
print "File %s does not exists." % self.opts.pidFile |
96 |
> |
print "Cannot detect status." |
97 |
> |
sys.exit (2) |
98 |
> |
|
99 |
> |
|
100 |
> |
class StartCommand (Command): |
101 |
> |
def run (self): |
102 |
> |
app = BonsaiServer (self.context) |
103 |
> |
opts, args = self.context.OptionParser ().parse_args () |
104 |
> |
filename = abspath (self.opts.pidFile) |
105 |
> |
try: |
106 |
> |
pid = getPidFromFile (filename) |
107 |
> |
if checkIfPidRunning (pid): |
108 |
> |
print "A process %s is alredy running using lock file %s. Nothing is done." % (pid, filename) |
109 |
> |
sys.exit (0) |
110 |
> |
else: |
111 |
> |
print "Process %s died unexpectedly. Removing lockfile %s." % (pid, filename) |
112 |
> |
os.unlink (filename) |
113 |
> |
except IOError: |
114 |
> |
# TODO: this should be a warning. |
115 |
> |
g_Logger.trace ("File %s does not exists. Will be created." % filename) |
116 |
> |
|
117 |
> |
open (filename, 'w').write (str (os.getpid ())) |
118 |
> |
self.context.addService (CmdLineArgs (self.context.OptionParser ())) |
119 |
> |
self.context.addService (Cfg ()) |
120 |
> |
if opts.profile: |
121 |
> |
import pstats |
122 |
> |
try: |
123 |
> |
import cProfile as profile |
124 |
> |
except ImportError: |
125 |
> |
import profile |
126 |
> |
profile.run ('app.start ()', 'bonsaiProfiler') |
127 |
> |
|
128 |
> |
p = pstats.Stats ('bonsaiProfiler') |
129 |
> |
p.strip_dirs().sort_stats (-1).print_stats() |
130 |
> |
else: |
131 |
> |
app.start () |
132 |
|
|
133 |
< |
context.OptionParser ().add_option ("--base-url", |
134 |
< |
help="Base URL for the server (for usage behind a proxy).", |
135 |
< |
default="http://localhost:8030", |
136 |
< |
dest="baseUrl") |
133 |
> |
def finish (self): |
134 |
> |
filename = abspath (self.opts.pidFile) |
135 |
> |
try: |
136 |
> |
os.unlink (filename) |
137 |
> |
except IOError: |
138 |
> |
print "Unable to remove lock file %s" % filename |
139 |
> |
|
140 |
> |
def getValidOptions (args): |
141 |
> |
validArguments = ["start", |
142 |
> |
"stop", |
143 |
> |
"restart"] |
144 |
> |
validOptions = ["--cfg", "--force-kill", "--pid-file", |
145 |
> |
"--log-file", "--log-level"] |
146 |
> |
|
147 |
> |
result = [] |
148 |
> |
for i in range (0, len (args)): |
149 |
> |
arg = sys.argv[i] |
150 |
> |
if arg in validArguments: |
151 |
> |
result.append (args[i]) |
152 |
|
|
153 |
< |
app = BonsaiServer (context) |
153 |
> |
for i in range (0, len (args)): |
154 |
> |
option = args[i] |
155 |
> |
if option in validOptions: |
156 |
> |
result.append (option) |
157 |
> |
result.append (args[i+1]) |
158 |
> |
return result |
159 |
> |
|
160 |
> |
class CmsWebApplication (object): |
161 |
> |
def __init__ (self): |
162 |
> |
self.context = Context () |
163 |
> |
self.context.addService (OptionParser ()) |
164 |
> |
self.parser = self.context.OptionParser () |
165 |
> |
self.__addOptions () |
166 |
> |
|
167 |
> |
def __addOptions (self): |
168 |
> |
self.parser.add_option ("--profile", |
169 |
> |
help="start server in profiler mode", |
170 |
> |
default=False, |
171 |
> |
action="store_true", |
172 |
> |
dest="profile") |
173 |
> |
|
174 |
> |
self.parser.add_option ("--pid-file", |
175 |
> |
help="File in which it is specified the pid of wanted instance", |
176 |
> |
default="pid.txt", |
177 |
> |
dest="pidFile", |
178 |
> |
metavar="FILE") |
179 |
> |
|
180 |
> |
self.parser.add_option ("--force-kill", |
181 |
> |
help="Uses SIGKILL rather than SIGTERM", |
182 |
> |
default=False, |
183 |
> |
action="store_true", |
184 |
> |
dest="forceKill", |
185 |
> |
metavar="FILE") |
186 |
> |
|
187 |
> |
def openFilename (option, opt_str, value, parser, *args, **kwargs): |
188 |
> |
try: |
189 |
> |
f=open (value, 'a') |
190 |
> |
except IOError: |
191 |
> |
print "WARNING: Unable to open log file %s. Using stderr." % value |
192 |
> |
f=sys.stderr |
193 |
> |
setattr (parser.values, option.dest, f) |
194 |
> |
|
195 |
> |
self.parser.add_option ("--log-file", |
196 |
> |
help="FILE to which redirect log messages", |
197 |
> |
dest="logFile", |
198 |
> |
default=sys.stderr, |
199 |
> |
action="callback", |
200 |
> |
callback=openFilename, |
201 |
> |
metavar="FILENAME", |
202 |
> |
type="str", |
203 |
> |
nargs=1) |
204 |
> |
|
205 |
> |
self.parser.add_option ("--log-level", |
206 |
> |
help="detail LEVEL for the main log", |
207 |
> |
dest="logLevel", |
208 |
> |
default=10, |
209 |
> |
metavar="LEVEL", |
210 |
> |
type="int") |
211 |
|
|
212 |
< |
opts, args = context.OptionParser ().parse_args () |
213 |
< |
context.addService (CmdLineArgs (context.OptionParser ())) |
212 |
> |
def run (self): |
213 |
> |
if "--help" in sys.argv: |
214 |
> |
g_Logger.detailLevel = -100 |
215 |
> |
validOptions = getValidOptions (sys.argv) |
216 |
|
|
217 |
< |
if opts.profile: |
218 |
< |
import pstats |
219 |
< |
try: |
220 |
< |
import cProfile as profile |
221 |
< |
except ImportError: |
222 |
< |
import profile |
223 |
< |
profile.run ('app.start ()', 'bonsaiProfiler') |
224 |
< |
|
225 |
< |
p = pstats.Stats('bonsaiProfiler') |
226 |
< |
p.strip_dirs().sort_stats(-1).print_stats() |
227 |
< |
else: |
228 |
< |
app.start () |
217 |
> |
opts, args = self.parser.parse_args (args=validOptions) |
218 |
> |
|
219 |
> |
g_Logger.stream = opts.logFile |
220 |
> |
if "--help" not in sys.argv: |
221 |
> |
g_Logger.detailLevel = opts.logLevel |
222 |
> |
|
223 |
> |
if not len (args): |
224 |
> |
args = ["start"] |
225 |
> |
|
226 |
> |
factory = CommandFactory (self.context, opts, args) |
227 |
> |
startCommand = factory.createByName (args[0]) |
228 |
> |
if not startCommand: |
229 |
> |
"Command %s not known." % args[0] |
230 |
> |
sys.exit (1) |
231 |
> |
startCommand.run () |
232 |
> |
startCommand.finish () |
233 |
> |
|
234 |
> |
if __name__ == '__main__': |
235 |
> |
app = CmsWebApplication () |
236 |
> |
app.run () |