Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 

20from buildgrid._exceptions import InvalidArgumentError 

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

22from buildgrid.server._authentication import AuthContext, authorize 

23from buildgrid.server.request_metadata_utils import printable_request_metadata 

24 

25 

26class CapabilitiesService(remote_execution_pb2_grpc.CapabilitiesServicer): 

27 

28 def __init__(self, server): 

29 self.__logger = logging.getLogger(__name__) 

30 

31 self.__instances = {} 

32 

33 remote_execution_pb2_grpc.add_CapabilitiesServicer_to_server(self, server) 

34 

35 # --- Public API --- 

36 

37 def add_instance(self, name, instance): 

38 self.__instances[name] = instance 

39 

40 def add_cas_instance(self, name, instance): 

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

42 

43 def add_action_cache_instance(self, name, instance): 

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

45 

46 def add_execution_instance(self, name, instance): 

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

48 

49 # --- Public API: Servicer --- 

50 

51 @authorize(AuthContext) 

52 def GetCapabilities(self, request, context): 

53 """Handles GetCapabilitiesRequest messages. 

54 

55 Args: 

56 request (GetCapabilitiesRequest): The incoming RPC request. 

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

58 """ 

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

60 f"([{printable_request_metadata(context)}])") 

61 

62 try: 

63 instance = self._get_instance(request.instance_name) 

64 

65 return instance.get_capabilities() 

66 

67 except InvalidArgumentError as e: 

68 self.__logger.error(e) 

69 context.set_details(str(e)) 

70 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

71 

72 return remote_execution_pb2.ServerCapabilities() 

73 

74 # --- Private API --- 

75 

76 def _get_instance(self, name): 

77 try: 

78 return self.__instances[name] 

79 

80 except KeyError: 

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