Martin Blanchard pushed to branch jmac/remote_execution_client at BuildStream / buildstream
Commits:
-
936bb93a
by Jim MacArthur at 2018-09-07T12:57:28Z
-
b0f46545
by Martin Blanchard at 2018-09-07T12:57:29Z
-
50bf3139
by Martin Blanchard at 2018-09-07T12:57:29Z
-
853e0af7
by Martin Blanchard at 2018-09-07T12:57:29Z
-
e7659c29
by Jim MacArthur at 2018-09-07T12:57:29Z
-
7306a954
by Jim MacArthur at 2018-09-07T12:57:29Z
-
c0ef7106
by Jim MacArthur at 2018-09-07T12:57:29Z
-
43651af0
by Jim MacArthur at 2018-09-07T12:57:29Z
-
da53c005
by Jim MacArthur at 2018-09-07T12:57:29Z
-
c73896bb
by Jim MacArthur at 2018-09-07T12:57:29Z
-
89b29b8b
by Jim MacArthur at 2018-09-07T12:57:29Z
-
116f80d2
by Jim MacArthur at 2018-09-07T12:57:29Z
-
05956c71
by Martin Blanchard at 2018-09-07T12:57:29Z
-
909b3be4
by Martin Blanchard at 2018-09-07T12:57:29Z
-
556f2abb
by Martin Blanchard at 2018-09-07T12:57:30Z
-
71e2b543
by Martin Blanchard at 2018-09-07T12:57:30Z
-
d4c515d0
by Martin Blanchard at 2018-09-07T12:57:30Z
-
923d443b
by Martin Blanchard at 2018-09-07T13:22:48Z
-
9ef53163
by Martin Blanchard at 2018-09-07T13:22:57Z
25 changed files:
- buildstream/_artifactcache/cascache.py
- buildstream/_project.py
- buildstream/buildelement.py
- buildstream/data/projectconfig.yaml
- buildstream/element.py
- buildstream/plugins/elements/autotools.py
- buildstream/plugins/elements/cmake.py
- buildstream/plugins/elements/make.py
- buildstream/plugins/elements/meson.py
- buildstream/plugins/elements/qmake.py
- buildstream/sandbox/__init__.py
- + buildstream/sandbox/_sandboxremote.py
- buildstream/sandbox/sandbox.py
- buildstream/storage/_casbaseddirectory.py
- doc/source/format_project.rst
- + tests/artifactcache/project/elements/compose-all.bst
- + tests/artifactcache/project/elements/import-bin.bst
- + tests/artifactcache/project/elements/import-dev.bst
- + tests/artifactcache/project/elements/target.bst
- + tests/artifactcache/project/files/bin-files/usr/bin/hello
- + tests/artifactcache/project/files/dev-files/usr/include/pony.h
- + tests/artifactcache/project/project.conf
- + tests/artifactcache/pull.py
- + tests/artifactcache/push.py
- tests/testutils/artifactshare.py
Changes:
| ... | ... | @@ -19,6 +19,7 @@ |
| 19 | 19 |
|
| 20 | 20 |
import hashlib
|
| 21 | 21 |
import itertools
|
| 22 |
+import io
|
|
| 22 | 23 |
import multiprocessing
|
| 23 | 24 |
import os
|
| 24 | 25 |
import signal
|
| ... | ... | @@ -76,6 +77,7 @@ class CASCache(ArtifactCache): |
| 76 | 77 |
################################################
|
| 77 | 78 |
# Implementation of abstract methods #
|
| 78 | 79 |
################################################
|
| 80 |
+ |
|
| 79 | 81 |
def contains(self, element, key):
|
| 80 | 82 |
refpath = self._refpath(self.get_artifact_fullname(element, key))
|
| 81 | 83 |
|
| ... | ... | @@ -153,6 +155,7 @@ class CASCache(ArtifactCache): |
| 153 | 155 |
q = multiprocessing.Queue()
|
| 154 | 156 |
for remote_spec in remote_specs:
|
| 155 | 157 |
# Use subprocess to avoid creation of gRPC threads in main BuildStream process
|
| 158 |
+ # See https://github.com/grpc/grpc/blob/master/doc/fork_support.md for details
|
|
| 156 | 159 |
p = multiprocessing.Process(target=self._initialize_remote, args=(remote_spec, q))
|
| 157 | 160 |
|
| 158 | 161 |
try:
|
| ... | ... | @@ -259,6 +262,25 @@ class CASCache(ArtifactCache): |
| 259 | 262 |
|
| 260 | 263 |
return False
|
| 261 | 264 |
|
| 265 |
+ def pull_tree(self, project, digest):
|
|
| 266 |
+ """ Pull a single Tree rather than an artifact.
|
|
| 267 |
+ Does not update local refs. """
|
|
| 268 |
+ |
|
| 269 |
+ for remote in self._remotes[project]:
|
|
| 270 |
+ try:
|
|
| 271 |
+ remote.init()
|
|
| 272 |
+ |
|
| 273 |
+ digest = self._fetch_tree(remote, digest)
|
|
| 274 |
+ |
|
| 275 |
+ # no need to pull from additional remotes
|
|
| 276 |
+ return digest
|
|
| 277 |
+ |
|
| 278 |
+ except grpc.RpcError as e:
|
|
| 279 |
+ if e.code() != grpc.StatusCode.NOT_FOUND:
|
|
| 280 |
+ raise
|
|
| 281 |
+ |
|
| 282 |
+ return None
|
|
| 283 |
+ |
|
| 262 | 284 |
def link_key(self, element, oldkey, newkey):
|
| 263 | 285 |
oldref = self.get_artifact_fullname(element, oldkey)
|
| 264 | 286 |
newref = self.get_artifact_fullname(element, newkey)
|
| ... | ... | @@ -267,8 +289,46 @@ class CASCache(ArtifactCache): |
| 267 | 289 |
|
| 268 | 290 |
self.set_ref(newref, tree)
|
| 269 | 291 |
|
| 292 |
+ def _push_refs_to_remote(self, refs, remote):
|
|
| 293 |
+ skipped_remote = True
|
|
| 294 |
+ try:
|
|
| 295 |
+ for ref in refs:
|
|
| 296 |
+ tree = self.resolve_ref(ref)
|
|
| 297 |
+ |
|
| 298 |
+ # Check whether ref is already on the server in which case
|
|
| 299 |
+ # there is no need to push the artifact
|
|
| 300 |
+ try:
|
|
| 301 |
+ request = buildstream_pb2.GetReferenceRequest()
|
|
| 302 |
+ request.key = ref
|
|
| 303 |
+ response = remote.ref_storage.GetReference(request)
|
|
| 304 |
+ |
|
| 305 |
+ if response.digest.hash == tree.hash and response.digest.size_bytes == tree.size_bytes:
|
|
| 306 |
+ # ref is already on the server with the same tree
|
|
| 307 |
+ continue
|
|
| 308 |
+ |
|
| 309 |
+ except grpc.RpcError as e:
|
|
| 310 |
+ if e.code() != grpc.StatusCode.NOT_FOUND:
|
|
| 311 |
+ # Intentionally re-raise RpcError for outer except block.
|
|
| 312 |
+ raise
|
|
| 313 |
+ |
|
| 314 |
+ self._send_directory(remote, tree)
|
|
| 315 |
+ |
|
| 316 |
+ request = buildstream_pb2.UpdateReferenceRequest()
|
|
| 317 |
+ request.keys.append(ref)
|
|
| 318 |
+ request.digest.hash = tree.hash
|
|
| 319 |
+ request.digest.size_bytes = tree.size_bytes
|
|
| 320 |
+ remote.ref_storage.UpdateReference(request)
|
|
| 321 |
+ |
|
| 322 |
+ skipped_remote = False
|
|
| 323 |
+ except grpc.RpcError as e:
|
|
| 324 |
+ if e.code() != grpc.StatusCode.RESOURCE_EXHAUSTED:
|
|
| 325 |
+ raise ArtifactError("Failed to push artifact {}: {}".format(refs, e), temporary=True) from e
|
|
| 326 |
+ |
|
| 327 |
+ return not skipped_remote
|
|
| 328 |
+ |
|
| 270 | 329 |
def push(self, element, keys):
|
| 271 |
- refs = [self.get_artifact_fullname(element, key) for key in keys]
|
|
| 330 |
+ |
|
| 331 |
+ refs = [self.get_artifact_fullname(element, key) for key in list(keys)]
|
|
| 272 | 332 |
|
| 273 | 333 |
project = element._get_project()
|
| 274 | 334 |
|
| ... | ... | @@ -278,95 +338,77 @@ class CASCache(ArtifactCache): |
| 278 | 338 |
|
| 279 | 339 |
for remote in push_remotes:
|
| 280 | 340 |
remote.init()
|
| 281 |
- skipped_remote = True
|
|
| 282 |
- element.info("Pushing {} -> {}".format(element._get_brief_display_key(), remote.spec.url))
|
|
| 283 | 341 |
|
| 284 |
- try:
|
|
| 285 |
- for ref in refs:
|
|
| 286 |
- tree = self.resolve_ref(ref)
|
|
| 287 |
- |
|
| 288 |
- # Check whether ref is already on the server in which case
|
|
| 289 |
- # there is no need to push the artifact
|
|
| 290 |
- try:
|
|
| 291 |
- request = buildstream_pb2.GetReferenceRequest()
|
|
| 292 |
- request.key = ref
|
|
| 293 |
- response = remote.ref_storage.GetReference(request)
|
|
| 294 |
- |
|
| 295 |
- if response.digest.hash == tree.hash and response.digest.size_bytes == tree.size_bytes:
|
|
| 296 |
- # ref is already on the server with the same tree
|
|
| 297 |
- continue
|
|
| 298 |
- |
|
| 299 |
- except grpc.RpcError as e:
|
|
| 300 |
- if e.code() != grpc.StatusCode.NOT_FOUND:
|
|
| 301 |
- # Intentionally re-raise RpcError for outer except block.
|
|
| 302 |
- raise
|
|
| 303 |
- |
|
| 304 |
- missing_blobs = {}
|
|
| 305 |
- required_blobs = self._required_blobs(tree)
|
|
| 306 |
- |
|
| 307 |
- # Limit size of FindMissingBlobs request
|
|
| 308 |
- for required_blobs_group in _grouper(required_blobs, 512):
|
|
| 309 |
- request = remote_execution_pb2.FindMissingBlobsRequest()
|
|
| 310 |
- |
|
| 311 |
- for required_digest in required_blobs_group:
|
|
| 312 |
- d = request.blob_digests.add()
|
|
| 313 |
- d.hash = required_digest.hash
|
|
| 314 |
- d.size_bytes = required_digest.size_bytes
|
|
| 315 |
- |
|
| 316 |
- response = remote.cas.FindMissingBlobs(request)
|
|
| 317 |
- for digest in response.missing_blob_digests:
|
|
| 318 |
- d = remote_execution_pb2.Digest()
|
|
| 319 |
- d.hash = digest.hash
|
|
| 320 |
- d.size_bytes = digest.size_bytes
|
|
| 321 |
- missing_blobs[d.hash] = d
|
|
| 322 |
- |
|
| 323 |
- # Upload any blobs missing on the server
|
|
| 324 |
- skipped_remote = False
|
|
| 325 |
- for digest in missing_blobs.values():
|
|
| 326 |
- uuid_ = uuid.uuid4()
|
|
| 327 |
- resource_name = '/'.join(['uploads', str(uuid_), 'blobs',
|
|
| 328 |
- digest.hash, str(digest.size_bytes)])
|
|
| 329 |
- |
|
| 330 |
- def request_stream(resname):
|
|
| 331 |
- with open(self.objpath(digest), 'rb') as f:
|
|
| 332 |
- assert os.fstat(f.fileno()).st_size == digest.size_bytes
|
|
| 333 |
- offset = 0
|
|
| 334 |
- finished = False
|
|
| 335 |
- remaining = digest.size_bytes
|
|
| 336 |
- while not finished:
|
|
| 337 |
- chunk_size = min(remaining, 64 * 1024)
|
|
| 338 |
- remaining -= chunk_size
|
|
| 339 |
- |
|
| 340 |
- request = bytestream_pb2.WriteRequest()
|
|
| 341 |
- request.write_offset = offset
|
|
| 342 |
- # max. 64 kB chunks
|
|
| 343 |
- request.data = f.read(chunk_size)
|
|
| 344 |
- request.resource_name = resname
|
|
| 345 |
- request.finish_write = remaining <= 0
|
|
| 346 |
- yield request
|
|
| 347 |
- offset += chunk_size
|
|
| 348 |
- finished = request.finish_write
|
|
| 349 |
- response = remote.bytestream.Write(request_stream(resource_name))
|
|
| 350 |
- |
|
| 351 |
- request = buildstream_pb2.UpdateReferenceRequest()
|
|
| 352 |
- request.keys.append(ref)
|
|
| 353 |
- request.digest.hash = tree.hash
|
|
| 354 |
- request.digest.size_bytes = tree.size_bytes
|
|
| 355 |
- remote.ref_storage.UpdateReference(request)
|
|
| 356 |
- |
|
| 357 |
- pushed = True
|
|
| 358 |
- |
|
| 359 |
- except grpc.RpcError as e:
|
|
| 360 |
- if e.code() != grpc.StatusCode.RESOURCE_EXHAUSTED:
|
|
| 361 |
- raise ArtifactError("Failed to push artifact {}: {}".format(refs, e), temporary=True) from e
|
|
| 342 |
+ element.info("Pushing {} -> {}".format(element._get_brief_display_key(), remote.spec.url))
|
|
| 362 | 343 |
|
| 363 |
- if skipped_remote:
|
|
| 344 |
+ if self._push_refs_to_remote(refs, remote):
|
|
| 345 |
+ pushed = True
|
|
| 346 |
+ else:
|
|
| 364 | 347 |
self.context.message(Message(
|
| 365 | 348 |
None,
|
| 366 | 349 |
MessageType.SKIPPED,
|
| 367 | 350 |
"Remote ({}) already has {} cached".format(
|
| 368 | 351 |
remote.spec.url, element._get_brief_display_key())
|
| 369 | 352 |
))
|
| 353 |
+ |
|
| 354 |
+ return pushed
|
|
| 355 |
+ |
|
| 356 |
+ def push_directory(self, project, directory):
|
|
| 357 |
+ |
|
| 358 |
+ push_remotes = [r for r in self._remotes[project] if r.spec.push]
|
|
| 359 |
+ |
|
| 360 |
+ if directory.ref is None:
|
|
| 361 |
+ return None
|
|
| 362 |
+ |
|
| 363 |
+ for remote in push_remotes:
|
|
| 364 |
+ remote.init()
|
|
| 365 |
+ |
|
| 366 |
+ self._send_directory(remote, directory.ref)
|
|
| 367 |
+ |
|
| 368 |
+ return directory.ref
|
|
| 369 |
+ |
|
| 370 |
+ def push_message(self, project, message):
|
|
| 371 |
+ |
|
| 372 |
+ push_remotes = [r for r in self._remotes[project] if r.spec.push]
|
|
| 373 |
+ |
|
| 374 |
+ message_buffer = message.SerializeToString()
|
|
| 375 |
+ message_sha = hashlib.sha256(message_buffer)
|
|
| 376 |
+ message_digest = remote_execution_pb2.Digest()
|
|
| 377 |
+ message_digest.hash = message_sha.hexdigest()
|
|
| 378 |
+ message_digest.size_bytes = len(message_buffer)
|
|
| 379 |
+ |
|
| 380 |
+ for remote in push_remotes:
|
|
| 381 |
+ remote.init()
|
|
| 382 |
+ |
|
| 383 |
+ with io.BytesIO(message_buffer) as b:
|
|
| 384 |
+ self._send_blob(remote, message_digest, b)
|
|
| 385 |
+ |
|
| 386 |
+ return message_digest
|
|
| 387 |
+ |
|
| 388 |
+ def _verify_digest_on_remote(self, remote, digest):
|
|
| 389 |
+ # Check whether ref is already on the server in which case
|
|
| 390 |
+ # there is no need to push the artifact
|
|
| 391 |
+ request = remote_execution_pb2.FindMissingBlobsRequest()
|
|
| 392 |
+ request.blob_digests.extend([digest])
|
|
| 393 |
+ |
|
| 394 |
+ response = remote.cas.FindMissingBlobs(request)
|
|
| 395 |
+ if digest in response.missing_blob_digests:
|
|
| 396 |
+ return False
|
|
| 397 |
+ |
|
| 398 |
+ return True
|
|
| 399 |
+ |
|
| 400 |
+ def verify_digest_pushed(self, project, digest):
|
|
| 401 |
+ |
|
| 402 |
+ push_remotes = [r for r in self._remotes[project] if r.spec.push]
|
|
| 403 |
+ |
|
| 404 |
+ pushed = False
|
|
| 405 |
+ |
|
| 406 |
+ for remote in push_remotes:
|
|
| 407 |
+ remote.init()
|
|
| 408 |
+ |
|
| 409 |
+ if self._verify_digest_on_remote(remote, digest):
|
|
| 410 |
+ pushed = True
|
|
| 411 |
+ |
|
| 370 | 412 |
return pushed
|
| 371 | 413 |
|
| 372 | 414 |
################################################
|
| ... | ... | @@ -599,6 +641,7 @@ class CASCache(ArtifactCache): |
| 599 | 641 |
################################################
|
| 600 | 642 |
# Local Private Methods #
|
| 601 | 643 |
################################################
|
| 644 |
+ |
|
| 602 | 645 |
def _checkout(self, dest, tree):
|
| 603 | 646 |
os.makedirs(dest, exist_ok=True)
|
| 604 | 647 |
|
| ... | ... | @@ -761,16 +804,16 @@ class CASCache(ArtifactCache): |
| 761 | 804 |
#
|
| 762 | 805 |
q.put(str(e))
|
| 763 | 806 |
|
| 764 |
- def _required_blobs(self, tree):
|
|
| 807 |
+ def _required_blobs(self, directory_digest):
|
|
| 765 | 808 |
# parse directory, and recursively add blobs
|
| 766 | 809 |
d = remote_execution_pb2.Digest()
|
| 767 |
- d.hash = tree.hash
|
|
| 768 |
- d.size_bytes = tree.size_bytes
|
|
| 810 |
+ d.hash = directory_digest.hash
|
|
| 811 |
+ d.size_bytes = directory_digest.size_bytes
|
|
| 769 | 812 |
yield d
|
| 770 | 813 |
|
| 771 | 814 |
directory = remote_execution_pb2.Directory()
|
| 772 | 815 |
|
| 773 |
- with open(self.objpath(tree), 'rb') as f:
|
|
| 816 |
+ with open(self.objpath(directory_digest), 'rb') as f:
|
|
| 774 | 817 |
directory.ParseFromString(f.read())
|
| 775 | 818 |
|
| 776 | 819 |
for filenode in directory.files:
|
| ... | ... | @@ -782,16 +825,16 @@ class CASCache(ArtifactCache): |
| 782 | 825 |
for dirnode in directory.directories:
|
| 783 | 826 |
yield from self._required_blobs(dirnode.digest)
|
| 784 | 827 |
|
| 785 |
- def _fetch_blob(self, remote, digest, out):
|
|
| 828 |
+ def _fetch_blob(self, remote, digest, stream):
|
|
| 786 | 829 |
resource_name = '/'.join(['blobs', digest.hash, str(digest.size_bytes)])
|
| 787 | 830 |
request = bytestream_pb2.ReadRequest()
|
| 788 | 831 |
request.resource_name = resource_name
|
| 789 | 832 |
request.read_offset = 0
|
| 790 | 833 |
for response in remote.bytestream.Read(request):
|
| 791 |
- out.write(response.data)
|
|
| 834 |
+ stream.write(response.data)
|
|
| 835 |
+ stream.flush()
|
|
| 792 | 836 |
|
| 793 |
- out.flush()
|
|
| 794 |
- assert digest.size_bytes == os.fstat(out.fileno()).st_size
|
|
| 837 |
+ assert digest.size_bytes == os.fstat(stream.fileno()).st_size
|
|
| 795 | 838 |
|
| 796 | 839 |
def _fetch_directory(self, remote, tree):
|
| 797 | 840 |
objpath = self.objpath(tree)
|
| ... | ... | @@ -827,6 +870,92 @@ class CASCache(ArtifactCache): |
| 827 | 870 |
digest = self.add_object(path=out.name)
|
| 828 | 871 |
assert digest.hash == tree.hash
|
| 829 | 872 |
|
| 873 |
+ def _fetch_tree(self, remote, digest):
|
|
| 874 |
+ # download but do not store the Tree object
|
|
| 875 |
+ with tempfile.NamedTemporaryFile(dir=self.tmpdir) as out:
|
|
| 876 |
+ self._fetch_blob(remote, digest, out)
|
|
| 877 |
+ |
|
| 878 |
+ tree = remote_execution_pb2.Tree()
|
|
| 879 |
+ |
|
| 880 |
+ with open(out.name, 'rb') as f:
|
|
| 881 |
+ tree.ParseFromString(f.read())
|
|
| 882 |
+ |
|
| 883 |
+ tree.children.extend([tree.root])
|
|
| 884 |
+ for directory in tree.children:
|
|
| 885 |
+ for filenode in directory.files:
|
|
| 886 |
+ fileobjpath = self.objpath(filenode.digest)
|
|
| 887 |
+ if os.path.exists(fileobjpath):
|
|
| 888 |
+ # already in local cache
|
|
| 889 |
+ continue
|
|
| 890 |
+ |
|
| 891 |
+ with tempfile.NamedTemporaryFile(dir=self.tmpdir) as f:
|
|
| 892 |
+ self._fetch_blob(remote, filenode.digest, f)
|
|
| 893 |
+ |
|
| 894 |
+ added_digest = self.add_object(path=f.name)
|
|
| 895 |
+ assert added_digest.hash == filenode.digest.hash
|
|
| 896 |
+ |
|
| 897 |
+ # place directory blob only in final location when we've downloaded
|
|
| 898 |
+ # all referenced blobs to avoid dangling references in the repository
|
|
| 899 |
+ dirbuffer = directory.SerializeToString()
|
|
| 900 |
+ dirdigest = self.add_object(buffer=dirbuffer)
|
|
| 901 |
+ assert dirdigest.size_bytes == len(dirbuffer)
|
|
| 902 |
+ |
|
| 903 |
+ return dirdigest
|
|
| 904 |
+ |
|
| 905 |
+ def _send_blob(self, remote, digest, stream, u_uid=uuid.uuid4()):
|
|
| 906 |
+ resource_name = '/'.join(['uploads', str(u_uid), 'blobs',
|
|
| 907 |
+ digest.hash, str(digest.size_bytes)])
|
|
| 908 |
+ |
|
| 909 |
+ def request_stream(resname, instream):
|
|
| 910 |
+ offset = 0
|
|
| 911 |
+ finished = False
|
|
| 912 |
+ remaining = digest.size_bytes
|
|
| 913 |
+ while not finished:
|
|
| 914 |
+ chunk_size = min(remaining, 64 * 1024)
|
|
| 915 |
+ remaining -= chunk_size
|
|
| 916 |
+ |
|
| 917 |
+ request = bytestream_pb2.WriteRequest()
|
|
| 918 |
+ request.write_offset = offset
|
|
| 919 |
+ # max. 64 kB chunks
|
|
| 920 |
+ request.data = instream.read(chunk_size)
|
|
| 921 |
+ request.resource_name = resname
|
|
| 922 |
+ request.finish_write = remaining <= 0
|
|
| 923 |
+ |
|
| 924 |
+ yield request
|
|
| 925 |
+ |
|
| 926 |
+ offset += chunk_size
|
|
| 927 |
+ finished = request.finish_write
|
|
| 928 |
+ |
|
| 929 |
+ response = remote.bytestream.Write(request_stream(resource_name, stream))
|
|
| 930 |
+ |
|
| 931 |
+ assert response.committed_size == digest.size_bytes
|
|
| 932 |
+ |
|
| 933 |
+ def _send_directory(self, remote, digest, u_uid=uuid.uuid4()):
|
|
| 934 |
+ required_blobs = self._required_blobs(digest)
|
|
| 935 |
+ |
|
| 936 |
+ missing_blobs = dict()
|
|
| 937 |
+ # Limit size of FindMissingBlobs request
|
|
| 938 |
+ for required_blobs_group in _grouper(required_blobs, 512):
|
|
| 939 |
+ request = remote_execution_pb2.FindMissingBlobsRequest()
|
|
| 940 |
+ |
|
| 941 |
+ for required_digest in required_blobs_group:
|
|
| 942 |
+ d = request.blob_digests.add()
|
|
| 943 |
+ d.hash = required_digest.hash
|
|
| 944 |
+ d.size_bytes = required_digest.size_bytes
|
|
| 945 |
+ |
|
| 946 |
+ response = remote.cas.FindMissingBlobs(request)
|
|
| 947 |
+ for missing_digest in response.missing_blob_digests:
|
|
| 948 |
+ d = remote_execution_pb2.Digest()
|
|
| 949 |
+ d.hash = missing_digest.hash
|
|
| 950 |
+ d.size_bytes = missing_digest.size_bytes
|
|
| 951 |
+ missing_blobs[d.hash] = d
|
|
| 952 |
+ |
|
| 953 |
+ # Upload any blobs missing on the server
|
|
| 954 |
+ for blob_digest in missing_blobs.values():
|
|
| 955 |
+ with open(self.objpath(blob_digest), 'rb') as f:
|
|
| 956 |
+ assert os.fstat(f.fileno()).st_size == blob_digest.size_bytes
|
|
| 957 |
+ self._send_blob(remote, blob_digest, f, u_uid=u_uid)
|
|
| 958 |
+ |
|
| 830 | 959 |
|
| 831 | 960 |
# Represents a single remote CAS cache.
|
| 832 | 961 |
#
|
| ... | ... | @@ -128,6 +128,7 @@ class Project(): |
| 128 | 128 |
self._shell_host_files = [] # A list of HostMount objects
|
| 129 | 129 |
|
| 130 | 130 |
self.artifact_cache_specs = None
|
| 131 |
+ self.remote_execution_url = None
|
|
| 131 | 132 |
self._sandbox = None
|
| 132 | 133 |
self._splits = None
|
| 133 | 134 |
|
| ... | ... | @@ -471,7 +472,7 @@ class Project(): |
| 471 | 472 |
'aliases', 'name',
|
| 472 | 473 |
'artifacts', 'options',
|
| 473 | 474 |
'fail-on-overlap', 'shell', 'fatal-warnings',
|
| 474 |
- 'ref-storage', 'sandbox', 'mirrors'
|
|
| 475 |
+ 'ref-storage', 'sandbox', 'mirrors', 'remote-execution'
|
|
| 475 | 476 |
])
|
| 476 | 477 |
|
| 477 | 478 |
#
|
| ... | ... | @@ -482,6 +483,11 @@ class Project(): |
| 482 | 483 |
# Load artifacts pull/push configuration for this project
|
| 483 | 484 |
self.artifact_cache_specs = ArtifactCache.specs_from_config_node(config, self.directory)
|
| 484 | 485 |
|
| 486 |
+ # Load remote-execution configuration for this project
|
|
| 487 |
+ remote_execution = _yaml.node_get(config, Mapping, 'remote-execution')
|
|
| 488 |
+ _yaml.node_validate(remote_execution, ['url'])
|
|
| 489 |
+ self.remote_execution_url = _yaml.node_get(remote_execution, str, 'url')
|
|
| 490 |
+ |
|
| 485 | 491 |
# Load sandbox environment variables
|
| 486 | 492 |
self.base_environment = _yaml.node_get(config, Mapping, 'environment')
|
| 487 | 493 |
self.base_env_nocache = _yaml.node_get(config, list, 'environment-nocache')
|
| ... | ... | @@ -155,6 +155,9 @@ class BuildElement(Element): |
| 155 | 155 |
command_dir = build_root
|
| 156 | 156 |
sandbox.set_work_directory(command_dir)
|
| 157 | 157 |
|
| 158 |
+ # Tell sandbox which directory is preserved in the finished artifact
|
|
| 159 |
+ sandbox.set_output_directory(install_root)
|
|
| 160 |
+ |
|
| 158 | 161 |
# Setup environment
|
| 159 | 162 |
sandbox.set_environment(self.get_environment())
|
| 160 | 163 |
|
| ... | ... | @@ -204,3 +204,6 @@ shell: |
| 204 | 204 |
# Command to run when `bst shell` does not provide a command
|
| 205 | 205 |
#
|
| 206 | 206 |
command: [ 'sh', '-i' ]
|
| 207 |
+ |
|
| 208 |
+remote-execution:
|
|
| 209 |
+ url: ""
|
|
| \ No newline at end of file |
| ... | ... | @@ -95,6 +95,7 @@ from . import _site |
| 95 | 95 |
from ._platform import Platform
|
| 96 | 96 |
from .plugin import CoreWarnings
|
| 97 | 97 |
from .sandbox._config import SandboxConfig
|
| 98 |
+from .sandbox._sandboxremote import SandboxRemote
|
|
| 98 | 99 |
|
| 99 | 100 |
from .storage.directory import Directory
|
| 100 | 101 |
from .storage._filebaseddirectory import FileBasedDirectory
|
| ... | ... | @@ -250,6 +251,12 @@ class Element(Plugin): |
| 250 | 251 |
# Extract Sandbox config
|
| 251 | 252 |
self.__sandbox_config = self.__extract_sandbox_config(meta)
|
| 252 | 253 |
|
| 254 |
+ # Extract remote execution URL
|
|
| 255 |
+ if not self.__is_junction:
|
|
| 256 |
+ self.__remote_execution_url = project.remote_execution_url
|
|
| 257 |
+ else:
|
|
| 258 |
+ self.__remote_execution_url = None
|
|
| 259 |
+ |
|
| 253 | 260 |
def __lt__(self, other):
|
| 254 | 261 |
return self.name < other.name
|
| 255 | 262 |
|
| ... | ... | @@ -1570,6 +1577,8 @@ class Element(Plugin): |
| 1570 | 1577 |
finally:
|
| 1571 | 1578 |
if collect is not None:
|
| 1572 | 1579 |
try:
|
| 1580 |
+ # Sandbox will probably have replaced its virtual directory, so get it again
|
|
| 1581 |
+ sandbox_vroot = sandbox.get_virtual_directory()
|
|
| 1573 | 1582 |
collectvdir = sandbox_vroot.descend(collect.lstrip(os.sep).split(os.sep))
|
| 1574 | 1583 |
except VirtualDirectoryError:
|
| 1575 | 1584 |
# No collect directory existed
|
| ... | ... | @@ -2146,7 +2155,32 @@ class Element(Plugin): |
| 2146 | 2155 |
project = self._get_project()
|
| 2147 | 2156 |
platform = Platform.get_platform()
|
| 2148 | 2157 |
|
| 2149 |
- if directory is not None and os.path.exists(directory):
|
|
| 2158 |
+ if self.__remote_execution_url and self.BST_VIRTUAL_DIRECTORY:
|
|
| 2159 |
+ if not self.__artifacts.has_push_remotes(element=self):
|
|
| 2160 |
+ # Give an early warning if remote execution will not work
|
|
| 2161 |
+ raise ElementError("Artifact {} is configured to use remote execution but has no push remotes. "
|
|
| 2162 |
+ .format(self.name) +
|
|
| 2163 |
+ "The remote artifact server(s) may not be correctly configured or contactable.")
|
|
| 2164 |
+ |
|
| 2165 |
+ self.info("Using a remote sandbox for artifact {}".format(self.name))
|
|
| 2166 |
+ |
|
| 2167 |
+ sandbox = SandboxRemote(context, project,
|
|
| 2168 |
+ directory,
|
|
| 2169 |
+ stdout=stdout,
|
|
| 2170 |
+ stderr=stderr,
|
|
| 2171 |
+ config=config,
|
|
| 2172 |
+ server_url=self.__remote_execution_url,
|
|
| 2173 |
+ allow_real_directory=False)
|
|
| 2174 |
+ yield sandbox
|
|
| 2175 |
+ |
|
| 2176 |
+ elif directory is not None and os.path.exists(directory):
|
|
| 2177 |
+ if self.__remote_execution_url:
|
|
| 2178 |
+ self.warn("Artifact {} is configured to use remote execution but element plugin does not support it."
|
|
| 2179 |
+ .format(self.name), detail="Element plugin '{kind}' does not support virtual directories."
|
|
| 2180 |
+ .format(kind=self.get_kind()), warning_token="remote-failure")
|
|
| 2181 |
+ |
|
| 2182 |
+ self.info("Falling back to local sandbox for artifact {}".format(self.name))
|
|
| 2183 |
+ |
|
| 2150 | 2184 |
sandbox = platform.create_sandbox(context, project,
|
| 2151 | 2185 |
directory,
|
| 2152 | 2186 |
stdout=stdout,
|
| ... | ... | @@ -57,7 +57,8 @@ from buildstream import BuildElement |
| 57 | 57 |
|
| 58 | 58 |
# Element implementation for the 'autotools' kind.
|
| 59 | 59 |
class AutotoolsElement(BuildElement):
|
| 60 |
- pass
|
|
| 60 |
+ # Supports virtual directories (required for remote execution)
|
|
| 61 |
+ BST_VIRTUAL_DIRECTORY = True
|
|
| 61 | 62 |
|
| 62 | 63 |
|
| 63 | 64 |
# Plugin entry point
|
| ... | ... | @@ -56,7 +56,8 @@ from buildstream import BuildElement |
| 56 | 56 |
|
| 57 | 57 |
# Element implementation for the 'cmake' kind.
|
| 58 | 58 |
class CMakeElement(BuildElement):
|
| 59 |
- pass
|
|
| 59 |
+ # Supports virtual directories (required for remote execution)
|
|
| 60 |
+ BST_VIRTUAL_DIRECTORY = True
|
|
| 60 | 61 |
|
| 61 | 62 |
|
| 62 | 63 |
# Plugin entry point
|
| ... | ... | @@ -38,7 +38,8 @@ from buildstream import BuildElement |
| 38 | 38 |
|
| 39 | 39 |
# Element implementation for the 'make' kind.
|
| 40 | 40 |
class MakeElement(BuildElement):
|
| 41 |
- pass
|
|
| 41 |
+ # Supports virtual directories (required for remote execution)
|
|
| 42 |
+ BST_VIRTUAL_DIRECTORY = True
|
|
| 42 | 43 |
|
| 43 | 44 |
|
| 44 | 45 |
# Plugin entry point
|
| ... | ... | @@ -53,7 +53,8 @@ from buildstream import BuildElement |
| 53 | 53 |
|
| 54 | 54 |
# Element implementation for the 'meson' kind.
|
| 55 | 55 |
class MesonElement(BuildElement):
|
| 56 |
- pass
|
|
| 56 |
+ # Supports virtual directories (required for remote execution)
|
|
| 57 |
+ BST_VIRTUAL_DIRECTORY = True
|
|
| 57 | 58 |
|
| 58 | 59 |
|
| 59 | 60 |
# Plugin entry point
|
| ... | ... | @@ -33,7 +33,8 @@ from buildstream import BuildElement |
| 33 | 33 |
|
| 34 | 34 |
# Element implementation for the 'qmake' kind.
|
| 35 | 35 |
class QMakeElement(BuildElement):
|
| 36 |
- pass
|
|
| 36 |
+ # Supports virtual directories (required for remote execution)
|
|
| 37 |
+ BST_VIRTUAL_DIRECTORY = True
|
|
| 37 | 38 |
|
| 38 | 39 |
|
| 39 | 40 |
# Plugin entry point
|
| ... | ... | @@ -20,3 +20,4 @@ |
| 20 | 20 |
from .sandbox import Sandbox, SandboxFlags
|
| 21 | 21 |
from ._sandboxchroot import SandboxChroot
|
| 22 | 22 |
from ._sandboxbwrap import SandboxBwrap
|
| 23 |
+from ._sandboxremote import SandboxRemote
|
| 1 |
+#!/usr/bin/env python3
|
|
| 2 |
+#
|
|
| 3 |
+# Copyright (C) 2018 Bloomberg LP
|
|
| 4 |
+#
|
|
| 5 |
+# This program is free software; you can redistribute it and/or
|
|
| 6 |
+# modify it under the terms of the GNU Lesser General Public
|
|
| 7 |
+# License as published by the Free Software Foundation; either
|
|
| 8 |
+# version 2 of the License, or (at your option) any later version.
|
|
| 9 |
+#
|
|
| 10 |
+# This library is distributed in the hope that it will be useful,
|
|
| 11 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 12 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
| 13 |
+# Lesser General Public License for more details.
|
|
| 14 |
+#
|
|
| 15 |
+# You should have received a copy of the GNU Lesser General Public
|
|
| 16 |
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
| 17 |
+#
|
|
| 18 |
+# Authors:
|
|
| 19 |
+# Jim MacArthur <jim macarthur codethink co uk>
|
|
| 20 |
+ |
|
| 21 |
+import os
|
|
| 22 |
+from urllib.parse import urlparse
|
|
| 23 |
+ |
|
| 24 |
+import grpc
|
|
| 25 |
+ |
|
| 26 |
+from . import Sandbox
|
|
| 27 |
+from ..storage._filebaseddirectory import FileBasedDirectory
|
|
| 28 |
+from ..storage._casbaseddirectory import CasBasedDirectory
|
|
| 29 |
+from .._protos.build.bazel.remote.execution.v2 import remote_execution_pb2, remote_execution_pb2_grpc
|
|
| 30 |
+from .._artifactcache.cascache import CASCache
|
|
| 31 |
+ |
|
| 32 |
+ |
|
| 33 |
+class SandboxError(Exception):
|
|
| 34 |
+ pass
|
|
| 35 |
+ |
|
| 36 |
+ |
|
| 37 |
+# SandboxRemote()
|
|
| 38 |
+#
|
|
| 39 |
+# This isn't really a sandbox, it's a stub which sends all the sources and build
|
|
| 40 |
+# commands to a remote server and retrieves the results from it.
|
|
| 41 |
+#
|
|
| 42 |
+class SandboxRemote(Sandbox):
|
|
| 43 |
+ |
|
| 44 |
+ def __init__(self, *args, **kwargs):
|
|
| 45 |
+ super().__init__(*args, **kwargs)
|
|
| 46 |
+ self.cascache = None
|
|
| 47 |
+ |
|
| 48 |
+ url = urlparse(kwargs['server_url'])
|
|
| 49 |
+ if not url.scheme or not url.hostname or not url.port:
|
|
| 50 |
+ raise SandboxError("Configured remote URL '{}' does not match the expected layout. "
|
|
| 51 |
+ .format(kwargs['server_url']) +
|
|
| 52 |
+ "It should be of the form <protocol>://<domain name>:<port>.")
|
|
| 53 |
+ elif url.scheme != 'http':
|
|
| 54 |
+ raise SandboxError("Configured remote '{}' uses an unsupported protocol. "
|
|
| 55 |
+ "Only plain HTTP is currenlty supported (no HTTPS).")
|
|
| 56 |
+ |
|
| 57 |
+ self.server_url = '{}:{}'.format(url.hostname, url.port)
|
|
| 58 |
+ |
|
| 59 |
+ def _get_cascache(self):
|
|
| 60 |
+ if self.cascache is None:
|
|
| 61 |
+ self.cascache = CASCache(self._get_context())
|
|
| 62 |
+ self.cascache.setup_remotes(use_config=True)
|
|
| 63 |
+ return self.cascache
|
|
| 64 |
+ |
|
| 65 |
+ def run_remote_command(self, command, input_root_digest, working_directory, environment):
|
|
| 66 |
+ # Sends an execution request to the remote execution server.
|
|
| 67 |
+ #
|
|
| 68 |
+ # This function blocks until it gets a response from the server.
|
|
| 69 |
+ #
|
|
| 70 |
+ environment_variables = [remote_execution_pb2.Command.
|
|
| 71 |
+ EnvironmentVariable(name=k, value=v)
|
|
| 72 |
+ for (k, v) in environment.items()]
|
|
| 73 |
+ |
|
| 74 |
+ # Create and send the Command object.
|
|
| 75 |
+ remote_command = remote_execution_pb2.Command(arguments=command,
|
|
| 76 |
+ working_directory=working_directory,
|
|
| 77 |
+ environment_variables=environment_variables,
|
|
| 78 |
+ output_files=[],
|
|
| 79 |
+ output_directories=[self._output_directory],
|
|
| 80 |
+ platform=None)
|
|
| 81 |
+ |
|
| 82 |
+ cascache = self._get_cascache()
|
|
| 83 |
+ # Upload the Command message to the remote CAS server
|
|
| 84 |
+ command_digest = cascache.push_message(self._get_project(), remote_command)
|
|
| 85 |
+ if not command_digest or not cascache.verify_digest_pushed(self._get_project(), command_digest):
|
|
| 86 |
+ # Command push failed
|
|
| 87 |
+ return None
|
|
| 88 |
+ |
|
| 89 |
+ # Create and send the action.
|
|
| 90 |
+ action = remote_execution_pb2.Action(command_digest=command_digest,
|
|
| 91 |
+ input_root_digest=input_root_digest,
|
|
| 92 |
+ timeout=None,
|
|
| 93 |
+ do_not_cache=False)
|
|
| 94 |
+ |
|
| 95 |
+ # Upload the Action message to the remote CAS server
|
|
| 96 |
+ action_digest = cascache.push_message(self._get_project(), action)
|
|
| 97 |
+ if not action_digest or not cascache.verify_digest_pushed(self._get_project(), action_digest):
|
|
| 98 |
+ # Action push failed
|
|
| 99 |
+ return None
|
|
| 100 |
+ |
|
| 101 |
+ # Next, try to create a communication channel to the BuildGrid server.
|
|
| 102 |
+ channel = grpc.insecure_channel(self.server_url)
|
|
| 103 |
+ stub = remote_execution_pb2_grpc.ExecutionStub(channel)
|
|
| 104 |
+ request = remote_execution_pb2.ExecuteRequest(action_digest=action_digest,
|
|
| 105 |
+ skip_cache_lookup=False)
|
|
| 106 |
+ try:
|
|
| 107 |
+ operation_iterator = stub.Execute(request)
|
|
| 108 |
+ except grpc.RpcError:
|
|
| 109 |
+ return None
|
|
| 110 |
+ |
|
| 111 |
+ operation = None
|
|
| 112 |
+ with self._get_context().timed_activity("Waiting for the remote build to complete"):
|
|
| 113 |
+ # It is advantageous to check operation_iterator.code() is grpc.StatusCode.OK here,
|
|
| 114 |
+ # which will check the server is actually contactable. However, calling it when the
|
|
| 115 |
+ # server is available seems to cause .code() to hang forever.
|
|
| 116 |
+ for operation in operation_iterator:
|
|
| 117 |
+ if operation.done:
|
|
| 118 |
+ break
|
|
| 119 |
+ |
|
| 120 |
+ return operation
|
|
| 121 |
+ |
|
| 122 |
+ def process_job_output(self, output_directories, output_files):
|
|
| 123 |
+ # Reads the remote execution server response to an execution request.
|
|
| 124 |
+ #
|
|
| 125 |
+ # output_directories is an array of OutputDirectory objects.
|
|
| 126 |
+ # output_files is an array of OutputFile objects.
|
|
| 127 |
+ #
|
|
| 128 |
+ # We only specify one output_directory, so it's an error
|
|
| 129 |
+ # for there to be any output files or more than one directory at the moment.
|
|
| 130 |
+ #
|
|
| 131 |
+ if output_files:
|
|
| 132 |
+ raise SandboxError("Output files were returned when we didn't request any.")
|
|
| 133 |
+ elif not output_directories:
|
|
| 134 |
+ error_text = "No output directory was returned from the build server."
|
|
| 135 |
+ raise SandboxError(error_text)
|
|
| 136 |
+ elif len(output_directories) > 1:
|
|
| 137 |
+ error_text = "More than one output directory was returned from the build server: {}."
|
|
| 138 |
+ raise SandboxError(error_text.format(output_directories))
|
|
| 139 |
+ |
|
| 140 |
+ tree_digest = output_directories[0].tree_digest
|
|
| 141 |
+ if tree_digest is None or not tree_digest.hash:
|
|
| 142 |
+ raise SandboxError("Output directory structure had no digest attached.")
|
|
| 143 |
+ |
|
| 144 |
+ cascache = self._get_cascache()
|
|
| 145 |
+ # Now do a pull to ensure we have the necessary parts.
|
|
| 146 |
+ dir_digest = cascache.pull_tree(self._get_project(), tree_digest)
|
|
| 147 |
+ if dir_digest is None or not dir_digest.hash or not dir_digest.size_bytes:
|
|
| 148 |
+ raise SandboxError("Output directory structure pulling from remote failed.")
|
|
| 149 |
+ |
|
| 150 |
+ path_components = os.path.split(self._output_directory)
|
|
| 151 |
+ |
|
| 152 |
+ # Now what we have is a digest for the output. Once we return, the calling process will
|
|
| 153 |
+ # attempt to descend into our directory and find that directory, so we need to overwrite
|
|
| 154 |
+ # that.
|
|
| 155 |
+ |
|
| 156 |
+ if not path_components:
|
|
| 157 |
+ # The artifact wants the whole directory; we could just return the returned hash in its
|
|
| 158 |
+ # place, but we don't have a means to do that yet.
|
|
| 159 |
+ raise SandboxError("Unimplemented: Output directory is empty or equal to the sandbox root.")
|
|
| 160 |
+ |
|
| 161 |
+ # At the moment, we will get the whole directory back in the first directory argument and we need
|
|
| 162 |
+ # to replace the sandbox's virtual directory with that. Creating a new virtual directory object
|
|
| 163 |
+ # from another hash will be interesting, though...
|
|
| 164 |
+ |
|
| 165 |
+ new_dir = CasBasedDirectory(self._get_context(), ref=dir_digest)
|
|
| 166 |
+ self._set_virtual_directory(new_dir)
|
|
| 167 |
+ |
|
| 168 |
+ def run(self, command, flags, *, cwd=None, env=None):
|
|
| 169 |
+ # Upload sources
|
|
| 170 |
+ upload_vdir = self.get_virtual_directory()
|
|
| 171 |
+ |
|
| 172 |
+ if isinstance(upload_vdir, FileBasedDirectory):
|
|
| 173 |
+ # Make a new temporary directory to put source in
|
|
| 174 |
+ upload_vdir = CasBasedDirectory(self._get_context(), ref=None)
|
|
| 175 |
+ upload_vdir.import_files(self.get_virtual_directory()._get_underlying_directory())
|
|
| 176 |
+ |
|
| 177 |
+ upload_vdir.recalculate_hash()
|
|
| 178 |
+ |
|
| 179 |
+ cascache = self._get_cascache()
|
|
| 180 |
+ # Now, push that key (without necessarily needing a ref) to the remote.
|
|
| 181 |
+ vdir_digest = cascache.push_directory(self._get_project(), upload_vdir)
|
|
| 182 |
+ if not vdir_digest or not cascache.verify_digest_pushed(self._get_project(), vdir_digest):
|
|
| 183 |
+ raise SandboxError("Failed to verify that source has been pushed to the remote artifact cache.")
|
|
| 184 |
+ |
|
| 185 |
+ # Set up environment and working directory
|
|
| 186 |
+ if cwd is None:
|
|
| 187 |
+ cwd = self._get_work_directory()
|
|
| 188 |
+ |
|
| 189 |
+ if cwd is None:
|
|
| 190 |
+ cwd = '/'
|
|
| 191 |
+ |
|
| 192 |
+ if env is None:
|
|
| 193 |
+ env = self._get_environment()
|
|
| 194 |
+ |
|
| 195 |
+ # We want command args as a list of strings
|
|
| 196 |
+ if isinstance(command, str):
|
|
| 197 |
+ command = [command]
|
|
| 198 |
+ |
|
| 199 |
+ # Now transmit the command to execute
|
|
| 200 |
+ operation = self.run_remote_command(command, upload_vdir.ref, cwd, env)
|
|
| 201 |
+ |
|
| 202 |
+ if operation is None:
|
|
| 203 |
+ # Failure of remote execution, usually due to an error in BuildStream
|
|
| 204 |
+ # NB This error could be raised in __run_remote_command
|
|
| 205 |
+ raise SandboxError("No response returned from server")
|
|
| 206 |
+ |
|
| 207 |
+ assert not operation.HasField('error') and operation.HasField('response')
|
|
| 208 |
+ |
|
| 209 |
+ execution_response = remote_execution_pb2.ExecuteResponse()
|
|
| 210 |
+ # The response is expected to be an ExecutionResponse message
|
|
| 211 |
+ assert operation.response.Is(execution_response.DESCRIPTOR)
|
|
| 212 |
+ |
|
| 213 |
+ operation.response.Unpack(execution_response)
|
|
| 214 |
+ |
|
| 215 |
+ if execution_response.status.code != 0:
|
|
| 216 |
+ # A normal error during the build: the remote execution system
|
|
| 217 |
+ # has worked correctly but the command failed.
|
|
| 218 |
+ # execution_response.error also contains 'message' (str) and
|
|
| 219 |
+ # 'details' (iterator of Any) which we ignore at the moment.
|
|
| 220 |
+ return execution_response.status.code
|
|
| 221 |
+ |
|
| 222 |
+ action_result = execution_response.result
|
|
| 223 |
+ |
|
| 224 |
+ self.process_job_output(action_result.output_directories, action_result.output_files)
|
|
| 225 |
+ |
|
| 226 |
+ return 0
|
| ... | ... | @@ -99,9 +99,11 @@ class Sandbox(): |
| 99 | 99 |
self.__stdout = kwargs['stdout']
|
| 100 | 100 |
self.__stderr = kwargs['stderr']
|
| 101 | 101 |
|
| 102 |
- # Setup the directories. Root should be available to subclasses, hence
|
|
| 103 |
- # being single-underscore. The others are private to this class.
|
|
| 102 |
+ # Setup the directories. Root and output_directory should be
|
|
| 103 |
+ # available to subclasses, hence being single-underscore. The
|
|
| 104 |
+ # others are private to this class.
|
|
| 104 | 105 |
self._root = os.path.join(directory, 'root')
|
| 106 |
+ self._output_directory = None
|
|
| 105 | 107 |
self.__directory = directory
|
| 106 | 108 |
self.__scratch = os.path.join(self.__directory, 'scratch')
|
| 107 | 109 |
for directory_ in [self._root, self.__scratch]:
|
| ... | ... | @@ -144,11 +146,17 @@ class Sandbox(): |
| 144 | 146 |
self._vdir = FileBasedDirectory(self._root)
|
| 145 | 147 |
return self._vdir
|
| 146 | 148 |
|
| 149 |
+ def _set_virtual_directory(self, virtual_directory):
|
|
| 150 |
+ """ Sets virtual directory. Useful after remote execution
|
|
| 151 |
+ has rewritten the working directory.
|
|
| 152 |
+ """
|
|
| 153 |
+ self._vdir = virtual_directory
|
|
| 154 |
+ |
|
| 147 | 155 |
def set_environment(self, environment):
|
| 148 | 156 |
"""Sets the environment variables for the sandbox
|
| 149 | 157 |
|
| 150 | 158 |
Args:
|
| 151 |
- directory (dict): The environment variables to use in the sandbox
|
|
| 159 |
+ environment (dict): The environment variables to use in the sandbox
|
|
| 152 | 160 |
"""
|
| 153 | 161 |
self.__env = environment
|
| 154 | 162 |
|
| ... | ... | @@ -160,6 +168,15 @@ class Sandbox(): |
| 160 | 168 |
"""
|
| 161 | 169 |
self.__cwd = directory
|
| 162 | 170 |
|
| 171 |
+ def set_output_directory(self, directory):
|
|
| 172 |
+ """Sets the output directory - the directory which is preserved
|
|
| 173 |
+ as an artifact after assembly.
|
|
| 174 |
+ |
|
| 175 |
+ Args:
|
|
| 176 |
+ directory (str): An absolute path within the sandbox
|
|
| 177 |
+ """
|
|
| 178 |
+ self._output_directory = directory
|
|
| 179 |
+ |
|
| 163 | 180 |
def mark_directory(self, directory, *, artifact=False):
|
| 164 | 181 |
"""Marks a sandbox directory and ensures it will exist
|
| 165 | 182 |
|
| ... | ... | @@ -543,6 +543,15 @@ class CasBasedDirectory(Directory): |
| 543 | 543 |
filelist.append(k)
|
| 544 | 544 |
return filelist
|
| 545 | 545 |
|
| 546 |
+ def recalculate_hash(self):
|
|
| 547 |
+ """ Recalcuates the hash for this directory and store the results in
|
|
| 548 |
+ the cache. If this directory has a parent, tell it to
|
|
| 549 |
+ recalculate (since changing this directory changes an entry in
|
|
| 550 |
+ the parent). Hashes for subdirectories also get recalculated.
|
|
| 551 |
+ """
|
|
| 552 |
+ self._recalculate_recursing_up()
|
|
| 553 |
+ self._recalculate_recursing_down()
|
|
| 554 |
+ |
|
| 546 | 555 |
def _get_identifier(self):
|
| 547 | 556 |
path = ""
|
| 548 | 557 |
if self.parent:
|
| ... | ... | @@ -204,6 +204,24 @@ with an artifact share. |
| 204 | 204 |
You can also specify a list of caches here; earlier entries in the list
|
| 205 | 205 |
will have higher priority than later ones.
|
| 206 | 206 |
|
| 207 |
+Remote execution
|
|
| 208 |
+~~~~~~~~~~~~~~~~
|
|
| 209 |
+BuildStream supports remote execution using the Google Remote Execution API
|
|
| 210 |
+(REAPI). A description of how remote execution works is beyond the scope
|
|
| 211 |
+of this document, but you can specify a remote server complying with the REAPI
|
|
| 212 |
+using the `remote-execution` option:
|
|
| 213 |
+ |
|
| 214 |
+.. code:: yaml
|
|
| 215 |
+ |
|
| 216 |
+ remote-execution:
|
|
| 217 |
+ |
|
| 218 |
+ # A url defining a remote execution server
|
|
| 219 |
+ url: http://buildserver.example.com:50051
|
|
| 220 |
+ |
|
| 221 |
+The url should contain a hostname and port separated by ':'. Only plain HTTP is
|
|
| 222 |
+currently suported (no HTTPS).
|
|
| 223 |
+ |
|
| 224 |
+The Remote Execution API can be found via https://github.com/bazelbuild/remote-apis.
|
|
| 207 | 225 |
|
| 208 | 226 |
.. _project_essentials_mirrors:
|
| 209 | 227 |
|
| 1 |
+kind: compose
|
|
| 2 |
+ |
|
| 3 |
+depends:
|
|
| 4 |
+- filename: import-bin.bst
|
|
| 5 |
+ type: build
|
|
| 6 |
+- filename: import-dev.bst
|
|
| 7 |
+ type: build
|
|
| 8 |
+ |
|
| 9 |
+config:
|
|
| 10 |
+ # Dont try running the sandbox, we dont have a
|
|
| 11 |
+ # runtime to run anything in this context.
|
|
| 12 |
+ integrate: False
|
| 1 |
+kind: import
|
|
| 2 |
+sources:
|
|
| 3 |
+- kind: local
|
|
| 4 |
+ path: files/bin-files
|
| 1 |
+kind: import
|
|
| 2 |
+sources:
|
|
| 3 |
+- kind: local
|
|
| 4 |
+ path: files/dev-files
|
| 1 |
+kind: stack
|
|
| 2 |
+description: |
|
|
| 3 |
+ |
|
| 4 |
+ Main stack target for the bst build test
|
|
| 5 |
+ |
|
| 6 |
+depends:
|
|
| 7 |
+- import-bin.bst
|
|
| 8 |
+- import-dev.bst
|
|
| 9 |
+- compose-all.bst
|
| 1 |
+#!/bin/bash
|
|
| 2 |
+ |
|
| 3 |
+echo "Hello !"
|
| 1 |
+#ifndef __PONY_H__
|
|
| 2 |
+#define __PONY_H__
|
|
| 3 |
+ |
|
| 4 |
+#define PONY_BEGIN "Once upon a time, there was a pony."
|
|
| 5 |
+#define PONY_END "And they lived happily ever after, the end."
|
|
| 6 |
+ |
|
| 7 |
+#define MAKE_PONY(story) \
|
|
| 8 |
+ PONY_BEGIN \
|
|
| 9 |
+ story \
|
|
| 10 |
+ PONY_END
|
|
| 11 |
+ |
|
| 12 |
+#endif /* __PONY_H__ */
|
| 1 |
+# Project config for frontend build test
|
|
| 2 |
+name: test
|
|
| 3 |
+ |
|
| 4 |
+element-path: elements
|
| 1 |
+import hashlib
|
|
| 2 |
+import multiprocessing
|
|
| 3 |
+import os
|
|
| 4 |
+import signal
|
|
| 5 |
+ |
|
| 6 |
+import pytest
|
|
| 7 |
+ |
|
| 8 |
+from buildstream import _yaml, _signals, utils
|
|
| 9 |
+from buildstream._artifactcache.cascache import CASCache
|
|
| 10 |
+from buildstream._context import Context
|
|
| 11 |
+from buildstream._project import Project
|
|
| 12 |
+from buildstream._protos.build.bazel.remote.execution.v2 import remote_execution_pb2
|
|
| 13 |
+ |
|
| 14 |
+from tests.testutils import cli, create_artifact_share
|
|
| 15 |
+ |
|
| 16 |
+ |
|
| 17 |
+# Project directory
|
|
| 18 |
+DATA_DIR = os.path.join(
|
|
| 19 |
+ os.path.dirname(os.path.realpath(__file__)),
|
|
| 20 |
+ "project",
|
|
| 21 |
+)
|
|
| 22 |
+ |
|
| 23 |
+ |
|
| 24 |
+# Handle messages from the pipeline
|
|
| 25 |
+def message_handler(message, context):
|
|
| 26 |
+ pass
|
|
| 27 |
+ |
|
| 28 |
+ |
|
| 29 |
+def tree_maker(cas, tree, directory):
|
|
| 30 |
+ if tree.root.ByteSize() == 0:
|
|
| 31 |
+ tree.root.CopyFrom(directory)
|
|
| 32 |
+ |
|
| 33 |
+ for directory_node in directory.directories:
|
|
| 34 |
+ child_directory = tree.children.add()
|
|
| 35 |
+ |
|
| 36 |
+ with open(cas.objpath(directory_node.digest), 'rb') as f:
|
|
| 37 |
+ child_directory.ParseFromString(f.read())
|
|
| 38 |
+ |
|
| 39 |
+ tree_maker(cas, tree, child_directory)
|
|
| 40 |
+ |
|
| 41 |
+ |
|
| 42 |
+@pytest.mark.datafiles(DATA_DIR)
|
|
| 43 |
+def test_pull(cli, tmpdir, datafiles):
|
|
| 44 |
+ project_dir = str(datafiles)
|
|
| 45 |
+ |
|
| 46 |
+ # Set up an artifact cache.
|
|
| 47 |
+ with create_artifact_share(os.path.join(str(tmpdir), 'artifactshare')) as share:
|
|
| 48 |
+ # Configure artifact share
|
|
| 49 |
+ artifact_dir = os.path.join(str(tmpdir), 'cache', 'artifacts')
|
|
| 50 |
+ user_config_file = str(tmpdir.join('buildstream.conf'))
|
|
| 51 |
+ user_config = {
|
|
| 52 |
+ 'scheduler': {
|
|
| 53 |
+ 'pushers': 1
|
|
| 54 |
+ },
|
|
| 55 |
+ 'artifacts': {
|
|
| 56 |
+ 'url': share.repo,
|
|
| 57 |
+ 'push': True,
|
|
| 58 |
+ }
|
|
| 59 |
+ }
|
|
| 60 |
+ |
|
| 61 |
+ # Write down the user configuration file
|
|
| 62 |
+ _yaml.dump(_yaml.node_sanitize(user_config), filename=user_config_file)
|
|
| 63 |
+ # Ensure CLI calls will use it
|
|
| 64 |
+ cli.configure(user_config)
|
|
| 65 |
+ |
|
| 66 |
+ # First build the project with the artifact cache configured
|
|
| 67 |
+ result = cli.run(project=project_dir, args=['build', 'target.bst'])
|
|
| 68 |
+ result.assert_success()
|
|
| 69 |
+ |
|
| 70 |
+ # Assert that we are now cached locally
|
|
| 71 |
+ assert cli.get_element_state(project_dir, 'target.bst') == 'cached'
|
|
| 72 |
+ # Assert that we shared/pushed the cached artifact
|
|
| 73 |
+ element_key = cli.get_element_key(project_dir, 'target.bst')
|
|
| 74 |
+ assert share.has_artifact('test', 'target.bst', element_key)
|
|
| 75 |
+ |
|
| 76 |
+ # Delete the artifact locally
|
|
| 77 |
+ cli.remove_artifact_from_cache(project_dir, 'target.bst')
|
|
| 78 |
+ |
|
| 79 |
+ # Assert that we are not cached locally anymore
|
|
| 80 |
+ assert cli.get_element_state(project_dir, 'target.bst') != 'cached'
|
|
| 81 |
+ |
|
| 82 |
+ # Fake minimal context
|
|
| 83 |
+ context = Context()
|
|
| 84 |
+ context.load(config=user_config_file)
|
|
| 85 |
+ context.artifactdir = os.path.join(str(tmpdir), 'cache', 'artifacts')
|
|
| 86 |
+ context.set_message_handler(message_handler)
|
|
| 87 |
+ |
|
| 88 |
+ # Load the project and CAS cache
|
|
| 89 |
+ project = Project(project_dir, context)
|
|
| 90 |
+ project.ensure_fully_loaded()
|
|
| 91 |
+ cas = CASCache(context)
|
|
| 92 |
+ |
|
| 93 |
+ # Assert that the element's artifact is **not** cached
|
|
| 94 |
+ element = project.load_elements(['target.bst'], cas)[0]
|
|
| 95 |
+ element_key = cli.get_element_key(project_dir, 'target.bst')
|
|
| 96 |
+ assert not cas.contains(element, element_key)
|
|
| 97 |
+ |
|
| 98 |
+ queue = multiprocessing.Queue()
|
|
| 99 |
+ # Use subprocess to avoid creation of gRPC threads in main BuildStream process
|
|
| 100 |
+ # See https://github.com/grpc/grpc/blob/master/doc/fork_support.md for details
|
|
| 101 |
+ process = multiprocessing.Process(target=_test_pull,
|
|
| 102 |
+ args=(user_config_file, project_dir, artifact_dir,
|
|
| 103 |
+ 'target.bst', element_key, queue))
|
|
| 104 |
+ |
|
| 105 |
+ try:
|
|
| 106 |
+ # Keep SIGINT blocked in the child process
|
|
| 107 |
+ with _signals.blocked([signal.SIGINT], ignore=False):
|
|
| 108 |
+ process.start()
|
|
| 109 |
+ |
|
| 110 |
+ error = queue.get()
|
|
| 111 |
+ process.join()
|
|
| 112 |
+ except KeyboardInterrupt:
|
|
| 113 |
+ utils._kill_process_tree(process.pid)
|
|
| 114 |
+ raise
|
|
| 115 |
+ |
|
| 116 |
+ assert not error
|
|
| 117 |
+ assert cas.contains(element, element_key)
|
|
| 118 |
+ |
|
| 119 |
+ |
|
| 120 |
+def _test_pull(user_config_file, project_dir, artifact_dir,
|
|
| 121 |
+ element_name, element_key, queue):
|
|
| 122 |
+ # Fake minimal context
|
|
| 123 |
+ context = Context()
|
|
| 124 |
+ context.load(config=user_config_file)
|
|
| 125 |
+ context.artifactdir = artifact_dir
|
|
| 126 |
+ context.set_message_handler(message_handler)
|
|
| 127 |
+ |
|
| 128 |
+ # Load the project manually
|
|
| 129 |
+ project = Project(project_dir, context)
|
|
| 130 |
+ project.ensure_fully_loaded()
|
|
| 131 |
+ |
|
| 132 |
+ # Create a local CAS cache handle
|
|
| 133 |
+ cas = CASCache(context)
|
|
| 134 |
+ |
|
| 135 |
+ # Load the target element
|
|
| 136 |
+ element = project.load_elements([element_name], cas)[0]
|
|
| 137 |
+ |
|
| 138 |
+ # Manually setup the CAS remote
|
|
| 139 |
+ cas.setup_remotes(use_config=True)
|
|
| 140 |
+ cas.initialize_remotes()
|
|
| 141 |
+ |
|
| 142 |
+ if cas.has_push_remotes(element=element):
|
|
| 143 |
+ # Push the element's artifact
|
|
| 144 |
+ if not cas.pull(element, element_key):
|
|
| 145 |
+ queue.put("Pull operation failed")
|
|
| 146 |
+ else:
|
|
| 147 |
+ queue.put(None)
|
|
| 148 |
+ else:
|
|
| 149 |
+ queue.put("No remote configured for element {}".format(element_name))
|
|
| 150 |
+ |
|
| 151 |
+ |
|
| 152 |
+@pytest.mark.datafiles(DATA_DIR)
|
|
| 153 |
+def test_pull_tree(cli, tmpdir, datafiles):
|
|
| 154 |
+ project_dir = str(datafiles)
|
|
| 155 |
+ |
|
| 156 |
+ # Set up an artifact cache.
|
|
| 157 |
+ with create_artifact_share(os.path.join(str(tmpdir), 'artifactshare')) as share:
|
|
| 158 |
+ # Configure artifact share
|
|
| 159 |
+ artifact_dir = os.path.join(str(tmpdir), 'cache', 'artifacts')
|
|
| 160 |
+ user_config_file = str(tmpdir.join('buildstream.conf'))
|
|
| 161 |
+ user_config = {
|
|
| 162 |
+ 'scheduler': {
|
|
| 163 |
+ 'pushers': 1
|
|
| 164 |
+ },
|
|
| 165 |
+ 'artifacts': {
|
|
| 166 |
+ 'url': share.repo,
|
|
| 167 |
+ 'push': True,
|
|
| 168 |
+ }
|
|
| 169 |
+ }
|
|
| 170 |
+ |
|
| 171 |
+ # Write down the user configuration file
|
|
| 172 |
+ _yaml.dump(_yaml.node_sanitize(user_config), filename=user_config_file)
|
|
| 173 |
+ # Ensure CLI calls will use it
|
|
| 174 |
+ cli.configure(user_config)
|
|
| 175 |
+ |
|
| 176 |
+ # First build the project with the artifact cache configured
|
|
| 177 |
+ result = cli.run(project=project_dir, args=['build', 'target.bst'])
|
|
| 178 |
+ result.assert_success()
|
|
| 179 |
+ |
|
| 180 |
+ # Assert that we are now cached locally
|
|
| 181 |
+ assert cli.get_element_state(project_dir, 'target.bst') == 'cached'
|
|
| 182 |
+ # Assert that we shared/pushed the cached artifact
|
|
| 183 |
+ element_key = cli.get_element_key(project_dir, 'target.bst')
|
|
| 184 |
+ assert share.has_artifact('test', 'target.bst', element_key)
|
|
| 185 |
+ |
|
| 186 |
+ # Fake minimal context
|
|
| 187 |
+ context = Context()
|
|
| 188 |
+ context.load(config=user_config_file)
|
|
| 189 |
+ context.artifactdir = os.path.join(str(tmpdir), 'cache', 'artifacts')
|
|
| 190 |
+ context.set_message_handler(message_handler)
|
|
| 191 |
+ |
|
| 192 |
+ # Load the project and CAS cache
|
|
| 193 |
+ project = Project(project_dir, context)
|
|
| 194 |
+ project.ensure_fully_loaded()
|
|
| 195 |
+ cas = CASCache(context)
|
|
| 196 |
+ |
|
| 197 |
+ # Assert that the element's artifact is cached
|
|
| 198 |
+ element = project.load_elements(['target.bst'], cas)[0]
|
|
| 199 |
+ element_key = cli.get_element_key(project_dir, 'target.bst')
|
|
| 200 |
+ assert cas.contains(element, element_key)
|
|
| 201 |
+ |
|
| 202 |
+ # Retrieve the Directory object from the cached artifact
|
|
| 203 |
+ artifact_ref = cas.get_artifact_fullname(element, element_key)
|
|
| 204 |
+ artifact_digest = cas.resolve_ref(artifact_ref)
|
|
| 205 |
+ |
|
| 206 |
+ queue = multiprocessing.Queue()
|
|
| 207 |
+ # Use subprocess to avoid creation of gRPC threads in main BuildStream process
|
|
| 208 |
+ # See https://github.com/grpc/grpc/blob/master/doc/fork_support.md for details
|
|
| 209 |
+ process = multiprocessing.Process(target=_test_push_tree,
|
|
| 210 |
+ args=(user_config_file, project_dir, artifact_dir,
|
|
| 211 |
+ artifact_digest, queue))
|
|
| 212 |
+ |
|
| 213 |
+ try:
|
|
| 214 |
+ # Keep SIGINT blocked in the child process
|
|
| 215 |
+ with _signals.blocked([signal.SIGINT], ignore=False):
|
|
| 216 |
+ process.start()
|
|
| 217 |
+ |
|
| 218 |
+ tree_hash, tree_size = queue.get()
|
|
| 219 |
+ process.join()
|
|
| 220 |
+ except KeyboardInterrupt:
|
|
| 221 |
+ utils._kill_process_tree(process.pid)
|
|
| 222 |
+ raise
|
|
| 223 |
+ |
|
| 224 |
+ assert tree_hash and tree_size
|
|
| 225 |
+ |
|
| 226 |
+ # Now delete the artifact locally
|
|
| 227 |
+ cli.remove_artifact_from_cache(project_dir, 'target.bst')
|
|
| 228 |
+ |
|
| 229 |
+ # Assert that we are not cached locally anymore
|
|
| 230 |
+ assert cli.get_element_state(project_dir, 'target.bst') != 'cached'
|
|
| 231 |
+ |
|
| 232 |
+ tree_digest = remote_execution_pb2.Digest(hash=tree_hash,
|
|
| 233 |
+ size_bytes=tree_size)
|
|
| 234 |
+ |
|
| 235 |
+ queue = multiprocessing.Queue()
|
|
| 236 |
+ # Use subprocess to avoid creation of gRPC threads in main BuildStream process
|
|
| 237 |
+ process = multiprocessing.Process(target=_test_pull_tree,
|
|
| 238 |
+ args=(user_config_file, project_dir, artifact_dir,
|
|
| 239 |
+ tree_digest, queue))
|
|
| 240 |
+ |
|
| 241 |
+ try:
|
|
| 242 |
+ # Keep SIGINT blocked in the child process
|
|
| 243 |
+ with _signals.blocked([signal.SIGINT], ignore=False):
|
|
| 244 |
+ process.start()
|
|
| 245 |
+ |
|
| 246 |
+ directory_hash, directory_size = queue.get()
|
|
| 247 |
+ process.join()
|
|
| 248 |
+ except KeyboardInterrupt:
|
|
| 249 |
+ utils._kill_process_tree(process.pid)
|
|
| 250 |
+ raise
|
|
| 251 |
+ |
|
| 252 |
+ assert directory_hash and directory_size
|
|
| 253 |
+ |
|
| 254 |
+ directory_digest = remote_execution_pb2.Digest(hash=directory_hash,
|
|
| 255 |
+ size_bytes=directory_size)
|
|
| 256 |
+ |
|
| 257 |
+ # Ensure the entire Tree stucture has been pulled
|
|
| 258 |
+ assert os.path.exists(cas.objpath(directory_digest))
|
|
| 259 |
+ |
|
| 260 |
+ |
|
| 261 |
+def _test_push_tree(user_config_file, project_dir, artifact_dir, artifact_digest, queue):
|
|
| 262 |
+ # Fake minimal context
|
|
| 263 |
+ context = Context()
|
|
| 264 |
+ context.load(config=user_config_file)
|
|
| 265 |
+ context.artifactdir = artifact_dir
|
|
| 266 |
+ context.set_message_handler(message_handler)
|
|
| 267 |
+ |
|
| 268 |
+ # Load the project manually
|
|
| 269 |
+ project = Project(project_dir, context)
|
|
| 270 |
+ project.ensure_fully_loaded()
|
|
| 271 |
+ |
|
| 272 |
+ # Create a local CAS cache handle
|
|
| 273 |
+ cas = CASCache(context)
|
|
| 274 |
+ |
|
| 275 |
+ # Manually setup the CAS remote
|
|
| 276 |
+ cas.setup_remotes(use_config=True)
|
|
| 277 |
+ cas.initialize_remotes()
|
|
| 278 |
+ |
|
| 279 |
+ if cas.has_push_remotes():
|
|
| 280 |
+ directory = remote_execution_pb2.Directory()
|
|
| 281 |
+ |
|
| 282 |
+ with open(cas.objpath(artifact_digest), 'rb') as f:
|
|
| 283 |
+ directory.ParseFromString(f.read())
|
|
| 284 |
+ |
|
| 285 |
+ # Build the Tree object while we are still cached
|
|
| 286 |
+ tree = remote_execution_pb2.Tree()
|
|
| 287 |
+ tree_maker(cas, tree, directory)
|
|
| 288 |
+ |
|
| 289 |
+ # Push the Tree as a regular message
|
|
| 290 |
+ tree_digest = cas.push_message(project, tree)
|
|
| 291 |
+ |
|
| 292 |
+ queue.put((tree_digest.hash, tree_digest.size_bytes))
|
|
| 293 |
+ else:
|
|
| 294 |
+ queue.put("No remote configured")
|
|
| 295 |
+ |
|
| 296 |
+ |
|
| 297 |
+def _test_pull_tree(user_config_file, project_dir, artifact_dir, artifact_digest, queue):
|
|
| 298 |
+ # Fake minimal context
|
|
| 299 |
+ context = Context()
|
|
| 300 |
+ context.load(config=user_config_file)
|
|
| 301 |
+ context.artifactdir = artifact_dir
|
|
| 302 |
+ context.set_message_handler(message_handler)
|
|
| 303 |
+ |
|
| 304 |
+ # Load the project manually
|
|
| 305 |
+ project = Project(project_dir, context)
|
|
| 306 |
+ project.ensure_fully_loaded()
|
|
| 307 |
+ |
|
| 308 |
+ # Create a local CAS cache handle
|
|
| 309 |
+ cas = CASCache(context)
|
|
| 310 |
+ |
|
| 311 |
+ # Manually setup the CAS remote
|
|
| 312 |
+ cas.setup_remotes(use_config=True)
|
|
| 313 |
+ cas.initialize_remotes()
|
|
| 314 |
+ |
|
| 315 |
+ if cas.has_push_remotes():
|
|
| 316 |
+ # Pull the artifact using the Tree object
|
|
| 317 |
+ directory_digest = cas.pull_tree(project, artifact_digest)
|
|
| 318 |
+ queue.put((directory_digest.hash, directory_digest.size_bytes))
|
|
| 319 |
+ else:
|
|
| 320 |
+ queue.put("No remote configured")
|
| 1 |
+import multiprocessing
|
|
| 2 |
+import os
|
|
| 3 |
+import signal
|
|
| 4 |
+ |
|
| 5 |
+import pytest
|
|
| 6 |
+ |
|
| 7 |
+from pluginbase import PluginBase
|
|
| 8 |
+from buildstream import _yaml, _signals, utils
|
|
| 9 |
+from buildstream._artifactcache.cascache import CASCache
|
|
| 10 |
+from buildstream._context import Context
|
|
| 11 |
+from buildstream._project import Project
|
|
| 12 |
+from buildstream._protos.build.bazel.remote.execution.v2 import remote_execution_pb2
|
|
| 13 |
+from buildstream.storage._casbaseddirectory import CasBasedDirectory
|
|
| 14 |
+ |
|
| 15 |
+from tests.testutils import cli, create_artifact_share
|
|
| 16 |
+ |
|
| 17 |
+ |
|
| 18 |
+# Project directory
|
|
| 19 |
+DATA_DIR = os.path.join(
|
|
| 20 |
+ os.path.dirname(os.path.realpath(__file__)),
|
|
| 21 |
+ "project",
|
|
| 22 |
+)
|
|
| 23 |
+ |
|
| 24 |
+ |
|
| 25 |
+# Handle messages from the pipeline
|
|
| 26 |
+def message_handler(message, context):
|
|
| 27 |
+ pass
|
|
| 28 |
+ |
|
| 29 |
+ |
|
| 30 |
+@pytest.mark.datafiles(DATA_DIR)
|
|
| 31 |
+def test_push(cli, tmpdir, datafiles):
|
|
| 32 |
+ project_dir = str(datafiles)
|
|
| 33 |
+ |
|
| 34 |
+ # First build the project without the artifact cache configured
|
|
| 35 |
+ result = cli.run(project=project_dir, args=['build', 'target.bst'])
|
|
| 36 |
+ result.assert_success()
|
|
| 37 |
+ |
|
| 38 |
+ # Assert that we are now cached locally
|
|
| 39 |
+ assert cli.get_element_state(project_dir, 'target.bst') == 'cached'
|
|
| 40 |
+ |
|
| 41 |
+ # Set up an artifact cache.
|
|
| 42 |
+ with create_artifact_share(os.path.join(str(tmpdir), 'artifactshare')) as share:
|
|
| 43 |
+ # Configure artifact share
|
|
| 44 |
+ artifact_dir = os.path.join(str(tmpdir), 'cache', 'artifacts')
|
|
| 45 |
+ user_config_file = str(tmpdir.join('buildstream.conf'))
|
|
| 46 |
+ user_config = {
|
|
| 47 |
+ 'scheduler': {
|
|
| 48 |
+ 'pushers': 1
|
|
| 49 |
+ },
|
|
| 50 |
+ 'artifacts': {
|
|
| 51 |
+ 'url': share.repo,
|
|
| 52 |
+ 'push': True,
|
|
| 53 |
+ }
|
|
| 54 |
+ }
|
|
| 55 |
+ |
|
| 56 |
+ # Write down the user configuration file
|
|
| 57 |
+ _yaml.dump(_yaml.node_sanitize(user_config), filename=user_config_file)
|
|
| 58 |
+ |
|
| 59 |
+ # Fake minimal context
|
|
| 60 |
+ context = Context()
|
|
| 61 |
+ context.load(config=user_config_file)
|
|
| 62 |
+ context.artifactdir = artifact_dir
|
|
| 63 |
+ context.set_message_handler(message_handler)
|
|
| 64 |
+ |
|
| 65 |
+ # Load the project manually
|
|
| 66 |
+ project = Project(project_dir, context)
|
|
| 67 |
+ project.ensure_fully_loaded()
|
|
| 68 |
+ |
|
| 69 |
+ # Create a local CAS cache handle
|
|
| 70 |
+ cas = CASCache(context)
|
|
| 71 |
+ |
|
| 72 |
+ # Assert that the element's artifact is cached
|
|
| 73 |
+ element = project.load_elements(['target.bst'], cas)[0]
|
|
| 74 |
+ element_key = cli.get_element_key(project_dir, 'target.bst')
|
|
| 75 |
+ assert cas.contains(element, element_key)
|
|
| 76 |
+ |
|
| 77 |
+ queue = multiprocessing.Queue()
|
|
| 78 |
+ # Use subprocess to avoid creation of gRPC threads in main BuildStream process
|
|
| 79 |
+ # See https://github.com/grpc/grpc/blob/master/doc/fork_support.md for details
|
|
| 80 |
+ process = multiprocessing.Process(target=_test_push,
|
|
| 81 |
+ args=(user_config_file, project_dir, artifact_dir,
|
|
| 82 |
+ 'target.bst', element_key, queue))
|
|
| 83 |
+ |
|
| 84 |
+ try:
|
|
| 85 |
+ # Keep SIGINT blocked in the child process
|
|
| 86 |
+ with _signals.blocked([signal.SIGINT], ignore=False):
|
|
| 87 |
+ process.start()
|
|
| 88 |
+ |
|
| 89 |
+ error = queue.get()
|
|
| 90 |
+ process.join()
|
|
| 91 |
+ except KeyboardInterrupt:
|
|
| 92 |
+ utils._kill_process_tree(process.pid)
|
|
| 93 |
+ raise
|
|
| 94 |
+ |
|
| 95 |
+ assert not error
|
|
| 96 |
+ assert share.has_artifact('test', 'target.bst', element_key)
|
|
| 97 |
+ |
|
| 98 |
+ |
|
| 99 |
+def _test_push(user_config_file, project_dir, artifact_dir,
|
|
| 100 |
+ element_name, element_key, queue):
|
|
| 101 |
+ # Fake minimal context
|
|
| 102 |
+ context = Context()
|
|
| 103 |
+ context.load(config=user_config_file)
|
|
| 104 |
+ context.artifactdir = artifact_dir
|
|
| 105 |
+ context.set_message_handler(message_handler)
|
|
| 106 |
+ |
|
| 107 |
+ # Load the project manually
|
|
| 108 |
+ project = Project(project_dir, context)
|
|
| 109 |
+ project.ensure_fully_loaded()
|
|
| 110 |
+ |
|
| 111 |
+ # Create a local CAS cache handle
|
|
| 112 |
+ cas = CASCache(context)
|
|
| 113 |
+ |
|
| 114 |
+ # Load the target element
|
|
| 115 |
+ element = project.load_elements([element_name], cas)[0]
|
|
| 116 |
+ |
|
| 117 |
+ # Manually setup the CAS remote
|
|
| 118 |
+ cas.setup_remotes(use_config=True)
|
|
| 119 |
+ cas.initialize_remotes()
|
|
| 120 |
+ |
|
| 121 |
+ if cas.has_push_remotes(element=element):
|
|
| 122 |
+ # Push the element's artifact
|
|
| 123 |
+ if not cas.push(element, [element_key]):
|
|
| 124 |
+ queue.put("Push operation failed")
|
|
| 125 |
+ else:
|
|
| 126 |
+ queue.put(None)
|
|
| 127 |
+ else:
|
|
| 128 |
+ queue.put("No remote configured for element {}".format(element_name))
|
|
| 129 |
+ |
|
| 130 |
+ |
|
| 131 |
+@pytest.mark.datafiles(DATA_DIR)
|
|
| 132 |
+def test_push_directory(cli, tmpdir, datafiles):
|
|
| 133 |
+ project_dir = str(datafiles)
|
|
| 134 |
+ |
|
| 135 |
+ # First build the project without the artifact cache configured
|
|
| 136 |
+ result = cli.run(project=project_dir, args=['build', 'target.bst'])
|
|
| 137 |
+ result.assert_success()
|
|
| 138 |
+ |
|
| 139 |
+ # Assert that we are now cached locally
|
|
| 140 |
+ assert cli.get_element_state(project_dir, 'target.bst') == 'cached'
|
|
| 141 |
+ |
|
| 142 |
+ # Set up an artifact cache.
|
|
| 143 |
+ with create_artifact_share(os.path.join(str(tmpdir), 'artifactshare')) as share:
|
|
| 144 |
+ # Configure artifact share
|
|
| 145 |
+ artifact_dir = os.path.join(str(tmpdir), 'cache', 'artifacts')
|
|
| 146 |
+ user_config_file = str(tmpdir.join('buildstream.conf'))
|
|
| 147 |
+ user_config = {
|
|
| 148 |
+ 'scheduler': {
|
|
| 149 |
+ 'pushers': 1
|
|
| 150 |
+ },
|
|
| 151 |
+ 'artifacts': {
|
|
| 152 |
+ 'url': share.repo,
|
|
| 153 |
+ 'push': True,
|
|
| 154 |
+ }
|
|
| 155 |
+ }
|
|
| 156 |
+ |
|
| 157 |
+ # Write down the user configuration file
|
|
| 158 |
+ _yaml.dump(_yaml.node_sanitize(user_config), filename=user_config_file)
|
|
| 159 |
+ |
|
| 160 |
+ # Fake minimal context
|
|
| 161 |
+ context = Context()
|
|
| 162 |
+ context.load(config=user_config_file)
|
|
| 163 |
+ context.artifactdir = os.path.join(str(tmpdir), 'cache', 'artifacts')
|
|
| 164 |
+ context.set_message_handler(message_handler)
|
|
| 165 |
+ |
|
| 166 |
+ # Load the project and CAS cache
|
|
| 167 |
+ project = Project(project_dir, context)
|
|
| 168 |
+ project.ensure_fully_loaded()
|
|
| 169 |
+ cas = CASCache(context)
|
|
| 170 |
+ |
|
| 171 |
+ # Assert that the element's artifact is cached
|
|
| 172 |
+ element = project.load_elements(['target.bst'], cas)[0]
|
|
| 173 |
+ element_key = cli.get_element_key(project_dir, 'target.bst')
|
|
| 174 |
+ assert cas.contains(element, element_key)
|
|
| 175 |
+ |
|
| 176 |
+ # Manually setup the CAS remote
|
|
| 177 |
+ cas.setup_remotes(use_config=True)
|
|
| 178 |
+ cas.initialize_remotes()
|
|
| 179 |
+ assert cas.has_push_remotes(element=element)
|
|
| 180 |
+ |
|
| 181 |
+ # Recreate the CasBasedDirectory object from the cached artifact
|
|
| 182 |
+ artifact_ref = cas.get_artifact_fullname(element, element_key)
|
|
| 183 |
+ artifact_digest = cas.resolve_ref(artifact_ref)
|
|
| 184 |
+ |
|
| 185 |
+ queue = multiprocessing.Queue()
|
|
| 186 |
+ # Use subprocess to avoid creation of gRPC threads in main BuildStream process
|
|
| 187 |
+ # See https://github.com/grpc/grpc/blob/master/doc/fork_support.md for details
|
|
| 188 |
+ process = multiprocessing.Process(target=_test_push_directory,
|
|
| 189 |
+ args=(user_config_file, project_dir, artifact_dir,
|
|
| 190 |
+ artifact_digest, queue))
|
|
| 191 |
+ |
|
| 192 |
+ try:
|
|
| 193 |
+ # Keep SIGINT blocked in the child process
|
|
| 194 |
+ with _signals.blocked([signal.SIGINT], ignore=False):
|
|
| 195 |
+ process.start()
|
|
| 196 |
+ |
|
| 197 |
+ directory_hash = queue.get()
|
|
| 198 |
+ process.join()
|
|
| 199 |
+ except KeyboardInterrupt:
|
|
| 200 |
+ utils._kill_process_tree(process.pid)
|
|
| 201 |
+ raise
|
|
| 202 |
+ |
|
| 203 |
+ assert directory_hash
|
|
| 204 |
+ assert artifact_digest.hash == directory_hash
|
|
| 205 |
+ assert share.has_object(artifact_digest)
|
|
| 206 |
+ |
|
| 207 |
+ |
|
| 208 |
+def _test_push_directory(user_config_file, project_dir, artifact_dir, artifact_digest, queue):
|
|
| 209 |
+ # Fake minimal context
|
|
| 210 |
+ context = Context()
|
|
| 211 |
+ context.load(config=user_config_file)
|
|
| 212 |
+ context.artifactdir = artifact_dir
|
|
| 213 |
+ context.set_message_handler(message_handler)
|
|
| 214 |
+ |
|
| 215 |
+ # Load the project manually
|
|
| 216 |
+ project = Project(project_dir, context)
|
|
| 217 |
+ project.ensure_fully_loaded()
|
|
| 218 |
+ |
|
| 219 |
+ # Create a local CAS cache handle
|
|
| 220 |
+ cas = CASCache(context)
|
|
| 221 |
+ |
|
| 222 |
+ # Manually setup the CAS remote
|
|
| 223 |
+ cas.setup_remotes(use_config=True)
|
|
| 224 |
+ cas.initialize_remotes()
|
|
| 225 |
+ |
|
| 226 |
+ if cas.has_push_remotes():
|
|
| 227 |
+ # Create a CasBasedDirectory from local CAS cache content
|
|
| 228 |
+ directory = CasBasedDirectory(context, ref=artifact_digest)
|
|
| 229 |
+ |
|
| 230 |
+ # Push the CasBasedDirectory object
|
|
| 231 |
+ directory_digest = cas.push_directory(project, directory)
|
|
| 232 |
+ |
|
| 233 |
+ queue.put(directory_digest.hash)
|
|
| 234 |
+ else:
|
|
| 235 |
+ queue.put("No remote configured")
|
|
| 236 |
+ |
|
| 237 |
+ |
|
| 238 |
+@pytest.mark.datafiles(DATA_DIR)
|
|
| 239 |
+def test_push_message(cli, tmpdir, datafiles):
|
|
| 240 |
+ project_dir = str(datafiles)
|
|
| 241 |
+ |
|
| 242 |
+ # Set up an artifact cache.
|
|
| 243 |
+ with create_artifact_share(os.path.join(str(tmpdir), 'artifactshare')) as share:
|
|
| 244 |
+ # Configure artifact share
|
|
| 245 |
+ artifact_dir = os.path.join(str(tmpdir), 'cache', 'artifacts')
|
|
| 246 |
+ user_config_file = str(tmpdir.join('buildstream.conf'))
|
|
| 247 |
+ user_config = {
|
|
| 248 |
+ 'scheduler': {
|
|
| 249 |
+ 'pushers': 1
|
|
| 250 |
+ },
|
|
| 251 |
+ 'artifacts': {
|
|
| 252 |
+ 'url': share.repo,
|
|
| 253 |
+ 'push': True,
|
|
| 254 |
+ }
|
|
| 255 |
+ }
|
|
| 256 |
+ |
|
| 257 |
+ # Write down the user configuration file
|
|
| 258 |
+ _yaml.dump(_yaml.node_sanitize(user_config), filename=user_config_file)
|
|
| 259 |
+ |
|
| 260 |
+ queue = multiprocessing.Queue()
|
|
| 261 |
+ # Use subprocess to avoid creation of gRPC threads in main BuildStream process
|
|
| 262 |
+ # See https://github.com/grpc/grpc/blob/master/doc/fork_support.md for details
|
|
| 263 |
+ process = multiprocessing.Process(target=_test_push_message,
|
|
| 264 |
+ args=(user_config_file, project_dir, artifact_dir, queue))
|
|
| 265 |
+ |
|
| 266 |
+ try:
|
|
| 267 |
+ # Keep SIGINT blocked in the child process
|
|
| 268 |
+ with _signals.blocked([signal.SIGINT], ignore=False):
|
|
| 269 |
+ process.start()
|
|
| 270 |
+ |
|
| 271 |
+ message_hash, message_size = queue.get()
|
|
| 272 |
+ process.join()
|
|
| 273 |
+ except KeyboardInterrupt:
|
|
| 274 |
+ utils._kill_process_tree(process.pid)
|
|
| 275 |
+ raise
|
|
| 276 |
+ |
|
| 277 |
+ assert message_hash and message_size
|
|
| 278 |
+ message_digest = remote_execution_pb2.Digest(hash=message_hash,
|
|
| 279 |
+ size_bytes=message_size)
|
|
| 280 |
+ assert share.has_object(message_digest)
|
|
| 281 |
+ |
|
| 282 |
+ |
|
| 283 |
+def _test_push_message(user_config_file, project_dir, artifact_dir, queue):
|
|
| 284 |
+ # Fake minimal context
|
|
| 285 |
+ context = Context()
|
|
| 286 |
+ context.load(config=user_config_file)
|
|
| 287 |
+ context.artifactdir = artifact_dir
|
|
| 288 |
+ context.set_message_handler(message_handler)
|
|
| 289 |
+ |
|
| 290 |
+ # Load the project manually
|
|
| 291 |
+ project = Project(project_dir, context)
|
|
| 292 |
+ project.ensure_fully_loaded()
|
|
| 293 |
+ |
|
| 294 |
+ # Create a local CAS cache handle
|
|
| 295 |
+ cas = CASCache(context)
|
|
| 296 |
+ |
|
| 297 |
+ # Manually setup the CAS remote
|
|
| 298 |
+ cas.setup_remotes(use_config=True)
|
|
| 299 |
+ cas.initialize_remotes()
|
|
| 300 |
+ |
|
| 301 |
+ if cas.has_push_remotes():
|
|
| 302 |
+ # Create an example message object
|
|
| 303 |
+ command = remote_execution_pb2.Command(arguments=['/usr/bin/gcc', '--help'],
|
|
| 304 |
+ working_directory='/buildstream-build',
|
|
| 305 |
+ output_directories=['/buildstream-install'])
|
|
| 306 |
+ |
|
| 307 |
+ # Push the message object
|
|
| 308 |
+ command_digest = cas.push_message(project, command)
|
|
| 309 |
+ |
|
| 310 |
+ queue.put((command_digest.hash, command_digest.size_bytes))
|
|
| 311 |
+ else:
|
|
| 312 |
+ queue.put("No remote configured")
|
| ... | ... | @@ -15,6 +15,7 @@ from buildstream._artifactcache.cascache import CASCache |
| 15 | 15 |
from buildstream._artifactcache.casserver import create_server
|
| 16 | 16 |
from buildstream._context import Context
|
| 17 | 17 |
from buildstream._exceptions import ArtifactError
|
| 18 |
+from buildstream._protos.build.bazel.remote.execution.v2 import remote_execution_pb2
|
|
| 18 | 19 |
|
| 19 | 20 |
|
| 20 | 21 |
# ArtifactShare()
|
| ... | ... | @@ -87,6 +88,23 @@ class ArtifactShare(): |
| 87 | 88 |
# Sleep until termination by signal
|
| 88 | 89 |
signal.pause()
|
| 89 | 90 |
|
| 91 |
+ # has_object():
|
|
| 92 |
+ #
|
|
| 93 |
+ # Checks whether the object is present in the share
|
|
| 94 |
+ #
|
|
| 95 |
+ # Args:
|
|
| 96 |
+ # digest (str): The object's digest
|
|
| 97 |
+ #
|
|
| 98 |
+ # Returns:
|
|
| 99 |
+ # (bool): True if the object exists in the share, otherwise false.
|
|
| 100 |
+ def has_object(self, digest):
|
|
| 101 |
+ |
|
| 102 |
+ assert isinstance(digest, remote_execution_pb2.Digest)
|
|
| 103 |
+ |
|
| 104 |
+ object_path = self.cas.objpath(digest)
|
|
| 105 |
+ |
|
| 106 |
+ return os.path.exists(object_path)
|
|
| 107 |
+ |
|
| 90 | 108 |
# has_artifact():
|
| 91 | 109 |
#
|
| 92 | 110 |
# Checks whether the artifact is present in the share
|
