blob: 23e77016bb54e5ba9afedeb720eadee9dda2cc21 [file] [log] [blame]
Chris Sosa7c931362010-10-12 02:49:011#!/usr/bin/python
2
Chris Sosa0356d3b2010-09-16 22:46:223# Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved.
[email protected]ded22402009-10-26 22:36:214# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
Chris Sosa7c931362010-10-12 02:49:017"""A CherryPy-based webserver to host images and build packages."""
8
9import cherrypy
Sean O'Connor14b6a0a2010-03-21 06:23:4810import optparse
[email protected]ded22402009-10-26 22:36:2111import os
[email protected]4dc25812009-10-27 23:46:2612import sys
[email protected]ded22402009-10-26 22:36:2113
Chris Sosa0356d3b2010-09-16 22:46:2214import autoupdate
Chris Sosa0356d3b2010-09-16 22:46:2215
16# Sets up global to share between classes.
[email protected]21a5ca32009-11-04 18:23:2317global updater
18updater = None
[email protected]ded22402009-10-26 22:36:2119
Chris Sosa7c931362010-10-12 02:49:0120def _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 Sosa374c62d2010-10-14 16:13:5427 'server.socket_timeout': 6000,
28 'response.timeout': 6000,
Chris Sosa7c931362010-10-12 02:49:0129 'tools.staticdir.root': os.getcwd(),
30 },
31 '/update':
32 {
33 # Gets rid of cherrypy parsing post file for args.
34 'request.process_request_body': False,
35 },
36 # Sets up the static dir for file hosting.
37 '/static':
38 { 'tools.staticdir.dir': 'static',
39 'tools.staticdir.on': True,
40 },
41 }
42 return base_config
[email protected]64244662009-11-12 00:52:0843
Darin Petkove17164a2010-08-11 20:24:4144
Chris Sosa0356d3b2010-09-16 22:46:2245def _PrepareToServeUpdatesOnly(image_dir):
46 """Sets up symlink to image_dir for serving purposes."""
47 assert os.path.exists(image_dir), '%s must exist.' % image_dir
48 # If we're serving out of an archived build dir (e.g. a
49 # buildbot), prepare this webserver's magic 'static/' dir with a
50 # link to the build archive.
Chris Sosa7c931362010-10-12 02:49:0151 cherrypy.log('Preparing autoupdate for "serve updates only" mode.',
52 'DEVSERVER')
Chris Sosa0356d3b2010-09-16 22:46:2253 if os.path.exists('static/archive'):
54 if image_dir != os.readlink('static/archive'):
Chris Sosa7c931362010-10-12 02:49:0155 cherrypy.log('removing stale symlink to %s' % image_dir, 'DEVSERVER')
Chris Sosa0356d3b2010-09-16 22:46:2256 os.unlink('static/archive')
57 os.symlink(image_dir, 'static/archive')
58 else:
59 os.symlink(image_dir, 'static/archive')
Chris Sosa7c931362010-10-12 02:49:0160 cherrypy.log('archive dir: %s ready to be used to serve images.' % image_dir,
61 'DEVSERVER')
62
63
64class DevServerRoot:
65 """The Root Class for the Dev Server.
66
67 CherryPy works as follows:
68 For each method in this class, cherrpy interprets root/path
69 as a call to an instance of DevServerRoot->method_name. For example,
70 a call to http://myhost/build will call build. CherryPy automatically
71 parses http args and places them as keyword arguments in each method.
72 For paths http://myhost/update/dir1/dir2, you can use *args so that
73 cherrypy uses the update method and puts the extra paths in args.
74 """
75
76 def build(self, board, pkg):
77 """Builds the package specified."""
78 cherrypy.log('emerging %s' % pkg, 'BUILD')
79 emerge_command = 'emerge-%s %s' % (board, pkg)
80 err = os.system(emerge_command)
81 if err != 0:
82 raise Exception('failed to execute %s' % emerge_command)
83 eclean_command = 'eclean-%s -d packages' % board
84 err = os.system(eclean_command)
85 if err != 0:
86 raise Exception('failed to execute %s' % emerge_command)
87
88 def index(self):
89 return 'Welcome to the Dev Server!'
90
91 def update(self, *args):
92 label = '/'.join(args)
93 body_length = int(cherrypy.request.headers['Content-Length'])
94 data = cherrypy.request.rfile.read(body_length)
95 return updater.HandleUpdatePing(data, label)
96
97 # Expose actual methods. Necessary to actually have these callable.
98 build.exposed = True
99 update.exposed = True
100 index.exposed = True
Chris Sosa0356d3b2010-09-16 22:46:22101
102
Sean O'Connor14b6a0a2010-03-21 06:23:48103if __name__ == '__main__':
104 usage = 'usage: %prog [options]'
105 parser = optparse.OptionParser(usage)
Sean O'Connore38ea152010-04-16 20:50:40106 parser.add_option('--archive_dir', dest='archive_dir',
Sean O'Connor14b6a0a2010-03-21 06:23:48107 help='serve archived builds only.')
Andrew de los Reyes9223f132010-05-08 00:08:17108 parser.add_option('--client_prefix', dest='client_prefix',
109 help='Required prefix for client software version.',
110 default='MementoSoftwareUpdate')
Andrew de los Reyes52620802010-04-12 20:40:07111 parser.add_option('--factory_config', dest='factory_config',
112 help='Config file for serving images from factory floor.')
Chris Sosa0356d3b2010-09-16 22:46:22113 parser.add_option('--image', dest='image',
114 help='Force update using this image.')
Chris Sosa7c931362010-10-12 02:49:01115 parser.add_option('--port', default=8080,
116 help='Port for the dev server to use.')
Sean O'Connor1f7fd362010-04-07 23:34:52117 parser.add_option('-t', action='store_true', dest='test_image')
118 parser.add_option('-u', '--urlbase', dest='urlbase',
119 help='base URL, other than devserver, for update images.')
Chris Sosa5d342a22010-09-28 23:54:41120 parser.add_option('--use_cached', action="store_true", default=False,
121 help='Prefer cached image regardless of timestamps.')
Andrew de los Reyes52620802010-04-12 20:40:07122 parser.add_option('--validate_factory_config', action="store_true",
123 dest='validate_factory_config',
124 help='Validate factory config file, then exit.')
Chris Sosa7c931362010-10-12 02:49:01125 parser.set_usage(parser.format_help())
126 (options, _) = parser.parse_args()
[email protected]21a5ca32009-11-04 18:23:23127
Chris Sosa7c931362010-10-12 02:49:01128 devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
129 root_dir = os.path.realpath('%s/../..' % devserver_dir)
Chris Sosa0356d3b2010-09-16 22:46:22130 serve_only = False
131
Sean O'Connor14b6a0a2010-03-21 06:23:48132 if options.archive_dir:
133 static_dir = os.path.realpath(options.archive_dir)
Chris Sosa0356d3b2010-09-16 22:46:22134 _PrepareToServeUpdatesOnly(static_dir)
135 serve_only = True
Sean O'Connor14b6a0a2010-03-21 06:23:48136 else:
Chris Sosa7c931362010-10-12 02:49:01137 static_dir = os.path.realpath('%s/static' % devserver_dir)
Sean O'Connor14b6a0a2010-03-21 06:23:48138 os.system('mkdir -p %s' % static_dir)
Chris Sosa0356d3b2010-09-16 22:46:22139
Chris Sosa7c931362010-10-12 02:49:01140 cherrypy.log('Source root is %s' % root_dir, 'DEVSERVER')
141 cherrypy.log('Serving from %s' % static_dir, 'DEVSERVER')
[email protected]21a5ca32009-11-04 18:23:23142
Andrew de los Reyes52620802010-04-12 20:40:07143 updater = autoupdate.Autoupdate(
144 root_dir=root_dir,
145 static_dir=static_dir,
Chris Sosa0356d3b2010-09-16 22:46:22146 serve_only=serve_only,
Andrew de los Reyes52620802010-04-12 20:40:07147 urlbase=options.urlbase,
148 test_image=options.test_image,
149 factory_config_path=options.factory_config,
Chris Sosa0356d3b2010-09-16 22:46:22150 client_prefix=options.client_prefix,
Chris Sosa5d342a22010-09-28 23:54:41151 forced_image=options.image,
Chris Sosa7c931362010-10-12 02:49:01152 use_cached=options.use_cached,
153 port=options.port)
154
155 # Sanity-check for use of validate_factory_config.
156 if not options.factory_config and options.validate_factory_config:
157 parser.error('You need a factory_config to validate.')
[email protected]64244662009-11-12 00:52:08158
Chris Sosa0356d3b2010-09-16 22:46:22159 if options.factory_config:
Chris Sosa7c931362010-10-12 02:49:01160 updater.ImportFactoryConfigFile(options.factory_config,
Chris Sosa0356d3b2010-09-16 22:46:22161 options.validate_factory_config)
Chris Sosa7c931362010-10-12 02:49:01162 # We don't run the dev server with this option.
163 if options.validate_factory_config:
164 sys.exit(0)
Chris Sosa0356d3b2010-09-16 22:46:22165
Chris Sosa7c931362010-10-12 02:49:01166 cherrypy.quickstart(DevServerRoot(), config=_GetConfig(options))