| 1 |
# Copyright 2015 Google Inc. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import hashlib |
| 16 |
import subprocess |
| 17 |
|
| 18 |
import build_artefact |
| 19 |
import cmds |
| 20 |
import util |
| 21 |
|
| 22 |
_NAME_CHARS = ([chr(i) for i in range(ord('0'), ord('9'))] |
| 23 |
+ [chr(i) for i in range(ord('a'), ord('z'))]) |
| 24 |
|
| 25 |
|
| 26 |
def hash_code_as_domain_name(n): |
| 27 |
n = n % (2**63) |
| 28 |
if n < 0: |
| 29 |
n += 2**63 |
| 30 |
radix = len(_NAME_CHARS) |
| 31 |
s = "" |
| 32 |
for i in range(12): |
| 33 |
d = n % radix |
| 34 |
s = _NAME_CHARS[d] + s |
| 35 |
n /= radix |
| 36 |
return s |
| 37 |
|
| 38 |
|
| 39 |
def hash_string(s): |
| 40 |
r = long(hashlib.sha1(s).hexdigest(), 16) |
| 41 |
if r < 0: |
| 42 |
r += 2**63 |
| 43 |
return r |
| 44 |
|
| 45 |
|
| 46 |
def hash_cmds(cmds): |
| 47 |
hash_code = 0l |
| 48 |
for cmd in cmds: |
| 49 |
if isinstance(cmd, basestring): |
| 50 |
hash_code ^= hash_string(cmd) |
| 51 |
elif cmd['kind'] == 'LiteralFile': |
| 52 |
hash_code ^= hash_string(cmd['content']) |
| 53 |
hash_code ^= hash_string(cmd['to']) |
| 54 |
hash_code ^= hash_string(cmd['filePermissions']) |
| 55 |
hash_code ^= hash_string(cmd['owner']) |
| 56 |
hash_code ^= hash_string(cmd['group']) |
| 57 |
elif cmd['kind'] == 'CopyFile': |
| 58 |
# TODO(dcunnin): Scan these files, compute hash |
| 59 |
hash_code ^= hash_string(cmd['from']) |
| 60 |
hash_code ^= hash_string(cmd['to']) |
| 61 |
hash_code ^= hash_string(cmd['dirPermissions']) |
| 62 |
hash_code ^= hash_string(cmd['filePermissions']) |
| 63 |
hash_code ^= hash_string(cmd['owner']) |
| 64 |
hash_code ^= hash_string(cmd['group']) |
| 65 |
raise RuntimeError('CopyFile not supported in image') |
| 66 |
elif cmd['kind'] == 'EnsureDir': |
| 67 |
hash_code ^= hash_string(cmd['dir']) |
| 68 |
hash_code ^= hash_string(cmd['dirPermissions']) |
| 69 |
hash_code ^= hash_string(cmd['owner']) |
| 70 |
hash_code ^= hash_string(cmd['group']) |
| 71 |
else: |
| 72 |
raise RuntimeError('Did not recognize image command kind: ' + cmd['kind']) |
| 73 |
return hash_code |
| 74 |
|
| 75 |
|
| 76 |
class PackerBuildArtefact(build_artefact.BuildArtefact): |
| 77 |
def __init__(self, cmds): |
| 78 |
self.cmds = cmds |
| 79 |
self.cachedHashCode = None |
| 80 |
self.packerProcess = None |
| 81 |
|
| 82 |
def builderHashCode(self): |
| 83 |
raise NotImplementedError("%s has no override" % self.__class__.__name__) |
| 84 |
|
| 85 |
def hashCode(self): |
| 86 |
if self.cachedHashCode is None: |
| 87 |
self.cachedHashCode = self.builderHashCode() ^ hash_cmds(self.cmds) |
| 88 |
return self.cachedHashCode |
| 89 |
|
| 90 |
def _configFileName(self): |
| 91 |
return self.name() + '.packer.json' |
| 92 |
|
| 93 |
def _config(self): |
| 94 |
provs = [ |
| 95 |
{ |
| 96 |
'type': 'shell', |
| 97 |
'execute_command': "{{ .Vars }} sudo -HE /bin/bash '{{ .Path }}'", |
| 98 |
'inline': cmds.compile_command_to_bash(cmd), |
| 99 |
} |
| 100 |
for cmd in self.cmds |
| 101 |
] |
| 102 |
return util.jsonstr({'builders': [self.builder()], 'provisioners': provs}) |
| 103 |
|
| 104 |
def name(self): |
| 105 |
return 'micromanage-%s' % hash_code_as_domain_name(self.hashCode()) |
| 106 |
|
| 107 |
def getOutputFiles(self, dirpath): |
| 108 |
return ['%s/%s' % (dirpath, self._configFileName())] |
| 109 |
|
| 110 |
def outputFiles(self, dirpath): |
| 111 |
config = self._config() |
| 112 |
dirfilename = '%s/%s' % (dirpath, self._configFileName()) |
| 113 |
with open(dirfilename, 'w') as f: |
| 114 |
f.write(config) |
| 115 |
|
| 116 |
def doBuild(self, dirpath): |
| 117 |
filename = self._configFileName() |
| 118 |
if self.packerProcess is not None: |
| 119 |
raise RuntimeError('Packer build already in progress.') |
| 120 |
|
| 121 |
command = ['packer', 'build', filename] |
| 122 |
logfilename = '%s/%s.log' % (dirpath, filename) |
| 123 |
print '%s > %s' % (' '.join(command), logfilename) |
| 124 |
with open(logfilename, 'w') as logfile: |
| 125 |
self.packerProcess = subprocess.Popen(command, stdout=logfile, cwd=dirpath) |
| 126 |
|
| 127 |
def wait(self, dirpath): |
| 128 |
if self.packerProcess is None: |
| 129 |
raise RuntimeError('No Packer build in progress.') |
| 130 |
exitcode = self.packerProcess.wait() |
| 131 |
if exitcode != 0: |
| 132 |
print 'Packer error: Exit code was %d' % exitcode |
| 133 |
filename = self._configFileName() |
| 134 |
logfilename = '%s/%s.log' % (dirpath, filename) |
| 135 |
with open(logfilename, 'r') as logfile: |
| 136 |
print logfile.read() |
| 137 |
raise RuntimeError('Error from packer.') |
| 138 |
|
| 139 |
|