... |
... |
@@ -25,10 +25,12 @@ from urllib.parse import urlparse |
25
|
25
|
import sys
|
26
|
26
|
|
27
|
27
|
import click
|
|
28
|
+from google.protobuf import json_format
|
28
|
29
|
import grpc
|
29
|
30
|
|
30
|
31
|
from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2, remote_execution_pb2_grpc
|
31
|
32
|
from buildgrid._protos.google.longrunning import operations_pb2, operations_pb2_grpc
|
|
33
|
+from buildgrid._protos.google.rpc import code_pb2
|
32
|
34
|
|
33
|
35
|
from ..cli import pass_context
|
34
|
36
|
|
... |
... |
@@ -65,45 +67,125 @@ def cli(context, remote, instance_name, client_key, client_cert, server_cert): |
65
|
67
|
context.logger.debug("Starting for remote {}".format(context.remote))
|
66
|
68
|
|
67
|
69
|
|
|
70
|
+def _print_operation_status(operation, print_details=False):
|
|
71
|
+ metadata = remote_execution_pb2.ExecuteOperationMetadata()
|
|
72
|
+ # The metadata is expected to be an ExecuteOperationMetadata message:
|
|
73
|
+ assert operation.metadata.Is(metadata.DESCRIPTOR)
|
|
74
|
+ operation.metadata.Unpack(metadata)
|
|
75
|
+
|
|
76
|
+ stage_name = remote_execution_pb2.ExecuteOperationMetadata.Stage.Name(
|
|
77
|
+ metadata.stage).upper()
|
|
78
|
+
|
|
79
|
+ if not operation.done:
|
|
80
|
+ if stage_name == 'CACHE_CHECK':
|
|
81
|
+ click.echo('CacheCheck: {}: Querying action-cache (stage={})'
|
|
82
|
+ .format(operation.name, metadata.stage))
|
|
83
|
+ elif stage_name == 'QUEUED':
|
|
84
|
+ click.echo('Queued: {}: Waiting for execution (stage={})'
|
|
85
|
+ .format(operation.name, metadata.stage))
|
|
86
|
+ elif stage_name == 'EXECUTING':
|
|
87
|
+ click.echo('Executing: {}: Currently running (stage={})'
|
|
88
|
+ .format(operation.name, metadata.stage))
|
|
89
|
+ else:
|
|
90
|
+ click.echo('Error: {}: In an invalid state (stage={})'
|
|
91
|
+ .format(operation.name, metadata.stage), err=True)
|
|
92
|
+ return
|
|
93
|
+
|
|
94
|
+ response = remote_execution_pb2.ExecuteResponse()
|
|
95
|
+ # The response is expected to be an ExecutionResponse message:
|
|
96
|
+ assert operation.response.Is(response.DESCRIPTOR)
|
|
97
|
+ operation.response.Unpack(response)
|
|
98
|
+
|
|
99
|
+ if response.status.code != code_pb2.OK:
|
|
100
|
+ click.echo('Failure: {}: {} (code={})'
|
|
101
|
+ .format(operation.name, response.status.message, response.status.code))
|
|
102
|
+ else:
|
|
103
|
+ if response.result.exit_code != 0:
|
|
104
|
+ click.echo('Success: {}: Completed with failure (stage={}, exit_code={})'
|
|
105
|
+ .format(operation.name, metadata.stage, response.result.exit_code))
|
|
106
|
+ else:
|
|
107
|
+ click.echo('Success: {}: Completed succesfully (stage={}, exit_code={})'
|
|
108
|
+ .format(operation.name, metadata.stage, response.result.exit_code))
|
|
109
|
+
|
|
110
|
+ if print_details:
|
|
111
|
+ metadata = response.result.execution_metadata
|
|
112
|
+ click.echo(' worker={}'.format(metadata.worker))
|
|
113
|
+
|
|
114
|
+ queued = metadata.queued_timestamp.ToDatetime()
|
|
115
|
+ click.echo(' queued_at={}'.format(queued))
|
|
116
|
+
|
|
117
|
+ worker_start = metadata.worker_start_timestamp.ToDatetime()
|
|
118
|
+ worker_completed = metadata.worker_completed_timestamp.ToDatetime()
|
|
119
|
+ click.echo(' work_duration={}'.format(worker_completed - worker_start))
|
|
120
|
+
|
|
121
|
+ fetch_start = metadata.input_fetch_start_timestamp.ToDatetime()
|
|
122
|
+ fetch_completed = metadata.input_fetch_completed_timestamp.ToDatetime()
|
|
123
|
+ click.echo(' fetch_duration={}'.format(fetch_completed - fetch_start))
|
|
124
|
+
|
|
125
|
+ execution_start = metadata.execution_start_timestamp.ToDatetime()
|
|
126
|
+ execution_completed = metadata.execution_completed_timestamp.ToDatetime()
|
|
127
|
+ click.echo(' exection_duration={}'.format(execution_completed - execution_start))
|
|
128
|
+
|
|
129
|
+ upload_start = metadata.output_upload_start_timestamp.ToDatetime()
|
|
130
|
+ upload_completed = metadata.output_upload_completed_timestamp.ToDatetime()
|
|
131
|
+ click.echo(' upload_duration={}'.format(upload_completed - upload_start))
|
|
132
|
+
|
|
133
|
+ click.echo(' total_duration={}'.format(worker_completed - queued))
|
|
134
|
+
|
|
135
|
+
|
68
|
136
|
@cli.command('status', short_help="Get the status of an operation.")
|
69
|
137
|
@click.argument('operation-name', nargs=1, type=click.STRING, required=True)
|
|
138
|
+@click.option('--json', is_flag=True, show_default=True,
|
|
139
|
+ help="Print operations status in JSON format.")
|
70
|
140
|
@pass_context
|
71
|
|
-def status(context, operation_name):
|
72
|
|
- context.logger.info("Getting operation status...")
|
|
141
|
+def status(context, operation_name, json):
|
73
|
142
|
stub = operations_pb2_grpc.OperationsStub(context.channel)
|
74
|
|
-
|
75
|
143
|
request = operations_pb2.GetOperationRequest(name=operation_name)
|
76
|
144
|
|
77
|
|
- response = stub.GetOperation(request)
|
78
|
|
- context.logger.info(response)
|
|
145
|
+ operation = stub.GetOperation(request)
|
|
146
|
+
|
|
147
|
+ if not json:
|
|
148
|
+ _print_operation_status(operation, print_details=True)
|
|
149
|
+ else:
|
|
150
|
+ click.echo(json_format.MessageToJson(operation))
|
79
|
151
|
|
80
|
152
|
|
81
|
153
|
@cli.command('list', short_help="List operations.")
|
|
154
|
+@click.option('--json', is_flag=True, show_default=True,
|
|
155
|
+ help="Print operations list in JSON format.")
|
82
|
156
|
@pass_context
|
83
|
|
-def lists(context):
|
84
|
|
- context.logger.info("Getting list of operations")
|
|
157
|
+def lists(context, json):
|
85
|
158
|
stub = operations_pb2_grpc.OperationsStub(context.channel)
|
86
|
|
-
|
87
|
159
|
request = operations_pb2.ListOperationsRequest(name=context.instance_name)
|
88
|
160
|
|
89
|
161
|
response = stub.ListOperations(request)
|
90
|
162
|
|
91
|
163
|
if not response.operations:
|
92
|
|
- context.logger.warning("No operations to list")
|
|
164
|
+ click.echo('Error: No operations to list', err=True)
|
93
|
165
|
return
|
94
|
166
|
|
95
|
|
- for op in response.operations:
|
96
|
|
- context.logger.info(op)
|
|
167
|
+ for operation in response.operations:
|
|
168
|
+ if not json:
|
|
169
|
+ _print_operation_status(operation)
|
|
170
|
+ else:
|
|
171
|
+ click.echo(json_format.MessageToJson(operation))
|
97
|
172
|
|
98
|
173
|
|
99
|
174
|
@cli.command('wait', short_help="Streams an operation until it is complete.")
|
100
|
175
|
@click.argument('operation-name', nargs=1, type=click.STRING, required=True)
|
|
176
|
+@click.option('--json', is_flag=True, show_default=True,
|
|
177
|
+ help="Print operations statuses in JSON format.")
|
101
|
178
|
@pass_context
|
102
|
|
-def wait(context, operation_name):
|
|
179
|
+def wait(context, operation_name, json):
|
103
|
180
|
stub = remote_execution_pb2_grpc.ExecutionStub(context.channel)
|
104
|
181
|
request = remote_execution_pb2.WaitExecutionRequest(name=operation_name)
|
105
|
182
|
|
106
|
|
- response = stub.WaitExecution(request)
|
|
183
|
+ operation_iterator = stub.WaitExecution(request)
|
107
|
184
|
|
108
|
|
- for stream in response:
|
109
|
|
- context.logger.info(stream)
|
|
185
|
+ for operation in operation_iterator:
|
|
186
|
+ if not json and operation.done:
|
|
187
|
+ _print_operation_status(operation, print_details=True)
|
|
188
|
+ elif not json:
|
|
189
|
+ _print_operation_status(operation)
|
|
190
|
+ else:
|
|
191
|
+ click.echo(json_format.MessageToJson(operation))
|