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 | |
75 endpoint.Execute(v8_root, self.ctx, tests, self.request, self.server.daemon) | |
76 | |
77 self._SendResponse() | |
78 | |
79 def _SendResponse(self, error_message=None): | |
80 try: | |
81 if error_message: | |
82 compression.Send([-1, error_message], self.request) | |
83 compression.Send(constants.END_OF_STREAM, self.request) | |
84 return | |
85 except Exception, e: | |
86 pass # Peer is gone. There's nothing we can do. | |
87 # Clean up. | |
88 self._Call("git checkout -f") | |
89 self._Call("git clean -f -d") | |
90 self._Call("rm -rf %s" % self.ctx.shell_dir) | |
Michael Starzinger
2012/09/21 13:20:40
As discussed offline, we should make sure this doe
Jakob Kummerow
2012/09/21 17:02:24
Done (by means of .gitignore).
| |
91 | |
92 def _UnpackBinary(self, binary, pubkey_fingerprint): | |
93 binary_name = binary["name"] | |
94 if binary_name == "libv8.so": | |
95 libdir = os.path.join(self.ctx.shell_dir, "lib.target") | |
96 if not os.path.exists(libdir): os.makedirs(libdir) | |
97 target = os.path.join(libdir, binary_name) | |
98 else: | |
99 target = os.path.join(self.ctx.shell_dir, binary_name) | |
100 pubkeyfile = "../trusted/%s.pem" % pubkey_fingerprint | |
101 if not signatures.VerifySignature(target, binary["blob"], | |
102 binary["sign"], pubkeyfile): | |
103 self._SendResponse("Signature verification failed") | |
104 return False | |
105 os.chmod(target, stat.S_IRWXU) | |
106 return True | |
107 | |
108 def _CheckoutRevision(self, base_revision): | |
109 code = self._Call("git checkout -f %s" % base_revision) | |
110 if code != 0: | |
111 self._Call("git fetch") | |
112 code = self._Call("git checkout -f %s" % base_revision) | |
113 if code != 0: | |
114 self._SendResponse("Error trying to check out base revision.") | |
115 return False | |
116 code = self._Call("git clean -f -d") | |
117 if code != 0: | |
118 self._SendResponse("Failed to reset checkout") | |
119 return False | |
120 return True | |
121 | |
122 def _ApplyPatch(self, patch): | |
123 patchfilename = "_dtest_incoming_patch.patch" | |
124 with open(patchfilename, "w") as f: | |
125 f.write(patch) | |
126 code = self._Call("patch -p1 < %s" % patchfilename) | |
Michael Starzinger
2012/09/21 13:20:40
As discussed offline, better use "git apply" here.
Jakob Kummerow
2012/09/21 17:02:24
Done.
| |
127 if code != 0: | |
128 self._SendResponse("Error applying patch.") | |
129 return False | |
130 return True | |
131 | |
132 def _Call(self, cmd): | |
133 return subprocess.call(cmd, shell=True) | |
134 | |
135 | |
136 class WorkSocketServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): | |
137 def __init__(self, daemon): | |
138 address = (daemon.ip, constants.PEER_PORT) | |
139 SocketServer.TCPServer.__init__(self, address, WorkHandler) | |
140 self.job_lock = threading.Lock() | |
141 self.daemon = daemon | |
OLD | NEW |