| OLD | NEW |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 # This is a reimplementation of RemoteClientNative but it uses (will use) | 5 # This is a reimplementation of RemoteClientNative but it uses (will use) |
| 6 # a gRPC method to communicate with a server instead of REST. | 6 # a gRPC method to communicate with a server instead of REST. |
| 7 | 7 |
| 8 import copy | 8 import copy |
| 9 import json | 9 import json |
| 10 import logging | 10 import logging |
| 11 import time | 11 import time |
| 12 | 12 |
| 13 import grpc | 13 import grpc |
| 14 import google.protobuf.json_format | 14 import google.protobuf.json_format |
| 15 from proto_bot import swarming_bot_pb2 | 15 from proto_bot import swarming_bot_pb2 |
| 16 from remote_client_errors import InternalError | 16 from remote_client_errors import InternalError |
| 17 from remote_client_errors import MintOAuthTokenError |
| 17 from remote_client_errors import PollError | 18 from remote_client_errors import PollError |
| 18 from utils import net | 19 from utils import net |
| 19 | 20 |
| 20 | 21 |
| 21 # How long to wait for a response from the server. Keeping the same as | 22 # How long to wait for a response from the server. Keeping the same as |
| 22 # the equivalent in remote_client.py for now. | 23 # the equivalent in remote_client.py for now. |
| 23 NET_CONNECTION_TIMEOUT_SEC = 5*60 | 24 NET_CONNECTION_TIMEOUT_SEC = 5*60 |
| 24 | 25 |
| 25 | 26 |
| 26 # How many times to retry a gRPC call | 27 # How many times to retry a gRPC call |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 logging.info('Updating to version: %s', bot_version) | 189 logging.info('Updating to version: %s', bot_version) |
| 189 request = swarming_bot_pb2.BotUpdateRequest() | 190 request = swarming_bot_pb2.BotUpdateRequest() |
| 190 request.bot_version = bot_version | 191 request.bot_version = bot_version |
| 191 response = call_grpc(self._stub.BotUpdate, request) | 192 response = call_grpc(self._stub.BotUpdate, request) |
| 192 with open(new_zip_fn, 'wb') as f: | 193 with open(new_zip_fn, 'wb') as f: |
| 193 f.write(response.bot_code) | 194 f.write(response.bot_code) |
| 194 | 195 |
| 195 def ping(self): | 196 def ping(self): |
| 196 pass | 197 pass |
| 197 | 198 |
| 199 def mint_oauth_token(self, task_id, bot_id, account_id, scopes): |
| 200 # pylint: disable=unused-argument |
| 201 raise MintOAuthTokenError( |
| 202 'mint_oauth_token is not supported in grpc protocol') |
| 203 |
| 198 | 204 |
| 199 def create_state_proto(state_dict, message): | 205 def create_state_proto(state_dict, message): |
| 200 """ Constructs a State message out of a state dict. | 206 """ Constructs a State message out of a state dict. |
| 201 | 207 |
| 202 Inspired by https://github.com/davyzhang/dict-to-protobuf, but all sub-dicts | 208 Inspired by https://github.com/davyzhang/dict-to-protobuf, but all sub-dicts |
| 203 need to be encoded as google.protobuf.Structs because only Structs can handle | 209 need to be encoded as google.protobuf.Structs because only Structs can handle |
| 204 free-form key-value pairs (and the mount points, for example, are not known | 210 free-form key-value pairs (and the mount points, for example, are not known |
| 205 at compile time). | 211 at compile time). |
| 206 | 212 |
| 207 Why not use Struct for the *entire* message? It's because json_format.Parse | 213 Why not use Struct for the *entire* message? It's because json_format.Parse |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 except grpc.RpcError as g: | 252 except grpc.RpcError as g: |
| 247 if g.code() is not grpc.StatusCode.UNAVAILABLE: | 253 if g.code() is not grpc.StatusCode.UNAVAILABLE: |
| 248 raise | 254 raise |
| 249 logging.warning('call_grpc - proxy is unavailable (attempt %d/%d)', | 255 logging.warning('call_grpc - proxy is unavailable (attempt %d/%d)', |
| 250 attempt, MAX_GRPC_ATTEMPTS) | 256 attempt, MAX_GRPC_ATTEMPTS) |
| 251 grpc_error = g | 257 grpc_error = g |
| 252 time.sleep(net.calculate_sleep_before_retry(attempt, MAX_GRPC_SLEEP)) | 258 time.sleep(net.calculate_sleep_before_retry(attempt, MAX_GRPC_SLEEP)) |
| 253 # If we get here, it must be because we got (and saved) an error | 259 # If we get here, it must be because we got (and saved) an error |
| 254 assert grpc_error is not None | 260 assert grpc_error is not None |
| 255 raise grpc_error | 261 raise grpc_error |
| OLD | NEW |