chiark / gitweb /
Add gradle 1.12 for the plugin version 0.10
[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 options, args = parser.parse_args()
45
46 config = {}
47 execfile('makebs.config.py', config)
48
49 if not os.path.exists('makebuildserver') or not os.path.exists(serverdir):
50     print 'This must be run from the correct directory!'
51     sys.exit(1)
52
53 if os.path.exists(boxfile):
54     os.remove(boxfile)
55
56 if options.clean:
57     vagrant(['destroy', '-f'], cwd=serverdir, printout=options.verbose)
58
59 # Update cached files.
60 cachedir = os.path.join('buildserver', 'cache')
61 if not os.path.exists(cachedir):
62     os.mkdir(cachedir)
63 cachefiles = [
64     ('android-sdk_r22.6-linux.tgz',
65      'https://dl.google.com/android/android-sdk_r22.6-linux.tgz',
66      'da4c25536ba7f85cdd37be8636fcc563480410788df30c3fc5b5c876e6220e5f'),
67     ('gradle-1.4-bin.zip',
68      'https://services.gradle.org/distributions/gradle-1.4-bin.zip',
69      'cd99e85fbcd0ae8b99e81c9992a2f10cceb7b5f009c3720ef3a0078f4f92e94e'),
70     ('gradle-1.6-bin.zip',
71      'https://services.gradle.org/distributions/gradle-1.6-bin.zip',
72      'de3e89d2113923dcc2e0def62d69be0947ceac910abd38b75ec333230183fac4'),
73     ('gradle-1.7-bin.zip',
74      'https://services.gradle.org/distributions/gradle-1.7-bin.zip',
75      '360c97d51621b5a1ecf66748c718594e5f790ae4fbc1499543e0c006033c9d30'),
76     ('gradle-1.8-bin.zip',
77      'https://services.gradle.org/distributions/gradle-1.8-bin.zip',
78      'a342bbfa15fd18e2482287da4959588f45a41b60910970a16e6d97959aea5703'),
79     ('gradle-1.9-bin.zip',
80      'https://services.gradle.org/distributions/gradle-1.9-bin.zip',
81      '097ddc2bcbc9da2bb08cbf6bf8079585e35ad088bafd42e8716bc96405db98e9'),
82     ('gradle-1.10-bin.zip',
83      'https://services.gradle.org/distributions/gradle-1.10-bin.zip',
84      '6e6db4fc595f27ceda059d23693b6f6848583950606112b37dfd0e97a0a0a4fe'),
85     ('gradle-1.11-bin.zip',
86      'https://services.gradle.org/distributions/gradle-1.11-bin.zip',
87      '07e235df824964f0e19e73ea2327ce345c44bcd06d44a0123d29ab287fc34091'),
88     ('gradle-1.12-bin.zip',
89      'https://services.gradle.org/distributions/gradle-1.12-bin.zip',
90      '8734b13a401f4311ee418173ed6ca8662d2b0a535be8ff2a43ecb1c13cd406ea'),
91     ('Kivy-1.7.2.tar.gz',
92      'https://pypi.python.org/packages/source/K/Kivy/Kivy-1.7.2.tar.gz',
93      '0485e2ef97b5086df886eb01f8303cb542183d2d71a159466f99ad6c8a1d03f1')
94     ]
95 if config['arch64']:
96     cachefiles.extend([
97     ('android-ndk-r9b-linux-x86_64.tar.bz2',
98      'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64.tar.bz2',
99      '8956e9efeea95f49425ded8bb697013b66e162b064b0f66b5c75628f76e0f532'),
100     ('android-ndk-r9b-linux-x86_64-legacy-toolchains.tar.bz2',
101      'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64-legacy-toolchains.tar.bz2',
102      'de93a394f7c8f3436db44568648f87738a8d09801a52f459dcad3fc047e045a1')])
103 else:
104     cachefiles.extend([
105     ('android-ndk-r9b-linux-x86.tar.bz2',
106      'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86.tar.bz2',
107      '748104b829dd12afb2fdb3044634963abb24cdb0aad3b26030abe2e9e65bfc81'),
108     ('android-ndk-r9b-linux-x86-legacy-toolchains.tar.bz2',
109      'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86-legacy-toolchains.tar.bz2',
110      '606aadf815ae28cc7b0154996247c70d609f111b14e44bcbcd6cad4c87fefb6f')])
111 wanted = []
112
113
114 def sha256_for_file(path):
115     with open(path, 'r') as f:
116         s = hashlib.sha256()
117         while True:
118             data = f.read(4096)
119             if not data:
120                 break
121             s.update(data)
122         return s.hexdigest()
123
124 for f, src, shasum in cachefiles:
125     relpath = os.path.join(cachedir, f)
126     if not os.path.exists(relpath):
127         print "Downloading " + f + " to cache"
128         if subprocess.call(['wget', src], cwd=cachedir) != 0:
129             print "...download of " + f + " failed."
130             sys.exit(1)
131     if shasum:
132         v = sha256_for_file(relpath)
133         if v != shasum:
134             print "Invalid shasum of '" + v + "' detected for " + f
135             sys.exit(1)
136         else:
137             print "...shasum verified for " + f
138
139     wanted.append(f)
140
141
142 # Generate an appropriate Vagrantfile for the buildserver, based on our
143 # settings...
144 vagrantfile = """
145 Vagrant::Config.run do |config|
146
147   config.vm.box = "{0}"
148   config.vm.box_url = "{1}"
149
150   config.vm.customize ["modifyvm", :id, "--memory", "{2}"]
151
152   config.vm.provision :shell, :path => "fixpaths.sh"
153 """.format(config['basebox'], config['baseboxurl'], config['memory'])
154 if 'aptproxy' in config and config['aptproxy']:
155     vagrantfile += """
156   config.vm.provision :shell, :inline => 'sudo echo "Acquire::http {{ Proxy \\"{0}\\"; }};" > /etc/apt/apt.conf.d/02proxy && sudo apt-get update'
157 """.format(config['aptproxy'])
158 vagrantfile += """
159   config.vm.provision :chef_solo do |chef|
160     chef.cookbooks_path = "cookbooks"
161     chef.log_level = :debug
162     chef.json = {
163       :settings => {
164         :sdk_loc => "/home/vagrant/android-sdk",
165         :ndk_loc => "/home/vagrant/android-ndk",
166         :user => "vagrant"
167       }
168     }
169     chef.add_recipe "fdroidbuild-general"
170     chef.add_recipe "android-sdk"
171     chef.add_recipe "android-ndk"
172     chef.add_recipe "gradle"
173     chef.add_recipe "kivy"
174   end
175 end
176 """
177
178 # Check against the existing Vagrantfile, and if they differ, we need to
179 # create a new box:
180 vf = os.path.join(serverdir, 'Vagrantfile')
181 writevf = True
182 if os.path.exists(vf):
183     vagrant(['halt'], serverdir)
184     with open(vf, 'r') as f:
185         oldvf = f.read()
186     if oldvf != vagrantfile:
187         print "Server configuration has changed, rebuild from scratch is required"
188         vagrant(['destroy', '-f'], serverdir)
189     else:
190         print "Re-provisioning existing server"
191         writevf = False
192 else:
193     print "No existing server - building from scratch"
194 if writevf:
195     with open(vf, 'w') as f:
196         f.write(vagrantfile)
197
198
199 print "Configuring build server VM"
200 returncode, out = vagrant(['up'], serverdir, printout=True)
201 with open(os.path.join(serverdir, 'up.log'), 'w') as log:
202     log.write(out)
203 if returncode != 0:
204     print "Failed to configure server"
205     sys.exit(1)
206
207 print "Writing buildserver ID"
208 p = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE)
209 buildserverid = p.communicate()[0].strip()
210 print "...ID is " + buildserverid
211 subprocess.call(
212     ['vagrant', 'ssh', '-c', 'sh -c "echo {0} >/home/vagrant/buildserverid"'
213     .format(buildserverid)],
214     cwd=serverdir)
215
216 print "Stopping build server VM"
217 vagrant(['halt'], serverdir)
218
219 print "Waiting for build server VM to be finished"
220 ready = False
221 while not ready:
222     time.sleep(2)
223     returncode, out = vagrant(['status'], serverdir)
224     if returncode != 0:
225         print "Error while checking status"
226         sys.exit(1)
227     for line in out.splitlines():
228         if line.startswith("default"):
229             if line.find("poweroff") != -1:
230                 ready = True
231             else:
232                 print "Status: " + line
233
234 print "Packaging"
235 vagrant(['package', '--output', os.path.join('..', boxfile)], serverdir,
236         printout=options.verbose)
237 print "Adding box"
238 vagrant(['box', 'add', 'buildserver', boxfile, '-f'],
239         printout=options.verbose)
240
241 os.remove(boxfile)