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 

18from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2 

19from buildgrid._protos.build.bazel.semver import semver_pb2 

20from buildgrid.settings import HIGH_REAPI_VERSION, LOW_REAPI_VERSION 

21 

22 

23class CapabilitiesInstance: 

24 

25 def __init__(self, cas_instance=None, action_cache_instance=None, execution_instance=None): 

26 self.__logger = logging.getLogger(__name__) 

27 

28 self._instance_name = None 

29 

30 self.__cas_instance = cas_instance 

31 self.__action_cache_instance = action_cache_instance 

32 self.__execution_instance = execution_instance 

33 

34 self.__high_api_version = None 

35 self.__low_api_version = None 

36 

37 # --- Public API --- 

38 

39 @property 

40 def instance_name(self): 

41 return self._instance_name 

42 

43 def register_instance_with_server(self, instance_name, server): 

44 """Names and registers the capabilities instance with a given server.""" 

45 if self._instance_name is None: 

46 server.add_capabilities_instance(self, instance_name) 

47 

48 self._instance_name = instance_name 

49 

50 else: 

51 raise AssertionError("Instance already registered") 

52 

53 def add_cas_instance(self, cas_instance): 

54 self.__cas_instance = cas_instance 

55 

56 def add_action_cache_instance(self, action_cache_instance): 

57 self.__action_cache_instance = action_cache_instance 

58 

59 def add_execution_instance(self, execution_instance): 

60 self.__execution_instance = execution_instance 

61 

62 def get_capabilities(self): 

63 cache_capabilities = self._get_cache_capabilities() 

64 execution_capabilities = self._get_capabilities_execution() 

65 

66 if self.__high_api_version is None: 

67 self.__high_api_version = self._split_semantic_version(HIGH_REAPI_VERSION) 

68 if self.__low_api_version is None: 

69 self.__low_api_version = self._split_semantic_version(LOW_REAPI_VERSION) 

70 

71 server_capabilities = remote_execution_pb2.ServerCapabilities() 

72 server_capabilities.cache_capabilities.CopyFrom(cache_capabilities) 

73 server_capabilities.execution_capabilities.CopyFrom(execution_capabilities) 

74 server_capabilities.low_api_version.CopyFrom(self.__low_api_version) 

75 server_capabilities.high_api_version.CopyFrom(self.__high_api_version) 

76 

77 return server_capabilities 

78 

79 # --- Private API --- 

80 

81 def _get_cache_capabilities(self): 

82 capabilities = remote_execution_pb2.CacheCapabilities() 

83 action_cache_update_capabilities = remote_execution_pb2.ActionCacheUpdateCapabilities() 

84 

85 if self.__cas_instance: 

86 capabilities.digest_function.extend([self.__cas_instance.hash_type()]) 

87 capabilities.max_batch_total_size_bytes = self.__cas_instance.max_batch_total_size_bytes() 

88 capabilities.symlink_absolute_path_strategy = self.__cas_instance.symlink_absolute_path_strategy() 

89 # TODO: execution priority #102 

90 # capabilities.cache_priority_capabilities = 

91 

92 if self.__action_cache_instance: 

93 capabilities.digest_function.extend([self.__action_cache_instance.hash_type()]) 

94 action_cache_update_capabilities.update_enabled = self.__action_cache_instance.allow_updates 

95 

96 capabilities.action_cache_update_capabilities.CopyFrom(action_cache_update_capabilities) 

97 return capabilities 

98 

99 def _get_capabilities_execution(self): 

100 capabilities = remote_execution_pb2.ExecutionCapabilities() 

101 if self.__execution_instance: 

102 capabilities.exec_enabled = True 

103 capabilities.digest_function = self.__execution_instance.hash_type() 

104 # TODO: execution priority #102 

105 # capabilities.execution_priority = 

106 

107 else: 

108 capabilities.exec_enabled = False 

109 

110 return capabilities 

111 

112 def _split_semantic_version(self, version_string): 

113 major_version, minor_version, patch_version = version_string.split('.') 

114 

115 semantic_version = semver_pb2.SemVer() 

116 semantic_version.major = int(major_version) 

117 semantic_version.minor = int(minor_version) 

118 semantic_version.patch = int(patch_version) 

119 

120 return semantic_version