2016-01-04 16:33:20 +01:00
#!/usr/bin/env python3
2012-09-24 15:04:58 +02:00
import os
2016-06-23 17:12:06 +02:00
import re
2016-06-10 11:42:42 +02:00
import requests
2016-09-06 14:22:29 +02:00
import stat
2012-09-24 15:04:58 +02:00
import sys
2017-04-09 00:57:59 +02:00
import shutil
2012-09-24 15:04:58 +02:00
import subprocess
2016-09-27 08:49:32 +02:00
import vagrant
2014-01-10 19:54:53 +01:00
import hashlib
2016-07-04 20:22:00 +02:00
import yaml
2017-05-22 16:57:47 +02:00
import json
2017-03-24 20:04:50 +01:00
import logging
2016-06-10 11:42:42 +02:00
from clint.textui import progress
2013-10-20 22:16:42 +02:00
from optparse import OptionParser
2016-06-20 11:33:46 +02:00
import fdroidserver.tail
2017-03-26 01:41:39 +01:00
import fdroidserver.vmtools
2013-05-31 08:48:39 +02:00
2014-05-07 16:13:22 +02:00
2013-10-20 22:16:42 +02:00
parser = OptionParser()
2017-03-24 20:04:50 +01:00
parser.add_option('-v', '--verbose', action="count", dest='verbosity', default=1,
2014-05-07 16:13:22 +02:00
help="Spew out even more information than normal")
2017-03-24 20:04:50 +01:00
parser.add_option('-q', action='store_const', const=0, dest='verbosity')
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")
2022-10-19 15:03:07 +02:00
parser.add_option('--skip-box-verification', action="store_true", default=False,
help="""Skip verifying the downloaded base box.""")
2017-03-24 20:04:50 +01:00
parser.add_option('--skip-cache-update', action="store_true", default=False,
help="""Skip downloading and checking cache."""
"""This assumes that the cache is already downloaded completely.""")
2022-10-19 10:31:36 +02:00
parser.add_option('--copy-caches-from-host', action="store_true", default=False,
help="""Copy gradle and pip caches into the buildserver VM""")
2017-05-22 17:29:12 +02:00
parser.add_option('--keep-box-file', action="store_true", default=False,
2017-05-22 15:50:32 +02:00
help="""Box file will not be deleted after adding it to box storage"""
""" (KVM-only).""")
2013-10-20 22:16:42 +02:00
options, args = parser.parse_args()
2019-02-20 15:54:32 +01:00
logformat = '%(levelname)s: %(message)s'
loglevel = logging.DEBUG
if options.verbosity == 1:
loglevel = logging.INFO
2017-03-24 20:04:50 +01:00
elif options.verbosity <= 0:
2019-02-20 15:54:32 +01:00
loglevel = logging.WARNING
logging.basicConfig(format=logformat, level=loglevel)
2017-03-24 20:04:50 +01:00
tail = None
2021-09-03 12:18:13 +02:00
BASEBOX_DEFAULT = 'fdroid/bullseye64'
2022-10-10 21:22:32 +02:00
BASEBOX_VERSION_DEFAULT = "11.20221010.1"
2018-09-25 15:47:33 +02:00
BASEBOX_CHECKSUMS = {
2022-10-10 21:22:32 +02:00
"11.20221010.1": {
"libvirt": {
"box.img": "c2114aa276c176fa65b8072f5dcd1e8a6ab9f7d15fd5da791727a0164fd43254",
"Vagrantfile": "f9c6fcbb47a4d0d33eb066859c8e87efd642287a638bd7da69a9e7a6f25fec47",
"metadata.json": "42b96a01106c25f3a222ddad0baead0b811cc64926f924fb836bbfa43580e646",
},
"virtualbox": {
"box.ovf": "5e4de5f1f4b481b2c1917c0b2f6e6334f4741cc18c5b278e3bafb094535ff2cb",
"box.vmdk": "737053bc886037ae76bb38a1776eba2a5579d49423de990e93ef4a3f0cab4f1c",
"Vagrantfile": "0bbc2ae97668d8da27ab97b766752dcd0bf9e41900e21057de15a58ee7fae47d",
"metadata.json": "ffdaa989f2f6932cd8042e1102371f405cc7ad38e324210a1326192e4689e83a",
}
},
2021-09-03 12:18:13 +02:00
'11.20220317.1': {
2021-05-27 15:54:45 +02:00
'libvirt': {
2021-09-03 12:18:13 +02:00
'box.img': 'fbde152a2f61d191983be9d1dbeae2591af32cca1ec27daa342485d97187515e',
'metadata.json': '42b96a01106c25f3a222ddad0baead0b811cc64926f924fb836bbfa43580e646',
'Vagrantfile': 'f9c6fcbb47a4d0d33eb066859c8e87efd642287a638bd7da69a9e7a6f25fec47',
2021-05-27 15:54:45 +02:00
},
'virtualbox': {
2021-09-03 12:18:13 +02:00
'box.ovf': 'becd5cea2666d42e12def13a91766aa0d4b0e8e6f53102486c2a6cdb4e401b08',
'box.vmdk': '49c96a58a3ee99681d348075864a290c60a8d334fddd21be453c825fcee75eda',
'metadata.json': 'ffdaa989f2f6932cd8042e1102371f405cc7ad38e324210a1326192e4689e83a',
'Vagrantfile': '0bbc2ae97668d8da27ab97b766752dcd0bf9e41900e21057de15a58ee7fae47d',
2021-05-27 15:54:45 +02:00
}
},
2018-09-25 15:47:33 +02:00
}
2022-10-19 11:31:38 +02:00
configfile = 'buildserver/Vagrantfile.yaml'
if not os.path.exists(configfile):
logging.warning('%s does not exist, copying template file.' % configfile)
shutil.copy('examples/Vagrantfile.yaml', configfile)
with open(configfile) as fp:
config = yaml.safe_load(fp)
2023-02-06 16:30:15 +01:00
if not isinstance(config, dict):
logging.info("config is empty or not a dict, using default.")
config = {}
2022-10-19 15:03:07 +02:00
with open('buildserver/Vagrantfile') as fp:
m = re.search(r"""\.vm\.box\s*=\s*["'](.*)["']""", fp.read())
if not m:
logging.error('Cannot find box name in buildserver/Vagrantfile!')
2023-03-05 14:38:42 +01:00
sys.exit(1)
2022-10-19 15:03:07 +02:00
config['basebox'] = m.group(1)
config['basebox_version'] = BASEBOX_VERSION_DEFAULT
2022-10-19 11:31:38 +02:00
config['cachedir'] = os.path.join(os.getenv('HOME'), '.cache', 'fdroidserver')
show_config_deprecation = False
2016-01-20 20:38:18 +01:00
if os.path.exists('makebuildserver.config.py'):
2022-10-19 11:31:38 +02:00
show_config_deprecation = True
logging.error('makebuildserver.config.py exists!')
2016-01-20 20:38:18 +01:00
elif os.path.exists('makebs.config.py'):
2022-10-19 11:31:38 +02:00
show_config_deprecation = True
2016-01-20 20:38:18 +01:00
# this is the old name for the config file
2022-10-19 11:31:38 +02:00
logging.error('makebs.config.py exists!')
if show_config_deprecation:
2022-11-04 11:32:59 +01:00
logging.error('Config is via %s and command line flags.' % configfile)
2022-10-19 11:31:38 +02:00
parser.print_help()
2023-03-05 14:38:42 +01:00
sys.exit(1)
2022-10-19 11:31:38 +02:00
2022-11-04 11:32:59 +01:00
logging.debug("Vagrantfile.yaml parsed -> %s", json.dumps(config, indent=4, sort_keys=True))
2013-10-20 22:16:42 +02:00
2013-05-25 14:55:16 +02:00
# Update cached files.
2019-02-20 16:26:57 +01:00
if not os.path.exists(config['cachedir']):
os.makedirs(config['cachedir'], 0o755)
logging.debug('created cachedir {} because it did not exists.'.format(config['cachedir']))
2014-05-31 23:00:27 +02:00
2016-09-23 15:19:15 +02:00
if config['vm_provider'] == 'libvirt':
2019-02-20 16:26:57 +01:00
tmp = config['cachedir']
2016-09-23 15:19:15 +02:00
while tmp != '/':
mode = os.stat(tmp).st_mode
if not (stat.S_IXUSR & mode and stat.S_IXGRP & mode and stat.S_IXOTH & mode):
2019-02-20 15:54:32 +01:00
logging.critical('ERROR: %s will not be accessible to the VM! To fix, run:', tmp)
logging.critical(' chmod a+X %s', tmp)
2016-09-23 15:19:15 +02:00
sys.exit(1)
tmp = os.path.dirname(tmp)
2019-02-20 16:26:57 +01:00
logging.debug('cache dir %s is accessible for libvirt vm.', config['cachedir'])
2016-09-06 14:22:29 +02:00
2019-02-20 15:47:43 +01:00
CACHE_FILES = [
2021-03-01 22:34:31 +01:00
('https://services.gradle.org/distributions/gradle-6.8.3-bin.zip',
'7faa7198769f872826c8ef4f1450f839ec27f0b4d5d1e51bade63667cbccd205'),
2021-05-17 18:59:55 +02:00
('https://services.gradle.org/distributions/gradle-7.0.2-bin.zip',
'0e46229820205440b48a5501122002842b82886e76af35f0f3a069243dca4b3c'),
2014-12-31 16:42:26 +01:00
]
2014-05-31 23:00:27 +02:00
2014-05-07 16:13:22 +02:00
2014-01-10 19:54:53 +01:00
def sha256_for_file(path):
2016-03-13 21:49:38 +01:00
with open(path, 'rb') as f:
2014-01-10 19:54:53 +01:00
s = hashlib.sha256()
while True:
data = f.read(4096)
if not data:
break
s.update(data)
return s.hexdigest()
2017-03-14 20:14:06 +01:00
2018-09-25 15:47:33 +02:00
def verify_file_sha256(path, sha256):
if sha256_for_file(path) != sha256:
2019-02-20 15:54:32 +01:00
logging.critical("File verification for '{path}' failed! "
"expected sha256 checksum: {checksum}"
.format(path=path, checksum=sha256))
2018-09-25 15:47:33 +02:00
sys.exit(1)
else:
2019-02-20 15:54:32 +01:00
logging.debug("sucessfully verifyed file '{path}' "
"('{checksum}')".format(path=path,
checksum=sha256))
2018-09-25 15:47:33 +02:00
def get_vagrant_home():
return os.environ.get('VAGRANT_HOME',
os.path.join(os.path.expanduser('~'),
'.vagrant.d'))
2016-06-23 16:38:39 +02:00
def run_via_vagrant_ssh(v, cmdlist):
if (isinstance(cmdlist, str) or isinstance(cmdlist, bytes)):
cmd = cmdlist
else:
cmd = ' '.join(cmdlist)
v._run_vagrant_command(['ssh', '-c', cmd])
2019-02-20 15:47:43 +01:00
def update_cache(cachedir):
2019-05-27 12:35:57 +02:00
count_files = 0
2019-02-20 15:47:43 +01:00
for srcurl, shasum in CACHE_FILES:
2016-09-25 18:55:29 +02:00
filename = os.path.basename(srcurl)
local_filename = os.path.join(cachedir, filename)
2019-05-27 12:35:57 +02:00
count_files = count_files + 1
2016-09-25 18:55:29 +02:00
if os.path.exists(local_filename):
2019-01-28 22:16:48 +01:00
if sha256_for_file(local_filename) == shasum:
2019-05-27 12:35:57 +02:00
logging.info("\t...shasum verified for '{filename}'\t({filecounter} of {filesum} files)".format(filename=local_filename, filecounter=count_files, filesum=len(CACHE_FILES)))
2019-01-28 22:16:48 +01:00
continue
2016-09-25 18:55:29 +02:00
local_length = os.path.getsize(local_filename)
else:
local_length = -1
resume_header = {}
download = True
try:
r = requests.head(srcurl, allow_redirects=True, timeout=60)
if r.status_code == 200:
content_length = int(r.headers.get('content-length'))
else:
content_length = local_length # skip the download
except requests.exceptions.RequestException as e:
content_length = local_length # skip the download
2019-02-20 15:54:32 +01:00
logging.warn('%s', e)
2016-09-25 18:55:29 +02:00
if local_length == content_length:
download = False
elif local_length > content_length:
2019-02-20 15:54:32 +01:00
logging.info('deleting corrupt file from cache: %s', local_filename)
2016-09-25 18:55:29 +02:00
os.remove(local_filename)
2019-02-20 15:54:32 +01:00
logging.info("Downloading %s to cache", filename)
2016-09-25 18:55:29 +02:00
elif local_length > -1 and local_length < content_length:
2019-02-20 15:54:32 +01:00
logging.info("Resuming download of %s", local_filename)
2016-09-25 18:55:29 +02:00
resume_header = {'Range': 'bytes=%d-%d' % (local_length, content_length)}
else:
2019-02-20 15:54:32 +01:00
logging.info("Downloading %s to cache", filename)
2016-06-10 11:42:42 +02:00
2016-09-25 18:55:29 +02:00
if download:
2019-05-28 12:07:36 +02:00
r = requests.get(srcurl, headers=resume_header, stream=True,
allow_redirects=True, timeout=60)
2016-06-10 11:42:42 +02:00
content_length = int(r.headers.get('content-length'))
2016-09-25 18:55:29 +02:00
with open(local_filename, 'ab') as f:
for chunk in progress.bar(r.iter_content(chunk_size=65536),
expected_size=(content_length / 65536) + 1):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
v = sha256_for_file(local_filename)
if v == shasum:
2019-05-27 12:35:57 +02:00
logging.info("\t...shasum verified for '{filename}'\t({filecounter} of {filesum} files)".format(filename=local_filename, filecounter=count_files, filesum=len(CACHE_FILES)))
2013-08-09 20:37:13 +02:00
else:
2019-02-20 15:54:32 +01:00
logging.critical("Invalid shasum of '%s' detected for %s", v, local_filename)
2016-09-25 18:55:29 +02:00
os.remove(local_filename)
sys.exit(1)
2016-07-04 20:22:00 +02:00
2017-03-24 20:04:50 +01:00
2017-04-13 12:49:14 +02:00
def debug_log_vagrant_vm(vm_dir, config):
2017-03-24 20:04:50 +01:00
if options.verbosity >= 3:
_vagrant_dir = os.path.join(vm_dir, '.vagrant')
2019-02-20 15:54:32 +01:00
logging.debug('check %s dir exists? -> %r', _vagrant_dir, os.path.isdir(_vagrant_dir))
logging.debug('> vagrant status')
2017-03-24 20:04:50 +01:00
subprocess.call(['vagrant', 'status'], cwd=vm_dir)
2019-02-20 15:54:32 +01:00
logging.debug('> vagrant box list')
2017-03-24 20:04:50 +01:00
subprocess.call(['vagrant', 'box', 'list'])
2017-04-13 12:49:14 +02:00
if config['vm_provider'] == 'libvirt':
2019-02-20 15:54:32 +01:00
logging.debug('> virsh -c qmeu:///system list --all')
2017-04-13 12:49:14 +02:00
subprocess.call(['virsh', '-c', 'qemu:///system', 'list', '--all'])
2017-05-23 11:59:53 +02:00
domain = 'buildserver_default'
2019-02-20 15:54:32 +01:00
logging.debug('> virsh -c qemu:///system snapshot-list %s', domain)
2017-05-23 11:59:53 +02:00
subprocess.call(['virsh', '-c', 'qemu:///system', 'snapshot-list', domain])
2017-03-24 20:04:50 +01:00
def main():
2019-02-20 16:26:57 +01:00
global config, tail
2017-03-24 20:04:50 +01:00
if options.skip_cache_update:
2019-02-20 15:54:32 +01:00
logging.info('skipping cache update and verification...')
2017-03-24 20:04:50 +01:00
else:
2019-02-20 16:26:57 +01:00
update_cache(config['cachedir'])
2017-03-24 20:04:50 +01:00
2016-09-25 18:55:29 +02:00
# use VirtualBox software virtualization if hardware is not available,
# like if this is being run in kvm or some other VM platform, like
# http://jenkins.debian.net, the values are 'on' or 'off'
2022-10-19 11:31:38 +02:00
if config.get('hwvirtex') != 'off' and os.path.exists('/proc/cpuinfo'):
2016-09-25 18:55:29 +02:00
with open('/proc/cpuinfo') as f:
contents = f.read()
if 'vmx' in contents or 'svm' in contents:
2022-10-19 11:31:38 +02:00
logging.debug('found \'vmx\' or \'svm\' in /proc/cpuinfo -> hwvirtex = \'on\'')
else:
logging.error('hwvirtex = \'on\' and no \'vmx\' or \'svm\' found in /proc/cpuinfo!')
2023-03-05 14:38:42 +01:00
sys.exit(1)
2016-09-25 18:55:29 +02:00
serverdir = os.path.join(os.getcwd(), 'buildserver')
logfilename = os.path.join(serverdir, 'up.log')
if not os.path.exists(logfilename):
open(logfilename, 'a').close() # create blank file
log_cm = vagrant.make_file_cm(logfilename)
v = vagrant.Vagrant(root=serverdir, out_cm=log_cm, err_cm=log_cm)
2020-01-14 22:51:19 +01:00
# https://phoenhex.re/2018-03-25/not-a-vagrant-bug
os_env = os.environ.copy()
os_env['VAGRANT_DISABLE_VBOXSYMLINKCREATE'] = '1'
v.env = os_env
2016-09-25 18:55:29 +02:00
2017-03-24 20:04:50 +01:00
if options.verbosity >= 2:
2016-09-25 18:55:29 +02:00
tail = fdroidserver.tail.Tail(logfilename)
tail.start()
2017-03-26 03:15:33 +02:00
vm = fdroidserver.vmtools.get_build_vm(serverdir, provider=config['vm_provider'])
2016-09-25 18:55:29 +02:00
if options.clean:
2017-03-26 01:41:39 +01:00
vm.destroy()
2016-09-25 18:55:29 +02:00
2018-07-28 16:48:15 +02:00
# Check if selected provider is supported
if config['vm_provider'] not in ['libvirt', 'virtualbox']:
2019-02-20 15:54:32 +01:00
logging.critical("Currently selected VM provider '{vm_provider}' "
"is not supported. (please choose from: "
"virtualbox, libvirt)"
.format(vm_provider=config['cm_provider']))
2018-07-28 16:48:15 +02:00
sys.exit(1)
2016-09-25 18:55:29 +02:00
2021-09-03 12:18:13 +02:00
# Download and verify pre-built Vagrant boxes
2022-10-19 15:03:07 +02:00
if not options.skip_box_verification:
2018-09-25 15:47:33 +02:00
buildserver_not_created = any([True for x in v.status() if x.state == 'not_created' and x.name == 'default'])
if buildserver_not_created or options.clean:
# make vagrant download and add basebox
2022-10-19 15:03:07 +02:00
target_basebox_installed = any([x for x in v.box_list() if x.name == config['basebox'] and x.provider == config['vm_provider'] and x.version == config['basebox_version']])
2018-09-25 15:47:33 +02:00
if not target_basebox_installed:
2022-10-19 15:03:07 +02:00
cmd = [shutil.which('vagrant'), 'box', 'add', config['basebox'],
2018-09-25 15:47:33 +02:00
'--box-version=' + config['basebox_version'],
'--provider=' + config['vm_provider']]
ret_val = subprocess.call(cmd)
if ret_val != 0:
2019-02-20 15:54:32 +01:00
logging.critical("downloading basebox '{box}' "
"({provider}, version {version}) failed."
.format(box=config['basebox'],
provider=config['vm_provider'],
version=config['basebox_version']))
2018-09-25 15:47:33 +02:00
sys.exit(1)
# verify box
2023-03-20 15:06:25 +01:00
if config['basebox_version'] not in BASEBOX_CHECKSUMS:
2019-02-20 15:54:32 +01:00
logging.critical("can not verify '{box}', "
"unknown basebox version '{version}'"
.format(box=config['basebox'],
version=config['basebox_version']))
2018-09-25 15:47:33 +02:00
sys.exit(1)
for filename, sha256 in BASEBOX_CHECKSUMS[config['basebox_version']][config['vm_provider']].items():
verify_file_sha256(os.path.join(get_vagrant_home(),
'boxes',
2022-10-19 15:03:07 +02:00
config['basebox'].replace('/', '-VAGRANTSLASH-'),
2018-09-25 15:47:33 +02:00
config['basebox_version'],
config['vm_provider'],
filename),
sha256)
2019-02-20 15:54:32 +01:00
logging.info("successfully verified: '{box}' "
"({provider}, version {version})"
.format(box=config['basebox'],
provider=config['vm_provider'],
version=config['basebox_version']))
2018-09-25 15:47:33 +02:00
else:
2019-02-20 15:54:32 +01:00
logging.debug('using unverified basebox ...')
2018-09-25 15:47:33 +02:00
2019-02-20 15:54:32 +01:00
logging.info("Configuring build server VM")
2017-04-13 12:49:14 +02:00
debug_log_vagrant_vm(serverdir, config)
2017-03-25 01:56:20 +01:00
try:
2017-05-22 17:12:34 +02:00
v.up(provision=True)
2017-10-20 04:38:39 +02:00
except subprocess.CalledProcessError:
2017-04-13 12:49:14 +02:00
debug_log_vagrant_vm(serverdir, config)
2020-10-06 03:06:08 +02:00
logging.error("'vagrant up' failed. Consult %s", logfilename)
2017-03-25 01:56:20 +01:00
sys.exit(1)
2016-09-25 18:55:29 +02:00
2022-10-19 10:31:36 +02:00
if options.copy_caches_from_host:
2016-06-23 17:12:06 +02:00
ssh_config = v.ssh_config()
user = re.search(r'User ([^ \n]+)', ssh_config).group(1)
hostname = re.search(r'HostName ([^ \n]+)', ssh_config).group(1)
port = re.search(r'Port ([0-9]+)', ssh_config).group(1)
key = re.search(r'IdentityFile ([^ \n]+)', ssh_config).group(1)
for d in ('.m2', '.gradle/caches', '.gradle/wrapper', '.pip_download_cache'):
fullpath = os.path.join(os.getenv('HOME'), d)
2021-03-12 13:14:55 +01:00
os.system('date')
print('rsyncing', fullpath, 'into VM')
2016-06-23 17:12:06 +02:00
if os.path.isdir(fullpath):
2017-12-07 11:52:10 +01:00
ssh_command = ' '.join(('ssh -i {0} -p {1}'.format(key, port),
'-o StrictHostKeyChecking=no',
'-o UserKnownHostsFile=/dev/null',
'-o LogLevel=FATAL',
'-o IdentitiesOnly=yes',
'-o PasswordAuthentication=no'))
2017-12-06 09:47:08 +01:00
# TODO vagrant 1.5+ provides `vagrant rsync`
2016-06-23 17:12:06 +02:00
run_via_vagrant_ssh(v, ['cd ~ && test -d', d, '|| mkdir -p', d])
2017-12-07 15:56:09 +01:00
subprocess.call(['rsync', '-ax', '--delete', '-e',
2017-12-06 09:47:08 +01:00
ssh_command,
2016-06-23 17:12:06 +02:00
fullpath + '/',
user + '@' + hostname + ':~/' + d + '/'])
# this file changes every time but should not be cached
run_via_vagrant_ssh(v, ['rm', '-f', '~/.gradle/caches/modules-2/modules-2.lock'])
run_via_vagrant_ssh(v, ['rm', '-fr', '~/.gradle/caches/*/plugin-resolution/'])
2019-02-20 15:54:32 +01:00
logging.info("Stopping build server VM")
2016-09-27 08:49:32 +02:00
v.halt()
2013-05-31 08:48:39 +02:00
2019-02-20 15:54:32 +01:00
logging.info("Packaging")
2016-09-26 11:22:05 +02:00
boxfile = os.path.join(os.getcwd(), 'buildserver.box')
if os.path.exists(boxfile):
os.remove(boxfile)
2017-02-14 22:58:35 +01:00
2022-04-18 17:49:07 +02:00
v.package(output=boxfile)
2016-09-25 18:55:29 +02:00
2019-02-20 15:54:32 +01:00
logging.info("Adding box")
2017-05-22 17:14:48 +02:00
vm.box_add('buildserver', boxfile, force=True)
2016-09-25 18:55:29 +02:00
2017-05-22 17:14:48 +02:00
if 'buildserver' not in subprocess.check_output(['vagrant', 'box', 'list']).decode('utf-8'):
2019-02-20 15:54:32 +01:00
logging.critical('could not add box \'%s\' as \'buildserver\', terminating', boxfile)
2017-05-22 17:12:34 +02:00
sys.exit(1)
2022-11-03 12:44:21 +01:00
# Boxes are stored in two places when using vagrant-libvirt:
#
# 1. `vagrant box add` -> ~/.vagrant.d/boxes/buildserver/0/libvirt/
# 2. `vagrant up` -> /var/lib/libvirt/images/buildserver_vagrant_box_image_0_box.img
#
# If the second box is not cleaned up, then `fdroid build` will
# continue to use that one from the second location, thereby
# ignoring the updated one at the first location. This process
# keeps the second one around until the new box is ready in case
# `fdroid build` is using it while this script is running.
img = 'buildserver_vagrant_box_image_0_box.img'
if os.path.exists(os.path.join('/var/lib/libvirt/images', img)):
subprocess.call(
['virsh', '-c', 'qemu:///system', 'vol-delete', '--pool', 'default', '--vol', img]
)
2017-05-22 17:29:12 +02:00
if not options.keep_box_file:
2019-02-20 15:54:32 +01:00
logging.debug("""box added to vagrant, removing generated box file '%s'""",
boxfile)
2017-05-22 17:29:12 +02:00
os.remove(boxfile)
2016-09-25 18:55:29 +02:00
2022-11-09 10:32:59 +01:00
# This was needed just to create the box, after that, it is unused.
vm.destroy()
2016-09-25 18:55:29 +02:00
if __name__ == '__main__':
2018-10-23 16:34:32 +02:00
if not os.path.exists('makebuildserver') and not os.path.exists('buildserver'):
logging.critical('This must be run as ./makebuildserver in fdroidserver.git!')
sys.exit(1)
2018-10-23 16:38:34 +02:00
if os.path.isfile('/usr/bin/systemd-detect-virt'):
try:
virt = subprocess.check_output('/usr/bin/systemd-detect-virt').strip().decode('utf-8')
except subprocess.CalledProcessError:
virt = 'none'
2022-09-05 17:14:51 +02:00
if virt in ('qemu', 'kvm', 'bochs'):
2018-10-23 16:38:34 +02:00
logging.info('Running in a VM guest, defaulting to QEMU/KVM via libvirt')
config['vm_provider'] = 'libvirt'
elif virt != 'none':
logging.info('Running in an unsupported VM guest (%s)!', virt)
logging.debug('detected virt: %s', virt)
2016-09-25 18:55:29 +02:00
try:
main()
finally:
if tail is not None:
tail.stop()