blob: 9ab44905eec56bb8bfae0b482fd4d7b6afb5f89e [file] [log] [blame]
Chris Sosa7c931362010-10-11 19:49:01 -07001#!/usr/bin/python
2
Chris Sosa0356d3b2010-09-16 15:46:22 -07003# Copyright (c) 2009-2010 The Chromium OS Authors. All rights reserved.
rtc@google.comded22402009-10-26 22:36:21 +00004# 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-11 19:49:01 -07007"""A CherryPy-based webserver to host images and build packages."""
8
9import cherrypy
Sean O'Connor14b6a0a2010-03-20 23:23:48 -070010import optparse
rtc@google.comded22402009-10-26 22:36:21 +000011import os
chocobo@google.com4dc25812009-10-27 23:46:26 +000012import sys
rtc@google.comded22402009-10-26 22:36:21 +000013
Chris Sosa0356d3b2010-09-16 15:46:22 -070014import autoupdate
Chris Sosa0356d3b2010-09-16 15:46:22 -070015
16# Sets up global to share between classes.
rtc@google.com21a5ca32009-11-04 18:23:23 +000017global updater
18updater = None
rtc@google.comded22402009-10-26 22:36:21 +000019
Chris Sosa7c931362010-10-11 19:49:01 -070020def _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 09:13:54 -070027 'server.socket_timeout': 6000,
28 'response.timeout': 6000,
Chris Sosa7c931362010-10-11 19:49:01 -070029 '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,
Chris Sosaf65f4b92010-10-21 15:57:51 -070035 'response.timeout': 10000,
Chris Sosa7c931362010-10-11 19:49:01 -070036 },
37 # Sets up the static dir for file hosting.
38 '/static':
39 { 'tools.staticdir.dir': 'static',
40 'tools.staticdir.on': True,
Chris Sosaf65f4b92010-10-21 15:57:51 -070041 'response.timeout': 10000,
Chris Sosa7c931362010-10-11 19:49:01 -070042 },
43 }
44 return base_config
rtc@google.com64244662009-11-12 00:52:08 +000045
Darin Petkove17164a2010-08-11 13:24:41 -070046
Chris Sosa0356d3b2010-09-16 15:46:22 -070047def _PrepareToServeUpdatesOnly(image_dir):
48 """Sets up symlink to image_dir for serving purposes."""
49 assert os.path.exists(image_dir), '%s must exist.' % image_dir
50 # If we're serving out of an archived build dir (e.g. a
51 # buildbot), prepare this webserver's magic 'static/' dir with a
52 # link to the build archive.
Chris Sosa7c931362010-10-11 19:49:01 -070053 cherrypy.log('Preparing autoupdate for "serve updates only" mode.',
54 'DEVSERVER')
Chris Sosa0356d3b2010-09-16 15:46:22 -070055 if os.path.exists('static/archive'):
56 if image_dir != os.readlink('static/archive'):
Chris Sosa7c931362010-10-11 19:49:01 -070057 cherrypy.log('removing stale symlink to %s' % image_dir, 'DEVSERVER')
Chris Sosa0356d3b2010-09-16 15:46:22 -070058 os.unlink('static/archive')
59 os.symlink(image_dir, 'static/archive')
60 else:
61 os.symlink(image_dir, 'static/archive')
Chris Sosa7c931362010-10-11 19:49:01 -070062 cherrypy.log('archive dir: %s ready to be used to serve images.' % image_dir,
63 'DEVSERVER')
64
65
66class DevServerRoot:
67 """The Root Class for the Dev Server.
68
69 CherryPy works as follows:
70 For each method in this class, cherrpy interprets root/path
71 as a call to an instance of DevServerRoot->method_name. For example,
72 a call to http://myhost/build will call build. CherryPy automatically
73 parses http args and places them as keyword arguments in each method.
74 For paths http://myhost/update/dir1/dir2, you can use *args so that
75 cherrypy uses the update method and puts the extra paths in args.
76 """
77
78 def build(self, board, pkg):
79 """Builds the package specified."""
80 cherrypy.log('emerging %s' % pkg, 'BUILD')
81 emerge_command = 'emerge-%s %s' % (board, pkg)
82 err = os.system(emerge_command)
83 if err != 0:
84 raise Exception('failed to execute %s' % emerge_command)
85 eclean_command = 'eclean-%s -d packages' % board
86 err = os.system(eclean_command)
87 if err != 0:
88 raise Exception('failed to execute %s' % emerge_command)
89
90 def index(self):
91 return 'Welcome to the Dev Server!'
92
93 def update(self, *args):
94 label = '/'.join(args)
95 body_length = int(cherrypy.request.headers['Content-Length'])
96 data = cherrypy.request.rfile.read(body_length)
97 return updater.HandleUpdatePing(data, label)
98
99 # Expose actual methods. Necessary to actually have these callable.
100 build.exposed = True
101 update.exposed = True
102 index.exposed = True
Chris Sosa0356d3b2010-09-16 15:46:22 -0700103
104
Sean O'Connor14b6a0a2010-03-20 23:23:48 -0700105if __name__ == '__main__':
106 usage = 'usage: %prog [options]'
107 parser = optparse.OptionParser(usage)
Sean O'Connore38ea152010-04-16 13:50:40 -0700108 parser.add_option('--archive_dir', dest='archive_dir',
Sean O'Connor14b6a0a2010-03-20 23:23:48 -0700109 help='serve archived builds only.')
Andrew de los Reyes9223f132010-05-07 17:08:17 -0700110 parser.add_option('--client_prefix', dest='client_prefix',
111 help='Required prefix for client software version.',
112 default='MementoSoftwareUpdate')
Andrew de los Reyes52620802010-04-12 13:40:07 -0700113 parser.add_option('--factory_config', dest='factory_config',
114 help='Config file for serving images from factory floor.')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700115 parser.add_option('--image', dest='image',
116 help='Force update using this image.')
Chris Sosa7c931362010-10-11 19:49:01 -0700117 parser.add_option('--port', default=8080,
118 help='Port for the dev server to use.')
Sean O'Connor1f7fd362010-04-07 16:34:52 -0700119 parser.add_option('-t', action='store_true', dest='test_image')
120 parser.add_option('-u', '--urlbase', dest='urlbase',
121 help='base URL, other than devserver, for update images.')
Chris Sosa5d342a22010-09-28 16:54:41 -0700122 parser.add_option('--use_cached', action="store_true", default=False,
123 help='Prefer cached image regardless of timestamps.')
Andrew de los Reyes52620802010-04-12 13:40:07 -0700124 parser.add_option('--validate_factory_config', action="store_true",
125 dest='validate_factory_config',
126 help='Validate factory config file, then exit.')
Chris Sosa7c931362010-10-11 19:49:01 -0700127 parser.set_usage(parser.format_help())
128 (options, _) = parser.parse_args()
rtc@google.com21a5ca32009-11-04 18:23:23 +0000129
Chris Sosa7c931362010-10-11 19:49:01 -0700130 devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
131 root_dir = os.path.realpath('%s/../..' % devserver_dir)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700132 serve_only = False
133
Sean O'Connor14b6a0a2010-03-20 23:23:48 -0700134 if options.archive_dir:
135 static_dir = os.path.realpath(options.archive_dir)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700136 _PrepareToServeUpdatesOnly(static_dir)
137 serve_only = True
Sean O'Connor14b6a0a2010-03-20 23:23:48 -0700138 else:
Chris Sosa7c931362010-10-11 19:49:01 -0700139 static_dir = os.path.realpath('%s/static' % devserver_dir)
Sean O'Connor14b6a0a2010-03-20 23:23:48 -0700140 os.system('mkdir -p %s' % static_dir)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700141
Chris Sosa7c931362010-10-11 19:49:01 -0700142 cherrypy.log('Source root is %s' % root_dir, 'DEVSERVER')
143 cherrypy.log('Serving from %s' % static_dir, 'DEVSERVER')
rtc@google.com21a5ca32009-11-04 18:23:23 +0000144
Andrew de los Reyes52620802010-04-12 13:40:07 -0700145 updater = autoupdate.Autoupdate(
146 root_dir=root_dir,
147 static_dir=static_dir,
Chris Sosa0356d3b2010-09-16 15:46:22 -0700148 serve_only=serve_only,
Andrew de los Reyes52620802010-04-12 13:40:07 -0700149 urlbase=options.urlbase,
150 test_image=options.test_image,
151 factory_config_path=options.factory_config,
Chris Sosa0356d3b2010-09-16 15:46:22 -0700152 client_prefix=options.client_prefix,
Chris Sosa5d342a22010-09-28 16:54:41 -0700153 forced_image=options.image,
Chris Sosa7c931362010-10-11 19:49:01 -0700154 use_cached=options.use_cached,
155 port=options.port)
156
157 # Sanity-check for use of validate_factory_config.
158 if not options.factory_config and options.validate_factory_config:
159 parser.error('You need a factory_config to validate.')
rtc@google.com64244662009-11-12 00:52:08 +0000160
Chris Sosa0356d3b2010-09-16 15:46:22 -0700161 if options.factory_config:
Chris Sosa7c931362010-10-11 19:49:01 -0700162 updater.ImportFactoryConfigFile(options.factory_config,
Chris Sosa0356d3b2010-09-16 15:46:22 -0700163 options.validate_factory_config)
Chris Sosa7c931362010-10-11 19:49:01 -0700164 # We don't run the dev server with this option.
165 if options.validate_factory_config:
166 sys.exit(0)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700167
Chris Sosa7c931362010-10-11 19:49:01 -0700168 cherrypy.quickstart(DevServerRoot(), config=_GetConfig(options))