Home | History | Annotate | Download | only in fake_device_server
      1 #! /usr/bin/env python
      2 
      3 # Copyright 2014 The Chromium OS Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 """Fake implementation of a Device Server.
      8 
      9 This module can be used in testing both in autotests and locally. To use locally
     10 you can just run this python module directly.
     11 """
     12 
     13 import argparse
     14 import logging
     15 import logging.handlers
     16 import cherrypy
     17 
     18 import common
     19 from fake_device_server import commands
     20 from fake_device_server import devices
     21 from fake_device_server import fail_control
     22 from fake_device_server import meta_handler
     23 from fake_device_server import oauth
     24 from fake_device_server import registration_tickets
     25 from fake_device_server import resource_delegate
     26 
     27 PORT = 9876
     28 
     29 
     30 def stop_server():
     31     """Stops the cherrypy server and blocks."""
     32     cherrypy.engine.stop()
     33 
     34 
     35 def start_server(generation):
     36     """Starts the cherrypy server and blocks.
     37    
     38     @param generation: string unique to this instance of the fake device server.
     39 
     40     """
     41     fail_control_handler = fail_control.FailControl()
     42     cherrypy.tree.mount(
     43         fail_control_handler, '/' + fail_control.FAIL_CONTROL_PATH,
     44         {'/':
     45             {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
     46         }
     47     )
     48     oauth_handler = oauth.OAuth(fail_control_handler)
     49     commands_handler = commands.Commands(oauth_handler, fail_control_handler)
     50     cherrypy.tree.mount(
     51         commands_handler, '/' + commands.COMMANDS_PATH,
     52         {'/':
     53             {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
     54         }
     55     )
     56     devices_resource = resource_delegate.ResourceDelegate({})
     57     # TODO(wiley): We need to validate device commands.
     58     devices_handler = devices.Devices(devices_resource,
     59                                       commands_handler,
     60                                       oauth_handler,
     61                                       fail_control_handler)
     62     cherrypy.tree.mount(
     63         devices_handler, '/' + devices.DEVICES_PATH,
     64         {'/':
     65             {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
     66         }
     67     )
     68     tickets = resource_delegate.ResourceDelegate({})
     69     registration_tickets_handler = registration_tickets.RegistrationTickets(
     70             tickets, devices_handler, fail_control_handler)
     71     cherrypy.tree.mount(
     72         registration_tickets_handler,
     73         '/' + registration_tickets.REGISTRATION_PATH,
     74         {'/':
     75             {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
     76         }
     77     )
     78     cherrypy.tree.mount(
     79         oauth_handler,
     80         '/' + oauth.OAUTH_PATH,
     81         {'/':
     82             {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
     83         }
     84     )
     85     cherrypy.tree.mount(
     86         meta_handler.MetaHandler(generation),
     87         '/' + meta_handler.META_HANDLER_PATH,
     88         {'/':
     89             {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
     90         }
     91     )
     92     # Don't parse POST for params.
     93     cherrypy.config.update({'global': {'request.process_request_body': False}})
     94     cherrypy.engine.start()
     95 
     96 
     97 def main():
     98     """Main method for callers who start this module directly."""
     99     parser = argparse.ArgumentParser(
    100         description='Acts like a fake instance of GCD')
    101     parser.add_argument('generation', metavar='generation', type=str,
    102                         help='Unique generation id for confirming health')
    103     args = parser.parse_args()
    104     cherrypy.config.update({'server.socket_port': PORT})
    105     start_server(args.generation)
    106     cherrypy.engine.block()
    107 
    108 
    109 if __name__ == '__main__':
    110     formatter = logging.Formatter(
    111             'fake_gcd_server: [%(levelname)s] %(message)s')
    112     handler = logging.handlers.SysLogHandler(address='/dev/log')
    113     handler.setFormatter(formatter)
    114     logging.basicConfig(level=logging.DEBUG)
    115     logging.getLogger().addHandler(handler)
    116     main()
    117