OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 # Virtual Me2Me implementation. This script runs and manages the processes | 6 # Virtual Me2Me implementation. This script runs and manages the processes |
7 # required for a Virtual Me2Me desktop, which are: X server, X desktop | 7 # required for a Virtual Me2Me desktop, which are: X server, X desktop |
8 # session, and Host process. | 8 # session, and Host process. |
9 # This script is intended to run continuously as a background daemon | 9 # This script is intended to run continuously as a background daemon |
10 # process, running under an ordinary (non-root) user account. | 10 # process, running under an ordinary (non-root) user account. |
11 | 11 |
12 import atexit | 12 import atexit |
13 import getpass | 13 import getpass |
| 14 import hashlib |
14 import json | 15 import json |
15 import logging | 16 import logging |
16 import os | 17 import os |
17 import random | 18 import random |
18 import signal | 19 import signal |
19 import socket | 20 import socket |
20 import subprocess | 21 import subprocess |
21 import sys | 22 import sys |
22 import time | 23 import time |
23 import urllib2 | 24 import urllib2 |
24 import uuid | 25 import uuid |
25 | 26 |
26 # Local modules | 27 # Local modules |
27 import gaia_auth | 28 import gaia_auth |
28 import keygen | 29 import keygen |
29 | 30 |
30 REMOTING_COMMAND = "remoting_me2me_host" | 31 REMOTING_COMMAND = "remoting_me2me_host" |
31 | 32 |
| 33 # Command-line switch for passing the config path to remoting_me2me_host. |
| 34 HOST_CONFIG_SWITCH_NAME = "host-config" |
| 35 |
32 SCRIPT_PATH = os.path.dirname(sys.argv[0]) | 36 SCRIPT_PATH = os.path.dirname(sys.argv[0]) |
33 if not SCRIPT_PATH: | 37 if not SCRIPT_PATH: |
34 SCRIPT_PATH = os.getcwd() | 38 SCRIPT_PATH = os.getcwd() |
35 | 39 |
36 # These are relative to SCRIPT_PATH. | 40 # These are relative to SCRIPT_PATH. |
37 EXE_PATHS_TO_TRY = [ | 41 EXE_PATHS_TO_TRY = [ |
38 ".", | 42 ".", |
39 "../../out/Debug", | 43 "../../out/Debug", |
40 "../../out/Release" | 44 "../../out/Release" |
41 ] | 45 ] |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 logging.info("Xvfb is active.") | 257 logging.info("Xvfb is active.") |
254 | 258 |
255 def launch_x_session(self): | 259 def launch_x_session(self): |
256 # Start desktop session | 260 # Start desktop session |
257 session_proc = subprocess.Popen("/etc/X11/Xsession", | 261 session_proc = subprocess.Popen("/etc/X11/Xsession", |
258 cwd=os.environ["HOME"], | 262 cwd=os.environ["HOME"], |
259 env=self.child_env) | 263 env=self.child_env) |
260 if not session_proc.pid: | 264 if not session_proc.pid: |
261 raise Exception("Could not start X session") | 265 raise Exception("Could not start X session") |
262 | 266 |
263 def launch_host(self): | 267 def launch_host(self, host): |
264 # Start remoting host | 268 # Start remoting host |
265 command = locate_executable(REMOTING_COMMAND) | 269 args = [locate_executable(REMOTING_COMMAND), |
266 self.host_proc = subprocess.Popen(command, env=self.child_env) | 270 "--%s=%s" % (HOST_CONFIG_SWITCH_NAME, host.config_file)] |
| 271 self.host_proc = subprocess.Popen(args, env=self.child_env) |
267 if not self.host_proc.pid: | 272 if not self.host_proc.pid: |
268 raise Exception("Could not start remoting host") | 273 raise Exception("Could not start remoting host") |
269 | 274 |
270 | 275 |
271 def main(): | 276 def main(): |
272 atexit.register(cleanup) | 277 atexit.register(cleanup) |
273 | 278 |
274 for s in [signal.SIGHUP, signal.SIGINT, signal.SIGTERM]: | 279 for s in [signal.SIGHUP, signal.SIGINT, signal.SIGTERM]: |
275 signal.signal(s, signal_handler) | 280 signal.signal(s, signal_handler) |
276 | 281 |
277 # Ensure full path to config directory exists. | 282 # Ensure full path to config directory exists. |
278 if not os.path.exists(CONFIG_DIR): | 283 if not os.path.exists(CONFIG_DIR): |
279 os.makedirs(CONFIG_DIR, mode=0700) | 284 os.makedirs(CONFIG_DIR, mode=0700) |
280 | 285 |
281 auth = Authentication(os.path.join(CONFIG_DIR, "auth.json")) | 286 auth = Authentication(os.path.join(CONFIG_DIR, "auth.json")) |
282 if not auth.load_config(): | 287 if not auth.load_config(): |
283 try: | 288 try: |
284 auth.refresh_tokens() | 289 auth.refresh_tokens() |
285 except: | 290 except: |
286 logging.error("Authentication failed.") | 291 logging.error("Authentication failed.") |
287 return 1 | 292 return 1 |
288 auth.save_config() | 293 auth.save_config() |
289 | 294 |
290 host = Host(os.path.join(CONFIG_DIR, "host.json")) | 295 host_hash = hashlib.md5(socket.gethostname()).hexdigest() |
| 296 host = Host(os.path.join(CONFIG_DIR, "host#%s.json" % host_hash)) |
291 | 297 |
292 if not host.load_config(): | 298 if not host.load_config(): |
293 host.create_config(auth) | 299 host.create_config(auth) |
294 host.save_config() | 300 host.save_config() |
295 | 301 |
296 logging.info("Using host_id: " + host.host_id) | 302 logging.info("Using host_id: " + host.host_id) |
297 | 303 |
298 desktop = Desktop() | 304 desktop = Desktop() |
299 desktop.launch_x_server() | 305 desktop.launch_x_server() |
300 desktop.launch_x_session() | 306 desktop.launch_x_session() |
301 desktop.launch_host() | 307 desktop.launch_host(host) |
302 | 308 |
303 while True: | 309 while True: |
304 pid, status = os.wait() | 310 pid, status = os.wait() |
305 logging.info("wait() returned (%s,%s)" % (pid, status)) | 311 logging.info("wait() returned (%s,%s)" % (pid, status)) |
306 | 312 |
307 if pid == desktop.x_proc.pid: | 313 if pid == desktop.x_proc.pid: |
308 logging.info("X server process terminated with code %d", status) | 314 logging.info("X server process terminated with code %d", status) |
309 break | 315 break |
310 | 316 |
311 if pid == desktop.host_proc.pid: | 317 if pid == desktop.host_proc.pid: |
312 logging.info("Host process terminated, relaunching") | 318 logging.info("Host process terminated, relaunching") |
313 desktop.launch_host() | 319 desktop.launch_host(host) |
314 | 320 |
315 if __name__ == "__main__": | 321 if __name__ == "__main__": |
316 logging.basicConfig(level=logging.DEBUG) | 322 logging.basicConfig(level=logging.DEBUG) |
317 sys.exit(main()) | 323 sys.exit(main()) |
OLD | NEW |