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, NotFoundError 

21from buildgrid._protos.buildstream.v2 import buildstream_pb2 

22from buildgrid._protos.buildstream.v2 import buildstream_pb2_grpc 

23from buildgrid.server._authentication import AuthContext, authorize 

24 

25 

26class ReferenceStorageService(buildstream_pb2_grpc.ReferenceStorageServicer): 

27 

28 def __init__(self, server): 

29 self.__logger = logging.getLogger(__name__) 

30 

31 self._instances = {} 

32 

33 buildstream_pb2_grpc.add_ReferenceStorageServicer_to_server(self, server) 

34 

35 # --- Public API --- 

36 

37 def add_instance(self, name, instance): 

38 self._instances[name] = instance 

39 

40 # --- Public API: Servicer --- 

41 

42 @authorize(AuthContext) 

43 def GetReference(self, request, context): 

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

45 

46 try: 

47 instance = self._get_instance(request.instance_name) 

48 digest = instance.get_digest_reference(request.key) 

49 response = buildstream_pb2.GetReferenceResponse() 

50 response.digest.CopyFrom(digest) 

51 return response 

52 

53 except InvalidArgumentError as e: 

54 self.__logger.error(e) 

55 context.set_details(str(e)) 

56 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

57 

58 except NotFoundError as e: 

59 self.__logger.debug(e) 

60 context.set_code(grpc.StatusCode.NOT_FOUND) 

61 

62 return buildstream_pb2.GetReferenceResponse() 

63 

64 @authorize(AuthContext) 

65 def UpdateReference(self, request, context): 

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

67 

68 try: 

69 instance = self._get_instance(request.instance_name) 

70 digest = request.digest 

71 

72 for key in request.keys: 

73 instance.update_reference(key, digest) 

74 

75 except InvalidArgumentError as e: 

76 self.__logger.error(e) 

77 context.set_details(str(e)) 

78 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

79 

80 except NotImplementedError: 

81 context.set_code(grpc.StatusCode.UNIMPLEMENTED) 

82 

83 return buildstream_pb2.UpdateReferenceResponse() 

84 

85 @authorize(AuthContext) 

86 def Status(self, request, context): 

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

88 

89 try: 

90 instance = self._get_instance(request.instance_name) 

91 allow_updates = instance.allow_updates 

92 return buildstream_pb2.StatusResponse(allow_updates=allow_updates) 

93 

94 except InvalidArgumentError as e: 

95 self.__logger.error(e) 

96 context.set_details(str(e)) 

97 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

98 

99 return buildstream_pb2.StatusResponse() 

100 

101 # --- Private API --- 

102 

103 def _get_instance(self, instance_name): 

104 try: 

105 return self._instances[instance_name] 

106 

107 except KeyError: 

108 raise InvalidArgumentError(f"Invalid instance name: [{instance_name}]")