chiark / gitweb /
buildserver: replace custom code with python-vagrant
[fdroidserver.git] / makebuildserver
index 69fc934616a371e6531dbad64aee33987604fd0e..4c196ff040e4c995d526b45a74f2f6fe27aada00 100755 (executable)
@@ -6,7 +6,7 @@ import requests
 import stat
 import sys
 import subprocess
-import time
+import vagrant
 import hashlib
 import yaml
 from clint.textui import progress
@@ -18,34 +18,7 @@ if not os.path.exists('makebuildserver') and not os.path.exists('buildserver'):
     sys.exit(1)
 
 
-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
-    """
-    p = subprocess.Popen(['vagrant'] + params, cwd=cwd,
-                         stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
-                         universal_newlines=True)
-    out = ''
-    if printout:
-        while True:
-            line = p.stdout.readline()
-            if len(line) == 0:
-                break
-            print(line.rstrip())
-            out += line
-        p.wait()
-    else:
-        out = p.communicate()[0]
-    return (p.returncode, out)
-
-
-boxfile = 'buildserver.box'
+boxfile = os.path.join(os.getcwd(), 'buildserver.box')
 serverdir = 'buildserver'
 
 parser = OptionParser()
@@ -96,12 +69,6 @@ if '__builtins__' in config:
 if os.path.exists(boxfile):
     os.remove(boxfile)
 
-if options.clean:
-    vagrant(['destroy', '-f'], cwd=serverdir, printout=options.verbose)
-    if config['vm_provider'] == 'libvirt':
-        subprocess.call(['virsh', 'undefine', 'buildserver_default'])
-        subprocess.call(['virsh', 'vol-delete', '/var/lib/libvirt/images/buildserver_default.img'])
-
 # Update cached files.
 cachedir = config['cachedir']
 if not os.path.exists(cachedir):
@@ -121,10 +88,10 @@ if config['apt_package_cache']:
     config['aptcachedir'] = cachedir + '/apt/archives'
 
 cachefiles = [
-    ('https://dl.google.com/android/repository/tools_r25.2.2-linux.zip',
-     '10d5185af8fee80a7ee093585bf84962d0a877b92a8fef489e2030e910f6a360'),
-    ('https://dl.google.com/android/repository/android_m2repository_r40.zip',
-     'be4c20cdecba6186d046b4a499895a9a8be1955427dc3f3e7495fa9dcc392d29'),
+    ('https://dl.google.com/android/repository/tools_r25.2.3-linux.zip',
+     '1b35bcb94e9a686dff6460c8bca903aa0281c6696001067f34ec00093145b560'),
+    ('https://dl.google.com/android/repository/android_m2repository_r47.zip',
+     'a3f91808dce50c1717737de90c18479ed3a78b147e06985247d138e7ab5123d0'),
     ('https://dl.google.com/android/repository/android-1.5_r04-linux.zip',
      '85b6c8f9797e56aa415d3a282428bb640c96b0acb17c11d41621bb2a5302fe64'),
     ('https://dl.google.com/android/repository/android-1.6_r03-linux.zip',
@@ -225,6 +192,10 @@ cachefiles = [
      'f2c02eb1d7e41ce314b5dac50440e7595380c4dd45b41ea1d7b0f86e49516927'),
     ('https://dl.google.com/android/repository/build-tools_r25-linux.zip',
      '74eb6931fd7a56859bd8e35d8d73ca8fe7ba6bfd4b7ffe560fe58b7354f2e3aa'),
+    ('https://dl.google.com/android/repository/build-tools_r25.0.1-linux.zip',
+     '671b4e00f5b986c7355507c7024b725a4b4cadf11ca61fa5b1334ec6ea57d94f'),
+    ('https://dl.google.com/android/repository/build-tools_r25.0.2-linux.zip',
+     '1d7ac9b6def16fb0254ec23c135c02dd9f6908073352a20315a017e4b2a904b0'),
     # the binaries that Google uses are here:
     # https://android.googlesource.com/platform/tools/external/gradle/+/studio-1.5/
     ('https://services.gradle.org/distributions/gradle-1.4-bin.zip',
@@ -281,6 +252,16 @@ cachefiles = [
      'c7de3442432253525902f7e8d7eac8b5fd6ce1623f96d76916af6d0e383010fc'),
     ('https://services.gradle.org/distributions/gradle-3.2-bin.zip',
      '5321b36837226dc0377047a328f12010f42c7bf88ee4a3b1cee0c11040082935'),
+    ('https://services.gradle.org/distributions/gradle-3.2.1-bin.zip',
+     '9843a3654d3e57dce54db06d05f18b664b95c22bf90c6becccb61fc63ce60689'),
+    ('https://services.gradle.org/distributions/gradle-3.3-bin.zip',
+     'c58650c278d8cf0696cab65108ae3c8d95eea9c1938e0eb8b997095d5ca9a292'),
+    ('https://services.gradle.org/distributions/gradle-3.4-bin.zip',
+     '72d0cd4dcdd5e3be165eb7cd7bbd25cf8968baf400323d9ab1bba622c3f72205'),
+    ('https://services.gradle.org/distributions/gradle-3.4.1-bin.zip',
+     'db1db193d479cc1202be843f17e4526660cfb0b21b57d62f3a87f88c878af9b2'),
+    ('https://services.gradle.org/distributions/gradle-3.5-bin.zip',
+     '0b7450798c190ff76b9f9a3d02e18b33d94553f708ebc08ebe09bdf99111d110'),
     ('https://dl.google.com/android/ndk/android-ndk-r10e-linux-x86_64.bin',
      '102d6723f67ff1384330d12c45854315d6452d6510286f4e5891e00a5a8f1d5a'),
     ('https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64.tar.bz2',
@@ -291,6 +272,10 @@ cachefiles = [
      'ba85dbe4d370e4de567222f73a3e034d85fc3011b3cbd90697f3e8dcace3ad94'),
     ('https://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip',
      'eafae2d614e5475a3bcfd7c5f201db5b963cc1290ee3e8ae791ff0c66757781e'),
+    ('https://dl.google.com/android/repository/android-ndk-r13b-linux-x86_64.zip',
+     '3524d7f8fca6dc0d8e7073a7ab7f76888780a22841a6641927123146c3ffd29c'),
+    ('https://dl.google.com/android/repository/android-ndk-r14-linux-x86_64.zip',
+     '3e622c2c9943964ea44cd56317d0769ed4c811bb4b40dc45b1f6965e4db9aa44'),
     ('https://download.qt.io/official_releases/qt/5.7/5.7.0/qt-opensource-linux-x64-android-5.7.0.run',
      'f7e55b7970e59bdaabb88cb7afc12e9061e933992bda2f076f52600358644586'),
 ]
@@ -376,18 +361,28 @@ elif os.path.exists('/proc/cpuinfo'):
     if 'vmx' in contents or 'svm' in contents:
         config['hwvirtex'] = 'on'
 
+logfilename = os.path.join(serverdir, 'up.log')
+log_cm = vagrant.make_file_cm(logfilename)
+v = vagrant.Vagrant(root=serverdir, out_cm=log_cm, err_cm=log_cm)
+
+if options.clean:
+    v.destroy()
+    if config['vm_provider'] == 'libvirt':
+        subprocess.call(['virsh', 'undefine', 'buildserver_default'])
+        subprocess.call(['virsh', 'vol-delete', '/var/lib/libvirt/images/buildserver_default.img'])
+
 # Check against the existing Vagrantfile.yaml, and if they differ, we
 # need to create a new box:
 vf = os.path.join(serverdir, 'Vagrantfile.yaml')
 writevf = True
 if os.path.exists(vf):
     print('Halting', serverdir)
-    vagrant(['halt'], serverdir)
+    v.halt()
     with open(vf, 'r', encoding='utf-8') as f:
         oldconfig = yaml.load(f)
     if config != oldconfig:
         print("Server configuration has changed, rebuild from scratch is required")
-        vagrant(['destroy', '-f'], serverdir)
+        v.destroy()
     else:
         print("Re-provisioning existing server")
         writevf = False
@@ -398,13 +393,12 @@ if writevf:
         yaml.dump(config, f)
 
 if config['vm_provider'] == 'libvirt':
-    returncode, out = vagrant(['box', 'list'], serverdir, printout=options.verbose)
     found_basebox = False
     needs_mutate = False
-    for line in out.splitlines():
-        if line.startswith(config['basebox']):
+    for box in v.box_list():
+        if box.name == config['basebox']:
             found_basebox = True
-            if line.split('(')[1].split(',')[0] != 'libvirt':
+            if box.provider != 'libvirt':
                 needs_mutate = True
             continue
     if not found_basebox:
@@ -413,24 +407,16 @@ if config['vm_provider'] == 'libvirt':
         else:
             baseboxurl = config['baseboxurl'][0]
         print('Adding', config['basebox'], 'from', baseboxurl)
-        vagrant(['box', 'add', '--name', config['basebox'], baseboxurl],
-                serverdir, printout=options.verbose)
+        v.box_add(config['basebox'], baseboxurl)
         needs_mutate = True
     if needs_mutate:
         print('Converting', config['basebox'], 'to libvirt format')
-        vagrant(['mutate', config['basebox'], 'libvirt'],
-                serverdir, printout=options.verbose)
+        v._call_vagrant_command(['mutate', config['basebox'], 'libvirt'])
         print('Removing virtualbox format copy of', config['basebox'])
-        vagrant(['box', 'remove', '--provider', 'virtualbox', config['basebox']],
-                serverdir, printout=options.verbose)
+        v.box_remove(config['basebox'], 'virtualbox')
 
 print("Configuring build server VM")
-returncode, out = vagrant(['up', '--provision'], serverdir, printout=True)
-with open(os.path.join(serverdir, 'up.log'), 'w') as log:
-    log.write(out)
-if returncode != 0:
-    print("Failed to configure server")
-    sys.exit(1)
+v.up(provision=True)
 
 print("Writing buildserver ID")
 p = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE,
@@ -443,28 +429,12 @@ subprocess.call(
     cwd=serverdir)
 
 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)
-    returncode, out = vagrant(['status'], serverdir)
-    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)
+v.halt()
 
 print("Packaging")
-vagrant(['package', '--output', os.path.join('..', boxfile)], serverdir,
-        printout=options.verbose)
+v.package(output=boxfile)
+
 print("Adding box")
-vagrant(['box', 'add', 'buildserver', boxfile, '-f'],
-        printout=options.verbose)
+v.box_add('buildserver', boxfile, force=True)
 
 os.remove(boxfile)