hammer.py 44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/env python3
from __future__ import print_function
import os
import sys
import glob
import argparse
import time
import platform
import subprocess
import logging
11 12
import datetime
import json
13
import multiprocessing
14
import xml.etree.ElementTree as ET
15 16 17

# TODO:
# - add docker provider
18
#   https://developer.fedoraproject.org/tools/docker/docker-installation.html
19
# - add CCACHE support
20 21 22
# - improve building from tarball
# - improve native-pkg builds
# - avoid using network if possible (e.g. check first if pkgs are installed)
23 24 25 26 27


SYSTEMS = {
    'fedora': ['27', '28', '29'],
    'centos': ['7'],
28 29 30
    #'rhel': ['7', '8'],
    'rhel': ['8'],
    'ubuntu': ['16.04', '18.04', '18.10'],
31 32
    'debian': ['8', '9'],
    #'freebsd': ['11.0', '11.1', '11.2', '12.0'],
33
    'freebsd': ['11.2', '12.0'],
34 35 36
}

IMAGE_TEMPLATES = {
37 38 39 40 41 42 43 44
    'fedora-27-lxc':           {'bare': 'lxc-fedora-27',               'kea': 'godfryd/kea-fedora-27'},
    'fedora-27-virtualbox':    {'bare': 'generic/fedora27',            'kea': 'godfryd/kea-fedora-27'},
    'fedora-28-lxc':           {'bare': 'lxc-fedora-28',               'kea': 'godfryd/kea-fedora-28'},
    'fedora-28-virtualbox':    {'bare': 'generic/fedora28',            'kea': 'godfryd/kea-fedora-28'},
    'fedora-29-lxc':           {'bare': 'godfryd/lxc-fedora-29',       'kea': 'godfryd/kea-fedora-29'},
    'fedora-29-virtualbox':    {'bare': 'generic/fedora29',            'kea': 'godfryd/kea-fedora-29'},
    'centos-7-lxc':            {'bare': 'godfryd/lxc-centos-7',        'kea': 'godfryd/kea-centos-7'},
    'centos-7-virtualbox':     {'bare': 'generic/centos7',             'kea': 'godfryd/kea-centos-7'},
45 46
#    'rhel-7-virtualbox':       {'bare': 'generic/rhel7',               'kea': 'generic/rhel7'},   # TODO: subsciption needed
    'rhel-8-virtualbox':       {'bare': 'generic/rhel8',               'kea': 'generic/rhel8'},
47 48 49 50 51 52 53 54 55 56 57 58
    'ubuntu-16.04-lxc':        {'bare': 'godfryd/lxc-ubuntu-16.04',    'kea': 'godfryd/kea-ubuntu-16.04'},
    'ubuntu-16.04-virtualbox': {'bare': 'ubuntu/xenial64',             'kea': 'godfryd/kea-ubuntu-16.04'},
    'ubuntu-18.04-lxc':        {'bare': 'godfryd/lxc-ubuntu-18.04',    'kea': 'godfryd/kea-ubuntu-18.04'},
    'ubuntu-18.04-virtualbox': {'bare': 'ubuntu/bionic64',             'kea': 'godfryd/kea-ubuntu-18.04'},
    'ubuntu-18.10-lxc':        {'bare': 'godfryd/lxc-ubuntu-18.10',    'kea': 'godfryd/kea-ubuntu-18.10'},
    'ubuntu-18.10-virtualbox': {'bare': 'ubuntu/cosmic64',             'kea': 'godfryd/kea-ubuntu-18.10'},
    'debian-8-lxc':            {'bare': 'godfryd/lxc-debian-8',        'kea': 'godfryd/kea-debian-8'},
    'debian-8-virtualbox':     {'bare': 'debian/jessie64',             'kea': 'godfryd/kea-debian-8'},
    'debian-9-lxc':            {'bare': 'godfryd/lxc-debian-9',        'kea': 'godfryd/kea-debian-9'},
    'debian-9-virtualbox':     {'bare': 'debian/stretch64',            'kea': 'godfryd/kea-debian-9'},
    'freebsd-11.2-virtualbox': {'bare': 'generic/freebsd11',           'kea': 'godfryd/kea-freebsd-11.2'},
    'freebsd-12.0-virtualbox': {'bare': 'generic/freebsd12',           'kea': 'godfryd/kea-freebsd-12.0'},
59 60 61 62 63 64
}

LXC_VAGRANTFILE_TPL = """# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
65
  config.vm.hostname = "{name}"
66 67

  config.vm.box = "{image_tpl}"
68 69 70 71 72 73

  config.vm.provider "lxc" do |lxc|
    lxc.container_name = "{name}"
  end

  config.vm.synced_folder '.', '/vagrant', disabled: true
74 75 76 77 78 79 80
end
"""

VBOX_VAGRANTFILE_TPL = """# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
81
  config.vm.hostname = "{name}"
82 83 84 85

  config.vm.box = "{image_tpl}"

  config.vm.provider "virtualbox" do |v|
86
    v.name = "{name}"
87 88 89 90 91 92 93 94 95 96
    v.memory = 8192

    nproc = Etc.nprocessors
    if nproc > 8
      nproc -= 2
    elsif nproc > 1
      nproc -= 1
    end
    v.cpus = nproc
  end
97 98

  config.vm.synced_folder '.', '/vagrant', disabled: true
99 100 101 102 103 104 105
end
"""


log = logging.getLogger()


106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
def red(txt):
    if sys.stdout.isatty():
        return '\033[1;31m%s\033[0;0m' % txt
    else:
        return txt

def green(txt):
    if sys.stdout.isatty():
        return '\033[0;32m%s\033[0;0m' % txt
    else:
        return txt

def blue(txt):
    if sys.stdout.isatty():
        return '\033[0;34m%s\033[0;0m' % txt
    else:
        return txt


125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
def get_system_revision():
    system = platform.system()
    if system == 'Linux':
        system, revision, _ = platform.dist()
        if system == 'debian':
            if revision.startswith('8.'):
                revision = '8'
        elif system == 'redhat':
            system = 'rhel'
            if revision.startswith('8.'):
                revision = '8'
    elif system == 'FreeBSD':
        system = system.lower()
        revision = platform.release()
    return system.lower(), revision


