chiark / gitweb /
Better makebuildserver verbose output
[fdroidserver.git] / makebuildserver
1 #!/usr/bin/env python2
2
3 import os
4 import sys
5 import subprocess
6 import time
7 from optparse import OptionParser
8
9 def vagrant(params, cwd=None, printout=False):
10     """Run vagrant.
11
12     :param: list of parameters to pass to vagrant
13     :cwd: directory to run in, or None for current directory
14     :printout: True to print output in realtime, False to just
15                return it
16     :returns: (ret, out) where ret is the return code, and out
17                is the stdout (and stderr) from vagrant
18     """
19     p = subprocess.Popen(['vagrant'] + params, cwd=cwd,
20             stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
21     out = ''
22     if printout:
23         while True:
24             line = p.stdout.readline()
25             if len(line) == 0:
26                 break
27             print line,
28             out += line
29         p.wait()
30     else:
31         out = p.communicate()[0]
32     return (p.returncode, out)
33
34 boxfile = 'buildserver.box'
35 serverdir = 'buildserver'
36
37 parser = OptionParser()
38 parser.add_option("-v", "--verbose", action="store_true", default=False,
39                       help="Spew out even more information than normal")
40 parser.add_option("-c", "--clean", action="store_true", default=False,
41                       help="Build from scratch, rather than attempting to update the existing server")
42 options, args = parser.parse_args()
43
44 config = {}
45 execfile('makebs.config.py', config)
46
47 if not os.path.exists('makebuildserver') or not os.path.exists(serverdir):
48     print 'This must be run from the correct directory!'
49     sys.exit(1)
50
51 if os.path.exists(boxfile):
52     os.remove(boxfile)
53
54 if options.clean:
55     vagrant(['destroy', '-f'], cwd=serverdir, printout=options.verbose)
56
57 # Update cached files.
58 cachedir = os.path.join('buildserver', 'cache')
59 if not os.path.exists(cachedir):
60     os.mkdir(cachedir)
61 cachefiles = [
62     ('android-sdk_r22.3-linux.tgz',
63      'http://dl.google.com/android/android-sdk_r22.3-linux.tgz',
64      '4077575c98075480e0156c10e48a1521e31c7952768271a206870e6813057f4f'),
65     ('gradle-1.4-bin.zip',
66      'http://services.gradle.org/distributions/gradle-1.4-bin.zip',
67      'cd99e85fbcd0ae8b99e81c9992a2f10cceb7b5f009c3720ef3a0078f4f92e94e'),
68     ('gradle-1.6-bin.zip',
69      'http://services.gradle.org/distributions/gradle-1.6-bin.zip',
70      'de3e89d2113923dcc2e0def62d69be0947ceac910abd38b75ec333230183fac4'),
71     ('gradle-1.7-bin.zip',
72      'http://services.gradle.org/distributions/gradle-1.7-bin.zip',
73      '360c97d51621b5a1ecf66748c718594e5f790ae4fbc1499543e0c006033c9d30'),
74     ('gradle-1.8-bin.zip',
75      'http://services.gradle.org/distributions/gradle-1.8-bin.zip',
76      'a342bbfa15fd18e2482287da4959588f45a41b60910970a16e6d97959aea5703'),
77     ('gradle-1.9-bin.zip',
78      'http://services.gradle.org/distributions/gradle-1.9-bin.zip',
79      '097ddc2bcbc9da2bb08cbf6bf8079585e35ad088bafd42e8716bc96405db98e9'),
80     ('Kivy-1.7.2.tar.gz',
81      'http://pypi.python.org/packages/source/K/Kivy/Kivy-1.7.2.tar.gz',
82      '0485e2ef97b5086df886eb01f8303cb542183d2d71a159466f99ad6c8a1d03f1')
83     ]
84 if config['arch64']:
85     cachefiles.extend([
86     ('android-ndk-r9b-linux-x86_64.tar.bz2',
87      'http://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64.tar.bz2',
88      '8956e9efeea95f49425ded8bb697013b66e162b064b0f66b5c75628f76e0f532'),
89     ('android-ndk-r9b-linux-x86_64-legacy-toolchains.tar.bz2',
90      'http://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64-legacy-toolchains.tar.bz2',
91      'de93a394f7c8f3436db44568648f87738a8d09801a52f459dcad3fc047e045a1')])
92 else:
93     cachefiles.extend([
94     ('android-ndk-r9b-linux-x86.tar.bz2',
95      'http://dl.google.com/android/ndk/android-ndk-r9b-linux-x86.tar.bz2',
96      '748104b829dd12afb2fdb3044634963abb24cdb0aad3b26030abe2e9e65bfc81'),
97     ('android-ndk-r9b-linux-x86-legacy-toolchains.tar.bz2',
98      'http://dl.google.com/android/ndk/android-ndk-r9b-linux-x86-legacy-toolchains.tar.bz2',
99      '606aadf815ae28cc7b0154996247c70d609f111b14e44bcbcd6cad4c87fefb6f')])
100 wanted = []
101 for f, src, shasum in cachefiles:
102     if not os.path.exists(os.path.join(cachedir, f)):
103         print "Downloading " + f + " to cache"
104         if subprocess.call(['wget', src], cwd=cachedir) != 0:
105             print "...download of " + f + " failed."
106             sys.exit(1)
107     if shasum:
108         p = subprocess.Popen(['shasum', '-a', '256', os.path.join(cachedir, f)],
109                 stdout=subprocess.PIPE)
110         v = p.communicate()[0].split(' ')[0]
111         if v != shasum:
112             print "Invalid shasum of '" + v + "' detected for " + f
113             sys.exit(1)
114         else:
115             print "...shasum verified for " + f
116
117     wanted.append(f)
118
119
120 # Generate an appropriate Vagrantfile for the buildserver, based on our
121 # settings...
122 vagrantfile = """
123 Vagrant::Config.run do |config|
124
125   config.vm.box = "{0}"
126   config.vm.box_url = "{1}"
127
128   config.vm.customize ["modifyvm", :id, "--memory", "{2}"]
129
130   config.vm.provision :shell, :path => "fixpaths.sh"
131 """.format(config['basebox'], config['baseboxurl'], config['memory'])
132 if 'aptproxy' in config and config['aptproxy']:
133     vagrantfile += """
134   config.vm.provision :shell, :inline => 'sudo echo "Acquire::http {{ Proxy \\"{0}\\"; }};" > /etc/apt/apt.conf.d/02proxy && sudo apt-get update'
135 """.format(config['aptproxy'])
136 vagrantfile += """
137   config.vm.provision :chef_solo do |chef|
138     chef.cookbooks_path = "cookbooks"
139     chef.log_level = :debug
140     chef.json = {
141       :settings => {
142         :sdk_loc => "/home/vagrant/android-sdk",
143         :ndk_loc => "/home/vagrant/android-ndk",
144         :user => "vagrant"
145       }
146     }
147     chef.add_recipe "fdroidbuild-general"
148     chef.add_recipe "android-sdk"
149     chef.add_recipe "android-ndk"
150     chef.add_recipe "kivy"
151   end
152 end
153 """
154
155 # Check against the existing Vagrantfile, and if they differ, we need to
156 # create a new box:
157 vf = os.path.join(serverdir, 'Vagrantfile')
158 writevf = True
159 if os.path.exists(vf):
160     vagrant(['halt'], serverdir)
161     with open(vf, 'r') as f:
162         oldvf = f.read()
163     if oldvf != vagrantfile:
164         print "Server configuration has changed, rebuild from scratch is required"
165         vagrant(['destroy', '-f'], serverdir)
166     else:
167         print "Re-provisioning existing server"
168         writevf = False
169 else:
170     print "No existing server - building from scratch"
171 if writevf:
172     with open(vf, 'w') as f:
173         f.write(vagrantfile)
174
175
176 print "Configuring build server VM"
177 returncode, out = vagrant(['up'], serverdir, printout=True)
178 with open(os.path.join(serverdir, 'up.log'), 'w') as log:
179     log.write(out)
180 if returncode != 0:
181     print "Failed to configure server"
182     sys.exit(1)
183
184 print "Writing buildserver ID"
185 p = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE)
186 buildserverid = p.communicate()[0].strip()
187 print "...ID is " + buildserverid
188 subprocess.call(
189     ['vagrant', 'ssh', '-c', 'sh -c "echo {0} >/home/vagrant/buildserverid"'
190     .format(buildserverid)],
191     cwd=serverdir)
192
193 print "Stopping build server VM"
194 vagrant(['halt'], serverdir)
195
196 print "Waiting for build server VM to be finished"
197 ready = False
198 while not ready:
199     time.sleep(2)
200     returncode, out = vagrant(['status'], serverdir)
201     if returncode != 0:
202         print "Error while checking status"
203         sys.exit(1)
204     for line in out.splitlines():
205         if line.startswith("default"):
206             if line.find("poweroff") != -1:
207                 ready = True
208             else:
209                 print "Status: " + line
210
211 print "Packaging"
212 vagrant(['package', '--output', os.path.join('..', boxfile)], serverdir,
213         printout=options.verbose)
214 print "Adding box"
215 vagrant(['box', 'add', 'buildserver', boxfile, '-f'],
216         printout=options.verbose)
217
218 os.remove(boxfile)
219