Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 1 | # Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 5 | import optparse |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 6 | import os |
chocobo@google.com | 4dc2581 | 2009-10-27 23:46:26 +0000 | [diff] [blame] | 7 | import sys |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 8 | import web |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 9 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 10 | import autoupdate |
| 11 | import buildutil |
| 12 | |
| 13 | # Sets up global to share between classes. |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 14 | global updater |
| 15 | updater = None |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 16 | |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 17 | |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 18 | class index: |
| 19 | def GET(self): |
Tan Gao | ba17536 | 2010-07-07 11:25:12 -0700 | [diff] [blame] | 20 | return render.index(None) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 21 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 22 | |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 23 | class update: |
| 24 | """ |
Tan Gao | ba17536 | 2010-07-07 11:25:12 -0700 | [diff] [blame] | 25 | Processes updates from the client machine. If an update is found, the url |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 26 | references a static link that can be served automagically from web.py. |
| 27 | """ |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 28 | def POST(self, args=None): |
| 29 | return updater.HandleUpdatePing(web.data(), args) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 30 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 31 | |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 32 | class build: |
| 33 | """ |
| 34 | builds the package specified by the pkg parameter and returns the name |
| 35 | of the output file. |
| 36 | """ |
Ryan Cairns | dd1ceb8 | 2010-03-02 21:35:01 -0800 | [diff] [blame] | 37 | def POST(self): |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 38 | input = web.input() |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 39 | web.debug('emerging %s ' % input.pkg) |
| 40 | emerge_command = 'emerge-%s %s' % (input.board, input.pkg) |
Ryan Cairns | dd1ceb8 | 2010-03-02 21:35:01 -0800 | [diff] [blame] | 41 | err = os.system(emerge_command) |
| 42 | if err != 0: |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 43 | raise Exception('failed to execute %s' % emerge_command) |
Mandeep Singh Baines | ea6b7a5 | 2010-08-17 14:03:57 -0700 | [diff] [blame] | 44 | eclean_command = 'eclean-%s -d packages' % input.board |
| 45 | err = os.system(eclean_command) |
| 46 | if err != 0: |
| 47 | raise Exception('failed to execute %s' % emerge_command) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 48 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 49 | |
Darin Petkov | e17164a | 2010-08-11 13:24:41 -0700 | [diff] [blame] | 50 | def OverrideWSGIServer(server_address, wsgi_app): |
| 51 | """Creates a CherryPyWSGIServer instance. |
| 52 | |
| 53 | Overrides web.py's WSGIServer routine (web.httpserver.WSGIServer) to |
| 54 | increase the accepted connection socket timeout from the default 10 |
| 55 | seconds to 10 minutes. The extra time is necessary to serve delta |
| 56 | updates as well as update requests from a low priority update_engine |
| 57 | process running on a heavily loaded Chrome OS device. |
| 58 | """ |
| 59 | web.debug('using local OverrideWSGIServer routine') |
| 60 | from web.wsgiserver import CherryPyWSGIServer |
| 61 | return CherryPyWSGIServer(server_address, wsgi_app, server_name="localhost", |
| 62 | timeout=600) |
| 63 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 64 | def _PrepareToServeUpdatesOnly(image_dir): |
| 65 | """Sets up symlink to image_dir for serving purposes.""" |
| 66 | assert os.path.exists(image_dir), '%s must exist.' % image_dir |
| 67 | # If we're serving out of an archived build dir (e.g. a |
| 68 | # buildbot), prepare this webserver's magic 'static/' dir with a |
| 69 | # link to the build archive. |
| 70 | web.debug('Preparing autoupdate for "serve updates only" mode.') |
| 71 | if os.path.exists('static/archive'): |
| 72 | if image_dir != os.readlink('static/archive'): |
| 73 | web.debug('removing stale symlink to %s' % image_dir) |
| 74 | os.unlink('static/archive') |
| 75 | os.symlink(image_dir, 'static/archive') |
| 76 | else: |
| 77 | os.symlink(image_dir, 'static/archive') |
| 78 | web.debug('archive dir: %s ready to be used to serve images.' % image_dir) |
| 79 | |
| 80 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 81 | if __name__ == '__main__': |
| 82 | usage = 'usage: %prog [options]' |
| 83 | parser = optparse.OptionParser(usage) |
Sean O'Connor | e38ea15 | 2010-04-16 13:50:40 -0700 | [diff] [blame] | 84 | parser.add_option('--archive_dir', dest='archive_dir', |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 85 | help='serve archived builds only.') |
Andrew de los Reyes | 9223f13 | 2010-05-07 17:08:17 -0700 | [diff] [blame] | 86 | parser.add_option('--client_prefix', dest='client_prefix', |
| 87 | help='Required prefix for client software version.', |
| 88 | default='MementoSoftwareUpdate') |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 89 | parser.add_option('--factory_config', dest='factory_config', |
| 90 | help='Config file for serving images from factory floor.') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 91 | parser.add_option('--image', dest='image', |
| 92 | help='Force update using this image.') |
Sean O'Connor | 1f7fd36 | 2010-04-07 16:34:52 -0700 | [diff] [blame] | 93 | parser.add_option('-t', action='store_true', dest='test_image') |
| 94 | parser.add_option('-u', '--urlbase', dest='urlbase', |
| 95 | help='base URL, other than devserver, for update images.') |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 96 | parser.add_option('--use_cached', action="store_true", default=False, |
| 97 | help='Prefer cached image regardless of timestamps.') |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 98 | parser.add_option('--validate_factory_config', action="store_true", |
| 99 | dest='validate_factory_config', |
| 100 | help='Validate factory config file, then exit.') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 101 | # Clean up the args, due to httpserver's hardcoded use of sys.argv. |
| 102 | options, sys.argv = parser.parse_args(sys.argv) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 103 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 104 | root_dir = os.path.realpath('%s/../..' % |
| 105 | os.path.dirname(os.path.abspath(sys.argv[0]))) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 106 | |
| 107 | serve_only = False |
| 108 | |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 109 | if options.archive_dir: |
| 110 | static_dir = os.path.realpath(options.archive_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 111 | _PrepareToServeUpdatesOnly(static_dir) |
| 112 | serve_only = True |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 113 | else: |
| 114 | static_dir = os.path.realpath('%s/static' % |
| 115 | os.path.dirname(os.path.abspath(sys.argv[0]))) |
| 116 | web.debug('dev root is %s' % root_dir) |
| 117 | os.system('mkdir -p %s' % static_dir) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 118 | |
| 119 | web.debug('Serving from %s' % static_dir) |
rtc@google.com | 21a5ca3 | 2009-11-04 18:23:23 +0000 | [diff] [blame] | 120 | |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 121 | updater = autoupdate.Autoupdate( |
| 122 | root_dir=root_dir, |
| 123 | static_dir=static_dir, |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 124 | serve_only=serve_only, |
Andrew de los Reyes | 5262080 | 2010-04-12 13:40:07 -0700 | [diff] [blame] | 125 | urlbase=options.urlbase, |
| 126 | test_image=options.test_image, |
| 127 | factory_config_path=options.factory_config, |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 128 | client_prefix=options.client_prefix, |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 129 | forced_image=options.image, |
| 130 | use_cached=options.use_cached) |
rtc@google.com | 6424466 | 2009-11-12 00:52:08 +0000 | [diff] [blame] | 131 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 132 | if options.factory_config: |
| 133 | updater.ImportFactoryConfigFile(options.factory_config, |
| 134 | options.validate_factory_config) |
| 135 | |
| 136 | if not options.validate_factory_config: |
| 137 | # We do not need to run the dev server for validating the factory config. |
| 138 | # TODO(nsanders): Write unit test to validate. |
| 139 | urls = ('/', 'index', |
| 140 | '/update', 'update', |
| 141 | '/update/(.+)', 'update', |
| 142 | '/build', 'build') |
| 143 | |
| 144 | # Overrides the default WSGIServer routine -- see OverrideWSGIServer. |
| 145 | web.httpserver.WSGIServer = OverrideWSGIServer |
| 146 | app = web.application(urls, globals(), autoreload=True) |
| 147 | render = web.template.render('templates/') |
| 148 | app.run() |