142 143 144
class ExecutionError(Exception): pass

def execute(cmd, timeout=60, cwd=None, env=None, raise_error=True, dry_run=False, log_file_path=None, quiet=False, check_times=False):
145
    log.info('>>>>> Executing %s in %s', cmd, cwd if cwd else os.getcwd())
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    if not check_times:
        timeout = None
    if dry_run:
        return 0
    if log_file_path:
        p = subprocess.Popen(cmd, cwd=cwd, env=env, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        with open(log_file_path, "wb") as log_file:
            t0 = time.time()
            t1 = time.time()
            while p.poll() is None and (timeout is None or t1 - t0 < timeout):
                line = p.stdout.readline()
                if line:
                    if not quiet:
                        print(line.decode(errors='ignore').strip() + '\r')
                    log_file.write(line)
                t1 = time.time()

            if p.poll() is None:
                raise ExecutionError('Execution timeout')
            exitcode = p.returncode

167
    else:
168 169 170 171 172 173 174
        p = subprocess.Popen(cmd, cwd=cwd, env=env, shell=True)
        ver = platform.python_version()
        if ver.startswith('2'):
            exitcode = p.wait()
        else:
            exitcode = p.wait(timeout)

175
    if exitcode != 0 and raise_error:
176
        raise ExecutionError("The command return non-zero exitcode %s, cmd: '%s'" % (exitcode, cmd))
177 178 179
    return exitcode


180 181 182 183 184 185 186 187
def install_yum(pkgs, env=None, check_times=False):
    if isinstance(pkgs, list):
        pkgs = ' '.join(pkgs)
    # skip.... to detect case when one packet is not found and no error is returned
    cmd = 'sudo yum install -y --setopt=skip_missing_names_on_install=False %s' % pkgs
    execute(cmd, env=env, check_times=check_times)


188
class VagrantEnv(object):
189 190
    def __init__(self, provider, system, sys_revision, features, image_template_variant, dry_run, quiet=False, check_times=False):
        self.provider = provider
191 192 193
        self.system = system
        self.sys_revision = sys_revision
        self.features = features
194 195 196
        self.dry_run = dry_run
        self.quiet = quiet
        self.check_times = check_times
197 198 199 200 201 202 203 204 205

        if provider == "virtualbox":
            vagrantfile_tpl = VBOX_VAGRANTFILE_TPL
        elif provider == "lxc":
            vagrantfile_tpl = LXC_VAGRANTFILE_TPL

        image_tpl = IMAGE_TEMPLATES["%s-%s-%s" % (system, sys_revision, provider)][image_template_variant]
        self.repo_dir = os.getcwd()

206 207 208 209
        self.name = "hmr-%s-%s-kea-srv" % (system, sys_revision.replace('.', '-'))

        vagrantfile = vagrantfile_tpl.format(image_tpl=image_tpl,
                                             name=self.name)
210 211 212

        sys_dir = "%s-%s" % (system, sys_revision)
        if provider == "virtualbox":
213
            self.vagrant_dir = os.path.join(self.repo_dir, 'hammer', sys_dir, 'vbox')
214
        elif provider == "lxc":
215 216 217 218
            self.vagrant_dir = os.path.join(self.repo_dir, 'hammer', sys_dir, 'lxc')

        if dry_run:
            return
219

220 221
        if not os.path.exists(self.vagrant_dir):
            os.makedirs(self.vagrant_dir)
222

223
        vagrantfile_path = os.path.join(self.vagrant_dir, "Vagrantfile")
224 225 226 227 228 229 230 231 232

        if os.path.exists(vagrantfile_path):
            # TODO: destroy any existing VM
            pass

        with open(vagrantfile_path, "w") as f:
            f.write(vagrantfile)

    def up(self):
233 234
        execute("vagrant box update", cwd=self.vagrant_dir, timeout=20 * 60, dry_run=self.dry_run)
        execute("vagrant up --no-provision --provider %s" % self.provider, cwd=self.vagrant_dir, timeout=15 * 60, dry_run=self.dry_run)
235 236

    def package(self):
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        if self.provider == 'virtualbox':
            cmd = "vagrant package --output kea-%s-%s.box" % (self.system, self.sys_revision)
            execute(cmd, cwd=self.vagrant_dir, timeout=4 * 60, dry_run=self.dry_run)

        elif self.provider == 'lxc':
            execute('vagrant halt', cwd=self.vagrant_dir, dry_run=self.dry_run, raise_error=False)

            box_path = os.path.join(self.vagrant_dir, 'kea-%s-%s.box' % (self.system, self.sys_revision))
            if os.path.exists(box_path):
                os.unlink(box_path)

            lxc_box_dir = os.path.join(self.vagrant_dir, 'lxc-box')
            if os.path.exists(lxc_box_dir):
                execute('sudo rm -rf %s' % lxc_box_dir)
            os.mkdir(lxc_box_dir)
            lxc_container_path = os.path.join('/var/lib/lxc', self.name)
            execute('sudo bash -c \'echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" > %s/rootfs/home/vagrant/.ssh/authorized_keys\'' % lxc_container_path)
            execute('sudo bash -c "cd %s && tar --numeric-owner --anchored --exclude=./rootfs/dev/log -czf %s/rootfs.tar.gz ./rootfs/*"' % (lxc_container_path, lxc_box_dir))
            execute('sudo cp %s/config %s/lxc-config' % (lxc_container_path, lxc_box_dir))
            execute('sudo chown `id -un`:`id -gn` *', cwd=lxc_box_dir)
            with open(os.path.join(lxc_box_dir, 'metadata.json'), 'w') as f:
                now = datetime.datetime.now()
                f.write('{\n')
                f.write('  "provider": "lxc",\n')
                f.write('  "version":  "1.0.0",\n')
                f.write('  "built-on": "%s"\n' % now.strftime('%c'))
                f.write('}\n')

            execute('tar -czf %s ./*' % box_path, cwd=lxc_box_dir)
            execute('sudo rm -rf %s' % lxc_box_dir)
267 268

    def run_build_and_test(self, tarball_path):
269 270 271
        if self.dry_run:
            return 0, 0

272 273 274 275 276
        if not tarball_path:
            name_ver = 'kea-1.5.0'
            execute('tar --transform "flags=r;s|^|%s/|" --exclude hammer --exclude "*~" --exclude .git -zcf /tmp/%s.tar.gz .' % (name_ver, name_ver))
            tarball_path = '/tmp/%s.tar.gz' % name_ver
        execute('vagrant upload %s %s.tar.gz' % (tarball_path, name_ver), cwd=self.vagrant_dir)
277 278 279

        log_file_path = os.path.join(self.vagrant_dir, 'build.log')
        log.info('Build log file stored to %s', log_file_path)
280 281

        t0 = time.time()
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

        bld_cmd = "%s hammer.py build -p local -t %s.tar.gz" % (self.python, name_ver)
        if self.features_arg:
            bld_cmd += ' ' + self.features_arg
        if self.nofeatures_arg:
            bld_cmd += ' ' + self.nofeatures_arg
        if self.check_times:
            bld_cmd += ' -i'
        self.execute(bld_cmd, timeout=40 * 60, log_file_path=log_file_path, quiet=self.quiet)  # timeout: 40 minutes

        ssh_cfg_path = self.dump_ssh_config()

        if 'native-pkg' in self.features:
            execute('scp -F %s -r default:/home/vagrant/rpm-root/RPMS/x86_64/ .' % ssh_cfg_path)

297 298
        t1 = time.time()
        dt = int(t1 - t0)
299 300

        log.info('Build log file stored to %s', log_file_path)
301 302 303 304
        log.info("")
        log.info(">>>>>> Build time %s:%s", dt // 60, dt % 60)
        log.info("")

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        total = 0
        passed = 0
        try:
            if 'unittest' in self.features:
                execute('scp -F %s -r default:/home/vagrant/unit-test-results.json .' % ssh_cfg_path, cwd=self.vagrant_dir)
                results_file = os.path.join(self.vagrant_dir, 'unit-test-results.json')
                if os.path.exists(results_file):
                    with open(results_file) as f:
                        txt = f.read()
                        results = json.loads(txt)
                        total = results['grand_total']
                        passed = results['grand_passed']
        except:
            log.exception('ignored issue with parsing unit test results')

        return total, passed

322 323 324 325
    def destroy(self, force=False):
        cmd = 'vagrant destroy'
        if force:
            cmd += ' --force'
326
        execute(cmd, cwd=self.vagrant_dir, timeout=3 * 60, dry_run=self.dry_run)  # timeout: 3 minutes
327 328

    def ssh(self):
329 330 331 332 333 334 335 336 337 338 339
        execute('vagrant ssh', cwd=self.vagrant_dir, timeout=None, dry_run=self.dry_run)

    def dump_ssh_config(self):
        ssh_cfg_path = os.path.join(self.vagrant_dir, 'ssh.cfg')
        execute('vagrant ssh-config > %s' % ssh_cfg_path, cwd=self.vagrant_dir)
        return ssh_cfg_path

    def execute(self, cmd, timeout=None, raise_error=True, log_file_path=None, quiet=False, env=None):
        if not env:
            env = os.environ.copy()
        env['LANGUAGE'] = env['LANG'] = env['LC_ALL'] = 'C'
340

341 342
        return execute('vagrant ssh -c "%s"' % cmd, env=env, cwd=self.vagrant_dir, timeout=timeout, raise_error=raise_error,
                       dry_run=self.dry_run, log_file_path=log_file_path, quiet=quiet, check_times=self.check_times)
343

344 345 346
    def prepare_deps(self):
        if self.features:
            self.features_arg = '--with ' + ' '.join(self.features)
347 348 349
        else:
            self.features_arg = ''

350
        nofeatures = set(DEFAULT_FEATURES) - self.features
351 352 353 354 355
        if nofeatures:
            self.nofeatures_arg = '--without ' + ' '.join(nofeatures)
        else:
            self.nofeatures_arg = ''

356
        if self.system == 'centos' and self.sys_revision == '7' or (self.system == 'debian' and self.sys_revision == '8' and self.provider != 'lxc'):
357 358 359 360 361 362 363 364 365
            self.python = 'python'
        elif self.system == 'freebsd':
            self.python = 'python3.6'
        else:
            self.python = 'python3'

        if self.system == 'rhel' and self.sys_revision == '8':
            exitcode = self.execute("sudo subscription-manager repos --list-enabled | grep rhel-8-for-x86_64-baseos-beta-rpms", raise_error=False)
            if exitcode != 0:
366 367 368 369 370
                env = os.environ.copy()
                with open(os.path.expanduser('~/rhel-creds.txt')) as f:
                    env['RHEL_USER'] = f.readline().strip()
                    env['RHEL_PASSWD'] = f.readline().strip()
                self.execute('sudo subscription-manager register --user $RHEL_USER --password "$RHEL_PASSWD"', env=env)
371 372 373 374 375 376
                self.execute("sudo subscription-manager refresh")
                self.execute("sudo subscription-manager attach --pool 8a85f99a67cdc3e70167e45c85f47429")
                self.execute("sudo subscription-manager repos --enable rhel-8-for-x86_64-baseos-beta-rpms")
                self.execute("sudo dnf install -y python36")

        hmr_py_path = os.path.join(self.repo_dir, 'hammer.py')
377
        execute('vagrant upload %s' % hmr_py_path, cwd=self.vagrant_dir, dry_run=self.dry_run)
378

379 380 381 382
        log_file_path = os.path.join(self.vagrant_dir, 'prepare.log')
        log.info('Prepare log file stored to %s', log_file_path)

        cmd = "{python} hammer.py prepare-deps {features} {nofeatures} {check_times}"
383 384
        cmd = cmd.format(features=self.features_arg,
                         nofeatures=self.nofeatures_arg,
385 386 387
                         python=self.python,
                         check_times='-i' if self.check_times else '')
        self.execute(cmd, timeout=40 * 60, log_file_path=log_file_path, quiet=self.quiet)
388 389 390 391 392


def _install_gtest_sources():
    if not os.path.exists('/usr/src/googletest-release-1.8.0/googletest'):
        execute('wget --no-verbose -O /tmp/gtest.tar.gz https://github.com/google/googletest/archive/release-1.8.0.tar.gz')
393
        execute('sudo tar -C /usr/src -zxf /tmp/gtest.tar.gz')
394 395 396
        os.unlink('/tmp/gtest.tar.gz')


397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
def _configure_mysql(system):
    if system in ['fedora', 'centos']:
        execute('sudo systemctl enable mariadb.service')
        execute('sudo systemctl start mariadb.service')
        time.sleep(5)
    cmd = "echo 'DROP DATABASE IF EXISTS keatest;' | sudo mysql -u root"
    execute(cmd)
    cmd = "echo 'DROP USER 'keatest'@'localhost';' | sudo mysql -u root"
    execute(cmd, raise_error=False)
    cmd = "echo 'DROP USER 'keatest_readonly'@'localhost';' | sudo mysql -u root"
    execute(cmd, raise_error=False)
    cmd = "bash -c \"cat <<EOF | sudo mysql -u root\n"
    cmd += "CREATE DATABASE keatest;\n"
    cmd += "CREATE USER 'keatest'@'localhost' IDENTIFIED BY 'keatest';\n"
    cmd += "CREATE USER 'keatest_readonly'@'localhost' IDENTIFIED BY 'keatest';\n"
    cmd += "GRANT ALL ON keatest.* TO 'keatest'@'localhost';\n"
    cmd += "GRANT SELECT ON keatest.* TO 'keatest_readonly'@'localhost';\n"
    cmd += "EOF\n\""
    execute(cmd)


def _configure_pgsql(system):
    if system in ['fedora', 'centos']:
        # https://fedoraproject.org/wiki/PostgreSQL
        exitcode = execute('sudo ls /var/lib/pgsql/data/postgresql.conf')
        if exitcode != 0:
            execute('sudo postgresql-setup --initdb --unit postgresql')
    execute('sudo systemctl start postgresql.service')
    cmd = "bash -c \"cat <<EOF | sudo -u postgres psql postgres\n"
    cmd += "DROP DATABASE IF EXISTS keatest;\n"
    cmd += "DROP USER IF EXISTS keatest;\n"
    cmd += "DROP USER IF EXISTS keatest_readonly;\n"
    cmd += "CREATE USER keatest WITH PASSWORD 'keatest';\n"
    cmd += "CREATE USER keatest_readonly WITH PASSWORD 'keatest';\n"
    cmd += "CREATE DATABASE keatest;\n"
    cmd += "GRANT ALL PRIVILEGES ON DATABASE keatest TO keatest;\n"
    cmd += "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES to keatest_readonly;\n"
    cmd += "EOF\n\""
    execute(cmd)


def _install_cassandra_deb():
    if not os.path.exists('/usr/sbin/cassandra'):
        execute('echo "deb http://www.apache.org/dist/cassandra/debian 311x main" | sudo tee /etc/apt/sources.list.d/cassandra.sources.list')
        execute('curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -')
        execute('sudo apt update')
        execute('sudo apt install -y cassandra libuv1 pkgconf')

    if not os.path.exists('/usr/include/cassandra.h'):
        execute('wget http://downloads.datastax.com/cpp-driver/ubuntu/18.04/cassandra/v2.11.0/cassandra-cpp-driver-dev_2.11.0-1_amd64.deb')
        execute('wget http://downloads.datastax.com/cpp-driver/ubuntu/18.04/cassandra/v2.11.0/cassandra-cpp-driver_2.11.0-1_amd64.deb')
        execute('sudo dpkg -i cassandra-cpp-driver-dev_2.11.0-1_amd64.deb cassandra-cpp-driver_2.11.0-1_amd64.deb')
        execute('rm -rf cassandra-cpp-driver-dev_2.11.0-1_amd64.deb cassandra-cpp-driver_2.11.0-1_amd64.deb')


def _install_cassandra_rpm(system):
    if not os.path.exists('/usr/bin/cassandra'):
        #execute('sudo dnf config-manager --add-repo https://www.apache.org/dist/cassandra/redhat/311x/')
        #execute('sudo rpm --import https://www.apache.org/dist/cassandra/KEYS')
        if system == 'fedora':
            install_yum('cassandra cassandra-server libuv libuv-devel')
        #elif system == 'centos':
        else:
            raise NotImplementedError

    execute('sudo systemctl start cassandra')

    if not os.path.exists('/usr/include/cassandra.h'):
        execute('wget http://downloads.datastax.com/cpp-driver/centos/7/cassandra/v2.11.0/cassandra-cpp-driver-2.11.0-1.el7.x86_64.rpm')
        execute('wget http://downloads.datastax.com/cpp-driver/centos/7/cassandra/v2.11.0/cassandra-cpp-driver-devel-2.11.0-1.el7.x86_64.rpm')
        execute('sudo rpm -i cassandra-cpp-driver-2.11.0-1.el7.x86_64.rpm cassandra-cpp-driver-devel-2.11.0-1.el7.x86_64.rpm')
        execute('rm -rf cassandra-cpp-driver-2.11.0-1.el7.x86_64.rpm cassandra-cpp-driver-devel-2.11.0-1.el7.x86_64.rpm')


def prepare_deps_local(features, check_times):
    env = os.environ.copy()
    env['LANGUAGE'] = env['LANG'] = env['LC_ALL'] = 'C'

475 476 477 478
    system, revision = get_system_revision()
    log.info('Preparing deps for %s %s', system, revision)

    if system == 'fedora':
479
        packages = ['make', 'autoconf', 'automake', 'libtool', 'gcc-c++', 'openssl-devel', 'log4cplus-devel', 'boost-devel']
480 481 482 483

        if 'native-pkg' in features:
            packages.extend(['rpm-build', 'mariadb-connector-c-devel'])

484 485 486 487 488 489 490 491 492 493 494
        if 'docs' in features:
            packages.extend(['libxslt', 'elinks', 'docbook-style-xsl'])

        if 'mysql' in features:
            packages.extend(['mariadb', 'mariadb-server', 'community-mysql-devel'])

        if 'pgsql' in features:
            packages.extend(['postgresql-devel', 'postgresql-server'])

        cmd = 'sudo dnf -y install %s' % ' '.join(packages)
        execute(cmd, env=env, timeout=120, check_times=check_times)
495 496 497 498

        if 'unittest' in features:
            _install_gtest_sources()

499
        execute('sudo dnf clean packages', env=env, check_times=check_times)
500

501 502 503 504 505
        if 'cql' in features:
            _install_cassandra_rpm()

    elif system == 'centos':
        install_yum('epel-release', env=env, check_times=check_times)
506 507 508 509 510

        packages = ['make', 'autoconf', 'automake', 'libtool', 'gcc-c++', 'openssl-devel', 'log4cplus-devel', 'boost-devel',
                    'mariadb-devel', 'postgresql-devel']

        if 'docs' in features:
511 512 513 514
            packages.extend(['libxslt', 'elinks', 'docbook-style-xsl'])

        if 'mysql' in features:
            packages.extend(['mariadb', 'mariadb-server', 'mariadb-devel'])
515

516 517 518 519
        if 'pgsql' in features:
            packages.extend(['postgresql-devel', 'postgresql-server'])

        install_yum(packages, env=env, check_times=check_times)
520 521 522 523

        if 'unittest' in features:
            _install_gtest_sources()

524 525 526 527
        if 'cql' in features:
            _install_cassandra_rpm()

    elif system == 'rhel':
528 529 530 531
        packages = ['make', 'autoconf', 'automake', 'libtool', 'gcc-c++', 'openssl-devel', 'boost-devel',
                    'mariadb-devel', 'postgresql-devel']
        packages.extend(['rpm-build'])

532 533
        if 'docs' in features and not revision == '8':
            packages.extend(['libxslt', 'elinks', 'docbook-style-xsl'])
534

535 536 537 538 539 540 541 542
        # if 'mysql' in features:
        #     packages.extend(['default-mysql-client-core', 'default-libmysqlclient-dev', 'mysql-server'])

        # if 'pgsql' in features:
        #     packages.extend(['postgresql-client', 'libpq-dev', 'postgresql-all'])

        install_cmd = 'sudo dnf -y install %s'
        execute(install_cmd % ' '.join(packages), env=env, check_times=check_times)
543 544 545

        # prepare lib4cplus as epel repos are not available for rhel 8 yet
        if revision == '8' and not os.path.exists('/usr/include/log4cplus/logger.h'):
546 547 548 549 550 551 552 553 554
            if not os.path.exists('srpms'):
                execute('mkdir srpms')
            execute('rm -rf srpms/*')
            execute('rm -rf rpmbuild')
            execute('wget --no-verbose -O srpms/log4cplus-1.1.3-0.4.rc3.el7.src.rpm https://rpmfind.net/linux/epel/7/SRPMS/Packages/l/log4cplus-1.1.3-0.4.rc3.el7.src.rpm',
                    check_times=check_times)
            execute('rpmbuild --rebuild srpms/log4cplus-1.1.3-0.4.rc3.el7.src.rpm', env=env, check_times=check_times)
            execute('sudo rpm -i rpmbuild/RPMS/x86_64/log4cplus-1.1.3-0.4.rc3.el8.x86_64.rpm', env=env, check_times=check_times)
            execute('sudo rpm -i rpmbuild/RPMS/x86_64/log4cplus-devel-1.1.3-0.4.rc3.el8.x86_64.rpm', env=env, check_times=check_times)
555 556 557 558

        if 'unittest' in features:
            _install_gtest_sources()

559 560 561
        if 'cql' in features:
            _install_cassandra_rpm()

562
    elif system == 'ubuntu':
563
        execute('sudo apt update', env=env, check_times=check_times)
564 565 566 567

        packages = ['gcc', 'g++', 'make', 'autoconf', 'automake', 'libtool', 'libssl-dev', 'liblog4cplus-dev', 'libboost-system-dev']

        if 'unittest' in features:
568 569 570 571
            if revision.startswith('16.'):
                _install_gtest_sources()
            else:
                packages.append('googletest')
572 573

        if 'docs' in features:
574
            packages.extend(['dblatex', 'xsltproc', 'elinks', 'docbook-xsl'])
575 576 577 578 579 580

        if 'native-pkg' in features:
            packages.extend(['build-essential', 'fakeroot', 'devscripts'])
            packages.extend(['bison', 'debhelper', 'default-libmysqlclient-dev', 'libmysqlclient-dev', 'docbook', 'docbook-xsl', 'flex', 'libboost-dev',
                             'libpq-dev', 'postgresql-server-dev-all', 'python3-dev'])

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
        if 'mysql' in features:
            packages.extend(['default-mysql-client-core', 'default-libmysqlclient-dev', 'mysql-server'])

        if 'pgsql' in features:
            packages.extend(['postgresql-client', 'libpq-dev', 'postgresql-all'])

        done = False
        while not done:
            try:
                execute('sudo apt install --no-install-recommends -y %s' % ' '.join(packages), env=env, timeout=240, check_times=check_times)
                done = True
            except:
                log.exception('ble')
                time.sleep(20)

        if 'cql' in features:
            _install_cassandra_deb()
598 599

    elif system == 'debian':
600
        execute('sudo apt update', env=env, check_times=check_times)
601 602 603 604

        packages = ['gcc', 'g++', 'make', 'autoconf', 'automake', 'libtool', 'libssl-dev', 'liblog4cplus-dev', 'libboost-system-dev']

        if 'docs' in features:
605
            packages.extend(['dblatex', 'xsltproc', 'elinks', 'docbook-xsl'])
606 607 608

        if 'unittest' in features:
            if revision == '8':
609 610
                _install_gtest_sources()
                #packages.append('libgtest-dev') # it does not work
611 612 613
            else:
                packages.append('googletest')

614 615 616 617 618 619 620
        if 'mysql' in features:
            packages.extend(['default-mysql-client-core', 'default-libmysqlclient-dev', 'mysql-server'])

        execute('sudo apt install --no-install-recommends -y %s' % ' '.join(packages), env=env, timeout=240, check_times=check_times)

        if 'cql' in features:
            _install_cassandra_deb()
621 622 623 624

    elif system == 'freebsd':
        packages = ['autoconf', 'automake', 'libtool', 'openssl', 'log4cplus', 'boost-libs']

625 626
        #execute('sudo portsnap --interactive fetch', timeout=240, check_times=check_times)
        #execute('sudo portsnap extract /usr/ports/devel/log4cplus', timeout=240, check_times=check_times)
627

628 629 630 631
        #execute('sudo make -C /usr/ports/devel/log4cplus install clean BATCH=yes', timeout=240, check_times=check_times)

        if 'docs' in features:
            packages.extend(['libxslt', 'elinks', 'docbook-xsl'])
632 633 634 635

        if 'unittest' in features:
            _install_gtest_sources()

636 637
        execute('sudo pkg install -y %s' % ' '.join(packages), env=env, timeout=6 * 60, check_times=check_times)

638 639 640
    else:
        raise NotImplementedError

641 642
    if 'mysql' in features:
        _configure_mysql(system)
643

644 645 646 647 648
    if 'pgsql' in features:
        _configure_pgsql(system)


def build_local(features, tarball_path, check_times):
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    env = os.environ.copy()
    env['LANGUAGE'] = env['LANG'] = env['LC_ALL'] = 'C'

    distro, revision = get_system_revision()

    execute('df -h')

    tarball_path = os.path.abspath(tarball_path)

    if 'native-pkg' in features:
        # native pkg build

        if distro in ['fedora', 'centos', 'rhel']:
            execute('rm -rf rpm-root')
            os.mkdir('rpm-root')
            os.mkdir('rpm-root/BUILD')
            os.mkdir('rpm-root/BUILDROOT')
            os.mkdir('rpm-root/RPMS')
            os.mkdir('rpm-root/SOURCES')
            os.mkdir('rpm-root/SPECS')
            os.mkdir('rpm-root/SRPMS')

            execute('rm -rf kea-src')
            os.mkdir('kea-src')
673
            execute('tar -zxf %s' % tarball_path, cwd='kea-src', check_times=check_times)
674 675 676 677 678
            src_path = glob.glob('kea-src/*')[0]
            rpm_dir = os.path.join(src_path, 'rpm')
            for f in os.listdir(rpm_dir):
                if f == 'kea.spec':
                    continue
679 680 681
                execute('cp %s rpm-root/SOURCES' % os.path.join(rpm_dir, f), check_times=check_times)
            execute('cp %s rpm-root/SPECS' % os.path.join(rpm_dir, 'kea.spec'), check_times=check_times)
            execute('cp %s rpm-root/SOURCES' % tarball_path, check_times=check_times)
682 683

            cmd = "rpmbuild -ba rpm-root/SPECS/kea.spec -D'_topdir /home/vagrant/rpm-root'"
684
            execute(cmd, env=env, timeout=60 * 40, check_times=check_times)
685 686

            if 'install' in features:
687
                execute('sudo rpm -i rpm-root/RPMS/x86_64/*rpm', check_times=check_times)
688 689

        elif distro in ['ubuntu', 'debian']:
690
            execute('rm -rf kea-src', check_times=check_times)
691
            os.mkdir('kea-src')
692
            execute('tar -zxf %s' % tarball_path, cwd='kea-src', check_times=check_times)
693 694
            src_path = glob.glob('kea-src/*')[0]

695
            execute('debuild -i -us -uc -b', env=env, cwd=src_path, timeout=60 * 40, check_times=check_times)
696 697

            if 'install' in features:
698
                execute('sudo dpkg -i kea-src/*deb', check_times=check_times)
699 700 701 702 703 704 705 706 707 708

        else:
            raise NotImplementedError

    else:
        # build straight from sources

        if tarball_path:
            execute('rm -rf kea-src')
            os.mkdir('kea-src')
709
            execute('tar -zxf %s' % tarball_path, cwd='kea-src', check_times=check_times)
710 711 712 713 714 715 716 717 718 719 720
            src_path = glob.glob('kea-src/*')[0]
        else:
            src_path = '.'

        execute('autoreconf -f -i', cwd=src_path, env=env)

        cmd = './configure'
        if 'mysql' in features:
            cmd += ' --with-mysql'
        if 'pgsql' in features:
            cmd += ' --with-pgsql'
721 722
        if 'cql' in features:
            cmd += ' --with-cql=/usr/bin/pkg-config'
723
        if 'unittest' in features:
724
            if distro in ['centos', 'fedora', 'rhel', 'freebsd']:
725 726
                cmd += ' --with-gtest-source=/usr/src/googletest-release-1.8.0/googletest/'
            elif distro == 'debian' and revision == '8':
727 728 729
                #cmd += ' --with-gtest-source=/usr/src/gtest' # it does not work
                cmd += ' --with-gtest-source=/usr/src/googletest-release-1.8.0/googletest/'
            elif distro == 'debian':
730
                cmd += ' --with-gtest-source=/usr/src/googletest/googletest'
731 732 733 734 735
            elif distro == 'ubuntu':
                if revision.startswith('16.'):
                    cmd += ' --with-gtest-source=/usr/src/googletest-release-1.8.0/googletest/'
                else:
                    cmd += ' --with-gtest-source=/usr/src/googletest/googletest'
736 737
            else:
                raise NotImplementedError
738
        if 'docs' in features and not (distro == 'rhel' and revision == '8'):
739 740 741 742 743 744
            cmd += ' --enable-generate-docs'

        if distro == 'freebsd':
            cmd += ' --with-boost-include=/usr/local/include'  # TODO: this should be fixed in ./configure.ac
            cmd += ' --with-boost-lib-dir=/usr/local/lib'      # TODO: this should be fixed in ./configure.ac

745
        execute(cmd, cwd=src_path, env=env, check_times=check_times)
746 747 748 749 750 751 752

        cpus = multiprocessing.cpu_count() - 1
        if distro == 'centos':
            cpus = cpus // 2
        if cpus == 0:
            cpus = 1
        cmd = 'make -j%s' % cpus
753
        execute(cmd, cwd=src_path, env=env, timeout=40 * 60, check_times=check_times)
754 755

        if 'unittest' in features:
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
            results_dir = os.path.abspath(os.path.join(src_path, 'tests_result'))
            execute('rm -rf %s' % results_dir)
            if not os.path.exists(results_dir):
                os.mkdir(results_dir)
            env['GTEST_OUTPUT'] = 'xml:%s/' % results_dir
            env['KEA_SOCKET_TEST_DIR'] = '/tmp/'
            execute('make check -k', cwd=src_path, env=env, timeout=60 * 60, raise_error=False, check_times=check_times)

            results = {}
            grand_total = 0
            grand_not_passed = 0
            for fn in os.listdir(results_dir):
                if not fn.endswith('.xml'):
                    continue
                fp = os.path.join(results_dir, fn)
                tree = ET.parse(fp)
                root = tree.getroot()
                total = int(root.get('tests'))
                failures = int(root.get('failures'))
                disabled = int(root.get('disabled'))
                errors = int(root.get('errors'))
                results[fn] = dict(total=total, failures=failures, disabled=disabled, errors=errors)
                grand_total += total
                grand_not_passed += failures + errors

            grand_passed = grand_total - grand_not_passed
            results['grand_passed'] = grand_total - grand_not_passed
            results['grand_total'] = grand_total

            result = '%s/%s passed' % (grand_passed, grand_total)
            if grand_not_passed > 0 or grand_total == 0:
                result = red(result)
            else:
                result = green(result)
            log.info('Unit test results: %s', result)

            with open('unit-test-results.json', 'w') as f:
                f.write(json.dumps(results))
794 795

        if 'install' in features:
796
            execute('sudo make install', cwd=src_path, env=env, check_times=check_times)
797 798 799 800

    execute('df -h')


801
def build_in_vagrant(provider, system, sys_revision, features, leave_system, tarball_path, dry_run, quiet, clean_start, check_times):
802 803 804 805 806 807
    log.info('')
    log.info(">>> Building %s, %s, %s" % (provider, system, sys_revision))
    log.info('')

    t0 = time.time()

808 809 810
    error = None
    total = 0
    passed = 0
811
    try:
812 813 814
        ve = VagrantEnv(provider, system, sys_revision, features, 'kea', dry_run, quiet, check_times)
        if clean_start:
            ve.destroy(force=True)
815
        ve.up()
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
        ve.prepare_deps()
        total, passed = ve.run_build_and_test(tarball_path)
        msg = ' - ' + green('all ok')
    except KeyboardInterrupt as e:
        error = e
        msg = ' - keyboard interrupt'
    except ExecutionError as e:
        error = e
        msg = ' - ' + red(str(e))
    except Exception as e:
        log.exception('Building erred')
        error = e
        msg = ' - ' + red(str(e))
    finally:
        if not leave_system:
            ve.destroy(force=True)
832 833 834 835 836

    t1 = time.time()
    dt = int(t1 - t0)

    log.info('')
837
    log.info(">>> Building %s, %s, %s completed in %s:%s%s", provider, system, sys_revision, dt // 60, dt % 60, msg)
838 839
    log.info('')

840
    return dt, error, total, passed
841 842


843 844 845
def package_box(provider, system, sys_revision, features, dry_run, check_times):
    ve = VagrantEnv(provider, system, sys_revision, features, 'bare', dry_run, check_times=check_times)
    ve.destroy(force=True)
846
    ve.up()
847 848
    ve.prepare_deps()
    # TODO cleanup
849 850 851
    ve.package()


852 853 854 855
def prepare_system(provider, system, sys_revision, features, dry_run, check_times, clean_start):
    ve = VagrantEnv(provider, system, sys_revision, features, 'kea', dry_run, check_times=check_times)
    if clean_start:
        ve.destroy(force=True)
856
    ve.up()
857
    ve.prepare_deps()
858 859


860 861
def ssh(provider, system, sys_revision, features, dry_run):
    ve = VagrantEnv(provider, system, sys_revision, features, 'kea', dry_run)
862 863 864 865
    ve.up()
    ve.ssh()


866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
def ensure_hammer_deps():
    distro, _ = get_system_revision()

    exitcode = execute('vagrant version', raise_error=False)
    if exitcode != 0:
        if distro in ['fedora', 'centos', 'rhel']:
            execute('wget --no-verbose -O /tmp/vagrant_2.2.2_x86_64.rpm https://releases.hashicorp.com/vagrant/2.2.2/vagrant_2.2.2_x86_64.rpm')
            execute('sudo rpm -i /tmp/vagrant_2.2.2_x86_64.rpm')
            os.unlink('/tmp/vagrant_2.2.2_x86_64.rpm')
        elif distro in ['debian', 'ubuntu']:
            execute('wget --no-verbose -O /tmp/vagrant_2.2.2_x86_64.deb https://releases.hashicorp.com/vagrant/2.2.2/vagrant_2.2.2_x86_64.deb')
            execute('sudo dpkg -i /tmp/vagrant_2.2.2_x86_64.deb')
            os.unlink('/tmp/vagrant_2.2.2_x86_64.deb')
        else:
            # TODO: check for packages here: https://www.vagrantup.com/downloads.html
            raise NotImplementedError

    exitcode = execute('vagrant plugin list | grep vagrant-lxc', raise_error=False)
    if exitcode != 0:
        execute('vagrant plugin install vagrant-lxc')


888
DEFAULT_FEATURES = ['install', 'unittest', 'docs']
889
ALL_FEATURES = ['install', 'unittest', 'docs', 'mysql', 'pgsql', 'cql', 'native-pkg']
890 891 892 893

def parse_args():
    parser = argparse.ArgumentParser(description='Kea develepment environment management tool.')

894
    parser.add_argument('command', choices=['package-box', 'prepare-system', 'build', 'prepare-deps', 'list-systems', 'ssh', 'ensure-hammer-deps'],
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
                        help='Commands.')
    parser.add_argument('-p', '--provider', default='virtualbox', choices=['lxc', 'virtualbox', 'all', 'local'],
                        help="Backend build executor. If 'all' then build is executed several times on all providers. "
                        "If 'local' then build is executed on current system. Default is 'virtualbox'.")
    parser.add_argument('-s', '--system', default='all', choices=list(SYSTEMS.keys()) + ['all'],
                        help="Build is executed on selected system. If 'all' then build is executed several times on all systems. "
                        "If provider is 'local' then this option is ignored. Default is 'all'.")
    parser.add_argument('-r', '--revision', default='all',
                        help="Revision of selected system. If 'all' then build is executed several times "
                        "on all revisions of selected system. To list supported systems and their revisions invoke 'list-systems'. "
                        "Default is 'all'.")
    parser.add_argument('-w', '--with', nargs='+', default=set(), choices=ALL_FEATURES,
                        help="Enabled, comma-separated features. Default is '%s'." % ' '.join(DEFAULT_FEATURES))
    parser.add_argument('-x', '--without', nargs='+', default=set(), choices=ALL_FEATURES,
                        help="Disabled, comma-separated features. Default is ''.")
    parser.add_argument('-l', '--leave-system', action='store_true',
911
                        help='At the end of the command do not destroy vagrant system. Default behavior is destroing the system.')
912 913 914
    parser.add_argument('-t', '--from-tarball',
                        help='Instead of building sources in current folder use provided tarball package (e.g. tar.gz).')
    parser.add_argument('-v', '--verbose', action='store_true', help='Enable verbose mode.')
915 916 917 918
    parser.add_argument('-q', '--quiet', action='store_true', help='Enable quiet mode.')
    parser.add_argument('-n', '--dry-run', action='store_true', help='Print only what would be done.')
    parser.add_argument('-c', '--clean-start', action='store_true', help='If there is pre-existing system then it is destroyed first.')
    parser.add_argument('-i', '--check-times', action='store_true', help='Do not allow executing commands infinitelly.')
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946

    args = parser.parse_args()

    return args


def list_systems():
    for system, revisions in SYSTEMS.items():
        print('%s:' % system)
        for r in revisions:
            providers = []
            for p in ['lxc', 'virtualbox']:
                k = '%s-%s-%s' % (system, r, p)
                if k in IMAGE_TEMPLATES:
                    providers.append(p)
            providers = ', '.join(providers)
            print('  - %s: %s' % (r, providers))


def _what_features(args):
    features = set(vars(args)['with'])
    features = features.union(DEFAULT_FEATURES)
    nofeatures = set(args.without)
    features = features.difference(nofeatures)

    return features


947
def _print_summary(results, features):
948
    print("")
949 950 951
    print("+===== Hammer Summary ====================================================+")
    print("|   provider |     system | revision |  duration |  status |   unit tests |")
    print("+------------+------------+----------+-----------+---------+--------------+")
952 953 954
    total_dt = 0
    for key, result in results.items():
        provider, system, revision = key
955 956
        dt, error, ut_total, ut_passed = result

957
        total_dt += dt
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
        if error is None:
            status = '     %s' % green('ok')
        elif error == 'not run':
            status = blue('not run')
        else:
            status = '  %s' % red('error')

        if 'unittest' in features:
            ut_results = '%s/%s' % (ut_passed, ut_total)
            padding = ' ' * (12 - len(ut_results))
            if ut_passed < ut_total or ut_total == 0:
                ut_results = padding + red(ut_results)
            else:
                ut_results = padding + green(ut_results)
        else:
            ut_results = ' not planned'
        print('| %10s | %10s | %8s | %6d:%02d | %s | %s |' % (provider, system, revision, dt // 60, dt % 60, status, ut_results))
    print("+------------+------------+----------+-----------+---------+--------------+")
    print("|                               Total: %6d:%02d |                        |" % (total_dt // 60, total_dt % 60))
    print("+=========================================================================+")
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996


def main():
    args = parse_args()

    level = logging.INFO
    if args.verbose:
        level = logging.DEBUG

    format = '[HAMMER]  %(asctime)-15s  %(message)s'
    logging.basicConfig(format=format, level=level)

    features = _what_features(args)

    if args.command == 'list-systems':
        list_systems()

    elif args.command == "package-box":
        log.info('Enabled features: %s', ' '.join(features))
997
        package_box(args.provider, args.system, args.revision, features, args.dry_run, args.check_times)
998 999 1000

    elif args.command == "prepare-system":
        log.info('Enabled features: %s', ' '.join(features))
1001
        prepare_system(args.provider, args.system, args.revision, features, args.dry_run, args.check_times, args.clean_start)
1002 1003 1004 1005

    elif args.command == "build":
        log.info('Enabled features: %s', ' '.join(features))
        if args.provider == 'local':
1006
            build_local(features, args.from_tarball, args.check_times)
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
            return

        if args.provider == 'all':
            providers = ['lxc', 'virtualbox']
        else:
            providers = [args.provider]

        if args.system == 'all':
            systems = SYSTEMS.keys()
        else:
            systems = [args.system]

1019
        plan = []
1020
        results = {}
1021
        log.info('Build plan:')
1022 1023 1024 1025 1026 1027 1028 1029
        for provider in providers:
            for system in systems:
                if args.revision == 'all':
                    revisions = SYSTEMS[system]
                else:
                    revisions = [args.revision]

                for revision in revisions:
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
                    if args.revision == 'all':
                        key = '%s-%s-%s' % (system, revision, provider)
                        if key not in IMAGE_TEMPLATES:
                            continue
                    plan.append((provider, system, revision))
                    log.info(' - %s, %s, %s', provider, system, revision)
                    results[(provider, system, revision)] = (0, 'not run')

        fail = False
        for provider, system, revision in plan:
            duration, error, total, passed = build_in_vagrant(provider, system, revision, features, args.leave_system, args.from_tarball,
                                                              args.dry_run, args.quiet, args.clean_start, args.check_times)
            results[(provider, system, revision)] = (duration, error, total, passed)

            if error:
                fail = True
                if isinstance(error, KeyboardInterrupt):
                    break
1048

1049
        _print_summary(results, features)
1050 1051 1052 1053 1054 1055

        if fail:
            sys.exit(1)

    elif args.command == "prepare-deps":
        log.info('Enabled features: %s', ' '.join(features))
1056
        prepare_deps_local(features, args.check_times)
1057 1058

    elif args.command == "ssh":
1059 1060 1061 1062
        ssh(args.provider, args.system, args.revision, features, args.dry_run)

    elif args.command == "ensure-hammer-deps":
        ensure_hammer_deps()
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072


if __name__ == '__main__':
    # results = {
    #     ('virtualbox', 'centos', '7'): (920, False),
    #     ('lxc', 'fedora', '29'): (120, False),
    # }
    # _print_summary(results)

    main()