blob: 3737cc08924481f9f89a2d8643b68b4c0add41d6 [file] [log] [blame]
rtc@google.comded22402009-10-26 22:36:21 +00001# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import autoupdate
Sean O'Connor14b6a0a2010-03-20 23:23:48 -07006import buildutil
7import optparse
rtc@google.comded22402009-10-26 22:36:21 +00008import os
chocobo@google.com4dc25812009-10-27 23:46:26 +00009import sys
Andrew de los Reyes52620802010-04-12 13:40:07 -070010import web
rtc@google.comded22402009-10-26 22:36:21 +000011
rtc@google.com21a5ca32009-11-04 18:23:23 +000012global updater
13updater = None
rtc@google.comded22402009-10-26 22:36:21 +000014
rtc@google.com64244662009-11-12 00:52:08 +000015
rtc@google.comded22402009-10-26 22:36:21 +000016class index:
17 def GET(self):
Tan Gaoba175362010-07-07 11:25:12 -070018 return render.index(None)
rtc@google.comded22402009-10-26 22:36:21 +000019
20class update:
21 """
Tan Gaoba175362010-07-07 11:25:12 -070022 Processes updates from the client machine. If an update is found, the url
rtc@google.comded22402009-10-26 22:36:21 +000023 references a static link that can be served automagically from web.py.
24 """
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070025 def POST(self, args=None):
26 return updater.HandleUpdatePing(web.data(), args)
rtc@google.com21a5ca32009-11-04 18:23:23 +000027
rtc@google.com64244662009-11-12 00:52:08 +000028class build:
29 """
30 builds the package specified by the pkg parameter and returns the name
31 of the output file.
32 """
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080033 def POST(self):
rtc@google.com64244662009-11-12 00:52:08 +000034 input = web.input()
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070035 web.debug('emerging %s ' % input.pkg)
36 emerge_command = 'emerge-%s %s' % (input.board, input.pkg)
Ryan Cairnsdd1ceb82010-03-02 21:35:01 -080037 err = os.system(emerge_command)
38 if err != 0:
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070039 raise Exception('failed to execute %s' % emerge_command)
rtc@google.comded22402009-10-26 22:36:21 +000040
Darin Petkove17164a2010-08-11 13:24:41 -070041def OverrideWSGIServer(server_address, wsgi_app):
42 """Creates a CherryPyWSGIServer instance.
43
44 Overrides web.py's WSGIServer routine (web.httpserver.WSGIServer) to
45 increase the accepted connection socket timeout from the default 10
46 seconds to 10 minutes. The extra time is necessary to serve delta
47 updates as well as update requests from a low priority update_engine
48 process running on a heavily loaded Chrome OS device.
49 """
50 web.debug('using local OverrideWSGIServer routine')
51 from web.wsgiserver import CherryPyWSGIServer
52 return CherryPyWSGIServer(server_address, wsgi_app, server_name="localhost",
53 timeout=600)
54
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070055if __name__ == '__main__':
56 usage = 'usage: %prog [options]'
57 parser = optparse.OptionParser(usage)
Sean O'Connore38ea152010-04-16 13:50:40 -070058 parser.add_option('--archive_dir', dest='archive_dir',
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070059 help='serve archived builds only.')
Andrew de los Reyes9223f132010-05-07 17:08:17 -070060 parser.add_option('--client_prefix', dest='client_prefix',
61 help='Required prefix for client software version.',
62 default='MementoSoftwareUpdate')
Andrew de los Reyes52620802010-04-12 13:40:07 -070063 parser.add_option('--factory_config', dest='factory_config',
64 help='Config file for serving images from factory floor.')
Sean O'Connor1f7fd362010-04-07 16:34:52 -070065 parser.add_option('-t', action='store_true', dest='test_image')
66 parser.add_option('-u', '--urlbase', dest='urlbase',
67 help='base URL, other than devserver, for update images.')
Andrew de los Reyes52620802010-04-12 13:40:07 -070068 parser.add_option('--validate_factory_config', action="store_true",
69 dest='validate_factory_config',
70 help='Validate factory config file, then exit.')
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070071 options, args = parser.parse_args()
72 # clean up the args, due to httpserver's hardcoded use of sys.argv
73 if options.archive_dir:
Andrew de los Reyes52620802010-04-12 13:40:07 -070074 sys.argv.remove('--archive_dir')
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070075 sys.argv.remove(options.archive_dir)
Andrew de los Reyes9223f132010-05-07 17:08:17 -070076 if '--client_prefix' in sys.argv:
77 sys.argv.remove('--client_prefix')
78 sys.argv.remove(options.client_prefix)
Andrew de los Reyes52620802010-04-12 13:40:07 -070079 if options.factory_config:
80 sys.argv.remove('--factory_config')
81 sys.argv.remove(options.factory_config)
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070082 if options.test_image:
83 sys.argv.remove('-t')
Sean O'Connor1f7fd362010-04-07 16:34:52 -070084 if options.urlbase:
85 sys.argv.remove('-u')
86 sys.argv.remove(options.urlbase)
Andrew de los Reyes52620802010-04-12 13:40:07 -070087 if options.validate_factory_config:
88 sys.argv.remove('--validate_factory_config')
rtc@google.com21a5ca32009-11-04 18:23:23 +000089
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070090 root_dir = os.path.realpath('%s/../..' %
91 os.path.dirname(os.path.abspath(sys.argv[0])))
92 if options.archive_dir:
93 static_dir = os.path.realpath(options.archive_dir)
94 assert os.path.exists(static_dir), '%s must exist.' % options.archive_dir
95 web.debug('using archive dir: %s' % static_dir)
96 else:
97 static_dir = os.path.realpath('%s/static' %
98 os.path.dirname(os.path.abspath(sys.argv[0])))
99 web.debug('dev root is %s' % root_dir)
100 os.system('mkdir -p %s' % static_dir)
101 web.debug('Serving images from %s' % static_dir)
rtc@google.com21a5ca32009-11-04 18:23:23 +0000102
Andrew de los Reyes52620802010-04-12 13:40:07 -0700103 updater = autoupdate.Autoupdate(
104 root_dir=root_dir,
105 static_dir=static_dir,
106 serve_only=options.archive_dir,
107 urlbase=options.urlbase,
108 test_image=options.test_image,
109 factory_config_path=options.factory_config,
Andrew de los Reyesfb4444b2010-06-29 18:11:28 -0700110 validate_factory_config=options.validate_factory_config,
111 client_prefix=options.client_prefix)
Andrew de los Reyes52620802010-04-12 13:40:07 -0700112 if options.validate_factory_config:
113 sys.exit(0)
rtc@google.com21a5ca32009-11-04 18:23:23 +0000114 urls = ('/', 'index',
rtc@google.com64244662009-11-12 00:52:08 +0000115 '/update', 'update',
Sean O'Connor14b6a0a2010-03-20 23:23:48 -0700116 '/update/(.+)', 'update',
rtc@google.com64244662009-11-12 00:52:08 +0000117 '/build', 'build')
118
Darin Petkove17164a2010-08-11 13:24:41 -0700119 # Overrides the default WSGIServer routine -- see OverrideWSGIServer.
120 web.httpserver.WSGIServer = OverrideWSGIServer
Sean O'Connor14b6a0a2010-03-20 23:23:48 -0700121 app = web.application(urls, globals(), autoreload=True)
rtc@google.com21a5ca32009-11-04 18:23:23 +0000122 render = web.template.render('templates/')
rtc@google.comded22402009-10-26 22:36:21 +0000123 app.run()