Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 3 | # Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved. |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [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-12 02:49:01 | [diff] [blame] | 7 | """A CherryPy-based webserver to host images and build packages.""" |
| 8 | |
| 9 | import cherrypy |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 10 | import optparse |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 11 | import os |
[email protected] | 4dc2581 | 2009-10-27 23:46:26 | [diff] [blame] | 12 | import sys |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 13 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 14 | import autoupdate |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 15 | |
| 16 | # Sets up global to share between classes. |
[email protected] | 21a5ca3 | 2009-11-04 18:23:23 | [diff] [blame] | 17 | global updater |
| 18 | updater = None |
[email protected] | ded2240 | 2009-10-26 22:36:21 | [diff] [blame] | 19 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 20 | def _GetConfig(options): |
| 21 | """Returns the configuration for the devserver.""" |
| 22 | base_config = { 'global': |
| 23 | { 'server.log_request_headers': True, |
| 24 | 'server.protocol_version': 'HTTP/1.1', |
| 25 | 'server.socket_host': '0.0.0.0', |
| 26 | 'server.socket_port': int(options.port), |
Chris Sosa | 374c62d | 2010-10-14 16:13:54 | [diff] [blame] | 27 | 'server.socket_timeout': 6000, |
| 28 | 'response.timeout': 6000, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 29 | 'tools.staticdir.root': os.getcwd(), |
| 30 | }, |
Chris Sosa | a1ef010 | 2010-10-21 23:22:35 | [diff] [blame] | 31 | '/build': |
| 32 | { |
| 33 | 'response.timeout': 100000, |
| 34 | }, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 35 | '/update': |
| 36 | { |
| 37 | # Gets rid of cherrypy parsing post file for args. |
| 38 | 'request.process_request_body': False, |
Chris Sosa | f65f4b9 | 2010-10-21 22:57:51 | [diff] [blame] | 39 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 40 | }, |
| 41 | # Sets up the static dir for file hosting. |
| 42 | '/static': |
| 43 | { 'tools.staticdir.dir': 'static', |
| 44 | 'tools.staticdir.on': True, |
Chris Sosa | f65f4b9 | 2010-10-21 22:57:51 | [diff] [blame] | 45 | 'response.timeout': 10000, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 46 | }, |
| 47 | } |
| 48 | return base_config |
[email protected] | 6424466 | 2009-11-12 00:52:08 | [diff] [blame] | 49 | |
Darin Petkov | e17164a | 2010-08-11 20:24:41 | [diff] [blame] | 50 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 51 | def _PrepareToServeUpdatesOnly(image_dir): |
| 52 | """Sets up symlink to image_dir for serving purposes.""" |
| 53 | assert os.path.exists(image_dir), '%s must exist.' % image_dir |
| 54 | # If we're serving out of an archived build dir (e.g. a |
| 55 | # buildbot), prepare this webserver's magic 'static/' dir with a |
| 56 | # link to the build archive. |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 57 | cherrypy.log('Preparing autoupdate for "serve updates only" mode.', |
| 58 | 'DEVSERVER') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 59 | if os.path.exists('static/archive'): |
| 60 | if image_dir != os.readlink('static/archive'): |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 61 | cherrypy.log('removing stale symlink to %s' % image_dir, 'DEVSERVER') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 62 | os.unlink('static/archive') |
| 63 | os.symlink(image_dir, 'static/archive') |
| 64 | else: |
| 65 | os.symlink(image_dir, 'static/archive') |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 66 | cherrypy.log('archive dir: %s ready to be used to serve images.' % image_dir, |
| 67 | 'DEVSERVER') |
| 68 | |
| 69 | |
| 70 | class DevServerRoot: |
| 71 | """The Root Class for the Dev Server. |
| 72 | |
| 73 | CherryPy works as follows: |
| 74 | For each method in this class, cherrpy interprets root/path |
| 75 | as a call to an instance of DevServerRoot->method_name. For example, |
| 76 | a call to http://myhost/build will call build. CherryPy automatically |
| 77 | parses http args and places them as keyword arguments in each method. |
| 78 | For paths http://myhost/update/dir1/dir2, you can use *args so that |
| 79 | cherrypy uses the update method and puts the extra paths in args. |
| 80 | """ |
| 81 | |
| 82 | def build(self, board, pkg): |
| 83 | """Builds the package specified.""" |
| 84 | cherrypy.log('emerging %s' % pkg, 'BUILD') |
| 85 | emerge_command = 'emerge-%s %s' % (board, pkg) |
| 86 | err = os.system(emerge_command) |
| 87 | if err != 0: |
| 88 | raise Exception('failed to execute %s' % emerge_command) |
| 89 | eclean_command = 'eclean-%s -d packages' % board |
| 90 | err = os.system(eclean_command) |
| 91 | if err != 0: |
| 92 | raise Exception('failed to execute %s' % emerge_command) |
| 93 | |
| 94 | def index(self): |
| 95 | return 'Welcome to the Dev Server!' |
| 96 | |
| 97 | def update(self, *args): |
| 98 | label = '/'.join(args) |
| 99 | body_length = int(cherrypy.request.headers['Content-Length']) |
| 100 | data = cherrypy.request.rfile.read(body_length) |
| 101 | return updater.HandleUpdatePing(data, label) |
| 102 | |
| 103 | # Expose actual methods. Necessary to actually have these callable. |
| 104 | build.exposed = True |
| 105 | update.exposed = True |
| 106 | index.exposed = True |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 107 | |
| 108 | |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 109 | if __name__ == '__main__': |
| 110 | usage = 'usage: %prog [options]' |
| 111 | parser = optparse.OptionParser(usage) |
Sean O'Connor | e38ea15 | 2010-04-16 20:50:40 | [diff] [blame] | 112 | parser.add_option('--archive_dir', dest='archive_dir', |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 113 | help='serve archived builds only.') |
Andrew de los Reyes | 9223f13 | 2010-05-08 00:08:17 | [diff] [blame] | 114 | parser.add_option('--client_prefix', dest='client_prefix', |
| 115 | help='Required prefix for client software version.', |
| 116 | default='MementoSoftwareUpdate') |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 117 | parser.add_option('--factory_config', dest='factory_config', |
| 118 | help='Config file for serving images from factory floor.') |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 119 | parser.add_option('--image', dest='image', |
| 120 | help='Force update using this image.') |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 121 | parser.add_option('--port', default=8080, |
| 122 | help='Port for the dev server to use.') |
Chris Sosa | 62f720b | 2010-10-27 04:39:48 | [diff] [blame^] | 123 | parser.add_option('--src_image', default='', |
| 124 | help='Image on remote machine for generating delta update.') |
Sean O'Connor | 1f7fd36 | 2010-04-07 23:34:52 | [diff] [blame] | 125 | parser.add_option('-t', action='store_true', dest='test_image') |
| 126 | parser.add_option('-u', '--urlbase', dest='urlbase', |
| 127 | help='base URL, other than devserver, for update images.') |
Chris Sosa | 5d342a2 | 2010-09-28 23:54:41 | [diff] [blame] | 128 | parser.add_option('--use_cached', action="store_true", default=False, |
| 129 | help='Prefer cached image regardless of timestamps.') |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 130 | parser.add_option('--validate_factory_config', action="store_true", |
| 131 | dest='validate_factory_config', |
| 132 | help='Validate factory config file, then exit.') |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 133 | parser.set_usage(parser.format_help()) |
| 134 | (options, _) = parser.parse_args() |
[email protected] | 21a5ca3 | 2009-11-04 18:23:23 | [diff] [blame] | 135 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 136 | devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 137 | root_dir = os.path.realpath('%s/../..' % devserver_dir) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 138 | serve_only = False |
| 139 | |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 140 | if options.archive_dir: |
| 141 | static_dir = os.path.realpath(options.archive_dir) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 142 | _PrepareToServeUpdatesOnly(static_dir) |
| 143 | serve_only = True |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 144 | else: |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 145 | static_dir = os.path.realpath('%s/static' % devserver_dir) |
Sean O'Connor | 14b6a0a | 2010-03-21 06:23:48 | [diff] [blame] | 146 | os.system('mkdir -p %s' % static_dir) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 147 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 148 | cherrypy.log('Source root is %s' % root_dir, 'DEVSERVER') |
| 149 | cherrypy.log('Serving from %s' % static_dir, 'DEVSERVER') |
[email protected] | 21a5ca3 | 2009-11-04 18:23:23 | [diff] [blame] | 150 | |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 151 | updater = autoupdate.Autoupdate( |
| 152 | root_dir=root_dir, |
| 153 | static_dir=static_dir, |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 154 | serve_only=serve_only, |
Andrew de los Reyes | 5262080 | 2010-04-12 20:40:07 | [diff] [blame] | 155 | urlbase=options.urlbase, |
| 156 | test_image=options.test_image, |
| 157 | factory_config_path=options.factory_config, |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 158 | client_prefix=options.client_prefix, |
Chris Sosa | 5d342a2 | 2010-09-28 23:54:41 | [diff] [blame] | 159 | forced_image=options.image, |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 160 | use_cached=options.use_cached, |
Chris Sosa | 62f720b | 2010-10-27 04:39:48 | [diff] [blame^] | 161 | port=options.port, |
| 162 | src_image=options.src_image) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 163 | |
| 164 | # Sanity-check for use of validate_factory_config. |
| 165 | if not options.factory_config and options.validate_factory_config: |
| 166 | parser.error('You need a factory_config to validate.') |
[email protected] | 6424466 | 2009-11-12 00:52:08 | [diff] [blame] | 167 | |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 168 | if options.factory_config: |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 169 | updater.ImportFactoryConfigFile(options.factory_config, |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 170 | options.validate_factory_config) |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 171 | # We don't run the dev server with this option. |
| 172 | if options.validate_factory_config: |
| 173 | sys.exit(0) |
Chris Sosa | 0356d3b | 2010-09-16 22:46:22 | [diff] [blame] | 174 | |
Chris Sosa | 7c93136 | 2010-10-12 02:49:01 | [diff] [blame] | 175 | cherrypy.quickstart(DevServerRoot(), config=_GetConfig(options)) |