Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 3 | # Copyright (c) 2009-2012 The Chromium OS Authors. All rights reserved. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 7 | """A CherryPy-based webserver to host images and build packages.""" |
| 8 | |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 9 | import cherrypy |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 10 | import json |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 11 | import optparse |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 12 | import os |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 13 | import re |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 14 | import shutil |
Mandeep Singh Baines | 38dcdda | 2012-12-07 17:55:33 -0800 | [diff] [blame] | 15 | import socket |
chocobo@google.com | 4dc2581 | 2009-10-27 23:46:26 +0000 | [diff] [blame] | 16 | import sys |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 17 | import subprocess |
| 18 | import tempfile |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 19 | import types |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 20 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 21 | import autoupdate |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 22 | import common_util |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 23 | import downloader |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 24 | import log_util |
| 25 | |
| 26 | |
| 27 | # Module-local log function. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 28 | def _Log(message, *args): |
| 29 | return log_util.LogWithTag('DEVSERVER', message, *args) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 30 | |
Frank Farzan | 4016087 | 2011-12-12 18:39:18 -0800 | [diff] [blame] | 31 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 32 | CACHED_ENTRIES = 12 |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 33 | |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 34 | TELEMETRY_FOLDER = 'telemetry_src' |
| 35 | TELEMETRY_DEPS = ['dep-telemetry_dep.tar.bz2', |
| 36 | 'dep-page_cycler_dep.tar.bz2', |
Simran Basi | 0d07868 | 2013-03-22 16:40:04 -0700 | [diff] [blame] | 37 | 'dep-chrome_test.tar.bz2', |
| 38 | 'dep-perf_data_dep.tar.bz2'] |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 39 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 40 | # Sets up global to share between classes. |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 41 | updater = None |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 42 | |
Frank Farzan | 4016087 | 2011-12-12 18:39:18 -0800 | [diff] [blame] | 43 | |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 44 | class DevServerError(Exception): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 45 | """Exception class used by this module.""" |
| 46 | pass |
| 47 | |
| 48 | |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 49 | def _LeadingWhiteSpaceCount(string): |
| 50 | """Count the amount of leading whitespace in a string. |
| 51 | |
| 52 | Args: |
| 53 | string: The string to count leading whitespace in. |
| 54 | Returns: |
| 55 | number of white space chars before characters start. |
| 56 | """ |
| 57 | matched = re.match('^\s+', string) |
| 58 | if matched: |
| 59 | return len(matched.group()) |
| 60 | |
| 61 | return 0 |
| 62 | |
| 63 | |
| 64 | def _PrintDocStringAsHTML(func): |
| 65 | """Make a functions docstring somewhat HTML style. |
| 66 | |
| 67 | Args: |
| 68 | func: The function to return the docstring from. |
| 69 | Returns: |
| 70 | A string that is somewhat formated for a web browser. |
| 71 | """ |
| 72 | # TODO(scottz): Make this parse Args/Returns in a prettier way. |
| 73 | # Arguments could be bolded and indented etc. |
| 74 | html_doc = [] |
| 75 | for line in func.__doc__.splitlines(): |
| 76 | leading_space = _LeadingWhiteSpaceCount(line) |
| 77 | if leading_space > 0: |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 78 | line = ' ' * leading_space + line |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 79 | |
| 80 | html_doc.append('<BR>%s' % line) |
| 81 | |
| 82 | return '\n'.join(html_doc) |
| 83 | |
| 84 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 85 | def _GetConfig(options): |
| 86 | """Returns the configuration for the devserver.""" |
Mandeep Singh Baines | 38dcdda | 2012-12-07 17:55:33 -0800 | [diff] [blame] | 87 | |
| 88 | # On a system with IPv6 not compiled into the kernel, |
| 89 | # AF_INET6 sockets will return a socket.error exception. |
| 90 | # On such systems, fall-back to IPv4. |
| 91 | socket_host = '::' |
| 92 | try: |
| 93 | socket.socket(socket.AF_INET6, socket.SOCK_STREAM) |
| 94 | except socket.error: |
| 95 | socket_host = '0.0.0.0' |
| 96 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 97 | base_config = { 'global': |
| 98 | { 'server.log_request_headers': True, |
| 99 | 'server.protocol_version': 'HTTP/1.1', |
Mandeep Singh Baines | 38dcdda | 2012-12-07 17:55:33 -0800 | [diff] [blame] | 100 | 'server.socket_host': socket_host, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 101 | 'server.socket_port': int(options.port), |
Chris Sosa | 374c62d | 2010-10-14 09:13:54 -0700 | [diff] [blame] | 102 | 'response.timeout': 6000, |
Chris Sosa | 6fe2394 | 2012-07-02 15:44:46 -0700 | [diff] [blame] | 103 | 'request.show_tracebacks': True, |
Chris Sosa | 72333d1 | 2012-06-13 11:28:05 -0700 | [diff] [blame] | 104 | 'server.socket_timeout': 60, |
Zdenek Behan | 1347a31 | 2011-02-10 03:59:17 +0100 | [diff] [blame] | 105 | 'tools.staticdir.root': |
| 106 | os.path.dirname(os.path.abspath(sys.argv[0])), |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 107 | }, |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 108 | '/api': |
| 109 | { |
| 110 | # Gets rid of cherrypy parsing post file for args. |
| 111 | 'request.process_request_body': False, |
| 112 | }, |
Chris Sosa | a1ef010 | 2010-10-21 16:22:35 -0700 | [diff] [blame] | 113 | '/build': |
| 114 | { |
| 115 | 'response.timeout': 100000, |
| 116 | }, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 117 | '/update': |
| 118 | { |
| 119 | # Gets rid of cherrypy parsing post file for args. |
| 120 | 'request.process_request_body': False, |
Chris Sosa | f65f4b9 | 2010-10-21 15:57:51 -0700 | [diff] [blame] | 121 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 122 | }, |
| 123 | # Sets up the static dir for file hosting. |
| 124 | '/static': |
| 125 | { 'tools.staticdir.dir': 'static', |
| 126 | 'tools.staticdir.on': True, |
Chris Sosa | f65f4b9 | 2010-10-21 15:57:51 -0700 | [diff] [blame] | 127 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 128 | }, |
| 129 | } |
Chris Sosa | 5f118ef | 2012-07-12 11:37:50 -0700 | [diff] [blame] | 130 | if options.production: |
Chris Sosa | d1ea86b | 2012-07-12 13:35:37 -0700 | [diff] [blame] | 131 | base_config['global'].update({'server.thread_pool': 75}) |
Scott Zawalski | 1c5e7cd | 2012-02-27 13:12:52 -0500 | [diff] [blame] | 132 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 133 | return base_config |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 134 | |
Darin Petkov | e17164a | 2010-08-11 13:24:41 -0700 | [diff] [blame] | 135 | |
Zdenek Behan | 608f46c | 2011-02-19 00:47:16 +0100 | [diff] [blame] | 136 | def _PrepareToServeUpdatesOnly(image_dir, static_dir): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 137 | """Sets up symlink to image_dir for serving purposes.""" |
| 138 | assert os.path.exists(image_dir), '%s must exist.' % image_dir |
| 139 | # If we're serving out of an archived build dir (e.g. a |
| 140 | # buildbot), prepare this webserver's magic 'static/' dir with a |
| 141 | # link to the build archive. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 142 | _Log('Preparing autoupdate for "serve updates only" mode.') |
Zdenek Behan | 608f46c | 2011-02-19 00:47:16 +0100 | [diff] [blame] | 143 | if os.path.lexists('%s/archive' % static_dir): |
| 144 | if image_dir != os.readlink('%s/archive' % static_dir): |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 145 | _Log('removing stale symlink to %s' % image_dir) |
Zdenek Behan | 608f46c | 2011-02-19 00:47:16 +0100 | [diff] [blame] | 146 | os.unlink('%s/archive' % static_dir) |
| 147 | os.symlink(image_dir, '%s/archive' % static_dir) |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 148 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 149 | else: |
Zdenek Behan | 608f46c | 2011-02-19 00:47:16 +0100 | [diff] [blame] | 150 | os.symlink(image_dir, '%s/archive' % static_dir) |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 151 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 152 | _Log('archive dir: %s ready to be used to serve images.' % image_dir) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 153 | |
| 154 | |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 155 | def _GetRecursiveMemberObject(root, member_list): |
| 156 | """Returns an object corresponding to a nested member list. |
| 157 | |
| 158 | Args: |
| 159 | root: the root object to search |
| 160 | member_list: list of nested members to search |
| 161 | Returns: |
| 162 | An object corresponding to the member name list; None otherwise. |
| 163 | """ |
| 164 | for member in member_list: |
| 165 | next_root = root.__class__.__dict__.get(member) |
| 166 | if not next_root: |
| 167 | return None |
| 168 | root = next_root |
| 169 | return root |
| 170 | |
| 171 | |
| 172 | def _IsExposed(name): |
| 173 | """Returns True iff |name| has an `exposed' attribute and it is set.""" |
| 174 | return hasattr(name, 'exposed') and name.exposed |
| 175 | |
| 176 | |
Gilad Arnold | 748c832 | 2012-10-12 09:51:35 -0700 | [diff] [blame] | 177 | def _GetExposedMethod(root, nested_member, ignored=None): |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 178 | """Returns a CherryPy-exposed method, if such exists. |
| 179 | |
| 180 | Args: |
| 181 | root: the root object for searching |
| 182 | nested_member: a slash-joined path to the nested member |
| 183 | ignored: method paths to be ignored |
| 184 | Returns: |
| 185 | A function object corresponding to the path defined by |member_list| from |
| 186 | the |root| object, if the function is exposed and not ignored; None |
| 187 | otherwise. |
| 188 | """ |
Gilad Arnold | 748c832 | 2012-10-12 09:51:35 -0700 | [diff] [blame] | 189 | method = (not (ignored and nested_member in ignored) and |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 190 | _GetRecursiveMemberObject(root, nested_member.split('/'))) |
| 191 | if (method and type(method) == types.FunctionType and _IsExposed(method)): |
| 192 | return method |
| 193 | |
| 194 | |
Gilad Arnold | 748c832 | 2012-10-12 09:51:35 -0700 | [diff] [blame] | 195 | def _FindExposedMethods(root, prefix, unlisted=None): |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 196 | """Finds exposed CherryPy methods. |
| 197 | |
| 198 | Args: |
| 199 | root: the root object for searching |
| 200 | prefix: slash-joined chain of members leading to current object |
| 201 | unlisted: URLs to be excluded regardless of their exposed status |
| 202 | Returns: |
| 203 | List of exposed URLs that are not unlisted. |
| 204 | """ |
| 205 | method_list = [] |
| 206 | for member in sorted(root.__class__.__dict__.keys()): |
| 207 | prefixed_member = prefix + '/' + member if prefix else member |
Gilad Arnold | 748c832 | 2012-10-12 09:51:35 -0700 | [diff] [blame] | 208 | if unlisted and prefixed_member in unlisted: |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 209 | continue |
| 210 | member_obj = root.__class__.__dict__[member] |
| 211 | if _IsExposed(member_obj): |
| 212 | if type(member_obj) == types.FunctionType: |
| 213 | method_list.append(prefixed_member) |
| 214 | else: |
| 215 | method_list += _FindExposedMethods( |
| 216 | member_obj, prefixed_member, unlisted) |
| 217 | return method_list |
| 218 | |
| 219 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 220 | class ApiRoot(object): |
| 221 | """RESTful API for Dev Server information.""" |
| 222 | exposed = True |
| 223 | |
| 224 | @cherrypy.expose |
| 225 | def hostinfo(self, ip): |
| 226 | """Returns a JSON dictionary containing information about the given ip. |
| 227 | |
Gilad Arnold | 1b90839 | 2012-10-05 11:36:27 -0700 | [diff] [blame] | 228 | Args: |
| 229 | ip: address of host whose info is requested |
| 230 | Returns: |
| 231 | A JSON dictionary containing all or some of the following fields: |
| 232 | last_event_type (int): last update event type received |
| 233 | last_event_status (int): last update event status received |
| 234 | last_known_version (string): last known version reported in update ping |
| 235 | forced_update_label (string): update label to force next update ping to |
| 236 | use, set by setnextupdate |
| 237 | See the OmahaEvent class in update_engine/omaha_request_action.h for |
| 238 | event type and status code definitions. If the ip does not exist an empty |
| 239 | string is returned. |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 240 | |
Gilad Arnold | 1b90839 | 2012-10-05 11:36:27 -0700 | [diff] [blame] | 241 | Example URL: |
| 242 | http://myhost/api/hostinfo?ip=192.168.1.5 |
| 243 | """ |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 244 | return updater.HandleHostInfoPing(ip) |
| 245 | |
| 246 | @cherrypy.expose |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 247 | def hostlog(self, ip): |
Gilad Arnold | 1b90839 | 2012-10-05 11:36:27 -0700 | [diff] [blame] | 248 | """Returns a JSON object containing a log of host event. |
| 249 | |
| 250 | Args: |
| 251 | ip: address of host whose event log is requested, or `all' |
| 252 | Returns: |
| 253 | A JSON encoded list (log) of dictionaries (events), each of which |
| 254 | containing a `timestamp' and other event fields, as described under |
| 255 | /api/hostinfo. |
| 256 | |
| 257 | Example URL: |
| 258 | http://myhost/api/hostlog?ip=192.168.1.5 |
| 259 | """ |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 260 | return updater.HandleHostLogPing(ip) |
| 261 | |
| 262 | @cherrypy.expose |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 263 | def setnextupdate(self, ip): |
| 264 | """Allows the response to the next update ping from a host to be set. |
| 265 | |
| 266 | Takes the IP of the host and an update label as normally provided to the |
Gilad Arnold | 1b90839 | 2012-10-05 11:36:27 -0700 | [diff] [blame] | 267 | /update command. |
| 268 | """ |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 269 | body_length = int(cherrypy.request.headers['Content-Length']) |
| 270 | label = cherrypy.request.rfile.read(body_length) |
| 271 | |
| 272 | if label: |
| 273 | label = label.strip() |
| 274 | if label: |
| 275 | return updater.HandleSetUpdatePing(ip, label) |
| 276 | raise cherrypy.HTTPError(400, 'No label provided.') |
| 277 | |
| 278 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 279 | @cherrypy.expose |
| 280 | def fileinfo(self, *path_args): |
| 281 | """Returns information about a given staged file. |
| 282 | |
| 283 | Args: |
| 284 | path_args: path to the file inside the server's static staging directory |
| 285 | Returns: |
| 286 | A JSON encoded dictionary with information about the said file, which may |
| 287 | contain the following keys/values: |
Gilad Arnold | 1b90839 | 2012-10-05 11:36:27 -0700 | [diff] [blame] | 288 | size (int): the file size in bytes |
| 289 | sha1 (string): a base64 encoded SHA1 hash |
| 290 | sha256 (string): a base64 encoded SHA256 hash |
| 291 | |
| 292 | Example URL: |
| 293 | http://myhost/api/fileinfo/some/path/to/file |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 294 | """ |
| 295 | file_path = os.path.join(updater.static_dir, *path_args) |
| 296 | if not os.path.exists(file_path): |
| 297 | raise DevServerError('file not found: %s' % file_path) |
| 298 | try: |
| 299 | file_size = os.path.getsize(file_path) |
| 300 | file_sha1 = common_util.GetFileSha1(file_path) |
| 301 | file_sha256 = common_util.GetFileSha256(file_path) |
| 302 | except os.error, e: |
| 303 | raise DevServerError('failed to get info for file %s: %s' % |
Gilad Arnold | 2902159 | 2013-02-15 16:19:17 -0800 | [diff] [blame] | 304 | (file_path, e)) |
| 305 | |
| 306 | is_delta = autoupdate.Autoupdate.IsDeltaFormatFile(file_path) |
| 307 | |
| 308 | return json.dumps({ |
| 309 | autoupdate.Autoupdate.SIZE_ATTR: file_size, |
| 310 | autoupdate.Autoupdate.SHA1_ATTR: file_sha1, |
| 311 | autoupdate.Autoupdate.SHA256_ATTR: file_sha256, |
| 312 | autoupdate.Autoupdate.ISDELTA_ATTR: is_delta |
| 313 | }) |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 314 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 315 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 316 | class DevServerRoot(object): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 317 | """The Root Class for the Dev Server. |
| 318 | |
| 319 | CherryPy works as follows: |
| 320 | For each method in this class, cherrpy interprets root/path |
| 321 | as a call to an instance of DevServerRoot->method_name. For example, |
| 322 | a call to http://myhost/build will call build. CherryPy automatically |
| 323 | parses http args and places them as keyword arguments in each method. |
| 324 | For paths http://myhost/update/dir1/dir2, you can use *args so that |
| 325 | cherrypy uses the update method and puts the extra paths in args. |
| 326 | """ |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 327 | # Method names that should not be listed on the index page. |
| 328 | _UNLISTED_METHODS = ['index', 'doc'] |
| 329 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 330 | api = ApiRoot() |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 331 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 332 | def __init__(self): |
Nick Sanders | 7dcaa2e | 2011-08-04 15:20:41 -0700 | [diff] [blame] | 333 | self._builder = None |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 334 | self._telemetry_lock_dict = common_util.LockDict() |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 335 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 336 | @cherrypy.expose |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 337 | def build(self, board, pkg, **kwargs): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 338 | """Builds the package specified.""" |
Nick Sanders | 7dcaa2e | 2011-08-04 15:20:41 -0700 | [diff] [blame] | 339 | import builder |
| 340 | if self._builder is None: |
| 341 | self._builder = builder.Builder() |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 342 | return self._builder.Build(board, pkg, kwargs) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 343 | |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 344 | @staticmethod |
| 345 | def _canonicalize_archive_url(archive_url): |
| 346 | """Canonicalizes archive_url strings. |
| 347 | |
| 348 | Raises: |
| 349 | DevserverError: if archive_url is not set. |
| 350 | """ |
| 351 | if archive_url: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 352 | if not archive_url.startswith('gs://'): |
| 353 | raise DevServerError("Archive URL isn't from Google Storage.") |
| 354 | |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 355 | return archive_url.rstrip('/') |
| 356 | else: |
| 357 | raise DevServerError("Must specify an archive_url in the request") |
| 358 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 359 | @cherrypy.expose |
Frank Farzan | bcb571e | 2012-01-03 11:48:17 -0800 | [diff] [blame] | 360 | def download(self, **kwargs): |
| 361 | """Downloads and archives full/delta payloads from Google Storage. |
| 362 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 363 | THIS METHOD IS DEPRECATED: use stage(..., artifacts=...) instead. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 364 | This methods downloads artifacts. It may download artifacts in the |
| 365 | background in which case a caller should call wait_for_status to get |
| 366 | the status of the background artifact downloads. They should use the same |
| 367 | args passed to download. |
| 368 | |
Frank Farzan | bcb571e | 2012-01-03 11:48:17 -0800 | [diff] [blame] | 369 | Args: |
| 370 | archive_url: Google Storage URL for the build. |
| 371 | |
| 372 | Example URL: |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 373 | http://myhost/download?archive_url=gs://chromeos-image-archive/ |
| 374 | x86-generic/R17-1208.0.0-a1-b338 |
Frank Farzan | bcb571e | 2012-01-03 11:48:17 -0800 | [diff] [blame] | 375 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 376 | return self.stage(archive_url=kwargs.get('archive_url'), |
| 377 | artifacts='full_payload,test_suites,stateful') |
| 378 | |
| 379 | @cherrypy.expose |
| 380 | def stage(self, **kwargs): |
| 381 | """Downloads and caches the artifacts from Google Storage URL. |
| 382 | |
| 383 | Downloads and caches the artifacts Google Storage URL. Returns once these |
| 384 | have been downloaded on the devserver. A call to this will attempt to cache |
| 385 | non-specified artifacts in the background for the given from the given URL |
| 386 | following the principle of spatial locality. Spatial locality of different |
| 387 | artifacts is explicitly defined in the build_artifact module. |
| 388 | |
| 389 | These artifacts will then be available from the static/ sub-directory of |
| 390 | the devserver. |
| 391 | |
| 392 | Args: |
| 393 | archive_url: Google Storage URL for the build. |
| 394 | artifacts: Comma separated list of artifacts to download. |
| 395 | |
| 396 | Example: |
| 397 | To download the autotest and test suites tarballs: |
| 398 | http://devserver_url:<port>/stage?archive_url=gs://your_url/path& |
| 399 | artifacts=autotest,test_suites |
| 400 | To download the full update payload: |
| 401 | http://devserver_url:<port>/stage?archive_url=gs://your_url/path& |
| 402 | artifacts=full_payload |
| 403 | |
| 404 | For both these examples, one could find these artifacts at: |
| 405 | http://devserver_url:<port>/static/archive/<relative_path>* |
| 406 | |
| 407 | Note for this example, relative path is the archive_url stripped of its |
| 408 | basename i.e. path/ in the examples above. Specific example: |
| 409 | |
| 410 | gs://chromeos-image-archive/x86-mario-release/R26-3920.0.0 |
| 411 | |
| 412 | Will get staged to: |
| 413 | |
| 414 | http://devserver_url:<port>/static/archive/x86-mario-release/R26-3920.0.0 |
| 415 | """ |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 416 | archive_url = self._canonicalize_archive_url(kwargs.get('archive_url')) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 417 | artifacts = kwargs.get('artifacts', '') |
| 418 | if not artifacts: |
| 419 | raise DevServerError('No artifacts specified.') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 420 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 421 | downloader.Downloader(updater.static_dir, archive_url).Download( |
| 422 | artifacts.split(',')) |
| 423 | return 'Success' |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 424 | |
| 425 | @cherrypy.expose |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 426 | def setup_telemetry(self, **kwargs): |
| 427 | """Extracts and sets up telemetry |
| 428 | |
| 429 | This method goes through the telemetry deps packages, and stages them on |
| 430 | the devserver to be used by the drones and the telemetry tests. |
| 431 | |
| 432 | Args: |
| 433 | archive_url: Google Storage URL for the build. |
| 434 | |
| 435 | Returns: |
| 436 | Path to the source folder for the telemetry codebase once it is staged. |
| 437 | """ |
| 438 | archive_url = kwargs.get('archive_url') |
| 439 | self.stage(archive_url=archive_url, artifacts='autotest') |
| 440 | |
| 441 | build = '/'.join(downloader.Downloader.ParseUrl(archive_url)) |
| 442 | build_path = os.path.join(updater.static_dir, build) |
| 443 | deps_path = os.path.join(build_path, 'autotest/packages') |
| 444 | telemetry_path = os.path.join(build_path, TELEMETRY_FOLDER) |
| 445 | src_folder = os.path.join(telemetry_path, 'src') |
| 446 | |
| 447 | with self._telemetry_lock_dict.lock(telemetry_path): |
| 448 | if os.path.exists(src_folder): |
| 449 | # Telemetry is already fully stage return |
| 450 | return src_folder |
| 451 | |
| 452 | common_util.MkDirP(telemetry_path) |
| 453 | |
| 454 | # Copy over the required deps tar balls to the telemetry directory. |
| 455 | for dep in TELEMETRY_DEPS: |
| 456 | dep_path = os.path.join(deps_path, dep) |
Simran Basi | 0d07868 | 2013-03-22 16:40:04 -0700 | [diff] [blame] | 457 | if not os.path.exists(dep_path): |
| 458 | # This dep does not exist (could be new), do not extract it. |
| 459 | continue |
Simran Basi | 4baad08 | 2013-02-14 13:39:18 -0800 | [diff] [blame] | 460 | try: |
| 461 | common_util.ExtractTarball(dep_path, telemetry_path) |
| 462 | except common_util.CommonUtilError as e: |
| 463 | shutil.rmtree(telemetry_path) |
| 464 | raise DevServerError(str(e)) |
| 465 | |
| 466 | # By default all the tarballs extract to test_src but some parts of |
| 467 | # the telemetry code specifically hardcoded to exist inside of 'src'. |
| 468 | test_src = os.path.join(telemetry_path, 'test_src') |
| 469 | try: |
| 470 | shutil.move(test_src, src_folder) |
| 471 | except shutil.Error: |
| 472 | # This can occur if src_folder already exists. Remove and retry move. |
| 473 | shutil.rmtree(src_folder) |
| 474 | raise DevServerError('Failure in telemetry setup for build %s. Appears' |
| 475 | ' that the test_src to src move failed.' % build) |
| 476 | |
| 477 | return src_folder |
| 478 | |
| 479 | @cherrypy.expose |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 480 | def wait_for_status(self, **kwargs): |
| 481 | """Waits for background artifacts to be downloaded from Google Storage. |
| 482 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 483 | THIS METHOD IS DEPRECATED: use stage(..., artifacts=...) instead. |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 484 | Args: |
| 485 | archive_url: Google Storage URL for the build. |
| 486 | |
| 487 | Example URL: |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 488 | http://myhost/wait_for_status?archive_url=gs://chromeos-image-archive/ |
| 489 | x86-generic/R17-1208.0.0-a1-b338 |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 490 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 491 | return self.stage(archive_url=kwargs.get('archive_url'), |
| 492 | artifacts='full_payload,test_suites,autotest,stateful') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 493 | |
| 494 | @cherrypy.expose |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 495 | def stage_debug(self, **kwargs): |
| 496 | """Downloads and stages debug symbol payloads from Google Storage. |
| 497 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 498 | THIS METHOD IS DEPRECATED: use stage(..., artifacts=...) instead. |
| 499 | This methods downloads the debug symbol build artifact |
| 500 | synchronously, and then stages it for use by symbolicate_dump. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 501 | |
| 502 | Args: |
| 503 | archive_url: Google Storage URL for the build. |
| 504 | |
| 505 | Example URL: |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 506 | http://myhost/stage_debug?archive_url=gs://chromeos-image-archive/ |
| 507 | x86-generic/R17-1208.0.0-a1-b338 |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 508 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 509 | return self.stage(archive_url=kwargs.get('archive_url'), |
| 510 | artifacts='symbols') |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 511 | |
| 512 | @cherrypy.expose |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 513 | def symbolicate_dump(self, minidump, **kwargs): |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 514 | """Symbolicates a minidump using pre-downloaded symbols, returns it. |
| 515 | |
| 516 | Callers will need to POST to this URL with a body of MIME-type |
| 517 | "multipart/form-data". |
| 518 | The body should include a single argument, 'minidump', containing the |
| 519 | binary-formatted minidump to symbolicate. |
| 520 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 521 | Args: |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 522 | archive_url: Google Storage URL for the build. |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 523 | minidump: The binary minidump file to symbolicate. |
| 524 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 525 | # Ensure the symbols have been staged. |
| 526 | archive_url = self._canonicalize_archive_url(kwargs.get('archive_url')) |
| 527 | if self.stage(archive_url=archive_url, artifacts='symbols') != 'Success': |
| 528 | raise DevServerError('Failed to stage symbols for %s' % archive_url) |
| 529 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 530 | to_return = '' |
| 531 | with tempfile.NamedTemporaryFile() as local: |
| 532 | while True: |
| 533 | data = minidump.file.read(8192) |
| 534 | if not data: |
| 535 | break |
| 536 | local.write(data) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 537 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 538 | local.flush() |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 539 | |
| 540 | symbols_directory = os.path.join(downloader.Downloader.GetBuildDir( |
| 541 | updater.static_dir, archive_url), 'debug', 'breakpad') |
| 542 | |
| 543 | stackwalk = subprocess.Popen( |
| 544 | ['minidump_stackwalk', local.name, symbols_directory], |
| 545 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 546 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 547 | to_return, error_text = stackwalk.communicate() |
| 548 | if stackwalk.returncode != 0: |
| 549 | raise DevServerError("Can't generate stack trace: %s (rc=%d)" % ( |
| 550 | error_text, stackwalk.returncode)) |
| 551 | |
| 552 | return to_return |
| 553 | |
| 554 | @cherrypy.expose |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 555 | def latestbuild(self, **params): |
| 556 | """Return a string representing the latest build for a given target. |
| 557 | |
| 558 | Args: |
| 559 | target: The build target, typically a combination of the board and the |
| 560 | type of build e.g. x86-mario-release. |
| 561 | milestone: The milestone to filter builds on. E.g. R16. Optional, if not |
| 562 | provided the latest RXX build will be returned. |
| 563 | Returns: |
| 564 | A string representation of the latest build if one exists, i.e. |
| 565 | R19-1993.0.0-a1-b1480. |
| 566 | An empty string if no latest could be found. |
| 567 | """ |
| 568 | if not params: |
| 569 | return _PrintDocStringAsHTML(self.latestbuild) |
| 570 | |
| 571 | if 'target' not in params: |
| 572 | raise cherrypy.HTTPError('500 Internal Server Error', |
| 573 | 'Error: target= is required!') |
| 574 | try: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 575 | return common_util.GetLatestBuildVersion( |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 576 | updater.static_dir, params['target'], |
| 577 | milestone=params.get('milestone')) |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 578 | except common_util.CommonUtilError as errmsg: |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 579 | raise cherrypy.HTTPError('500 Internal Server Error', str(errmsg)) |
| 580 | |
| 581 | @cherrypy.expose |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 582 | def controlfiles(self, **params): |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 583 | """Return a control file or a list of all known control files. |
| 584 | |
| 585 | Example URL: |
| 586 | To List all control files: |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 587 | http://dev-server/controlfiles?board=x86-alex-release&build=R18-1514.0.0 |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 588 | To return the contents of a path: |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 589 | http://dev-server/controlfiles?board=x86-alex-release&build=R18-1514.0.0&control_path=client/sleeptest/control |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 590 | |
| 591 | Args: |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 592 | build: The build i.e. x86-alex-release/R18-1514.0.0-a1-b1450. |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 593 | control_path: If you want the contents of a control file set this |
| 594 | to the path. E.g. client/site_tests/sleeptest/control |
| 595 | Optional, if not provided return a list of control files is returned. |
| 596 | Returns: |
| 597 | Contents of a control file if control_path is provided. |
| 598 | A list of control files if no control_path is provided. |
| 599 | """ |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 600 | if not params: |
| 601 | return _PrintDocStringAsHTML(self.controlfiles) |
| 602 | |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 603 | if 'build' not in params: |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 604 | raise cherrypy.HTTPError('500 Internal Server Error', |
Scott Zawalski | 84a39c9 | 2012-01-13 15:12:42 -0500 | [diff] [blame] | 605 | 'Error: build= is required!') |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 606 | |
| 607 | if 'control_path' not in params: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 608 | return common_util.GetControlFileList( |
| 609 | updater.static_dir, params['build']) |
Scott Zawalski | 4647ce6 | 2012-01-03 17:17:28 -0500 | [diff] [blame] | 610 | else: |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 611 | return common_util.GetControlFile( |
| 612 | updater.static_dir, params['build'], params['control_path']) |
Frank Farzan | 4016087 | 2011-12-12 18:39:18 -0800 | [diff] [blame] | 613 | |
| 614 | @cherrypy.expose |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 615 | def stage_images(self, **kwargs): |
| 616 | """Downloads and stages a Chrome OS image from Google Storage. |
| 617 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 618 | THIS METHOD IS DEPRECATED: use stage(..., artifacts=...) instead. |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 619 | This method downloads a zipped archive from a specified GS location, then |
| 620 | extracts and stages the specified list of images and stages them under |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 621 | static/BOARD/BUILD/. Download is synchronous. |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 622 | |
| 623 | Args: |
| 624 | archive_url: Google Storage URL for the build. |
| 625 | image_types: comma-separated list of images to download, may include |
| 626 | 'test', 'recovery', and 'base' |
| 627 | |
| 628 | Example URL: |
| 629 | http://myhost/stage_images?archive_url=gs://chromeos-image-archive/ |
| 630 | x86-generic/R17-1208.0.0-a1-b338&image_types=test,base |
| 631 | """ |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 632 | image_types = kwargs.get('image_types').split(',') |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 633 | image_types_list = [image + '_image' for image in image_types] |
| 634 | self.stage(archive_url=kwargs.get('archive_url'), artifacts=','.join( |
| 635 | image_types_list)) |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 636 | |
| 637 | @cherrypy.expose |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 638 | def index(self): |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 639 | """Presents a welcome message and documentation links.""" |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 640 | return ('Welcome to the Dev Server!<br>\n' |
| 641 | '<br>\n' |
| 642 | 'Here are the available methods, click for documentation:<br>\n' |
| 643 | '<br>\n' |
| 644 | '%s' % |
| 645 | '<br>\n'.join( |
| 646 | [('<a href=doc/%s>%s</a>' % (name, name)) |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 647 | for name in _FindExposedMethods( |
| 648 | self, '', unlisted=self._UNLISTED_METHODS)])) |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 649 | |
| 650 | @cherrypy.expose |
| 651 | def doc(self, *args): |
| 652 | """Shows the documentation for available methods / URLs. |
| 653 | |
| 654 | Example: |
| 655 | http://myhost/doc/update |
| 656 | """ |
Gilad Arnold | d5ebaaa | 2012-10-02 11:52:38 -0700 | [diff] [blame] | 657 | name = '/'.join(args) |
| 658 | method = _GetExposedMethod(self, name) |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 659 | if not method: |
| 660 | raise DevServerError("No exposed method named `%s'" % name) |
| 661 | if not method.__doc__: |
| 662 | raise DevServerError("No documentation for exposed method `%s'" % name) |
| 663 | return '<pre>\n%s</pre>' % method.__doc__ |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 664 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 665 | @cherrypy.expose |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 666 | def update(self, *args): |
Gilad Arnold | f8f769f | 2012-09-24 08:43:01 -0700 | [diff] [blame] | 667 | """Handles an update check from a Chrome OS client. |
| 668 | |
| 669 | The HTTP request should contain the standard Omaha-style XML blob. The URL |
| 670 | line may contain an additional intermediate path to the update payload. |
| 671 | |
| 672 | Example: |
| 673 | http://myhost/update/optional/path/to/payload |
| 674 | """ |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 675 | label = '/'.join(args) |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 676 | body_length = int(cherrypy.request.headers.get('Content-Length', 0)) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 677 | data = cherrypy.request.rfile.read(body_length) |
| 678 | return updater.HandleUpdatePing(data, label) |
| 679 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 680 | |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 681 | def _CleanCache(cache_dir, wipe): |
| 682 | """Wipes any excess cached items in the cache_dir. |
| 683 | |
| 684 | Args: |
| 685 | cache_dir: the directory we are wiping from. |
| 686 | wipe: If True, wipe all the contents -- not just the excess. |
| 687 | """ |
| 688 | if wipe: |
| 689 | # Clear the cache and exit on error. |
| 690 | cmd = 'rm -rf %s/*' % cache_dir |
| 691 | if os.system(cmd) != 0: |
| 692 | _Log('Failed to clear the cache with %s' % cmd) |
| 693 | sys.exit(1) |
| 694 | else: |
| 695 | # Clear all but the last N cached updates |
| 696 | cmd = ('cd %s; ls -tr | head --lines=-%d | xargs rm -rf' % |
| 697 | (cache_dir, CACHED_ENTRIES)) |
| 698 | if os.system(cmd) != 0: |
| 699 | _Log('Failed to clean up old delta cache files with %s' % cmd) |
| 700 | sys.exit(1) |
| 701 | |
| 702 | |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 703 | def main(): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 704 | usage = 'usage: %prog [options]' |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 705 | parser = optparse.OptionParser(usage=usage) |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 706 | parser.add_option('--archive_dir', |
| 707 | metavar='PATH', |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 708 | help='Enables serve-only mode. Serves archived builds only') |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 709 | parser.add_option('--board', |
| 710 | help='when pre-generating update, board for latest image') |
| 711 | parser.add_option('--clear_cache', |
Satoru Takabayashi | d733cbe | 2011-11-15 09:36:32 -0800 | [diff] [blame] | 712 | action='store_true', default=False, |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 713 | help='clear out all cached updates and exit') |
| 714 | parser.add_option('--critical_update', |
| 715 | action='store_true', default=False, |
| 716 | help='present update payload as critical') |
| 717 | parser.add_option('--data_dir', |
| 718 | metavar='PATH', |
| 719 | default=os.path.dirname(os.path.abspath(sys.argv[0])), |
| 720 | help='writable directory where static lives') |
| 721 | parser.add_option('--exit', |
| 722 | action='store_true', |
| 723 | help='do not start server (yet pregenerate/clear cache)') |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 724 | parser.add_option('--for_vm', |
| 725 | dest='vm', action='store_true', |
| 726 | help='update is for a vm image') |
Gilad Arnold | 8318eac | 2012-10-04 12:52:23 -0700 | [diff] [blame] | 727 | parser.add_option('--host_log', |
| 728 | action='store_true', default=False, |
| 729 | help='record history of host update events (/api/hostlog)') |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 730 | parser.add_option('--image', |
| 731 | metavar='FILE', |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 732 | help='Force update using this image. Can only be used when ' |
| 733 | 'not in serve-only mode as it is used to generate a ' |
| 734 | 'payload.') |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 735 | parser.add_option('--logfile', |
| 736 | metavar='PATH', |
| 737 | help='log output to this file instead of stdout') |
Gilad Arnold | a564b4b | 2012-10-04 10:32:44 -0700 | [diff] [blame] | 738 | parser.add_option('--max_updates', |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 739 | metavar='NUM', default= -1, type='int', |
Gilad Arnold | a564b4b | 2012-10-04 10:32:44 -0700 | [diff] [blame] | 740 | help='maximum number of update checks handled positively ' |
| 741 | '(default: unlimited)') |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 742 | parser.add_option('-p', '--pregenerate_update', |
| 743 | action='store_true', default=False, |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 744 | help='pre-generate update payload. Can only be used when ' |
| 745 | 'not in serve-only mode as it is used to generate a ' |
| 746 | 'payload.') |
Gilad Arnold | 9714d9b | 2012-10-04 10:09:42 -0700 | [diff] [blame] | 747 | parser.add_option('--payload', |
| 748 | metavar='PATH', |
| 749 | help='use update payload from specified directory') |
| 750 | parser.add_option('--port', |
| 751 | default=8080, type='int', |
| 752 | help='port for the dev server to use (default: 8080)') |
| 753 | parser.add_option('--private_key', |
| 754 | metavar='PATH', default=None, |
| 755 | help='path to the private key in pem format') |
| 756 | parser.add_option('--production', |
| 757 | action='store_true', default=False, |
| 758 | help='have the devserver use production values') |
| 759 | parser.add_option('--proxy_port', |
| 760 | metavar='PORT', default=None, type='int', |
| 761 | help='port to have the client connect to (testing support)') |
| 762 | parser.add_option('--remote_payload', |
| 763 | action='store_true', default=False, |
| 764 | help='Payload is being served from a remote machine') |
| 765 | parser.add_option('--src_image', |
| 766 | metavar='PATH', default='', |
| 767 | help='source image for generating delta updates from') |
| 768 | parser.add_option('-t', '--test_image', |
| 769 | action='store_true', |
| 770 | help='whether or not to use test images') |
| 771 | parser.add_option('-u', '--urlbase', |
| 772 | metavar='URL', |
| 773 | help='base URL for update images, other than the devserver') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 774 | (options, _) = parser.parse_args() |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 775 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 776 | devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 777 | root_dir = os.path.realpath('%s/../..' % devserver_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 778 | serve_only = False |
| 779 | |
Zdenek Behan | 608f46c | 2011-02-19 00:47:16 +0100 | [diff] [blame] | 780 | static_dir = os.path.realpath('%s/static' % options.data_dir) |
| 781 | os.system('mkdir -p %s' % static_dir) |
| 782 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 783 | if options.archive_dir: |
Zdenek Behan | 608f46c | 2011-02-19 00:47:16 +0100 | [diff] [blame] | 784 | # TODO(zbehan) Remove legacy support: |
| 785 | # archive_dir is the directory where static/archive will point. |
| 786 | # If this is an absolute path, all is fine. If someone calls this |
| 787 | # using a relative path, that is relative to src/platform/dev/. |
| 788 | # That use case is unmaintainable, but since applications use it |
| 789 | # with =./static, instead of a boolean flag, we'll make this relative |
| 790 | # to devserver_dir to keep these unbroken. For now. |
| 791 | archive_dir = options.archive_dir |
| 792 | if not os.path.isabs(archive_dir): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 793 | archive_dir = os.path.realpath(os.path.join(devserver_dir, archive_dir)) |
Zdenek Behan | 608f46c | 2011-02-19 00:47:16 +0100 | [diff] [blame] | 794 | _PrepareToServeUpdatesOnly(archive_dir, static_dir) |
Zdenek Behan | 6d93e55 | 2011-03-02 22:35:49 +0100 | [diff] [blame] | 795 | static_dir = os.path.realpath(archive_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 796 | serve_only = True |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 797 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 798 | cache_dir = os.path.join(static_dir, 'cache') |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 799 | # If our devserver is only supposed to serve payloads, we shouldn't be mucking |
| 800 | # with the cache at all. If the devserver hadn't previously generated a cache |
| 801 | # and is expected, the caller is using it wrong. |
| 802 | if serve_only: |
| 803 | # Extra check to make sure we're not being called incorrectly. |
| 804 | if (options.clear_cache or options.exit or options.pregenerate_update or |
| 805 | options.board or options.image): |
| 806 | parser.error('Incompatible flags detected for serve_only mode.') |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 807 | |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 808 | elif os.path.exists(cache_dir): |
| 809 | _CleanCache(cache_dir, options.clear_cache) |
Chris Sosa | 6b8c374 | 2011-01-31 12:12:17 -0800 | [diff] [blame] | 810 | else: |
| 811 | os.makedirs(cache_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 812 | |
Chris Sosa | dbc2008 | 2012-12-10 13:39:11 -0800 | [diff] [blame] | 813 | _Log('Using cache directory %s' % cache_dir) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 814 | _Log('Data dir is %s' % options.data_dir) |
| 815 | _Log('Source root is %s' % root_dir) |
| 816 | _Log('Serving from %s' % static_dir) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 817 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 818 | # We allow global use here to share with cherrypy classes. |
| 819 | # pylint: disable=W0603 |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 820 | global updater |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 821 | updater = autoupdate.Autoupdate( |
| 822 | root_dir=root_dir, |
| 823 | static_dir=static_dir, |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 824 | serve_only=serve_only, |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 825 | urlbase=options.urlbase, |
| 826 | test_image=options.test_image, |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 827 | forced_image=options.image, |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 828 | payload_path=options.payload, |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 829 | proxy_port=options.proxy_port, |
Chris Sosa | 4136e69 | 2010-10-28 23:42:37 -0700 | [diff] [blame] | 830 | src_image=options.src_image, |
Chris Sosa | e67b78f | 2010-11-04 17:33:16 -0700 | [diff] [blame] | 831 | vm=options.vm, |
Chris Sosa | 08d55a2 | 2011-01-19 16:08:02 -0800 | [diff] [blame] | 832 | board=options.board, |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 833 | copy_to_static_root=not options.exit, |
| 834 | private_key=options.private_key, |
Satoru Takabayashi | d733cbe | 2011-11-15 09:36:32 -0800 | [diff] [blame] | 835 | critical_update=options.critical_update, |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 836 | remote_payload=options.remote_payload, |
Gilad Arnold | a564b4b | 2012-10-04 10:32:44 -0700 | [diff] [blame] | 837 | max_updates=options.max_updates, |
Gilad Arnold | 8318eac | 2012-10-04 12:52:23 -0700 | [diff] [blame] | 838 | host_log=options.host_log, |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 839 | ) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 840 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 841 | if options.pregenerate_update: |
| 842 | updater.PreGenerateUpdate() |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 843 | |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 844 | # If the command line requested after setup, it's time to do it. |
| 845 | if not options.exit: |
Chris Sosa | 66e2d9c | 2012-07-11 14:14:14 -0700 | [diff] [blame] | 846 | # Handle options that must be set globally in cherrypy. |
Chris Sosa | 2f1c41e | 2012-07-10 14:32:33 -0700 | [diff] [blame] | 847 | if options.production: |
Chris Sosa | 66e2d9c | 2012-07-11 14:14:14 -0700 | [diff] [blame] | 848 | cherrypy.config.update({'environment': 'production'}) |
| 849 | if not options.logfile: |
| 850 | cherrypy.config.update({'log.screen': True}) |
| 851 | else: |
| 852 | cherrypy.config.update({'log.error_file': options.logfile, |
| 853 | 'log.access_file': options.logfile}) |
Chris Sosa | 2f1c41e | 2012-07-10 14:32:33 -0700 | [diff] [blame] | 854 | |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 855 | cherrypy.quickstart(DevServerRoot(), config=_GetConfig(options)) |
Chris Sosa | cde6bf4 | 2012-05-31 18:36:39 -0700 | [diff] [blame] | 856 | |
| 857 | |
| 858 | if __name__ == '__main__': |
| 859 | main() |