OLD | NEW |
(Empty) | |
| 1 # Copyright 2012 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are |
| 4 # met: |
| 5 # |
| 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided |
| 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. |
| 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 |
| 29 import os |
| 30 import SocketServer |
| 31 import stat |
| 32 import subprocess |
| 33 import threading |
| 34 |
| 35 from . import compression |
| 36 from . import constants |
| 37 from . import discovery |
| 38 from . import signatures |
| 39 from ..network import endpoint |
| 40 from ..objects import workpacket |
| 41 |
| 42 |
| 43 class WorkHandler(SocketServer.BaseRequestHandler): |
| 44 |
| 45 def handle(self): |
| 46 rec = compression.Receiver(self.request) |
| 47 while not rec.IsDone(): |
| 48 data = rec.Current() |
| 49 with self.server.job_lock: |
| 50 self._WorkOnWorkPacket(data) |
| 51 rec.Advance() |
| 52 |
| 53 def _WorkOnWorkPacket(self, data): |
| 54 server_root = self.server.daemon.root |
| 55 v8_root = os.path.join(server_root, "v8") |
| 56 os.chdir(v8_root) |
| 57 packet = workpacket.WorkPacket.Unpack(data) |
| 58 self.ctx = packet.context |
| 59 self.ctx.shell_dir = os.path.join("out", |
| 60 "%s.%s" % (self.ctx.arch, self.ctx.mode)) |
| 61 if not os.path.isdir(self.ctx.shell_dir): |
| 62 os.makedirs(self.ctx.shell_dir) |
| 63 for binary in packet.binaries: |
| 64 if not self._UnpackBinary(binary, packet.pubkey_fingerprint): |
| 65 return |
| 66 |
| 67 if not self._CheckoutRevision(packet.base_revision): |
| 68 return |
| 69 |
| 70 if not self._ApplyPatch(packet.patch): |
| 71 return |
| 72 |
| 73 tests = packet.tests |
| 74 endpoint.Execute(v8_root, self.ctx, tests, self.request, self.server.daemon) |
| 75 self._SendResponse() |
| 76 |
| 77 def _SendResponse(self, error_message=None): |
| 78 try: |
| 79 if error_message: |
| 80 compression.Send([-1, error_message], self.request) |
| 81 compression.Send(constants.END_OF_STREAM, self.request) |
| 82 return |
| 83 except Exception, e: |
| 84 pass # Peer is gone. There's nothing we can do. |
| 85 # Clean up. |
| 86 self._Call("git checkout -f") |
| 87 self._Call("git clean -f -d") |
| 88 self._Call("rm -rf %s" % self.ctx.shell_dir) |
| 89 |
| 90 def _UnpackBinary(self, binary, pubkey_fingerprint): |
| 91 binary_name = binary["name"] |
| 92 if binary_name == "libv8.so": |
| 93 libdir = os.path.join(self.ctx.shell_dir, "lib.target") |
| 94 if not os.path.exists(libdir): os.makedirs(libdir) |
| 95 target = os.path.join(libdir, binary_name) |
| 96 else: |
| 97 target = os.path.join(self.ctx.shell_dir, binary_name) |
| 98 pubkeyfile = "../trusted/%s.pem" % pubkey_fingerprint |
| 99 if not signatures.VerifySignature(target, binary["blob"], |
| 100 binary["sign"], pubkeyfile): |
| 101 self._SendResponse("Signature verification failed") |
| 102 return False |
| 103 os.chmod(target, stat.S_IRWXU) |
| 104 return True |
| 105 |
| 106 def _CheckoutRevision(self, base_revision): |
| 107 code = self._Call("git checkout -f %s" % base_revision) |
| 108 if code != 0: |
| 109 self._Call("git fetch") |
| 110 code = self._Call("git checkout -f %s" % base_revision) |
| 111 if code != 0: |
| 112 self._SendResponse("Error trying to check out base revision.") |
| 113 return False |
| 114 code = self._Call("git clean -f -d") |
| 115 if code != 0: |
| 116 self._SendResponse("Failed to reset checkout") |
| 117 return False |
| 118 return True |
| 119 |
| 120 def _ApplyPatch(self, patch): |
| 121 patchfilename = "_dtest_incoming_patch.patch" |
| 122 with open(patchfilename, "w") as f: |
| 123 f.write(patch) |
| 124 code = self._Call("git apply %s" % patchfilename) |
| 125 if code != 0: |
| 126 self._SendResponse("Error applying patch.") |
| 127 return False |
| 128 return True |
| 129 |
| 130 def _Call(self, cmd): |
| 131 return subprocess.call(cmd, shell=True) |
| 132 |
| 133 |
| 134 class WorkSocketServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): |
| 135 def __init__(self, daemon): |
| 136 address = (daemon.ip, constants.PEER_PORT) |
| 137 SocketServer.TCPServer.__init__(self, address, WorkHandler) |
| 138 self.job_lock = threading.Lock() |
| 139 self.daemon = daemon |
OLD | NEW |