blob: b87fab45c6e4876a8c95f4770bda7cba56e02417 [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#
2# Copyright 2008 Google Inc. All Rights Reserved.
3#
4
jamesren1a2914a2010-02-12 00:44:31 +00005import os
mblighbe630eb2008-08-01 16:41:48 +00006from autotest_lib.frontend.afe import rpc_client_lib
7from autotest_lib.frontend.afe.json_rpc import proxy
showardf4a68992010-02-03 20:29:59 +00008from autotest_lib.client.common_lib import global_config, utils
mblighbe630eb2008-08-01 16:41:48 +00009
mbligh37eceaa2008-12-15 22:56:37 +000010GLOBAL_CONFIG = global_config.global_config
11DEFAULT_SERVER = 'autotest'
Scott Zawalski347aaf42012-04-03 16:33:00 -040012AFE_RPC_PATH = '/afe/server/noauth/rpc/'
13TKO_RPC_PATH = '/new_tko/server/noauth/rpc/'
mbligh37eceaa2008-12-15 22:56:37 +000014
mblighbe630eb2008-08-01 16:41:48 +000015
mblighb68405d2010-03-11 18:32:39 +000016class AuthError(Exception):
17 pass
18
19
mblighbe630eb2008-08-01 16:41:48 +000020def get_autotest_server(web_server=None):
21 if not web_server:
22 if 'AUTOTEST_WEB' in os.environ:
23 web_server = os.environ['AUTOTEST_WEB']
24 else:
mbligh37eceaa2008-12-15 22:56:37 +000025 web_server = 'http://' + GLOBAL_CONFIG.get_config_value(
26 'SERVER', 'hostname', default=DEFAULT_SERVER)
mblighbe630eb2008-08-01 16:41:48 +000027
28 # if the name doesn't start with http://,
29 # nonexistant hosts get an obscure error
30 if not web_server.startswith('http://'):
31 web_server = 'http://' + web_server
32
33 return web_server
34
35
mbligh759b6d02008-10-21 16:28:18 +000036class rpc_comm(object):
37 """Shared AFE/TKO RPC class stuff"""
mbligh33a8a7e2008-11-18 14:57:11 +000038 def __init__(self, web_server, rpc_path, username):
39 self.username = username
mblighbe630eb2008-08-01 16:41:48 +000040 self.web_server = get_autotest_server(web_server)
mblighb68405d2010-03-11 18:32:39 +000041 try:
42 self.proxy = self._connect(rpc_path)
43 except rpc_client_lib.AuthError, s:
44 raise AuthError(s)
mblighbe630eb2008-08-01 16:41:48 +000045
mbligh11efd232008-11-27 00:20:46 +000046
showard55579572008-09-19 00:44:35 +000047 def _connect(self, rpc_path):
mblighbe630eb2008-08-01 16:41:48 +000048 # This does not fail even if the address is wrong.
49 # We need to wait for an actual RPC to fail
jamesren1a2914a2010-02-12 00:44:31 +000050 headers = rpc_client_lib.authorization_headers(self.username,
51 self.web_server)
showard55579572008-09-19 00:44:35 +000052 rpc_server = self.web_server + rpc_path
mblighbe630eb2008-08-01 16:41:48 +000053 return rpc_client_lib.get_proxy(rpc_server, headers=headers)
54
55
showard55579572008-09-19 00:44:35 +000056 def run(self, op, *args, **data):
mblighe2490722008-12-22 14:55:50 +000057 if 'AUTOTEST_CLI_DEBUG' in os.environ:
mbligh91967632009-03-16 21:45:52 +000058 print self.web_server, op, args, data
mblighbe630eb2008-08-01 16:41:48 +000059 function = getattr(self.proxy, op)
mbligh91967632009-03-16 21:45:52 +000060 result = function(*args, **data)
61 if 'AUTOTEST_CLI_DEBUG' in os.environ:
62 print 'result:', result
63 return result
mbligh759b6d02008-10-21 16:28:18 +000064
65
66class afe_comm(rpc_comm):
67 """Handles the AFE setup and communication through RPC"""
mbligh33a8a7e2008-11-18 14:57:11 +000068 def __init__(self, web_server=None, rpc_path=AFE_RPC_PATH, username=None):
69 super(afe_comm, self).__init__(web_server, rpc_path, username)
mbligh759b6d02008-10-21 16:28:18 +000070
71
72class tko_comm(rpc_comm):
73 """Handles the TKO setup and communication through RPC"""
mbligh33a8a7e2008-11-18 14:57:11 +000074 def __init__(self, web_server=None, rpc_path=TKO_RPC_PATH, username=None):
75 super(tko_comm, self).__init__(web_server, rpc_path, username)