Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 528 lines (414 sloc) 14.665 kb
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
1 #!/usr/bin/env python
2
3 # -*- coding: utf-8; mode: python -*-
4 #
5 # Cherokee-admin
6 #
7 # Authors:
8 # Alvaro Lopez Ortega <alvaro@alobbs.com>
9 #
10 # Copyright (C) 2001-2010 Alvaro Lopez Ortega
11 #
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of version 2 of the GNU General Public
14 # License as published by the Free Software Foundation.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 # 02110-1301, USA.
25 #
26
27 import re
28 import os
29 import sys
30 import time
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
31 import glob
26e9052 @alobbs Optimizes how the cherokee-admin process output is read.
alobbs authored
32 import fcntl
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
33 import select
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
34 import signal
409e713 @alobbs Fixes a couple of minor issues with cherokee-admin-launcher.
alobbs authored
35 import urllib2
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
36 import threading
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
37 import subprocess
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
38
39
40 # Constants
41 ADMIN_HOST = "localhost"
42 ADMIN_PORT = 9090
43 ADMIN_LAUNCH_TIMEOUT = 15
44
45 # Paths
8634b56 @alobbs Look for a local cherokee-admin binary, if not found, try with
alobbs authored
46 cherokee_admin_path = 'cherokee-admin'
4ea53bc @alobbs cherokee-admin-launcher tries to figure out a LD_LIBRARY_PATH whenever
alobbs authored
47 cherokee_admin_envs = {}
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
48 runner = None
49 exiting = False
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
50 desktop_preferred = None
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
51
209f15b @alobbs cherokee-admin-launcher now tries to run cherokee-admin as root. For
alobbs authored
52 # Defaults
b721eeb @alobbs Adds /opt/cherokee-dev to the list of known paths. The 'Easy Install'
alobbs authored
53 PATH_PREFIXES = ['/usr', '/usr/local', '/opt/local', '/usr/gnu', '/opt/cherokee', '/opt/cherokee-dev']
209f15b @alobbs cherokee-admin-launcher now tries to run cherokee-admin as root. For
alobbs authored
54 DEFAULT_PATHS = [os.path.join (x,'sbin') for x in PATH_PREFIXES]
55 DEFAULT_PATHS += [os.path.join (x,'bin') for x in PATH_PREFIXES]
56
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
57
26e9052 @alobbs Optimizes how the cherokee-admin process output is read.
alobbs authored
58 def set_non_blocking (fd):
59 fl = fcntl.fcntl (fd, fcntl.F_GETFL)
60 fcntl.fcntl (fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
61
209f15b @alobbs cherokee-admin-launcher now tries to run cherokee-admin as root. For
alobbs authored
62
63 def report_error (description, title="ERROR"):
64 # MacOS X
65 osascript_bin = bin_in_path ("osascript")
66 if osascript_bin:
67 dialog = "display dialog \\\"%(description)s\\\" with title \\\"%(title)s\\\" buttons \\\"Close\\\" with icon stop" %(locals())
68 command = "osascript -e 'tell application \"Finder\"' -e \"activate\" -e \"%(dialog)s\" -e 'end tell'" %(locals())
69 os.system (command)
70
71 # Text mode
72 print '[%(title)s] - %(description)s'%(locals())
73
74
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
75 def find_sudo_askpass():
76 cwd = os.path.abspath (os.path.realpath (__file__) + '/..')
77
78 # MacOS X: osascript
79 osascript_bin = bin_in_path ("osascript")
80 askpass_bin = bin_in_path ("cherokee-macos-askpass", [cwd])
81
82 if osascript_bin and askpass_bin:
83 return askpass_bin
84
85 # X-Window
e8b20f0 @alobbs Try to figure the desktop type when launching a browser under X Window.
alobbs authored
86 def check_gnome():
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
87 gaskpass_bin = bin_in_path ('gaskpass')
88 if gaskpass_bin:
89 return gaskpass_bin
90
91 extra_paths = ['/usr/lib*/openssh/']
92 gnome_ssh_askpass_bin = bin_in_path ('gnome-ssh-askpass', extra_paths)
93 if gnome_ssh_askpass_bin:
94 return gnome_ssh_askpass_bin
95
e8b20f0 @alobbs Try to figure the desktop type when launching a browser under X Window.
alobbs authored
96 def check_kde():
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
97 ksshaskpass_bin = bin_in_path ('ksshaskpass')
98 if ksshaskpass_bin:
99 return ksshaskpass_bin
100
101 kwalletaskpass_bin = bin_in_path ('kwalletaskpass')
102 if kwalletaskpass_bin:
103 return kwalletaskpass_bin
104
e8b20f0 @alobbs Try to figure the desktop type when launching a browser under X Window.
alobbs authored
105 X_display = os.getenv ("DISPLAY")
106 if X_display:
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
107 # Your desktop first
108 if desktop_preferred == 'gnome':
e8b20f0 @alobbs Try to figure the desktop type when launching a browser under X Window.
alobbs authored
109 bin = check_gnome()
110 if bin: return bin
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
111
112 if desktop_preferred == 'kde':
e8b20f0 @alobbs Try to figure the desktop type when launching a browser under X Window.
alobbs authored
113 bin = check_kde()
114 if bin: return bin
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
115
116 # Then, a few neutral options
e8b20f0 @alobbs Try to figure the desktop type when launching a browser under X Window.
alobbs authored
117
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
118 # GTK+
119 gtk_led_askpass_bin = bin_in_path ('gtk-led-askpass')
120 if gtk_led_askpass_bin:
121 return gtk_led_askpass_bin
122
123 # X11
124 extra_paths = ['/usr/lib*/ssh/']
125 x11_ssh_askpass_bin = bin_in_path ('x11-ssh-askpass', extra_paths)
126 if x11_ssh_askpass_bin:
127 return x11_ssh_askpass_bin
128
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
129 # Finally, the 'other' desktop
130 if desktop_preferred == 'gnome':
131 bin = check_kde()
132 if bin: return bin
133
134 if desktop_preferred == 'kde':
135 bin = check_gnome()
136 if bin: return bin
137
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
138
4ea53bc @alobbs cherokee-admin-launcher tries to figure out a LD_LIBRARY_PATH whenever
alobbs authored
139 def build_command_as_root (cmd, internal_envs=[]):
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
140 assert type(cmd) == list
141
4ea53bc @alobbs cherokee-admin-launcher tries to figure out a LD_LIBRARY_PATH whenever
alobbs authored
142 command = internal_envs[:] + cmd[:]
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
143 env = os.environ.copy()
144
145 # If not root
146 uid = os.getuid()
147 if uid != 0:
148 if not os.getenv ('SUDO_ASKPASS'):
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
149 askpass = find_sudo_askpass()
150 if not askpass:
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
151 report_error ("Did not find a suitable SUDO_ASKPASS application")
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
152 else:
153 env['SUDO_ASKPASS'] = askpass
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
154
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
155 # Add sudo
156 use_askpass = (env.get('SUDO_ASKPASS') != None)
157 if use_askpass:
158 command = [bin_in_path('sudo'), '-A'] + command
159 else:
160 command = [bin_in_path('sudo')] + command
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
161
162 return (command, env)
163
164
165 def run_as_root (cmd):
166 cmd_env = build_command_as_root (cmd)
167 command, env = cmd_env
168 return subprocess.call (command, env=env, shell=False, close_fds=True)
169
170
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
171 class Admin_Runner (threading.Thread):
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
172 def __init__ (self, event_launched):
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
173 threading.Thread.__init__ (self)
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
174 self.daemon = True
175
908f301 @alobbs Bugfix (and some clean up): Do not use the -A parameter of sudo if a
alobbs authored
176 self.url = ''
177 self.user = ''
178 self.password = ''
179 self.needs_auth = True
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
180 self.popen = None
209f15b @alobbs cherokee-admin-launcher now tries to run cherokee-admin as root. For
alobbs authored
181
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
182 self.launching = True
183 self.event_launched = event_launched
184
4ea53bc @alobbs cherokee-admin-launcher tries to figure out a LD_LIBRARY_PATH whenever
alobbs authored
185 internal_envs = []
186 for k in cherokee_admin_envs:
187 internal_envs += ["%s=%s"%(k, cherokee_admin_envs[k])]
188
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
189 cmd = [cherokee_admin_path] + sys.argv[1:]
4ea53bc @alobbs cherokee-admin-launcher tries to figure out a LD_LIBRARY_PATH whenever
alobbs authored
190 cmd_env = build_command_as_root (cmd, internal_envs)
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
191 self.command = cmd_env[0]
192 self.environ = cmd_env[1]
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
193
65c0216 @alobbs Better parameter handling. It adds support for parting grouped
alobbs authored
194 # Detect -u, --unsecure, -<something>u
908f301 @alobbs Bugfix (and some clean up): Do not use the -A parameter of sudo if a
alobbs authored
195 self._check_auth()
196
197 def _check_auth (self):
198 # Detects --unsecure
65c0216 @alobbs Better parameter handling. It adds support for parting grouped
alobbs authored
199 self.needs_auth = not ('--unsecure' in sys.argv)
200
908f301 @alobbs Bugfix (and some clean up): Do not use the -A parameter of sudo if a
alobbs authored
201 # Detects -u and -<something>u
65c0216 @alobbs Better parameter handling. It adds support for parting grouped
alobbs authored
202 for arg in sys.argv:
203 if len(arg) >= 2 and arg[0] == '-' and arg[1] != '-':
204 if 'u' in arg:
205 self.needs_auth = False
a8790aa @alobbs Adds support for cherokee-admin's '--unsecure'/'-u' parameter.
alobbs authored
206
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
207 def run (self):
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
208 try:
209 return self._run_guts()
210 except KeyboardInterrupt:
211 pass
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
212
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
213 def _run_guts (self):
214 p = subprocess.Popen (self.command,
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
215 stdout=subprocess.PIPE,
216 stderr=subprocess.PIPE,
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
217 env=self.environ, close_fds=True)
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
218 self.popen = p
219
220 stdout_f, stderr_f = (p.stdout, p.stderr)
221 stdout_fd, stderr_fd = stdout_f.fileno(), stderr_f.fileno()
222 stdout, stderr = '', ''
223
224 set_non_blocking (stdout_fd)
225 set_non_blocking (stderr_fd)
226
ae0c658 @alobbs cherokee-admin-launcher should not remain alive if cherokee-admin is
alobbs authored
227 global exiting
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
228 while not exiting:
b8dc645 @alobbs Tries to fix a couple of issues regarding signal management.
alobbs authored
229 # Poll I/O activity
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
230 r,w,e = select.select([stdout_fd, stderr_fd], [], [stdout_fd, stderr_fd], 1)
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
231
232 if e:
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
233 print "ERROR: select", e
b8dc645 @alobbs Tries to fix a couple of issues regarding signal management.
alobbs authored
234 break
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
235
236 # Read output
237 new_line = False
238
239 if stdout_fd in r:
240 data = stdout_f.read(1024)
241 if not data: break
242 os.write (sys.stdout.fileno(), data)
243 stdout += data
b8dc645 @alobbs Tries to fix a couple of issues regarding signal management.
alobbs authored
244 new_line = '\n' in data
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
245
246 if stderr_fd in r:
247 data = stderr_f.read(1024)
248 if not data: break
249 os.write (sys.stderr.fileno(), data)
250 stderr += data
b8dc645 @alobbs Tries to fix a couple of issues regarding signal management.
alobbs authored
251 new_line = '\n' in data
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
252
b8dc645 @alobbs Tries to fix a couple of issues regarding signal management.
alobbs authored
253 # Launching status
254 if not self.launching:
255 continue
256
257 got_info = bool(self.url)
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
258 if self.needs_auth:
b8dc645 @alobbs Tries to fix a couple of issues regarding signal management.
alobbs authored
259 got_info &= bool(self.user) and bool(self.password)
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
260
b8dc645 @alobbs Tries to fix a couple of issues regarding signal management.
alobbs authored
261 if got_info:
262 self.launching = False
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
263 self.event_launched.set()
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
264
b8dc645 @alobbs Tries to fix a couple of issues regarding signal management.
alobbs authored
265 stdout = ''
266 stderr = ''
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
267 continue
268
269 # Parse connection info
270 if new_line:
271 tmp = re.findall (r'\s+URL:\s+(http.+)\n', stdout)
272 if tmp:
273 self.url = tmp[0]
274
275 tmp = re.findall (r'\s+User:\s+(\w+)', stdout)
276 if tmp:
277 self.user = tmp[0]
278
279 tmp = re.findall (r'\s+One-time Password:\s+(\w+)', stdout)
280 if tmp:
281 self.password = tmp[0]
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
282
ae0c658 @alobbs cherokee-admin-launcher should not remain alive if cherokee-admin is
alobbs authored
283 # Error or process exited
284 exiting = True
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
285 self.event_launched.set()
286
287 print "Cheroke admin exiting..."
ae0c658 @alobbs cherokee-admin-launcher should not remain alive if cherokee-admin is
alobbs authored
288
289
409e713 @alobbs Fixes a couple of minor issues with cherokee-admin-launcher.
alobbs authored
290 def http_GET_admin (host=ADMIN_HOST, port=ADMIN_PORT, req='/'):
291 URI = "http://%s:%s%s" %(host, port, req)
292 try:
293 resp = urllib2.urlopen (URI)
294 except urllib2.URLError, e:
295 description = str(e)
296 for key in ('61,', '111,', 'connection refused'):
297 if key in description.lower():
298 return False
299 return description
300 except Exception, e:
301 return str(e)
302
303 content = resp.read()
304 return content
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
305
306
cad13d8 @alobbs Since the internal bin_in_path() function is called a lot, a cache was
alobbs authored
307 _bin_in_path_cache = {}
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
308 def bin_in_path (bin, extra_paths=[]):
cad13d8 @alobbs Since the internal bin_in_path() function is called a lot, a cache was
alobbs authored
309 global _bin_in_path_cache
310
311 # Cache hit
312 if bin in _bin_in_path_cache:
313 return _bin_in_path_cache[bin]
314
315 # Cache miss: check it out
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
316 paths_tmp = os.getenv('PATH').split(':') + extra_paths + DEFAULT_PATHS
317
318 # Solve ? and *
319 paths = []
320 for p in paths_tmp:
321 paths += glob.glob(p)
209f15b @alobbs cherokee-admin-launcher now tries to run cherokee-admin as root. For
alobbs authored
322
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
323 # Check file
209f15b @alobbs cherokee-admin-launcher now tries to run cherokee-admin as root. For
alobbs authored
324 for e in paths:
325 fp = os.path.join (e, bin)
326 if os.access (fp, os.X_OK):
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
327 _bin_in_path_cache[bin] = fp
209f15b @alobbs cherokee-admin-launcher now tries to run cherokee-admin as root. For
alobbs authored
328 return fp
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
329
cad13d8 @alobbs Since the internal bin_in_path() function is called a lot, a cache was
alobbs authored
330 _bin_in_path_cache[bin] = False
331
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
332
333 def launch_browser (url, user, password):
a8790aa @alobbs Adds support for cherokee-admin's '--unsecure'/'-u' parameter.
alobbs authored
334 if user and password:
335 host = re.findall (r'http://(.+)/', url)[0]
336 URI = 'http://%(user)s:%(password)s@%(host)s/' %(locals())
337 else:
338 URI = url
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
339
340 # MacOS X
406eb4a @alobbs Performs a better check on the suitable binaries. It also changes how
alobbs authored
341 if os.access ("/usr/bin/open", os.X_OK):
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
342 return os.system ("open '%(URI)s'" %(locals()))
343
344 # Your desktop
345 if desktop_preferred == 'gnome' and bin_in_path ('gnome-open'):
346 return os.system ("gnome-open '%(URI)s'" %(locals()))
347
5e44f23 Fixes C&P related bug
taher authored
348 elif desktop_preferred == 'kde':
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
349 if bin_in_path ('kde-open'):
350 return os.system ("kde-open '%(URI)s'" %(locals()))
351 elif bin_in_path ('kfmclient'):
352 return os.system ("kfmclient openURL '%(URI)s'" %(locals()))
353
354 # A few neutral options
355 if bin_in_path ('xdg-open'):
356 return os.system ("xdg-open '%(URI)s'" %(locals()))
357
358 # Last option: 'the other' desktop
359 if desktop_preferred == 'gnome':
360 if bin_in_path ('kde-open'):
361 return os.system ("kde-open '%(URI)s'" %(locals()))
362 elif bin_in_path ('kfmclient'):
363 return os.system ("kfmclient openURL '%(URI)s'" %(locals()))
364
5e44f23 Fixes C&P related bug
taher authored
365 elif desktop_preferred == 'kde' and bin_in_path ('gnome-open'):
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
366 return os.system ("gnome-open '%(URI)s'" %(locals()))
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
367
3fe6d83 @alobbs Print an error message if cherokee-admin-launcher cannot find a
alobbs authored
368 # Error
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
369 report_error ("Did not find a way to open: %(url)s" %(locals()))
3fe6d83 @alobbs Print an error message if cherokee-admin-launcher cannot find a
alobbs authored
370
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
371
8634b56 @alobbs Look for a local cherokee-admin binary, if not found, try with
alobbs authored
372 def find_cherokee_admin():
373 global cherokee_admin_path
374
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
375 # Development
8634b56 @alobbs Look for a local cherokee-admin binary, if not found, try with
alobbs authored
376 path = os.path.abspath (os.path.realpath (__file__) + '/../cherokee-admin')
377 if os.path.exists (path):
378 cherokee_admin_path = path
379 return
380
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
381 # Proper installation
8634b56 @alobbs Look for a local cherokee-admin binary, if not found, try with
alobbs authored
382 path = os.path.abspath (os.path.realpath (__file__) + '/../../sbin/cherokee-admin')
383 if os.path.exists (path):
384 cherokee_admin_path = path
385 return
386
ee2e670 @alobbs A few improvements to the cherokee-admin-launcher.
alobbs authored
387 # Not found
388 return True
209f15b @alobbs cherokee-admin-launcher now tries to run cherokee-admin as root. For
alobbs authored
389
4ea53bc @alobbs cherokee-admin-launcher tries to figure out a LD_LIBRARY_PATH whenever
alobbs authored
390 def figure_cherokee_admin_envs():
391 global cherokee_admin_envs
392
393 if sys.platform == 'darwin':
394 ld_env = 'DYLD_LIBRARY_PATH'
395 else:
396 ld_env = 'LD_LIBRARY_PATH'
397 ld_env_val = os.getenv (ld_env) or ''
398
399 # Installation
400 lib_path = os.path.abspath (os.path.realpath (__file__) + '/../../lib')
401 if not glob.glob('%s/libcherokee*' %(lib_path)):
402 # Development
403 lib_path = os.path.abspath (os.path.realpath (__file__) + '/../.libs')
404 if not glob.glob('%s/libcherokee*' %(lib_path)):
405 # No libcherokee found
406 return True
407
408 if not lib_path in ld_env_val:
409 if ld_env_val:
410 cherokee_admin_envs[ld_env] = '%s:%s' %(ld_env_val, lib_path)
411 else:
412 cherokee_admin_envs[ld_env] = lib_path
413
209f15b @alobbs cherokee-admin-launcher now tries to run cherokee-admin as root. For
alobbs authored
414
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
415 def quit_signal (num, stack):
416 global exiting
417 exiting = True
418 print
419 print "Cherokee-admin-launcher exiting.."
420
c9361a3 @alobbs cherokee-admin-launcher launches a new browser whenever a signal
alobbs authored
421 def browser_signal (num, stack):
422 if (not runner) or exiting:
423 return
424 launch_browser (runner.url, runner.user, runner.password)
425
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
426
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
427 def do_launching():
c3ea297 @alobbs Somehow patches did not merge well
alobbs authored
428 global desktop_preferred
c68291f @alobbs Improves how GNOME/KDE applications are detected. It tries to stick to
alobbs authored
429
688763b @alobbs Fixes a syntax and indentation issues
alobbs authored
430 # Find cherokee-admin
431 error = find_cherokee_admin()
432 if error:
433 report_error ("Could not find cherokee-admin")
434 return
409e713 @alobbs Fixes a couple of minor issues with cherokee-admin-launcher.
alobbs authored
435
688763b @alobbs Fixes a syntax and indentation issues
alobbs authored
436 error = figure_cherokee_admin_envs()
437 if error:
438 report_error ("Could not figure cherokee-admin environment variables")
439 return
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
440
688763b @alobbs Fixes a syntax and indentation issues
alobbs authored
441 # Desktop preferrence
442 ps_lines = [l.lower() for l in os.popen ("ps ax").readlines()]
443 procs_gnome = len (filter (lambda l: "gnome" in l, ps_lines))
444 procs_kde = len (filter (lambda l: "kde" in l, ps_lines))
445
446 if procs_kde > procs_gnome:
447 desktop_preferred = 'kde'
448 else:
449 desktop_preferred = 'gnome'
450
451 # Ensure port is empty
452 print "Checking TCP port %(ADMIN_PORT)s availability.."%(globals()),
453 response = http_GET_admin()
454 if not response:
455 print "OK"
456 else:
457 print
458 report_error ("The 9090 port is already in use.")
459 raise SystemExit
460
461 # Launch Cherokee-admin
462 event_launched = threading.Event()
463
464 global runner
465 runner = Admin_Runner (event_launched)
466 command = runner.command[:]
467
468 runner.start()
469
470 print "Launching: %s" %(' '.join (command))
471 event_launched.wait()
472
473 # Wait for it to be available
474 print "Connecting..",
475 wait_timeout = time.time() + ADMIN_LAUNCH_TIMEOUT
476
477 while True:
478 if exiting:
479 return
480
481 response = http_GET_admin()
482 if response:
483 print "OK"
484 break
485 if time.time() < wait_timeout:
486 time.sleep(0.3)
487 else:
488 print "Timeout"
489 return
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
490
688763b @alobbs Fixes a syntax and indentation issues
alobbs authored
491 # Launching browser
492 print "Launching browser..",
493 launch_browser (runner.url, runner.user, runner.password)
494 print "OK"
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
495
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
496 def clean_up():
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
497 # Exiting
498 if runner and runner.popen and runner.popen.pid:
499 kill_command = ['/bin/sh', '-c', 'kill %s'%(runner.popen.pid)]
500 run_as_root (kill_command)
ae0c658 @alobbs cherokee-admin-launcher should not remain alive if cherokee-admin is
alobbs authored
501 try:
502 os.waitpid (runner.popen.pid, 0)
503 except OSError:
504 pass
dbdb241 @alobbs Adds a new 'cherokee-admin-launcher' script to launch Cherokee-admin
alobbs authored
505
506 if __name__ == '__main__':
507 if '--help' in sys.argv:
508 os.system ('%(cherokee_admin_path)s --help' %(globals()))
509 raise SystemExit
510
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
511 # Set signal handlers
512 signal.signal (signal.SIGINT, quit_signal)
513 signal.signal (signal.SIGTERM, quit_signal)
c9361a3 @alobbs cherokee-admin-launcher launches a new browser whenever a signal
alobbs authored
514 signal.signal (signal.SIGHUP , browser_signal)
2839e9a @alobbs Try to ensure the cherokee-admin process dies when a SIGINT signal is
alobbs authored
515
f341a5b @alobbs Fixes a couple of concurrency issues.
alobbs authored
516 # admin and broweser
517 try:
518 do_launching()
519 except KeyboardInterrupt:
520 exiting = True
521
522 # Wait for it to finish
523 while not exiting:
524 time.sleep (.1)
525
526 # Final clean up
527 clean_up()
Something went wrong with that request. Please try again.