chiark / gitweb /
buildserver: run dir check as very first thing
[fdroidserver.git] / makebuildserver
1 #!/usr/bin/env python3
2
3 import os
4 import pathlib
5 import requests
6 import stat
7 import sys
8 import subprocess
9 import time
10 import hashlib
11 import yaml
12 from clint.textui import progress
13 from optparse import OptionParser
14
15
16 if not os.path.exists('makebuildserver') and not os.path.exists('buildserver'):
17     print('This must be run as ./makebuildserver in fdroidserver.git!')
18     sys.exit(1)
19
20
21 def vagrant(params, cwd=None, printout=False):
22     """Run vagrant.
23
24     :param: list of parameters to pass to vagrant
25     :cwd: directory to run in, or None for current directory
26     :printout: True to print output in realtime, False to just
27                return it
28     :returns: (ret, out) where ret is the return code, and out
29                is the stdout (and stderr) from vagrant
30     """
31     p = subprocess.Popen(['vagrant'] + params, cwd=cwd,
32                          stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
33                          universal_newlines=True)
34     out = ''
35     if printout:
36         while True:
37             line = p.stdout.readline()
38             if len(line) == 0:
39                 break
40             print(line.rstrip())
41             out += line
42         p.wait()
43     else:
44         out = p.communicate()[0]
45     return (p.returncode, out)
46
47 boxfile = 'buildserver.box'
48 serverdir = 'buildserver'
49
50 parser = OptionParser()
51 parser.add_option("-v", "--verbose", action="store_true", default=False,
52                   help="Spew out even more information than normal")
53 parser.add_option("-c", "--clean", action="store_true", default=False,
54                   help="Build from scratch, rather than attempting to update the existing server")
55 options, args = parser.parse_args()
56
57 # set up default config
58 cachedir = os.path.join(os.getenv('HOME'), '.cache', 'fdroidserver')
59 config = {
60     'basebox': 'jessie64',
61     'baseboxurl': [
62         pathlib.Path(os.path.join(cachedir, 'jessie64.box')).as_uri(),
63         'https://f-droid.org/jessie64.box',
64     ],
65     'debian_mirror': 'http://http.debian.net/debian/',
66     'apt_package_cache': False,
67     'boot_timeout': 600,
68     'cachedir': cachedir,
69     'cpus': 1,
70     'memory': 1024,
71     'hwvirtex': 'off',
72     'vm_provider': 'virtualbox',
73 }
74
75 if os.path.isfile('/usr/bin/systemd-detect-virt'):
76     try:
77         virt = subprocess.check_output('/usr/bin/systemd-detect-virt').strip().decode('utf-8')
78     except subprocess.CalledProcessError as e:
79         virt = 'none'
80     if virt == 'qemu' or virt == 'kvm' or virt == 'bochs':
81         print('Running in a VM guest, defaulting to QEMU/KVM via libvirt')
82         config['vm_provider'] = 'libvirt'
83     elif virt != 'none':
84         print('Running in an unsupported VM guest (' + virt + ')!')
85
86 # load config file, if present
87 if os.path.exists('makebuildserver.config.py'):
88     exec(compile(open('makebuildserver.config.py').read(), 'makebuildserver.config.py', 'exec'), config)
89 elif os.path.exists('makebs.config.py'):
90     # this is the old name for the config file
91     exec(compile(open('makebs.config.py').read(), 'makebs.config.py', 'exec'), config)
92 if '__builtins__' in config:
93     del(config['__builtins__'])  # added by compile/exec
94
95 if os.path.exists(boxfile):
96     os.remove(boxfile)
97
98 if options.clean:
99     vagrant(['destroy', '-f'], cwd=serverdir, printout=options.verbose)
100     if config['vm_provider'] == 'libvirt':
101         subprocess.call(['virsh', 'undefine', 'buildserver_default'])
102         subprocess.call(['virsh', 'vol-delete', '/var/lib/libvirt/images/buildserver_default.img'])
103
104 # Update cached files.
105 cachedir = config['cachedir']
106 if not os.path.exists(cachedir):
107     os.makedirs(cachedir, 0o755)
108
109 if config['vm_provider'] == 'libvirt':
110     tmp = cachedir
111     while tmp != '/':
112         mode = os.stat(tmp).st_mode
113         if not (stat.S_IXUSR & mode and stat.S_IXGRP & mode and stat.S_IXOTH & mode):
114             print('ERROR:', tmp, 'will not be accessible to the VM!  To fix, run:')
115             print('  chmod a+X', tmp)
116             sys.exit(1)
117         tmp = os.path.dirname(tmp)
118
119 if config['apt_package_cache']:
120     config['aptcachedir'] = cachedir + '/apt/archives'
121
122 cachefiles = [
123     ('https://dl.google.com/android/repository/tools_r25.2.2-linux.zip',
124      '10d5185af8fee80a7ee093585bf84962d0a877b92a8fef489e2030e910f6a360'),
125     ('https://dl.google.com/android/repository/android_m2repository_r38.zip',
126      '69660df7b98764065242bf283807c06aff42ff689eac561fa6e8f249af60201f'),
127     ('https://dl.google.com/android/repository/android-1.5_r04-linux.zip',
128      '85b6c8f9797e56aa415d3a282428bb640c96b0acb17c11d41621bb2a5302fe64'),
129     ('https://dl.google.com/android/repository/android-1.6_r03-linux.zip',
130      'a8c4e3b32269c6b04c2adeabd112fce42f292dab1a40ef3b08ea7d4212be0df4'),
131     ('https://dl.google.com/android/repository/android-2.0_r01-linux.zip',
132      'e70e2151b49613f23f40828c771ab85e241eed361cab037c6312df77f2612f0a'),
133     ('https://dl.google.com/android/repository/android-2.0.1_r01-linux.zip',
134      'f47b46177b17f6368461f85bc2a27d0d2c437929f588ea27105712bc3185f664'),
135     ('https://dl.google.com/android/repository/android-2.1_r03.zip',
136      'b9cc140a9b879586181b22cfc7d4aa18b979251e16e9b17771c5d0acb71ba940'),
137     ('https://dl.google.com/android/repository/android-2.2_r03.zip',
138      '7c9ea1bd7cb225504bd085d7c93ae27d52bd88d29b621d28108f82fef68177c0'),
139     ('https://dl.google.com/android/repository/android-2.3.1_r02.zip',
140      'b2ab4896d0a4857e4f688f69eb08b0e1a8074709d4445a92a83ece7ec7cd198c'),
141     ('https://dl.google.com/android/repository/android-2.3.3_r02.zip',
142      '54bdb0f1ca06ba5747061ddeea20f431af72c448334fd4d3d7f84ea2ccd29fea'),
143     ('https://dl.google.com/android/repository/android-3.0_r02.zip',
144      '1cacae7b6e1b5a5d73c06f5d29d2ea92d16674df8fd5507681290e77d1647a1c'),
145     ('https://dl.google.com/android/repository/android-3.1_r03.zip',
146      '7570c86a86488a146aa2141a65a24d81800959c1907ff4f1d2c13bbafab230c5'),
147     ('https://dl.google.com/android/repository/android-3.2_r01.zip',
148      'ff6b26ad34d7060a72ba504b0314cef8ba3138005561705adec5ad470a073d9b'),
149     ('https://dl.google.com/android/repository/android-14_r04.zip',
150      'da1af15c77ba41d062eb6d0ef5921cc424ab6167587033b830609d65f04802b6'),
151     ('https://dl.google.com/android/repository/android-15_r05.zip',
152      '5bc1f93aae86b4336ffc4cae9eb8ec41a9a8fd677582dd86a9629798f019bed9'),
153     ('https://dl.google.com/android/repository/android-16_r05.zip',
154      'fd7f269a423d1f1d079eabf9f918ceab49108702a1c6bb2589d57c23393503d3'),
155     ('https://dl.google.com/android/repository/android-17_r03.zip',
156      'b66e73fb2639f8c916fde4369aa29012a5c531e156dbb205fe3788fe998fbbe8'),
157     ('https://dl.google.com/android/repository/android-18_r03.zip',
158      '166ae9cf299747a5faa8f04168f0ee47cd7466a975d8b44acaaa62a43e767568'),
159     ('https://dl.google.com/android/repository/android-19_r04.zip',
160      '5efc3a3a682c1d49128daddb6716c433edf16e63349f32959b6207524ac04039'),
161     ('https://dl.google.com/android/repository/android-20_r02.zip',
162      'ef08c453e16ab6e656cf5d9413ef61cb8c650607d33b24ee4ce08dafdfe965a7'),
163     ('https://dl.google.com/android/repository/android-21_r02.zip',
164      'a76cd7ad3080ac6ce9f037cb935b399a1bad396c0605d4ff42f693695f1dcefe'),
165     ('https://dl.google.com/android/repository/android-22_r02.zip',
166      '45eb581bbe53c9256f34c26b2cea919543c0079140897ac721cf88c0b9f6789e'),
167     ('https://dl.google.com/android/repository/platform-23_r03.zip',
168      '4b4bcddead3319708275c54c76294707bfaa953d767e34f1a5b599f3edd0076c'),
169     ('https://dl.google.com/android/repository/platform-24_r02.zip',
170      'f268f5945c6ece7ea95c1c252067280854d2a20da924e22ae4720287df8bdbc9'),
171     ('https://dl.google.com/android/repository/build-tools_r17-linux.zip',
172      '4c8444972343a19045236f6924bd7f12046287c70dace96ab88b2159c8ec0e74'),
173     ('https://dl.google.com/android/repository/build-tools_r18.0.1-linux.zip',
174      'a9b7b1bdfd864780fdd03fa1683f3fe712a4276cf200646833808cb9159bafc0'),
175     ('https://dl.google.com/android/repository/build-tools_r18.1-linux.zip',
176      '0753606738f31cc346426db1d46b7d021bc1bdaff63085f9ee9d278ee054d3c9'),
177     ('https://dl.google.com/android/repository/build-tools_r18.1.1-linux.zip',
178      '7e4ed326b53078f4f23276ddab52c400011f7593dfbb6508c0a6671954dba8b0'),
179     ('https://dl.google.com/android/repository/build-tools_r19-linux.zip',
180      '9442e1c5212ed594e344a231fa93e7a017a5ef8cc661117011f1d3142eca7acc'),
181     ('https://dl.google.com/android/repository/build-tools_r19.0.1-linux.zip',
182      'b068edaff05c3253a63e9c8f0e1786429799b7e4b01514a847a8b291beb9232e'),
183     ('https://dl.google.com/android/repository/build-tools_r19.0.2-linux.zip',
184      '06124fad0d4bde21191240d61df2059a8546c085064a9a57d024c36fa2c9bebb'),
185     ('https://dl.google.com/android/repository/build-tools_r19.0.3-linux.zip',
186      'bc9b3db0de4a3e233a170274293359051a758f1e3f0d0d852ff4ad6d90d0a794'),
187     ('https://dl.google.com/android/repository/build-tools_r19.1-linux.zip',
188      '3833b409f78c002a83244e220be380ea6fa44d604e0d47de4b7e5daefe7cd3f4'),
189     ('https://dl.google.com/android/repository/build-tools_r20-linux.zip',
190      '296e09d62095d80e6eaa06a64cfa4c6f9f317c2d67ad8da6514523ec66f5c871'),
191     ('https://dl.google.com/android/repository/build-tools_r21-linux.zip',
192      '12b818f38fe1b68091b94545988317438efbf41eb61fd36b72cd79f536044065'),
193     ('https://dl.google.com/android/repository/build-tools_r21.0.1-linux.zip',
194      'a8922e80d3dd0cf6df14b29a7862448fa111b48086c639168d4b18c92431f559'),
195     ('https://dl.google.com/android/repository/build-tools_r21.0.2-linux.zip',
196      '859b17a6b65d063dfd86c163489b736b12bdeecd9173fdddb3e9f32e0fe584b7'),
197     ('https://dl.google.com/android/repository/build-tools_r21.1-linux.zip',
198      '022a85b92360272379b2f04b8a4d727e754dbe7eb8ab5a9568190e33e480d8f1'),
199     ('https://dl.google.com/android/repository/build-tools_r21.1.1-linux.zip',
200      '29b612484de6b5cde0df6de655e413f7611b0557b440538397afa69b557e2f08'),
201     ('https://dl.google.com/android/repository/build-tools_r21.1.2-linux.zip',
202      '3f88efc2d5316fb73f547f35b472610eed5e6f3f56762750ddad1c7d1d81660d'),
203     ('https://dl.google.com/android/repository/build-tools_r22-linux.zip',
204      '061c021243f04c80c19568a6e3a027c00d8e269c9311d7bf07fced60fbde7bd5'),
205     ('https://dl.google.com/android/repository/build-tools_r22.0.1-linux.zip',
206      '91e5524bf227aad1135ddd10905518ac49f74797d33d48920dcf8364b9fde214'),
207     ('https://dl.google.com/android/repository/build-tools_r23-linux.zip',
208      '56bf4fc6c43638c55fef4a0937bad38281945725459841879b436c6922df786c'),
209     ('https://dl.google.com/android/repository/build-tools_r23.0.1-linux.zip',
210      'e56b3ef7b760ad06a7cee9b2d52ba7f43133dcecedfa5357f8845b3a80aeeecf'),
211     ('https://dl.google.com/android/repository/build-tools_r23.0.2-linux.zip',
212      '82754f551a6e36eaf516fbdd00c95ff0ccd19f81d1e134125b6ac4916f7ed9b6'),
213     ('https://dl.google.com/android/repository/build-tools_r23.0.3-linux.zip',
214      'd961663d4a9e128841751c0156548a347c882c081c83942e53788d8949bf34e1'),
215     ('https://dl.google.com/android/repository/build-tools_r24-linux.zip',
216      'b4871f357224c5f660fd2bbee04d8c7d1c187eeddfd9702cc84503529e3b3724'),
217     ('https://dl.google.com/android/repository/build-tools_r24.0.1-linux.zip',
218      'a38ac637db357a31e33e38248399cb0edcc15040dca041370da38b6daf50c84d'),
219     ('https://dl.google.com/android/repository/build-tools_r24.0.2-linux.zip',
220      '924e29b8a189afbd119d44eae450fc0c9f197ed6f835df223931e45007987d95'),
221     # the binaries that Google uses are here:
222     # https://android.googlesource.com/platform/tools/external/gradle/+/studio-1.5/
223     ('https://services.gradle.org/distributions/gradle-1.4-bin.zip',
224      'cd99e85fbcd0ae8b99e81c9992a2f10cceb7b5f009c3720ef3a0078f4f92e94e'),
225     ('https://services.gradle.org/distributions/gradle-1.6-bin.zip',
226      'de3e89d2113923dcc2e0def62d69be0947ceac910abd38b75ec333230183fac4'),
227     ('https://services.gradle.org/distributions/gradle-1.7-bin.zip',
228      '360c97d51621b5a1ecf66748c718594e5f790ae4fbc1499543e0c006033c9d30'),
229     ('https://services.gradle.org/distributions/gradle-1.8-bin.zip',
230      'a342bbfa15fd18e2482287da4959588f45a41b60910970a16e6d97959aea5703'),
231     ('https://services.gradle.org/distributions/gradle-1.9-bin.zip',
232      '097ddc2bcbc9da2bb08cbf6bf8079585e35ad088bafd42e8716bc96405db98e9'),
233     ('https://services.gradle.org/distributions/gradle-1.10-bin.zip',
234      '6e6db4fc595f27ceda059d23693b6f6848583950606112b37dfd0e97a0a0a4fe'),
235     ('https://services.gradle.org/distributions/gradle-1.11-bin.zip',
236      '07e235df824964f0e19e73ea2327ce345c44bcd06d44a0123d29ab287fc34091'),
237     ('https://services.gradle.org/distributions/gradle-1.12-bin.zip',
238      '8734b13a401f4311ee418173ed6ca8662d2b0a535be8ff2a43ecb1c13cd406ea'),
239     ('https://services.gradle.org/distributions/gradle-2.1-bin.zip',
240      '3eee4f9ea2ab0221b89f8e4747a96d4554d00ae46d8d633f11cfda60988bf878'),
241     ('https://services.gradle.org/distributions/gradle-2.2-bin.zip',
242      '91e5655fe11ef414449f218c4fa2985b3a49b7903c57556da109c84fa26e1dfb'),
243     ('https://services.gradle.org/distributions/gradle-2.2.1-bin.zip',
244      '420aa50738299327b611c10b8304b749e8d3a579407ee9e755b15921d95ff418'),
245     ('https://services.gradle.org/distributions/gradle-2.3-bin.zip',
246      '010dd9f31849abc3d5644e282943b1c1c355f8e2635c5789833979ce590a3774'),
247     ('https://services.gradle.org/distributions/gradle-2.4-bin.zip',
248      'c4eaecc621a81f567ded1aede4a5ddb281cc02a03a6a87c4f5502add8fc2f16f'),
249     ('https://services.gradle.org/distributions/gradle-2.5-bin.zip',
250      '3f953e0cb14bb3f9ebbe11946e84071547bf5dfd575d90cfe9cc4e788da38555'),
251     ('https://services.gradle.org/distributions/gradle-2.6-bin.zip',
252      '18a98c560af231dfa0d3f8e0802c20103ae986f12428bb0a6f5396e8f14e9c83'),
253     ('https://services.gradle.org/distributions/gradle-2.7-bin.zip',
254      'cde43b90945b5304c43ee36e58aab4cc6fb3a3d5f9bd9449bb1709a68371cb06'),
255     ('https://services.gradle.org/distributions/gradle-2.8-bin.zip',
256      'a88db9c2f104defdaa8011c58cf6cda6c114298ae3695ecfb8beb30da3a903cb'),
257     ('https://services.gradle.org/distributions/gradle-2.9-bin.zip',
258      'c9159ec4362284c0a38d73237e224deae6139cbde0db4f0f44e1c7691dd3de2f'),
259     ('https://services.gradle.org/distributions/gradle-2.10-bin.zip',
260      '66406247f745fc6f05ab382d3f8d3e120c339f34ef54b86f6dc5f6efc18fbb13'),
261     ('https://services.gradle.org/distributions/gradle-2.11-bin.zip',
262      '8d7437082356c9fd6309a4479c8db307673965546daea445c6c72759cd6b1ed6'),
263     ('https://services.gradle.org/distributions/gradle-2.12-bin.zip',
264      'e77064981906cd0476ff1e0de3e6fef747bd18e140960f1915cca8ff6c33ab5c'),
265     ('https://services.gradle.org/distributions/gradle-2.13-bin.zip',
266      '0f665ec6a5a67865faf7ba0d825afb19c26705ea0597cec80dd191b0f2cbb664'),
267     ('https://services.gradle.org/distributions/gradle-2.14-bin.zip',
268      '993b4f33b652c689e9721917d8e021cab6bbd3eae81b39ab2fd46fdb19a928d5'),
269     ('https://services.gradle.org/distributions/gradle-2.14.1-bin.zip',
270      'cfc61eda71f2d12a572822644ce13d2919407595c2aec3e3566d2aab6f97ef39'),
271     ('https://services.gradle.org/distributions/gradle-3.0-bin.zip',
272      '39c906941a474444afbddc38144ed44166825acb0a57b0551dddb04bbf157f80'),
273     ('https://services.gradle.org/distributions/gradle-3.1-bin.zip',
274      'c7de3442432253525902f7e8d7eac8b5fd6ce1623f96d76916af6d0e383010fc'),
275     ('https://dl.google.com/android/ndk/android-ndk-r10e-linux-x86_64.bin',
276      '102d6723f67ff1384330d12c45854315d6452d6510286f4e5891e00a5a8f1d5a'),
277     ('https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64.tar.bz2',
278      '8956e9efeea95f49425ded8bb697013b66e162b064b0f66b5c75628f76e0f532'),
279     ('https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64-legacy-toolchains.tar.bz2',
280      'de93a394f7c8f3436db44568648f87738a8d09801a52f459dcad3fc047e045a1'),
281     ('https://dl.google.com/android/repository/android-ndk-r11c-linux-x86_64.zip',
282      'ba85dbe4d370e4de567222f73a3e034d85fc3011b3cbd90697f3e8dcace3ad94'),
283     ('https://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip',
284      'eafae2d614e5475a3bcfd7c5f201db5b963cc1290ee3e8ae791ff0c66757781e'),
285     ('https://download.qt.io/official_releases/qt/5.7/5.7.0/qt-opensource-linux-x64-android-5.7.0.run',
286      'f7e55b7970e59bdaabb88cb7afc12e9061e933992bda2f076f52600358644586'),
287 ]
288
289
290 def sha256_for_file(path):
291     with open(path, 'rb') as f:
292         s = hashlib.sha256()
293         while True:
294             data = f.read(4096)
295             if not data:
296                 break
297             s.update(data)
298         return s.hexdigest()
299
300 for srcurl, shasum in cachefiles:
301     filename = os.path.basename(srcurl)
302     local_filename = os.path.join(cachedir, filename)
303
304     if os.path.exists(local_filename):
305         local_length = os.path.getsize(local_filename)
306     else:
307         local_length = -1
308
309     resume_header = {}
310     download = True
311
312     try:
313         r = requests.head(srcurl, allow_redirects=True, timeout=60)
314         if r.status_code == 200:
315             content_length = int(r.headers.get('content-length'))
316         else:
317             content_length = local_length  # skip the download
318     except requests.exceptions.RequestException as e:
319         content_length = local_length  # skip the download
320         print(e)
321
322     if local_length == content_length:
323         download = False
324     elif local_length > content_length:
325         print('deleting corrupt file from cache: ' + local_filename)
326         os.remove(local_filename)
327         print("Downloading " + filename + " to cache")
328     elif local_length > -1 and local_length < content_length:
329         print("Resuming download of " + local_filename)
330         resume_header = {'Range': 'bytes=%d-%d' % (local_length, content_length)}
331     else:
332         print("Downloading " + filename + " to cache")
333
334     if download:
335         r = requests.get(srcurl, headers=resume_header,
336                          stream=True, verify=False, allow_redirects=True)
337         content_length = int(r.headers.get('content-length'))
338         with open(local_filename, 'ab') as f:
339             for chunk in progress.bar(r.iter_content(chunk_size=65536),
340                                       expected_size=(content_length / 65536) + 1):
341                 if chunk:  # filter out keep-alive new chunks
342                     f.write(chunk)
343
344     v = sha256_for_file(local_filename)
345     if v == shasum:
346         print("\t...shasum verified for " + local_filename)
347     else:
348         print("Invalid shasum of '" + v + "' detected for " + local_filename)
349         os.remove(local_filename)
350         sys.exit(1)
351
352 local_qt_filename = os.path.join(cachedir, 'qt-opensource-linux-x64-android-5.7.0.run')
353 print("Setting executable bit for " + local_qt_filename)
354 os.chmod(local_qt_filename, 0o755)
355
356 # use VirtualBox software virtualization if hardware is not available,
357 # like if this is being run in kvm or some other VM platform, like
358 # http://jenkins.debian.net, the values are 'on' or 'off'
359 if sys.platform.startswith('darwin'):
360     # all < 10 year old Macs work, and OSX servers as VM host are very
361     # rare, but this could also be auto-detected if someone codes it
362     config['hwvirtex'] = 'on'
363 elif os.path.exists('/proc/cpuinfo'):
364     with open('/proc/cpuinfo') as f:
365         contents = f.read()
366     if 'vmx' in contents or 'svm' in contents:
367         config['hwvirtex'] = 'on'
368
369 # Check against the existing Vagrantfile.yaml, and if they differ, we
370 # need to create a new box:
371 vf = os.path.join(serverdir, 'Vagrantfile.yaml')
372 writevf = True
373 if os.path.exists(vf):
374     print('Halting', serverdir)
375     vagrant(['halt'], serverdir)
376     with open(vf, 'r', encoding='utf-8') as f:
377         oldconfig = yaml.load(f)
378     if config != oldconfig:
379         print("Server configuration has changed, rebuild from scratch is required")
380         vagrant(['destroy', '-f'], serverdir)
381     else:
382         print("Re-provisioning existing server")
383         writevf = False
384 else:
385     print("No existing server - building from scratch")
386 if writevf:
387     with open(vf, 'w', encoding='utf-8') as f:
388         yaml.dump(config, f)
389
390 if config['vm_provider'] == 'libvirt':
391     returncode, out = vagrant(['box', 'list'], serverdir, printout=options.verbose)
392     found_basebox = False
393     needs_mutate = False
394     for line in out.splitlines():
395         if line.startswith(config['basebox']):
396             found_basebox = True
397             if line.split('(')[1].split(',')[0] != 'libvirt':
398                 needs_mutate = True
399             continue
400     if not found_basebox:
401         if isinstance(config['baseboxurl'], str):
402             baseboxurl = config['baseboxurl']
403         else:
404             baseboxurl = config['baseboxurl'][0]
405         print('Adding', config['basebox'], 'from', baseboxurl)
406         vagrant(['box', 'add', '--name', config['basebox'], baseboxurl],
407                 serverdir, printout=options.verbose)
408         needs_mutate = True
409     if needs_mutate:
410         print('Converting', config['basebox'], 'to libvirt format')
411         vagrant(['mutate', config['basebox'], 'libvirt'],
412                 serverdir, printout=options.verbose)
413         print('Removing virtualbox format copy of', config['basebox'])
414         vagrant(['box', 'remove', '--provider', 'virtualbox', config['basebox']],
415                 serverdir, printout=options.verbose)
416
417 print("Configuring build server VM")
418 returncode, out = vagrant(['up', '--provision'], serverdir, printout=True)
419 with open(os.path.join(serverdir, 'up.log'), 'w') as log:
420     log.write(out)
421 if returncode != 0:
422     print("Failed to configure server")
423     sys.exit(1)
424
425 print("Writing buildserver ID")
426 p = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE,
427                      universal_newlines=True)
428 buildserverid = p.communicate()[0].strip()
429 print("...ID is " + buildserverid)
430 subprocess.call(
431     ['vagrant', 'ssh', '-c', 'sh -c "echo {0} >/home/vagrant/buildserverid"'
432         .format(buildserverid)],
433     cwd=serverdir)
434
435 print("Stopping build server VM")
436 vagrant(['halt'], serverdir)
437
438 print("Waiting for build server VM to be finished")
439 ready = False
440 while not ready:
441     time.sleep(2)
442     returncode, out = vagrant(['status'], serverdir)
443     if returncode != 0:
444         print("Error while checking status")
445         sys.exit(1)
446     for line in out.splitlines():
447         if line.startswith("default"):
448             if line.find("poweroff") != -1:
449                 ready = True
450             else:
451                 print("Status: " + line)
452
453 print("Packaging")
454 vagrant(['package', '--output', os.path.join('..', boxfile)], serverdir,
455         printout=options.verbose)
456 print("Adding box")
457 vagrant(['box', 'add', 'buildserver', boxfile, '-f'],
458         printout=options.verbose)
459
460 os.remove(boxfile)