mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2008 Google Inc. All Rights Reserved. |
| 3 | # |
| 4 | |
jamesren | 1a2914a | 2010-02-12 00:44:31 +0000 | [diff] [blame] | 5 | import os |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 6 | from autotest_lib.frontend.afe import rpc_client_lib |
| 7 | from autotest_lib.frontend.afe.json_rpc import proxy |
showard | f4a6899 | 2010-02-03 20:29:59 +0000 | [diff] [blame] | 8 | from autotest_lib.client.common_lib import global_config, utils |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 9 | |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 10 | GLOBAL_CONFIG = global_config.global_config |
| 11 | DEFAULT_SERVER = 'autotest' |
Scott Zawalski | 347aaf4 | 2012-04-03 16:33:00 -0400 | [diff] [blame] | 12 | AFE_RPC_PATH = '/afe/server/noauth/rpc/' |
| 13 | TKO_RPC_PATH = '/new_tko/server/noauth/rpc/' |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 14 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 15 | |
mbligh | b68405d | 2010-03-11 18:32:39 +0000 | [diff] [blame] | 16 | class AuthError(Exception): |
| 17 | pass |
| 18 | |
| 19 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 20 | def 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: |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 25 | web_server = 'http://' + GLOBAL_CONFIG.get_config_value( |
| 26 | 'SERVER', 'hostname', default=DEFAULT_SERVER) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 27 | |
| 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 | |
mbligh | 759b6d0 | 2008-10-21 16:28:18 +0000 | [diff] [blame] | 36 | class rpc_comm(object): |
| 37 | """Shared AFE/TKO RPC class stuff""" |
mbligh | 33a8a7e | 2008-11-18 14:57:11 +0000 | [diff] [blame] | 38 | def __init__(self, web_server, rpc_path, username): |
| 39 | self.username = username |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 40 | self.web_server = get_autotest_server(web_server) |
mbligh | b68405d | 2010-03-11 18:32:39 +0000 | [diff] [blame] | 41 | try: |
| 42 | self.proxy = self._connect(rpc_path) |
| 43 | except rpc_client_lib.AuthError, s: |
| 44 | raise AuthError(s) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 45 | |
mbligh | 11efd23 | 2008-11-27 00:20:46 +0000 | [diff] [blame] | 46 | |
showard | 5557957 | 2008-09-19 00:44:35 +0000 | [diff] [blame] | 47 | def _connect(self, rpc_path): |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 48 | # This does not fail even if the address is wrong. |
| 49 | # We need to wait for an actual RPC to fail |
jamesren | 1a2914a | 2010-02-12 00:44:31 +0000 | [diff] [blame] | 50 | headers = rpc_client_lib.authorization_headers(self.username, |
| 51 | self.web_server) |
showard | 5557957 | 2008-09-19 00:44:35 +0000 | [diff] [blame] | 52 | rpc_server = self.web_server + rpc_path |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 53 | return rpc_client_lib.get_proxy(rpc_server, headers=headers) |
| 54 | |
| 55 | |
showard | 5557957 | 2008-09-19 00:44:35 +0000 | [diff] [blame] | 56 | def run(self, op, *args, **data): |
mbligh | e249072 | 2008-12-22 14:55:50 +0000 | [diff] [blame] | 57 | if 'AUTOTEST_CLI_DEBUG' in os.environ: |
mbligh | 9196763 | 2009-03-16 21:45:52 +0000 | [diff] [blame] | 58 | print self.web_server, op, args, data |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 59 | function = getattr(self.proxy, op) |
mbligh | 9196763 | 2009-03-16 21:45:52 +0000 | [diff] [blame] | 60 | result = function(*args, **data) |
| 61 | if 'AUTOTEST_CLI_DEBUG' in os.environ: |
| 62 | print 'result:', result |
| 63 | return result |
mbligh | 759b6d0 | 2008-10-21 16:28:18 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | class afe_comm(rpc_comm): |
| 67 | """Handles the AFE setup and communication through RPC""" |
mbligh | 33a8a7e | 2008-11-18 14:57:11 +0000 | [diff] [blame] | 68 | def __init__(self, web_server=None, rpc_path=AFE_RPC_PATH, username=None): |
| 69 | super(afe_comm, self).__init__(web_server, rpc_path, username) |
mbligh | 759b6d0 | 2008-10-21 16:28:18 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | class tko_comm(rpc_comm): |
| 73 | """Handles the TKO setup and communication through RPC""" |
mbligh | 33a8a7e | 2008-11-18 14:57:11 +0000 | [diff] [blame] | 74 | def __init__(self, web_server=None, rpc_path=TKO_RPC_PATH, username=None): |
| 75 | super(tko_comm, self).__init__(web_server, rpc_path, username) |