2013-10-31 13:25:39 +01:00
|
|
|
#!/usr/bin/env python2
|
2012-09-24 15:04:58 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import time
|
2014-01-10 19:54:53 +01:00
|
|
|
import hashlib
|
2013-10-20 22:16:42 +02:00
|
|
|
from optparse import OptionParser
|
2013-05-31 08:48:39 +02:00
|
|
|
|
2014-05-07 16:13:22 +02:00
|
|
|
|
2013-05-31 08:48:39 +02:00
|
|
|
def vagrant(params, cwd=None, printout=False):
|
|
|
|
"""Run vagrant.
|
|
|
|
|
|
|
|
:param: list of parameters to pass to vagrant
|
|
|
|
:cwd: directory to run in, or None for current directory
|
|
|
|
:printout: True to print output in realtime, False to just
|
|
|
|
return it
|
|
|
|
:returns: (ret, out) where ret is the return code, and out
|
|
|
|
is the stdout (and stderr) from vagrant
|
|
|
|
"""
|
2012-09-24 15:04:58 +02:00
|
|
|
p = subprocess.Popen(['vagrant'] + params, cwd=cwd,
|
2014-05-07 16:13:22 +02:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2013-05-31 08:48:39 +02:00
|
|
|
out = ''
|
|
|
|
if printout:
|
|
|
|
while True:
|
|
|
|
line = p.stdout.readline()
|
|
|
|
if len(line) == 0:
|
|
|
|
break
|
|
|
|
print line,
|
|
|
|
out += line
|
|
|
|
p.wait()
|
|
|
|
else:
|
|
|
|
out = p.communicate()[0]
|
|
|
|
return (p.returncode, out)
|
2012-09-24 15:04:58 +02:00
|
|
|
|
|
|
|
boxfile = 'buildserver.box'
|
|
|
|
serverdir = 'buildserver'
|
|
|
|
|
2013-10-20 22:16:42 +02:00
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("-v", "--verbose", action="store_true", default=False,
|
2014-05-07 16:13:22 +02:00
|
|
|
help="Spew out even more information than normal")
|
2013-10-20 22:16:42 +02:00
|
|
|
parser.add_option("-c", "--clean", action="store_true", default=False,
|
2014-05-07 16:13:22 +02:00
|
|
|
help="Build from scratch, rather than attempting to update the existing server")
|
2013-10-20 22:16:42 +02:00
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
2013-12-06 16:38:46 +01:00
|
|
|
config = {}
|
|
|
|
execfile('makebs.config.py', config)
|
2013-10-20 22:16:42 +02:00
|
|
|
|
2013-12-30 17:18:45 +01:00
|
|
|
if not os.path.exists('makebuildserver') or not os.path.exists(serverdir):
|
2012-09-24 15:04:58 +02:00
|
|
|
print 'This must be run from the correct directory!'
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if os.path.exists(boxfile):
|
|
|
|
os.remove(boxfile)
|
|
|
|
|
2013-10-20 22:16:42 +02:00
|
|
|
if options.clean:
|
|
|
|
vagrant(['destroy', '-f'], cwd=serverdir, printout=options.verbose)
|
2013-05-25 14:55:16 +02:00
|
|
|
|
|
|
|
# Update cached files.
|
|
|
|
cachedir = os.path.join('buildserver', 'cache')
|
|
|
|
if not os.path.exists(cachedir):
|
|
|
|
os.mkdir(cachedir)
|
2014-05-31 23:00:27 +02:00
|
|
|
|
2013-05-25 14:55:16 +02:00
|
|
|
cachefiles = [
|
2014-07-03 15:36:43 +02:00
|
|
|
('android-sdk_r23.0.2-linux.tgz',
|
|
|
|
'https://dl.google.com/android/android-sdk_r23.0.2-linux.tgz',
|
|
|
|
'a86741fee9140c340b60fe545566db7c0a43a0963f3c7e64d07b4d05ebbe89f4'),
|
2014-01-09 15:44:58 +01:00
|
|
|
('gradle-1.4-bin.zip',
|
2014-05-20 16:40:43 +02:00
|
|
|
'https://services.gradle.org/distributions/gradle-1.4-bin.zip',
|
2014-01-09 15:44:58 +01:00
|
|
|
'cd99e85fbcd0ae8b99e81c9992a2f10cceb7b5f009c3720ef3a0078f4f92e94e'),
|
|
|
|
('gradle-1.6-bin.zip',
|
2014-05-20 16:40:43 +02:00
|
|
|
'https://services.gradle.org/distributions/gradle-1.6-bin.zip',
|
2014-01-09 15:44:58 +01:00
|
|
|
'de3e89d2113923dcc2e0def62d69be0947ceac910abd38b75ec333230183fac4'),
|
|
|
|
('gradle-1.7-bin.zip',
|
2014-05-20 16:40:43 +02:00
|
|
|
'https://services.gradle.org/distributions/gradle-1.7-bin.zip',
|
2014-01-09 15:44:58 +01:00
|
|
|
'360c97d51621b5a1ecf66748c718594e5f790ae4fbc1499543e0c006033c9d30'),
|
|
|
|
('gradle-1.8-bin.zip',
|
2014-05-20 16:40:43 +02:00
|
|
|
'https://services.gradle.org/distributions/gradle-1.8-bin.zip',
|
2014-01-09 15:44:58 +01:00
|
|
|
'a342bbfa15fd18e2482287da4959588f45a41b60910970a16e6d97959aea5703'),
|
2013-12-22 13:22:45 +01:00
|
|
|
('gradle-1.9-bin.zip',
|
2014-05-20 16:40:43 +02:00
|
|
|
'https://services.gradle.org/distributions/gradle-1.9-bin.zip',
|
2013-12-22 13:22:45 +01:00
|
|
|
'097ddc2bcbc9da2bb08cbf6bf8079585e35ad088bafd42e8716bc96405db98e9'),
|
2014-01-27 13:21:44 +01:00
|
|
|
('gradle-1.10-bin.zip',
|
2014-05-20 16:40:43 +02:00
|
|
|
'https://services.gradle.org/distributions/gradle-1.10-bin.zip',
|
2014-01-27 13:21:44 +01:00
|
|
|
'6e6db4fc595f27ceda059d23693b6f6848583950606112b37dfd0e97a0a0a4fe'),
|
2014-03-07 15:21:18 +01:00
|
|
|
('gradle-1.11-bin.zip',
|
2014-05-20 16:40:43 +02:00
|
|
|
'https://services.gradle.org/distributions/gradle-1.11-bin.zip',
|
2014-03-07 15:21:18 +01:00
|
|
|
'07e235df824964f0e19e73ea2327ce345c44bcd06d44a0123d29ab287fc34091'),
|
2014-05-20 16:40:43 +02:00
|
|
|
('gradle-1.12-bin.zip',
|
|
|
|
'https://services.gradle.org/distributions/gradle-1.12-bin.zip',
|
|
|
|
'8734b13a401f4311ee418173ed6ca8662d2b0a535be8ff2a43ecb1c13cd406ea'),
|
2013-11-24 23:11:02 +01:00
|
|
|
('Kivy-1.7.2.tar.gz',
|
2014-01-31 02:33:37 +01:00
|
|
|
'https://pypi.python.org/packages/source/K/Kivy/Kivy-1.7.2.tar.gz',
|
2013-11-24 23:11:02 +01:00
|
|
|
'0485e2ef97b5086df886eb01f8303cb542183d2d71a159466f99ad6c8a1d03f1')
|
|
|
|
]
|
2014-05-31 23:00:27 +02:00
|
|
|
|
2013-11-04 10:22:22 +01:00
|
|
|
if config['arch64']:
|
2013-05-31 08:48:39 +02:00
|
|
|
cachefiles.extend([
|
2014-05-31 23:00:27 +02:00
|
|
|
('android-ndk-r9b-linux-x86_64.tar.bz2',
|
|
|
|
'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64.tar.bz2',
|
|
|
|
'8956e9efeea95f49425ded8bb697013b66e162b064b0f66b5c75628f76e0f532'),
|
|
|
|
('android-ndk-r9b-linux-x86_64-legacy-toolchains.tar.bz2',
|
|
|
|
'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64-legacy-toolchains.tar.bz2',
|
|
|
|
'de93a394f7c8f3436db44568648f87738a8d09801a52f459dcad3fc047e045a1')])
|
2013-05-31 08:48:39 +02:00
|
|
|
else:
|
|
|
|
cachefiles.extend([
|
2014-05-31 23:00:27 +02:00
|
|
|
('android-ndk-r9b-linux-x86.tar.bz2',
|
|
|
|
'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86.tar.bz2',
|
|
|
|
'748104b829dd12afb2fdb3044634963abb24cdb0aad3b26030abe2e9e65bfc81'),
|
|
|
|
('android-ndk-r9b-linux-x86-legacy-toolchains.tar.bz2',
|
|
|
|
'https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86-legacy-toolchains.tar.bz2',
|
|
|
|
'606aadf815ae28cc7b0154996247c70d609f111b14e44bcbcd6cad4c87fefb6f')])
|
2013-05-25 14:55:16 +02:00
|
|
|
wanted = []
|
2014-01-10 19:54:53 +01:00
|
|
|
|
2014-05-07 16:13:22 +02:00
|
|
|
|
2014-01-10 19:54:53 +01:00
|
|
|
def sha256_for_file(path):
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
s = hashlib.sha256()
|
|
|
|
while True:
|
|
|
|
data = f.read(4096)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
s.update(data)
|
|
|
|
return s.hexdigest()
|
|
|
|
|
2013-08-09 20:37:13 +02:00
|
|
|
for f, src, shasum in cachefiles:
|
2014-01-10 19:54:53 +01:00
|
|
|
relpath = os.path.join(cachedir, f)
|
|
|
|
if not os.path.exists(relpath):
|
2013-05-25 14:55:16 +02:00
|
|
|
print "Downloading " + f + " to cache"
|
2013-05-31 08:48:39 +02:00
|
|
|
if subprocess.call(['wget', src], cwd=cachedir) != 0:
|
2013-05-25 14:55:16 +02:00
|
|
|
print "...download of " + f + " failed."
|
|
|
|
sys.exit(1)
|
2013-08-09 20:37:13 +02:00
|
|
|
if shasum:
|
2014-01-10 19:54:53 +01:00
|
|
|
v = sha256_for_file(relpath)
|
2013-08-09 20:37:13 +02:00
|
|
|
if v != shasum:
|
|
|
|
print "Invalid shasum of '" + v + "' detected for " + f
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
print "...shasum verified for " + f
|
|
|
|
|
2013-05-25 14:55:16 +02:00
|
|
|
wanted.append(f)
|
|
|
|
|
|
|
|
|
2013-05-31 08:48:39 +02:00
|
|
|
# Generate an appropriate Vagrantfile for the buildserver, based on our
|
|
|
|
# settings...
|
|
|
|
vagrantfile = """
|
|
|
|
Vagrant::Config.run do |config|
|
|
|
|
|
2014-08-27 16:40:36 +02:00
|
|
|
if Vagrant.has_plugin?("vagrant-cachier")
|
|
|
|
config.cache.scope = :box
|
|
|
|
end
|
|
|
|
|
2013-05-31 08:48:39 +02:00
|
|
|
config.vm.box = "{0}"
|
|
|
|
config.vm.box_url = "{1}"
|
|
|
|
|
|
|
|
config.vm.customize ["modifyvm", :id, "--memory", "{2}"]
|
|
|
|
|
|
|
|
config.vm.provision :shell, :path => "fixpaths.sh"
|
2013-11-04 10:22:22 +01:00
|
|
|
""".format(config['basebox'], config['baseboxurl'], config['memory'])
|
|
|
|
if 'aptproxy' in config and config['aptproxy']:
|
2013-05-31 08:48:39 +02:00
|
|
|
vagrantfile += """
|
|
|
|
config.vm.provision :shell, :inline => 'sudo echo "Acquire::http {{ Proxy \\"{0}\\"; }};" > /etc/apt/apt.conf.d/02proxy && sudo apt-get update'
|
2013-11-04 10:22:22 +01:00
|
|
|
""".format(config['aptproxy'])
|
2013-05-31 08:48:39 +02:00
|
|
|
vagrantfile += """
|
|
|
|
config.vm.provision :chef_solo do |chef|
|
|
|
|
chef.cookbooks_path = "cookbooks"
|
2013-12-30 17:04:16 +01:00
|
|
|
chef.log_level = :debug
|
2013-05-31 08:48:39 +02:00
|
|
|
chef.json = {
|
|
|
|
:settings => {
|
|
|
|
:sdk_loc => "/home/vagrant/android-sdk",
|
|
|
|
:ndk_loc => "/home/vagrant/android-ndk",
|
|
|
|
:user => "vagrant"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chef.add_recipe "fdroidbuild-general"
|
|
|
|
chef.add_recipe "android-sdk"
|
|
|
|
chef.add_recipe "android-ndk"
|
2014-01-09 18:30:35 +01:00
|
|
|
chef.add_recipe "gradle"
|
2013-11-24 23:11:02 +01:00
|
|
|
chef.add_recipe "kivy"
|
2013-05-31 08:48:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Check against the existing Vagrantfile, and if they differ, we need to
|
|
|
|
# create a new box:
|
|
|
|
vf = os.path.join(serverdir, 'Vagrantfile')
|
|
|
|
writevf = True
|
|
|
|
if os.path.exists(vf):
|
|
|
|
vagrant(['halt'], serverdir)
|
|
|
|
with open(vf, 'r') as f:
|
|
|
|
oldvf = f.read()
|
|
|
|
if oldvf != vagrantfile:
|
|
|
|
print "Server configuration has changed, rebuild from scratch is required"
|
|
|
|
vagrant(['destroy', '-f'], serverdir)
|
|
|
|
else:
|
|
|
|
print "Re-provisioning existing server"
|
|
|
|
writevf = False
|
|
|
|
else:
|
|
|
|
print "No existing server - building from scratch"
|
|
|
|
if writevf:
|
|
|
|
with open(vf, 'w') as f:
|
|
|
|
f.write(vagrantfile)
|
|
|
|
|
|
|
|
|
2012-09-24 15:04:58 +02:00
|
|
|
print "Configuring build server VM"
|
2013-05-31 08:48:39 +02:00
|
|
|
returncode, out = vagrant(['up'], serverdir, printout=True)
|
2013-02-12 16:20:30 +01:00
|
|
|
with open(os.path.join(serverdir, 'up.log'), 'w') as log:
|
2013-05-31 08:48:39 +02:00
|
|
|
log.write(out)
|
2013-01-21 12:09:56 +01:00
|
|
|
if returncode != 0:
|
|
|
|
print "Failed to configure server"
|
2013-04-10 10:36:31 +02:00
|
|
|
sys.exit(1)
|
2013-11-09 12:51:39 +01:00
|
|
|
|
|
|
|
print "Writing buildserver ID"
|
|
|
|
p = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE)
|
|
|
|
buildserverid = p.communicate()[0].strip()
|
|
|
|
print "...ID is " + buildserverid
|
|
|
|
subprocess.call(
|
|
|
|
['vagrant', 'ssh', '-c', 'sh -c "echo {0} >/home/vagrant/buildserverid"'
|
2014-05-31 23:00:27 +02:00
|
|
|
.format(buildserverid)],
|
2013-11-09 12:51:39 +01:00
|
|
|
cwd=serverdir)
|
|
|
|
|
2012-09-24 15:04:58 +02:00
|
|
|
print "Stopping build server VM"
|
|
|
|
vagrant(['halt'], serverdir)
|
|
|
|
|
|
|
|
print "Waiting for build server VM to be finished"
|
|
|
|
ready = False
|
|
|
|
while not ready:
|
|
|
|
time.sleep(2)
|
2013-05-31 08:48:39 +02:00
|
|
|
returncode, out = vagrant(['status'], serverdir)
|
2012-09-24 15:04:58 +02:00
|
|
|
if returncode != 0:
|
|
|
|
print "Error while checking status"
|
|
|
|
sys.exit(1)
|
|
|
|
for line in out.splitlines():
|
|
|
|
if line.startswith("default"):
|
|
|
|
if line.find("poweroff") != -1:
|
|
|
|
ready = True
|
|
|
|
else:
|
|
|
|
print "Status: " + line
|
|
|
|
|
|
|
|
print "Packaging"
|
2014-01-09 22:31:58 +01:00
|
|
|
vagrant(['package', '--output', os.path.join('..', boxfile)], serverdir,
|
|
|
|
printout=options.verbose)
|
2012-09-24 15:04:58 +02:00
|
|
|
print "Adding box"
|
2014-01-09 22:31:58 +01:00
|
|
|
vagrant(['box', 'add', 'buildserver', boxfile, '-f'],
|
|
|
|
printout=options.verbose)
|
2012-09-24 15:04:58 +02:00
|
|
|
|
2014-01-09 22:31:58 +01:00
|
|
|
os.remove(boxfile)
|