Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/capabilities/service.py: 34.15%

41 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-22 21:04 +0000

1# Copyright (C) 2018 Bloomberg LP 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# <http://www.apache.org/licenses/LICENSE-2.0> 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15 

16import logging 

17 

18import grpc 

19 

20 

21from buildgrid._exceptions import InvalidArgumentError, RetriableError 

22from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2, remote_execution_pb2_grpc 

23from buildgrid.server._authentication import AuthContext, authorize 

24from buildgrid.server.request_metadata_utils import printable_request_metadata 

25 

26 

27class CapabilitiesService(remote_execution_pb2_grpc.CapabilitiesServicer): 

28 

29 def __init__(self, server): 

30 self.__logger = logging.getLogger(__name__) 

31 

32 self.__instances = {} 

33 

34 remote_execution_pb2_grpc.add_CapabilitiesServicer_to_server(self, server) 

35 

36 # --- Public API --- 

37 

38 def add_instance(self, name, instance): 

39 self.__instances[name] = instance 

40 

41 def add_cas_instance(self, name, instance): 

42 self.__instances[name].add_cas_instance(instance) 

43 

44 def add_action_cache_instance(self, name, instance): 

45 self.__instances[name].add_action_cache_instance(instance) 

46 

47 def add_execution_instance(self, name, instance): 

48 self.__instances[name].add_execution_instance(instance) 

49 

50 # --- Public API: Servicer --- 

51 

52 @authorize(AuthContext) 

53 def GetCapabilities(self, request, context): 

54 """Handles GetCapabilitiesRequest messages. 

55 

56 Args: 

57 request (GetCapabilitiesRequest): The incoming RPC request. 

58 context (grpc.ServicerContext): Context for the RPC call. 

59 """ 

60 self.__logger.info(f"GetCapabilities request from [{context.peer()}] " 

61 f"([{printable_request_metadata(context.invocation_metadata())}])") 

62 

63 try: 

64 instance = self._get_instance(request.instance_name) 

65 

66 return instance.get_capabilities() 

67 

68 except InvalidArgumentError as e: 

69 self.__logger.info(e) 

70 context.set_details(str(e)) 

71 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

72 

73 except RetriableError as e: 

74 self.__logger.info(f"Retriable error, client should retry in: {e.retry_info.retry_delay}") 

75 context.abort_with_status(e.error_status) 

76 

77 except Exception as e: 

78 self.__logger.exception( 

79 f"Unexpected error in GetCapabilities; request=[{request}]" 

80 ) 

81 context.set_details(str(e)) 

82 context.set_code(grpc.StatusCode.INTERNAL) 

83 

84 return remote_execution_pb2.ServerCapabilities() 

85 

86 # --- Private API --- 

87 

88 def _get_instance(self, name): 

89 try: 

90 return self.__instances[name] 

91 

92 except KeyError: 

93 raise InvalidArgumentError(f"Instance doesn't exist on server: [{name}]")