Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 | 45 |
| 46 IS_INSTALLED = (os.path.basename(sys.argv[0]) != 'me2me_virtual_host.py') | 46 IS_INSTALLED = (os.path.basename(sys.argv[0]) != 'me2me_virtual_host.py') |
| 47 | 47 |
| 48 if IS_INSTALLED: | 48 if IS_INSTALLED: |
| 49 HOST_BINARY_NAME = "chrome-remote-desktop-host" | 49 HOST_BINARY_NAME = "chrome-remote-desktop-host" |
| 50 else: | 50 else: |
| 51 HOST_BINARY_NAME = "remoting_me2me_host" | 51 HOST_BINARY_NAME = "remoting_me2me_host" |
| 52 | 52 |
| 53 CHROME_REMOTING_GROUP_NAME = "chrome-remote-desktop" | 53 CHROME_REMOTING_GROUP_NAME = "chrome-remote-desktop" |
| 54 | 54 |
| 55 CONFIG_DIR = os.path.expanduser("~/.config/chrome-remote-desktop") | |
| 56 HOME_DIR = os.environ["HOME"] | 55 HOME_DIR = os.environ["HOME"] |
| 56 CONFIG_DIR = HOME_DIR + "/.config/chrome-remote-desktop" | |
|
Lambros
2014/02/12 20:50:32
Use os.path.join instead of string concatenation?
Sergey Ulanov
2014/02/13 02:25:44
Done.
| |
| 57 SESSION_FILE_PATH = HOME_DIR + "/.chrome-remote-desktop-session" | |
| 57 | 58 |
| 58 X_LOCK_FILE_TEMPLATE = "/tmp/.X%d-lock" | 59 X_LOCK_FILE_TEMPLATE = "/tmp/.X%d-lock" |
| 59 FIRST_X_DISPLAY_NUMBER = 20 | 60 FIRST_X_DISPLAY_NUMBER = 20 |
| 60 | 61 |
| 61 # Amount of time to wait between relaunching processes. | 62 # Amount of time to wait between relaunching processes. |
| 62 SHORT_BACKOFF_TIME = 5 | 63 SHORT_BACKOFF_TIME = 5 |
| 63 LONG_BACKOFF_TIME = 60 | 64 LONG_BACKOFF_TIME = 60 |
| 64 | 65 |
| 65 # How long a process must run in order not to be counted against the restart | 66 # How long a process must run in order not to be counted against the restart |
| 66 # thresholds. | 67 # thresholds. |
| 67 MINIMUM_PROCESS_LIFETIME = 60 | 68 MINIMUM_PROCESS_LIFETIME = 60 |
| 68 | 69 |
| 69 # Thresholds for switching from fast- to slow-restart and for giving up | 70 # Thresholds for switching from fast- to slow-restart and for giving up |
| 70 # trying to restart entirely. | 71 # trying to restart entirely. |
| 71 SHORT_BACKOFF_THRESHOLD = 5 | 72 SHORT_BACKOFF_THRESHOLD = 5 |
| 72 MAX_LAUNCH_FAILURES = SHORT_BACKOFF_THRESHOLD + 10 | 73 MAX_LAUNCH_FAILURES = SHORT_BACKOFF_THRESHOLD + 10 |
| 73 | 74 |
| 74 # Globals needed by the atexit cleanup() handler. | 75 # Globals needed by the atexit cleanup() handler. |
| 75 g_desktops = [] | 76 g_desktops = [] |
| 76 g_host_hash = hashlib.md5(socket.gethostname()).hexdigest() | 77 g_host_hash = hashlib.md5(socket.gethostname()).hexdigest() |
| 77 | 78 |
| 79 def load_lsb_release_file(): | |
|
Lambros
2014/02/12 20:50:32
optional: I've just found out about Python's "plat
Sergey Ulanov
2014/02/13 02:25:44
actually platform.dist() is deprecated in 2.6. Th
| |
| 80 result = {} | |
| 81 try: | |
| 82 for line in open('/etc/lsb-release').readlines(): | |
| 83 parts = (line.split('#', 2)[0]).split('=', 2) | |
| 84 if len(parts) == 2: | |
| 85 result[parts[0].strip()] = parts[1].strip() | |
| 86 except IOError as e: | |
| 87 logging.error('Failed to load /etc/lsb-release: ' + str(e)) | |
| 88 return result | |
| 89 | |
| 90 def is_supported_platform(): | |
| 91 # Always assume that the system is supported if the config directory or | |
| 92 # session file exist. | |
| 93 if os.path.isdir(CONFIG_DIR) or os.path.isfile(SESSION_FILE_PATH): | |
| 94 return True | |
| 95 | |
| 96 # The host has been tested only on Ubuntu. | |
| 97 lsb_release = load_lsb_release_file() | |
| 98 return (lsb_release.has_key('DISTRIB_ID') and | |
| 99 lsb_release['DISTRIB_ID'] == 'Ubuntu') | |
| 100 | |
| 78 class Config: | 101 class Config: |
| 79 def __init__(self, path): | 102 def __init__(self, path): |
| 80 self.path = path | 103 self.path = path |
| 81 self.data = {} | 104 self.data = {} |
| 82 self.changed = False | 105 self.changed = False |
| 83 | 106 |
| 84 def load(self): | 107 def load(self): |
| 85 """Loads the config from file. | 108 """Loads the config from file. |
| 86 | 109 |
| 87 Raises: | 110 Raises: |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 495 A string containing the command to run, or a list of strings containing | 518 A string containing the command to run, or a list of strings containing |
| 496 the executable program and its arguments, which is suitable for passing as | 519 the executable program and its arguments, which is suitable for passing as |
| 497 the first parameter of subprocess.Popen(). If a suitable session cannot | 520 the first parameter of subprocess.Popen(). If a suitable session cannot |
| 498 be found, returns None. | 521 be found, returns None. |
| 499 """ | 522 """ |
| 500 # If the session wrapper script (see below) is given a specific session as an | 523 # If the session wrapper script (see below) is given a specific session as an |
| 501 # argument (such as ubuntu-2d on Ubuntu 12.04), the wrapper will run that | 524 # argument (such as ubuntu-2d on Ubuntu 12.04), the wrapper will run that |
| 502 # session instead of looking for custom .xsession files in the home directory. | 525 # session instead of looking for custom .xsession files in the home directory. |
| 503 # So it's necessary to test for these files here. | 526 # So it's necessary to test for these files here. |
| 504 XSESSION_FILES = [ | 527 XSESSION_FILES = [ |
| 505 "~/.chrome-remote-desktop-session", | 528 SESSION_FILE_PATH, |
| 506 "~/.xsession", | 529 "~/.xsession", |
| 507 "~/.Xsession" ] | 530 "~/.Xsession" ] |
| 508 for startup_file in XSESSION_FILES: | 531 for startup_file in XSESSION_FILES: |
| 509 startup_file = os.path.expanduser(startup_file) | 532 startup_file = os.path.expanduser(startup_file) |
| 510 if os.path.exists(startup_file): | 533 if os.path.exists(startup_file): |
| 511 # Use the same logic that a Debian system typically uses with ~/.xsession | 534 # Use the same logic that a Debian system typically uses with ~/.xsession |
| 512 # (see /etc/X11/Xsession.d/50x11-common_determine-startup), to determine | 535 # (see /etc/X11/Xsession.d/50x11-common_determine-startup), to determine |
| 513 # exactly how to run this file. | 536 # exactly how to run this file. |
| 514 if os.access(startup_file, os.X_OK): | 537 if os.access(startup_file, os.X_OK): |
| 515 # "/bin/sh -c" is smart about how to execute the session script and | 538 # "/bin/sh -c" is smart about how to execute the session script and |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 895 "available (if the Xvfb server supports this).") | 918 "available (if the Xvfb server supports this).") |
| 896 parser.add_option("-f", "--foreground", dest="foreground", default=False, | 919 parser.add_option("-f", "--foreground", dest="foreground", default=False, |
| 897 action="store_true", | 920 action="store_true", |
| 898 help="Don't run as a background daemon.") | 921 help="Don't run as a background daemon.") |
| 899 parser.add_option("", "--start", dest="start", default=False, | 922 parser.add_option("", "--start", dest="start", default=False, |
| 900 action="store_true", | 923 action="store_true", |
| 901 help="Start the host.") | 924 help="Start the host.") |
| 902 parser.add_option("-k", "--stop", dest="stop", default=False, | 925 parser.add_option("-k", "--stop", dest="stop", default=False, |
| 903 action="store_true", | 926 action="store_true", |
| 904 help="Stop the daemon currently running.") | 927 help="Stop the daemon currently running.") |
| 928 parser.add_option("", "--get-status", dest="get_status", default=False, | |
| 929 action="store_true", | |
| 930 help="Prints host status") | |
| 905 parser.add_option("", "--check-running", dest="check_running", default=False, | 931 parser.add_option("", "--check-running", dest="check_running", default=False, |
| 906 action="store_true", | 932 action="store_true", |
| 907 help="Return 0 if the daemon is running, or 1 otherwise.") | 933 help="Return 0 if the daemon is running, or 1 otherwise.") |
| 908 parser.add_option("", "--config", dest="config", action="store", | 934 parser.add_option("", "--config", dest="config", action="store", |
| 909 help="Use the specified configuration file.") | 935 help="Use the specified configuration file.") |
| 910 parser.add_option("", "--reload", dest="reload", default=False, | 936 parser.add_option("", "--reload", dest="reload", default=False, |
| 911 action="store_true", | 937 action="store_true", |
| 912 help="Signal currently running host to reload the config.") | 938 help="Signal currently running host to reload the config.") |
| 913 parser.add_option("", "--add-user", dest="add_user", default=False, | 939 parser.add_option("", "--add-user", dest="add_user", default=False, |
| 914 action="store_true", | 940 action="store_true", |
| 915 help="Add current user to the chrome-remote-desktop group.") | 941 help="Add current user to the chrome-remote-desktop group.") |
| 916 parser.add_option("", "--host-version", dest="host_version", default=False, | 942 parser.add_option("", "--host-version", dest="host_version", default=False, |
| 917 action="store_true", | 943 action="store_true", |
| 918 help="Prints version of the host.") | 944 help="Prints version of the host.") |
| 919 (options, args) = parser.parse_args() | 945 (options, args) = parser.parse_args() |
| 920 | 946 |
| 921 # Determine the filename of the host configuration and PID files. | 947 # Determine the filename of the host configuration and PID files. |
| 922 if not options.config: | 948 if not options.config: |
| 923 options.config = os.path.join(CONFIG_DIR, "host#%s.json" % g_host_hash) | 949 options.config = os.path.join(CONFIG_DIR, "host#%s.json" % g_host_hash) |
| 924 | 950 |
| 925 # Check for a modal command-line option (start, stop, etc.) | 951 # Check for a modal command-line option (start, stop, etc.) |
| 952 | |
| 953 if options.get_status: | |
| 954 pid = get_daemon_pid() | |
| 955 if pid != 0: | |
| 956 print "STARTED" | |
| 957 elif is_supported_platform(): | |
| 958 print "STOPPED" | |
| 959 else: | |
| 960 print "NOT_IMPLEMENTED" | |
| 961 return 0 | |
| 962 | |
| 963 # TODO(sergeyu): Remove --check-running once NPAPI plugin and NM host are | |
| 964 # updated to always use get-status flag instead. | |
| 926 if options.check_running: | 965 if options.check_running: |
| 927 pid = get_daemon_pid() | 966 pid = get_daemon_pid() |
| 928 return 0 if pid != 0 else 1 | 967 return 0 if pid != 0 else 1 |
| 929 | 968 |
| 930 if options.stop: | 969 if options.stop: |
| 931 pid = get_daemon_pid() | 970 pid = get_daemon_pid() |
| 932 if pid == 0: | 971 if pid == 0: |
| 933 print "The daemon is not currently running" | 972 print "The daemon is not currently running" |
| 934 else: | 973 else: |
| 935 print "Killing process %s" % pid | 974 print "Killing process %s" % pid |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1161 else: | 1200 else: |
| 1162 logging.info("Host exited with status %s." % os.WEXITSTATUS(status)) | 1201 logging.info("Host exited with status %s." % os.WEXITSTATUS(status)) |
| 1163 elif os.WIFSIGNALED(status): | 1202 elif os.WIFSIGNALED(status): |
| 1164 logging.info("Host terminated by signal %s." % os.WTERMSIG(status)) | 1203 logging.info("Host terminated by signal %s." % os.WTERMSIG(status)) |
| 1165 | 1204 |
| 1166 | 1205 |
| 1167 if __name__ == "__main__": | 1206 if __name__ == "__main__": |
| 1168 logging.basicConfig(level=logging.DEBUG, | 1207 logging.basicConfig(level=logging.DEBUG, |
| 1169 format="%(asctime)s:%(levelname)s:%(message)s") | 1208 format="%(asctime)s:%(levelname)s:%(message)s") |
| 1170 sys.exit(main()) | 1209 sys.exit(main()) |
| OLD | NEW |