From c8234919df4b3b58d8eeb19785bab640f957033c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20P=C3=B6hn?= Date: Tue, 23 May 2017 12:53:07 +0200 Subject: [PATCH] refactored vm related code from build.py to vmtools.py --- fdroidserver/build.py | 57 +---------------------------------------- fdroidserver/vmtools.py | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 56 deletions(-) diff --git a/fdroidserver/build.py b/fdroidserver/build.py index c04db9d3..89131360 100644 --- a/fdroidserver/build.py +++ b/fdroidserver/build.py @@ -28,7 +28,6 @@ import traceback import time import requests import tempfile -import textwrap from configparser import ConfigParser from argparse import ArgumentParser import logging @@ -40,7 +39,6 @@ from . import scanner from . import vmtools from .common import FDroidPopen, SdkToolsPopen from .exception import FDroidException, BuildException, VCSException -from .vmtools import FDroidBuildVmException try: import paramiko @@ -48,58 +46,6 @@ except ImportError: pass -def vm_new_get_clean_builder(serverdir, reset=False): - if not os.path.isdir(serverdir): - if os.path.islink(serverdir): - os.unlink(serverdir) - logging.info("buildserver path does not exists, creating %s", serverdir) - os.makedirs(serverdir) - vagrantfile = os.path.join(serverdir, 'Vagrantfile') - if not os.path.isfile(vagrantfile): - with open(os.path.join('builder', 'Vagrantfile'), 'w') as f: - f.write(textwrap.dedent("""\ - # generated file, do not change. - - Vagrant.configure("2") do |config| - config.vm.box = "buildserver" - config.vm.synced_folder ".", "/vagrant", disabled: true - end - """)) - vm = vmtools.get_build_vm(serverdir) - if reset: - logging.info('resetting buildserver by request') - elif not vm.vagrant_uuid_okay(): - logging.info('resetting buildserver, bceause vagrant vm is not okay.') - reset = True - elif not vm.snapshot_exists('fdroidclean'): - logging.info("resetting buildserver, because snapshot 'fdroidclean' is not present.") - reset = True - - if reset: - vm.destroy() - vm.up() - vm.suspend() - - if reset: - logging.info('buildserver recreated: taking a clean snapshot') - vm.snapshot_create('fdroidclean') - else: - logging.info('builserver ok: reverting to clean snapshot') - vm.snapshot_revert('fdroidclean') - vm.up() - - try: - sshinfo = vm.sshinfo() - except FDroidBuildVmException: - # workaround because libvirt sometimes likes to forget - # about ssh connection info even thou the vm is running - vm.halt() - vm.up() - sshinfo = vm.sshinfo() - - return sshinfo - - # Note that 'force' here also implies test mode. def build_server(app, build, vcs, build_dir, output_dir, log_dir, force): """Do a build on the builder vm. @@ -123,8 +69,7 @@ def build_server(app, build, vcs, build_dir, output_dir, log_dir, force): else: logging.getLogger("paramiko").setLevel(logging.WARN) - # sshinfo = vm_get_clean_builder() - sshinfo = vm_new_get_clean_builder('builder') + sshinfo = vmtools.get_clean_builder('builder') try: if not buildserverid: diff --git a/fdroidserver/vmtools.py b/fdroidserver/vmtools.py index e502f3d3..2b198ed1 100644 --- a/fdroidserver/vmtools.py +++ b/fdroidserver/vmtools.py @@ -18,6 +18,7 @@ from os import remove as rmfile from os.path import isdir, isfile, join as joinpath, basename, abspath, expanduser +import os import math import json import tarfile @@ -31,6 +32,58 @@ from logging import getLogger logger = getLogger('fdroidserver-vmtools') +def get_clean_builder(serverdir, reset=False): + if not os.path.isdir(serverdir): + if os.path.islink(serverdir): + os.unlink(serverdir) + logger.info("buildserver path does not exists, creating %s", serverdir) + os.makedirs(serverdir) + vagrantfile = os.path.join(serverdir, 'Vagrantfile') + if not os.path.isfile(vagrantfile): + with open(os.path.join('builder', 'Vagrantfile'), 'w') as f: + f.write(textwrap.dedent("""\ + # generated file, do not change. + + Vagrant.configure("2") do |config| + config.vm.box = "buildserver" + config.vm.synced_folder ".", "/vagrant", disabled: true + end + """)) + vm = get_build_vm(serverdir) + if reset: + logger.info('resetting buildserver by request') + elif not vm.vagrant_uuid_okay(): + logger.info('resetting buildserver, bceause vagrant vm is not okay.') + reset = True + elif not vm.snapshot_exists('fdroidclean'): + logger.info("resetting buildserver, because snapshot 'fdroidclean' is not present.") + reset = True + + if reset: + vm.destroy() + vm.up() + vm.suspend() + + if reset: + logger.info('buildserver recreated: taking a clean snapshot') + vm.snapshot_create('fdroidclean') + else: + logger.info('builserver ok: reverting to clean snapshot') + vm.snapshot_revert('fdroidclean') + vm.up() + + try: + sshinfo = vm.sshinfo() + except FDroidBuildVmException: + # workaround because libvirt sometimes likes to forget + # about ssh connection info even thou the vm is running + vm.halt() + vm.up() + sshinfo = vm.sshinfo() + + return sshinfo + + def _check_call(cmd, shell=False, cwd=None): logger.debug(' '.join(cmd)) return subprocess.check_call(cmd, shell=shell, cwd=cwd)