chiark / gitweb /
Download platforms via makebuildserver
[fdroidserver.git] / makebuildserver
1 #!/usr/bin/env python2
2
3 import os
4 import sys
5 import subprocess
6 import time
7 import hashlib
8 from optparse import OptionParser
9
10
11 def vagrant(params, cwd=None, printout=False):
12     """Run vagrant.
13
14     :param: list of parameters to pass to vagrant
15     :cwd: directory to run in, or None for current directory
16     :printout: True to print output in realtime, False to just
17                return it
18     :returns: (ret, out) where ret is the return code, and out
19                is the stdout (and stderr) from vagrant
20     """
21     p = subprocess.Popen(['vagrant'] + params, cwd=cwd,
22                          stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
23     out = ''
24     if printout:
25         while True:
26             line = p.stdout.readline()
27             if len(line) == 0:
28                 break
29             print line,
30             out += line
31         p.wait()
32     else:
33         out = p.communicate()[0]
34     return (p.returncode, out)
35
36 boxfile = 'buildserver.box'
37 serverdir = 'buildserver'
38
39 parser = OptionParser()
40 parser.add_option("-v", "--verbose", action="store_true", default=False,
41                   help="Spew out even more information than normal")
42 parser.add_option("-c", "--clean", action="store_true", default=False,
43                   help="Build from scratch, rather than attempting to update the existing server")
44 parser.add_option("--debian-mirror", default="http://ftp.uk.debian.org/debian/",
45                   help="Use the specified Debian mirror in the box's /etc/apt/sources.list.")
46 options, args = parser.parse_args()
47
48 config = {}
49 execfile('makebs.config.py', config)
50
51 if not os.path.exists('makebuildserver') or not os.path.exists(serverdir):
52     print 'This must be run from the correct directory!'
53     sys.exit(1)
54
55 if os.path.exists(boxfile):
56     os.remove(boxfile)
57
58 if options.clean:
59     vagrant(['destroy', '-f'], cwd=serverdir, printout=options.verbose)
60
61 # Update cached files.
62 cachedir = os.path.join('buildserver', 'cache')
63 if not os.path.exists(cachedir):
64     os.mkdir(cachedir)
65
66 cachefiles = [
67     ('android-sdk_r24.4.1-linux.tgz',
68      'https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz',
69      'e16917ad685c1563ccbc5dd782930ee1a700a1b6a6fd3e44b83ac694650435e9'),
70     ('android-platform-3.zip',
71      'https://dl.google.com/android/repository/android-1.5_r04-linux.zip',
72      '85b6c8f9797e56aa415d3a282428bb640c96b0acb17c11d41621bb2a5302fe64'),
73     ('android-platform-4.zip',
74      'https://dl.google.com/android/repository/android-1.6_r03-linux.zip',
75      'a8c4e3b32269c6b04c2adeabd112fce42f292dab1a40ef3b08ea7d4212be0df4'),
76     ('android-platform-5.zip',
77      'https://dl.google.com/android/repository/android-2.0_r01-linux.zip',
78      'e70e2151b49613f23f40828c771ab85e241eed361cab037c6312df77f2612f0a'),
79     ('android-platform-6.zip',
80      'https://dl.google.com/android/repository/android-2.0.1_r01-linux.zip',
81      'f47b46177b17f6368461f85bc2a27d0d2c437929f588ea27105712bc3185f664'),
82     ('android-platform-7.zip',
83      'https://dl.google.com/android/repository/android-2.1_r03-linux.zip',
84      'b9cc140a9b879586181b22cfc7d4aa18b979251e16e9b17771c5d0acb71ba940'),
85     ('android-platform-8.zip',
86      'https://dl.google.com/android/repository/android-2.2_r03-linux.zip',
87      '7c9ea1bd7cb225504bd085d7c93ae27d52bd88d29b621d28108f82fef68177c0'),
88     ('android-platform-9.zip',
89      'https://dl.google.com/android/repository/android-2.3.1_r02-linux.zip',
90      'b2ab4896d0a4857e4f688f69eb08b0e1a8074709d4445a92a83ece7ec7cd198c'),
91     ('android-platform-10.zip',
92      'https://dl.google.com/android/repository/android-2.3.3_r02-linux.zip',
93      '54bdb0f1ca06ba5747061ddeea20f431af72c448334fd4d3d7f84ea2ccd29fea'),
94     ('android-platform-11.zip',
95      'https://dl.google.com/android/repository/android-3.0_r02-linux.zip',
96      '1cacae7b6e1b5a5d73c06f5d29d2ea92d16674df8fd5507681290e77d1647a1c'),
97     ('android-platform-12.zip',
98      'https://dl.google.com/android/repository/android-3.1_r03-linux.zip',
99      '7570c86a86488a146aa2141a65a24d81800959c1907ff4f1d2c13bbafab230c5'),
100     ('android-platform-13.zip',
101      'https://dl.google.com/android/repository/android-3.2_r01-linux.zip',
102      'ff6b26ad34d7060a72ba504b0314cef8ba3138005561705adec5ad470a073d9b'),
103     ('android-platform-14.zip',
104      'https://dl.google.com/android/repository/android-14_r04.zip',
105      'da1af15c77ba41d062eb6d0ef5921cc424ab6167587033b830609d65f04802b6'),
106     ('android-platform-15.zip',
107      'https://dl.google.com/android/repository/android-15_r05.zip',
108      '5bc1f93aae86b4336ffc4cae9eb8ec41a9a8fd677582dd86a9629798f019bed9'),
109     ('android-platform-16.zip',
110      'https://dl.google.com/android/repository/android-16_r05.zip',
111      'fd7f269a423d1f1d079eabf9f918ceab49108702a1c6bb2589d57c23393503d3'),
112     ('android-platform-17.zip',
113      'https://dl.google.com/android/repository/android-17_r03.zip',
114      'b66e73fb2639f8c916fde4369aa29012a5c531e156dbb205fe3788fe998fbbe8'),
115     ('android-platform-18.zip',
116      'https://dl.google.com/android/repository/android-18_r03.zip',
117      '166ae9cf299747a5faa8f04168f0ee47cd7466a975d8b44acaaa62a43e767568'),
118     ('android-platform-19.zip',
119      'https://dl.google.com/android/repository/android-19_r04.zip',
120      '5efc3a3a682c1d49128daddb6716c433edf16e63349f32959b6207524ac04039'),
121     ('android-platform-20.zip',
122      'https://dl.google.com/android/repository/android-20_r02.zip',
123      'ef08c453e16ab6e656cf5d9413ef61cb8c650607d33b24ee4ce08dafdfe965a7'),
124     ('android-platform-21.zip',
125      'https://dl.google.com/android/repository/android-21_r02.zip',
126      'a76cd7ad3080ac6ce9f037cb935b399a1bad396c0605d4ff42f693695f1dcefe'),
127     ('android-platform-22.zip',
128      'https://dl.google.com/android/repository/android-22_r02.zip',
129      '45eb581bbe53c9256f34c26b2cea919543c0079140897ac721cf88c0b9f6789e'),
130     ('android-platform-23.zip',
131      'https://dl.google.com/android/repository/android-23_r01.zip',
132      '16e828bec35521f5529c998a6d312aaa6155cd4aabb5507d9eb2d4b3c9a1aff0'),
133     ('gradle-1.4-bin.zip',
134      'https://services.gradle.org/distributions/gradle-1.4-bin.zip',
135      'cd99e85fbcd0ae8b99e81c9992a2f10cceb7b5f009c3720ef3a0078f4f92e94e'),
136     ('gradle-1.6-bin.zip',
137      'https://services.gradle.org/distributions/gradle-1.6-bin.zip',
138      'de3e89d2113923dcc2e0def62d69be0947ceac910abd38b75ec333230183fac4'),
139     ('gradle-1.7-bin.zip',
140      'https://services.gradle.org/distributions/gradle-1.7-bin.zip',
141      '360c97d51621b5a1ecf66748c718594e5f790ae4fbc1499543e0c006033c9d30'),
142     ('gradle-1.8-bin.zip',
143      'https://services.gradle.org/distributions/gradle-1.8-bin.zip',
144      'a342bbfa15fd18e2482287da4959588f45a41b60910970a16e6d97959aea5703'),
145     ('gradle-1.9-bin.zip',
146      'https://services.gradle.org/distributions/gradle-1.9-bin.zip',
147      '097ddc2bcbc9da2bb08cbf6bf8079585e35ad088bafd42e8716bc96405db98e9'),
148     ('gradle-1.10-bin.zip',
149      'https://services.gradle.org/distributions/gradle-1.10-bin.zip',
150      '6e6db4fc595f27ceda059d23693b6f6848583950606112b37dfd0e97a0a0a4fe'),
151     ('gradle-1.11-bin.zip',
152      'https://services.gradle.org/distributions/gradle-1.11-bin.zip',
153      '07e235df824964f0e19e73ea2327ce345c44bcd06d44a0123d29ab287fc34091'),
154     ('gradle-1.12-bin.zip',
155      'https://services.gradle.org/distributions/gradle-1.12-bin.zip',
156      '8734b13a401f4311ee418173ed6ca8662d2b0a535be8ff2a43ecb1c13cd406ea'),
157     ('gradle-2.1-bin.zip',
158      'https://services.gradle.org/distributions/gradle-2.1-bin.zip',
159      '3eee4f9ea2ab0221b89f8e4747a96d4554d00ae46d8d633f11cfda60988bf878'),
160     ('gradle-2.2.1-bin.zip',
161      'https://services.gradle.org/distributions/gradle-2.2.1-bin.zip',
162      '420aa50738299327b611c10b8304b749e8d3a579407ee9e755b15921d95ff418'),
163     ('gradle-2.3-bin.zip',
164      'https://services.gradle.org/distributions/gradle-2.3-bin.zip',
165      '010dd9f31849abc3d5644e282943b1c1c355f8e2635c5789833979ce590a3774'),
166     ('gradle-2.4-bin.zip',
167      'https://services.gradle.org/distributions/gradle-2.4-bin.zip',
168      'c4eaecc621a81f567ded1aede4a5ddb281cc02a03a6a87c4f5502add8fc2f16f'),
169     ('gradle-2.5-bin.zip',
170      'https://services.gradle.org/distributions/gradle-2.5-bin.zip',
171      '3f953e0cb14bb3f9ebbe11946e84071547bf5dfd575d90cfe9cc4e788da38555'),
172     ('gradle-2.6-bin.zip',
173      'https://services.gradle.org/distributions/gradle-2.6-bin.zip',
174      '18a98c560af231dfa0d3f8e0802c20103ae986f12428bb0a6f5396e8f14e9c83'),
175     ('gradle-2.7-bin.zip',
176      'https://services.gradle.org/distributions/gradle-2.7-bin.zip',
177      'cde43b90945b5304c43ee36e58aab4cc6fb3a3d5f9bd9449bb1709a68371cb06'),
178     ('gradle-2.8-bin.zip',
179      'https://services.gradle.org/distributions/gradle-2.8-bin.zip',
180      'a88db9c2f104defdaa8011c58cf6cda6c114298ae3695ecfb8beb30da3a903cb'),
181     ('Kivy-1.7.2.tar.gz',
182      'https://pypi.python.org/packages/source/K/Kivy/Kivy-1.7.2.tar.gz',
183      '0485e2ef97b5086df886eb01f8303cb542183d2d71a159466f99ad6c8a1d03f1'),
184 ]
185
186 if config['arch64']:
187     cachefiles.extend([
188         ('android-ndk-r10e-linux-x86_64.bin',
189          'https://dl.google.com/android/ndk/android-ndk-r10e-linux-x86_64.bin',
190          '102d6723f67ff1384330d12c45854315d6452d6510286f4e5891e00a5a8f1d5a'),
191         ('android-ndk-r9b-linux-x86_64.tar.bz2',
192          'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64.tar.bz2',
193          '8956e9efeea95f49425ded8bb697013b66e162b064b0f66b5c75628f76e0f532'),
194         ('android-ndk-r9b-linux-x86_64-legacy-toolchains.tar.bz2',
195          'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64-legacy-toolchains.tar.bz2',
196          'de93a394f7c8f3436db44568648f87738a8d09801a52f459dcad3fc047e045a1')])
197 else:
198     cachefiles.extend([
199         ('android-ndk-r10e-linux-x86.bin',
200          'https://dl.google.com/android/ndk/android-ndk-r10e-linux-x86.bin',
201          '92b07d25aaad9b341a7f2b2a62402d508e948bf2dea3ee7b65a6aeb18bca7df5'),
202         ('android-ndk-r9b-linux-x86.tar.bz2',
203          'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86.tar.bz2',
204          '748104b829dd12afb2fdb3044634963abb24cdb0aad3b26030abe2e9e65bfc81'),
205         ('android-ndk-r9b-linux-x86-legacy-toolchains.tar.bz2',
206          'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86-legacy-toolchains.tar.bz2',
207          '606aadf815ae28cc7b0154996247c70d609f111b14e44bcbcd6cad4c87fefb6f')])
208 wanted = []
209
210
211 def sha256_for_file(path):
212     with open(path, 'r') as f:
213         s = hashlib.sha256()
214         while True:
215             data = f.read(4096)
216             if not data:
217                 break
218             s.update(data)
219         return s.hexdigest()
220
221 for f, src, shasum in cachefiles:
222     relpath = os.path.join(cachedir, f)
223     if not os.path.exists(relpath):
224         print "Downloading " + f + " to cache"
225         if subprocess.call(['wget', src, '-O', f], cwd=cachedir) != 0:
226             print "...download of " + f + " failed."
227             sys.exit(1)
228     if shasum:
229         v = sha256_for_file(relpath)
230         if v != shasum:
231             print "Invalid shasum of '" + v + "' detected for " + f
232             sys.exit(1)
233         else:
234             print "...shasum verified for " + f
235
236     wanted.append(f)
237
238 # allow specifying a list/tuple that includes cached local copy
239 if type(config['baseboxurl']) in (list, tuple) or config['baseboxurl'][0] in ('(', '['):
240     baseboxurl = config['baseboxurl']
241 else:
242     baseboxurl = '"{0}"'.format(config['baseboxurl'])
243
244 # Generate an appropriate Vagrantfile for the buildserver, based on our
245 # settings...
246 vagrantfile = """
247 Vagrant.configure("2") do |config|
248
249   if Vagrant.has_plugin?("vagrant-cachier")
250     config.cache.scope = :box
251     config.cache.auto_detect = false
252     config.cache.enable :apt
253     config.cache.enable :chef
254   end
255
256   config.vm.box = "{0}"
257   config.vm.box_url = {1}
258
259   config.vm.provider "virtualbox" do |v|
260     v.customize ["modifyvm", :id, "--memory", "{2}"]
261     v.customize ["modifyvm", :id, "--cpus", "{3}"]
262   end
263
264   config.vm.provision :shell, :path => "fixpaths.sh"
265 """.format(config['basebox'],
266            baseboxurl,
267            config['memory'],
268            config.get('cpus', 1))
269 if 'aptproxy' in config and config['aptproxy']:
270     vagrantfile += """
271   config.vm.provision :shell, :inline => 'sudo echo "Acquire::http {{ Proxy \\"{0}\\"; }};" > /etc/apt/apt.conf.d/02proxy && sudo apt-get update'
272 """.format(config['aptproxy'])
273
274 vagrantfile += """
275   config.vm.provision :chef_solo do |chef|
276     chef.cookbooks_path = "cookbooks"
277     chef.log_level = :debug
278     chef.json = {
279       :settings => {
280         :sdk_loc => "/home/vagrant/android-sdk",
281         :ndk_loc => "/home/vagrant/android-ndk",
282         :debian_mirror => "%s",
283         :user => "vagrant"
284       }
285     }
286     chef.add_recipe "fdroidbuild-general"
287     chef.add_recipe "android-sdk"
288     chef.add_recipe "android-ndk"
289     chef.add_recipe "gradle"
290     chef.add_recipe "kivy"
291   end
292 end
293 """ % (options.debian_mirror)
294
295 # Check against the existing Vagrantfile, and if they differ, we need to
296 # create a new box:
297 vf = os.path.join(serverdir, 'Vagrantfile')
298 writevf = True
299 if os.path.exists(vf):
300     vagrant(['halt'], serverdir)
301     with open(vf, 'r') as f:
302         oldvf = f.read()
303     if oldvf != vagrantfile:
304         print "Server configuration has changed, rebuild from scratch is required"
305         vagrant(['destroy', '-f'], serverdir)
306     else:
307         print "Re-provisioning existing server"
308         writevf = False
309 else:
310     print "No existing server - building from scratch"
311 if writevf:
312     with open(vf, 'w') as f:
313         f.write(vagrantfile)
314
315
316 print "Configuring build server VM"
317 returncode, out = vagrant(['up', '--provision'], serverdir, printout=True)
318 with open(os.path.join(serverdir, 'up.log'), 'w') as log:
319     log.write(out)
320 if returncode != 0:
321     print "Failed to configure server"
322     sys.exit(1)
323
324 print "Writing buildserver ID"
325 p = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE)
326 buildserverid = p.communicate()[0].strip()
327 print "...ID is " + buildserverid
328 subprocess.call(
329     ['vagrant', 'ssh', '-c', 'sh -c "echo {0} >/home/vagrant/buildserverid"'
330         .format(buildserverid)],
331     cwd=serverdir)
332
333 print "Stopping build server VM"
334 vagrant(['halt'], serverdir)
335
336 print "Waiting for build server VM to be finished"
337 ready = False
338 while not ready:
339     time.sleep(2)
340     returncode, out = vagrant(['status'], serverdir)
341     if returncode != 0:
342         print "Error while checking status"
343         sys.exit(1)
344     for line in out.splitlines():
345         if line.startswith("default"):
346             if line.find("poweroff") != -1:
347                 ready = True
348             else:
349                 print "Status: " + line
350
351 print "Packaging"
352 vagrant(['package', '--output', os.path.join('..', boxfile)], serverdir,
353         printout=options.verbose)
354 print "Adding box"
355 vagrant(['box', 'add', 'buildserver', boxfile, '-f'],
356         printout=options.verbose)
357
358 os.remove(boxfile)