blob: d97650b00f3bcbd3e5a4208c1c25d5a56f772b80 [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:
Prathmesh Prabhu02163aa2017-12-12 19:13:21 -080025 web_server = GLOBAL_CONFIG.get_config_value(
mbligh37eceaa2008-12-15 22:56:37 +000026 'SERVER', 'hostname', default=DEFAULT_SERVER)
mblighbe630eb2008-08-01 16:41:48 +000027
Prathmesh Prabhu2892ff62017-12-19 10:21:31 -080028 web_server = rpc_client_lib.add_protocol(web_server)
mblighbe630eb2008-08-01 16:41:48 +000029 return web_server
30
31
mbligh759b6d02008-10-21 16:28:18 +000032class rpc_comm(object):
33 """Shared AFE/TKO RPC class stuff"""
mbligh33a8a7e2008-11-18 14:57:11 +000034 def __init__(self, web_server, rpc_path, username):
35 self.username = username
mblighbe630eb2008-08-01 16:41:48 +000036 self.web_server = get_autotest_server(web_server)
mblighb68405d2010-03-11 18:32:39 +000037 try:
38 self.proxy = self._connect(rpc_path)
39 except rpc_client_lib.AuthError, s:
40 raise AuthError(s)
mblighbe630eb2008-08-01 16:41:48 +000041
mbligh11efd232008-11-27 00:20:46 +000042
showard55579572008-09-19 00:44:35 +000043 def _connect(self, rpc_path):
mblighbe630eb2008-08-01 16:41:48 +000044 # This does not fail even if the address is wrong.
45 # We need to wait for an actual RPC to fail
jamesren1a2914a2010-02-12 00:44:31 +000046 headers = rpc_client_lib.authorization_headers(self.username,
47 self.web_server)
showard55579572008-09-19 00:44:35 +000048 rpc_server = self.web_server + rpc_path
mblighbe630eb2008-08-01 16:41:48 +000049 return rpc_client_lib.get_proxy(rpc_server, headers=headers)
50
51
showard55579572008-09-19 00:44:35 +000052 def run(self, op, *args, **data):
mblighe2490722008-12-22 14:55:50 +000053 if 'AUTOTEST_CLI_DEBUG' in os.environ:
mbligh91967632009-03-16 21:45:52 +000054 print self.web_server, op, args, data
mblighbe630eb2008-08-01 16:41:48 +000055 function = getattr(self.proxy, op)
mbligh91967632009-03-16 21:45:52 +000056 result = function(*args, **data)
57 if 'AUTOTEST_CLI_DEBUG' in os.environ:
58 print 'result:', result
59 return result
mbligh759b6d02008-10-21 16:28:18 +000060
61
62class afe_comm(rpc_comm):
63 """Handles the AFE setup and communication through RPC"""
mbligh33a8a7e2008-11-18 14:57:11 +000064 def __init__(self, web_server=None, rpc_path=AFE_RPC_PATH, username=None):
65 super(afe_comm, self).__init__(web_server, rpc_path, username)
mbligh759b6d02008-10-21 16:28:18 +000066
67
68class tko_comm(rpc_comm):
69 """Handles the TKO setup and communication through RPC"""
mbligh33a8a7e2008-11-18 14:57:11 +000070 def __init__(self, web_server=None, rpc_path=TKO_RPC_PATH, username=None):
71 super(tko_comm, self).__init__(web_server, rpc_path, username)