Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 3 | # Copyright (c) 2009-2010 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 | |
| 9 | import cherrypy |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 10 | import optparse |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 11 | import os |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 12 | import subprocess |
chocobo@google.com | 4dc2581 | 2009-10-27 23:46:26 +0000 | [diff] [blame] | 13 | import sys |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 14 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 15 | import autoupdate |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 16 | import builder |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 17 | |
Chris Sosa | d3de9ef | 2011-01-25 14:38:59 -0800 | [diff] [blame^] | 18 | CACHED_ENTRIES = 12 |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 19 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 20 | # Sets up global to share between classes. |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 21 | global updater |
| 22 | updater = None |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 23 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 24 | def _GetConfig(options): |
| 25 | """Returns the configuration for the devserver.""" |
| 26 | base_config = { 'global': |
| 27 | { 'server.log_request_headers': True, |
| 28 | 'server.protocol_version': 'HTTP/1.1', |
| 29 | 'server.socket_host': '0.0.0.0', |
| 30 | 'server.socket_port': int(options.port), |
Chris Sosa | 374c62d | 2010-10-14 09:13:54 -0700 | [diff] [blame] | 31 | 'server.socket_timeout': 6000, |
| 32 | 'response.timeout': 6000, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 33 | 'tools.staticdir.root': os.getcwd(), |
| 34 | }, |
Chris Sosa | a1ef010 | 2010-10-21 16:22:35 -0700 | [diff] [blame] | 35 | '/build': |
| 36 | { |
| 37 | 'response.timeout': 100000, |
| 38 | }, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 39 | '/update': |
| 40 | { |
| 41 | # Gets rid of cherrypy parsing post file for args. |
| 42 | 'request.process_request_body': False, |
Chris Sosa | f65f4b9 | 2010-10-21 15:57:51 -0700 | [diff] [blame] | 43 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 44 | }, |
| 45 | # Sets up the static dir for file hosting. |
| 46 | '/static': |
| 47 | { 'tools.staticdir.dir': 'static', |
| 48 | 'tools.staticdir.on': True, |
Chris Sosa | f65f4b9 | 2010-10-21 15:57:51 -0700 | [diff] [blame] | 49 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 50 | }, |
| 51 | } |
Chris Sosa | d3de9ef | 2011-01-25 14:38:59 -0800 | [diff] [blame^] | 52 | if options.production: |
| 53 | base_config['global']['server.environment'] = 'production' |
| 54 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 55 | return base_config |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 56 | |
Darin Petkov | e17164a | 2010-08-11 13:24:41 -0700 | [diff] [blame] | 57 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 58 | def _PrepareToServeUpdatesOnly(image_dir): |
| 59 | """Sets up symlink to image_dir for serving purposes.""" |
| 60 | assert os.path.exists(image_dir), '%s must exist.' % image_dir |
| 61 | # If we're serving out of an archived build dir (e.g. a |
| 62 | # buildbot), prepare this webserver's magic 'static/' dir with a |
| 63 | # link to the build archive. |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 64 | cherrypy.log('Preparing autoupdate for "serve updates only" mode.', |
| 65 | 'DEVSERVER') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 66 | if os.path.exists('static/archive'): |
| 67 | if image_dir != os.readlink('static/archive'): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 68 | cherrypy.log('removing stale symlink to %s' % image_dir, 'DEVSERVER') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 69 | os.unlink('static/archive') |
| 70 | os.symlink(image_dir, 'static/archive') |
| 71 | else: |
| 72 | os.symlink(image_dir, 'static/archive') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 73 | cherrypy.log('archive dir: %s ready to be used to serve images.' % image_dir, |
| 74 | 'DEVSERVER') |
| 75 | |
| 76 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 77 | class DevServerRoot(object): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 78 | """The Root Class for the Dev Server. |
| 79 | |
| 80 | CherryPy works as follows: |
| 81 | For each method in this class, cherrpy interprets root/path |
| 82 | as a call to an instance of DevServerRoot->method_name. For example, |
| 83 | a call to http://myhost/build will call build. CherryPy automatically |
| 84 | parses http args and places them as keyword arguments in each method. |
| 85 | For paths http://myhost/update/dir1/dir2, you can use *args so that |
| 86 | cherrypy uses the update method and puts the extra paths in args. |
| 87 | """ |
| 88 | |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 89 | def __init__(self): |
| 90 | self._builder = builder.Builder() |
| 91 | |
| 92 | def build(self, board, pkg, **kwargs): |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 93 | """Builds the package specified.""" |
David Rochberg | 7c79a81 | 2011-01-19 14:24:45 -0500 | [diff] [blame] | 94 | return self._builder.Build(board, pkg, kwargs) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 95 | |
| 96 | def index(self): |
| 97 | return 'Welcome to the Dev Server!' |
| 98 | |
| 99 | def update(self, *args): |
| 100 | label = '/'.join(args) |
| 101 | body_length = int(cherrypy.request.headers['Content-Length']) |
| 102 | data = cherrypy.request.rfile.read(body_length) |
| 103 | return updater.HandleUpdatePing(data, label) |
| 104 | |
| 105 | # Expose actual methods. Necessary to actually have these callable. |
| 106 | build.exposed = True |
| 107 | update.exposed = True |
| 108 | index.exposed = True |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 109 | |
| 110 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 111 | if __name__ == '__main__': |
| 112 | usage = 'usage: %prog [options]' |
| 113 | parser = optparse.OptionParser(usage) |
Sean O'Connor | e38ea15 | 2010-04-16 13:50:40 -0700 | [diff] [blame] | 114 | parser.add_option('--archive_dir', dest='archive_dir', |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 115 | help='serve archived builds only.') |
Chris Sosa | e67b78f | 2010-11-04 17:33:16 -0700 | [diff] [blame] | 116 | parser.add_option('--board', dest='board', |
| 117 | help='When pre-generating update, board for latest image.') |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 118 | parser.add_option('--clear_cache', action='store_true', default=False, |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 119 | help='Clear out all cached udpates and exit') |
Andrew de los Reyes | 9223f13 | 2010-05-07 17:08:17 -0700 | [diff] [blame] | 120 | parser.add_option('--client_prefix', dest='client_prefix', |
| 121 | help='Required prefix for client software version.', |
| 122 | default='MementoSoftwareUpdate') |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 123 | parser.add_option('--exit', action='store_true', default=False, |
| 124 | help='Don\'t start the server (still pregenerate or clear' |
| 125 | 'cache).') |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 126 | parser.add_option('--factory_config', dest='factory_config', |
| 127 | help='Config file for serving images from factory floor.') |
Chris Sosa | 4136e69 | 2010-10-28 23:42:37 -0700 | [diff] [blame] | 128 | parser.add_option('--for_vm', dest='vm', default=False, action='store_true', |
| 129 | help='Update is for a vm image.') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 130 | parser.add_option('--image', dest='image', |
| 131 | help='Force update using this image.') |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 132 | parser.add_option('-p', '--pregenerate_update', action='store_true', |
| 133 | default=False, help='Pre-generate update payload.') |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 134 | parser.add_option('--payload', dest='payload', |
| 135 | help='Use update payload from specified directory.') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 136 | parser.add_option('--port', default=8080, |
| 137 | help='Port for the dev server to use.') |
Chris Sosa | d3de9ef | 2011-01-25 14:38:59 -0800 | [diff] [blame^] | 138 | parser.add_option('--production', action='store_true', default=False, |
| 139 | help='Have the devserver use production values.') |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 140 | parser.add_option('--proxy_port', default=None, |
| 141 | help='Port to have the client connect to (testing support)') |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 142 | parser.add_option('--src_image', default='', |
| 143 | help='Image on remote machine for generating delta update.') |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 144 | parser.add_option('-t', action='store_true', dest='test_image') |
| 145 | parser.add_option('-u', '--urlbase', dest='urlbase', |
| 146 | help='base URL, other than devserver, for update images.') |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 147 | parser.add_option('--validate_factory_config', action="store_true", |
| 148 | dest='validate_factory_config', |
| 149 | help='Validate factory config file, then exit.') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 150 | parser.set_usage(parser.format_help()) |
| 151 | (options, _) = parser.parse_args() |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 152 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 153 | devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 154 | root_dir = os.path.realpath('%s/../..' % devserver_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 155 | serve_only = False |
| 156 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 157 | if options.archive_dir: |
| 158 | static_dir = os.path.realpath(options.archive_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 159 | _PrepareToServeUpdatesOnly(static_dir) |
| 160 | serve_only = True |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 161 | else: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 162 | static_dir = os.path.realpath('%s/static' % devserver_dir) |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 163 | os.system('mkdir -p %s' % static_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 164 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 165 | cache_dir = os.path.join(static_dir, 'cache') |
| 166 | cherrypy.log('Using cache directory %s' % cache_dir, 'DEVSERVER') |
| 167 | |
| 168 | if options.clear_cache: |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 169 | # Clear the cache and exit on error |
| 170 | if os.system('sudo rm -rf %s' % cache_dir) != 0: |
| 171 | cherrypy.log('Failed to clear the cache with %s' % cmd, |
| 172 | 'DEVSERVER') |
| 173 | sys.exit(1) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 174 | |
| 175 | if os.path.exists(cache_dir): |
| 176 | # Clear all but the last N cached updates |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 177 | cmd = ('cd %s; ls -tr | head --lines=-%d | xargs rm -rf' % |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 178 | (cache_dir, CACHED_ENTRIES)) |
| 179 | if os.system(cmd) != 0: |
| 180 | cherrypy.log('Failed to clean up old delta cache files with %s' % cmd, |
| 181 | 'DEVSERVER') |
| 182 | sys.exit(1) |
| 183 | |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 184 | cherrypy.log('Source root is %s' % root_dir, 'DEVSERVER') |
| 185 | cherrypy.log('Serving from %s' % static_dir, 'DEVSERVER') |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 186 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 187 | updater = autoupdate.Autoupdate( |
| 188 | root_dir=root_dir, |
| 189 | static_dir=static_dir, |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 190 | serve_only=serve_only, |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 191 | urlbase=options.urlbase, |
| 192 | test_image=options.test_image, |
| 193 | factory_config_path=options.factory_config, |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 194 | client_prefix=options.client_prefix, |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 195 | forced_image=options.image, |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 196 | forced_payload=options.payload, |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 197 | port=options.port, |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 198 | proxy_port=options.proxy_port, |
Chris Sosa | 4136e69 | 2010-10-28 23:42:37 -0700 | [diff] [blame] | 199 | src_image=options.src_image, |
Chris Sosa | e67b78f | 2010-11-04 17:33:16 -0700 | [diff] [blame] | 200 | vm=options.vm, |
Chris Sosa | 08d55a2 | 2011-01-19 16:08:02 -0800 | [diff] [blame] | 201 | board=options.board, |
| 202 | copy_to_static_root=not options.exit) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 203 | |
| 204 | # Sanity-check for use of validate_factory_config. |
| 205 | if not options.factory_config and options.validate_factory_config: |
| 206 | parser.error('You need a factory_config to validate.') |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 207 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 208 | if options.factory_config: |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 209 | updater.ImportFactoryConfigFile(options.factory_config, |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 210 | options.validate_factory_config) |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 211 | # We don't run the dev server with this option. |
| 212 | if options.validate_factory_config: |
| 213 | sys.exit(0) |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 214 | elif options.pregenerate_update: |
Chris Sosa | e67b78f | 2010-11-04 17:33:16 -0700 | [diff] [blame] | 215 | if not updater.PreGenerateUpdate(): |
| 216 | sys.exit(1) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 217 | |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 218 | # If the command line requested after setup, it's time to do it. |
| 219 | if not options.exit: |
| 220 | cherrypy.quickstart(DevServerRoot(), config=_GetConfig(options)) |