[libvirt] [jenkins-ci PATCH v3 00/12] lcitool: Rewrite in Python (and add Dockefile generator)

pylint is reasonably happy with the script now: $ pylint lcitool No config file found, using default configuration ************* Module lcitool C: 1, 0: Missing module docstring (missing-docstring) C: 37, 0: Missing class docstring (missing-docstring) C: 47, 4: Missing method docstring (missing-docstring) W:108,15: Catching too general exception Exception (broad-except) R:427, 4: Too many branches (14/12) (too-many-branches) R:289, 0: Too few public methods (1/2) (too-few-public-methods) ------------------------------------------------------------------- Your code has been rated at 9.21/10 (previous run: 9.21/10, +0.00) The remaining issues are not considered blockers and will be addressed, if at all, later down the line. Changes from [v2]: * address review comments; * improve pycodestyle and pylint compliance; * replace FSF address with an URL. Changes from [v1]: * add Dockerfile generator; * rename the 'list' action to 'hosts' to better fit along with the additional 'projects' action; * always list items in alphabetical order; * move some generic functions to an Util class. [v2] https://www.redhat.com/archives/libvir-list/2018-July/msg00795.html [v1] https://www.redhat.com/archives/libvir-list/2018-July/msg00717.html Andrea Bolognani (12): lcitool: Drop shell implementation lcitool: Stub out Python implementation lcitool: Add tool configuration handling lcitool: Add inventory handling lcitool: Implement the 'hosts' action lcitool: Implement the 'install' action lcitool: Implement the 'update' action guests: Update documentation guests: Add Docker-related information to the inventory lcitool: Add projects information handling lcitool: Implement the 'projects' action lcitool: Implement the 'dockerfile' action guests/README.markdown | 8 +- guests/host_vars/libvirt-centos-7/docker.yml | 2 + guests/host_vars/libvirt-debian-8/docker.yml | 2 + guests/host_vars/libvirt-debian-9/docker.yml | 2 + .../host_vars/libvirt-debian-sid/docker.yml | 2 + guests/host_vars/libvirt-fedora-27/docker.yml | 2 + guests/host_vars/libvirt-fedora-28/docker.yml | 2 + .../libvirt-fedora-rawhide/docker.yml | 2 + guests/host_vars/libvirt-ubuntu-16/docker.yml | 2 + guests/host_vars/libvirt-ubuntu-18/docker.yml | 2 + guests/lcitool | 730 ++++++++++++------ 11 files changed, 529 insertions(+), 227 deletions(-) create mode 100644 guests/host_vars/libvirt-centos-7/docker.yml create mode 100644 guests/host_vars/libvirt-debian-8/docker.yml create mode 100644 guests/host_vars/libvirt-debian-9/docker.yml create mode 100644 guests/host_vars/libvirt-debian-sid/docker.yml create mode 100644 guests/host_vars/libvirt-fedora-27/docker.yml create mode 100644 guests/host_vars/libvirt-fedora-28/docker.yml create mode 100644 guests/host_vars/libvirt-fedora-rawhide/docker.yml create mode 100644 guests/host_vars/libvirt-ubuntu-16/docker.yml create mode 100644 guests/host_vars/libvirt-ubuntu-18/docker.yml -- 2.17.1

We're going to rewrite the script completely, and getting the current implementation out of the way firts will make the diffs more reasonable. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 227 ------------------------------------------------- 1 file changed, 227 deletions(-) delete mode 100755 guests/lcitool diff --git a/guests/lcitool b/guests/lcitool deleted file mode 100755 index 0c1520e..0000000 --- a/guests/lcitool +++ /dev/null @@ -1,227 +0,0 @@ -#!/bin/sh - -# ------------------- -# Utility functions -# ------------------- - -# die MESSAGE -# -# Abort the program after displaying $MESSAGE on standard error. -die() { - echo "$1" >&2 - exit 1 -} - -# hash_file PASS_FILE -# -# Generate a password hash from the contents of PASS_FILE. -hash_file() { - PASS_FILE="$1" - - perl -le ' - my @chars = ("A".."Z", "a".."z", "0".."9"); - my $salt; $salt .= $chars[rand @chars] for 1..16; - my $handle; open($handle, "'"$PASS_FILE"'"); - my $pass = <$handle>; chomp($pass); - print crypt("$pass", "\$6\$$salt\$");' -} - -# yaml_var FILE VAR -# -# Read $FILE and output the value of YAML variable $VAR. Only trivial YAML -# values are supported, eg. strings and numbers that don't depend on the -# value of other variables. That's enough for our use case. -yaml_var() { - grep "^$2:\\s*" "$1" 2>/dev/null | tail -1 | sed "s/$2:\\s*//g" -} - -# load_install_config FILE -# -# Read all known configuration variables from $FILE and set them in the -# environment. Configuration variables that have already been set in -# the environment will not be updated. -load_install_config() { - INSTALL_URL=${INSTALL_URL:-$(yaml_var "$1" install_url)} - INSTALL_CONFIG=${INSTALL_CONFIG:-$(yaml_var "$1" install_config)} - INSTALL_VIRT_TYPE=${INSTALL_VIRT_TYPE:-$(yaml_var "$1" install_virt_type)} - INSTALL_ARCH=${INSTALL_ARCH:-$(yaml_var "$1" install_arch)} - INSTALL_MACHINE=${INSTALL_MACHINE:-$(yaml_var "$1" install_machine)} - INSTALL_CPU_MODEL=${INSTALL_CPU_MODEL:-$(yaml_var "$1" install_cpu_model)} - INSTALL_VCPUS=${INSTALL_VCPUS:-$(yaml_var "$1" install_vcpus)} - INSTALL_MEMORY_SIZE=${INSTALL_MEMORY_SIZE:-$(yaml_var "$1" install_memory_size)} - INSTALL_DISK_SIZE=${INSTALL_DISK_SIZE:-$(yaml_var "$1" install_disk_size)} - INSTALL_STORAGE_POOL=${INSTALL_STORAGE_POOL:-$(yaml_var "$1" install_storage_pool)} - INSTALL_NETWORK=${INSTALL_NETWORK:-$(yaml_var "$1" install_network)} -} - -# load_config -# -# Read tool configuration and perform the necessary validation. -load_config() { - CONFIG_DIR="$HOME/.config/$PROGRAM_NAME" - - mkdir -p "$CONFIG_DIR" >/dev/null 2>&1 || { - die "$PROGRAM_NAME: $CONFIG_DIR: Unable to create config directory" - } - - FLAVOR_FILE="$CONFIG_DIR/flavor" - VAULT_PASS_FILE="$CONFIG_DIR/vault-password" - ROOT_PASS_FILE="$CONFIG_DIR/root-password" - - # Two flavors are supported: test (default) and jenkins. Read the - # flavor from configuration, validate it and write it back in case - # it was not present - FLAVOR="$(cat "$FLAVOR_FILE" 2>/dev/null)" - FLAVOR=${FLAVOR:-test} - test "$FLAVOR" = test || test "$FLAVOR" = jenkins || { - die "$PROGRAM_NAME: Invalid flavor '$FLAVOR'" - } - echo "$FLAVOR" >"$FLAVOR_FILE" || { - die "$PROGRAM_NAME: $FLAVOR_FILE: Unable to save flavor" - } - - test "$FLAVOR" = jenkins && { - # The vault password is only needed for the jenkins flavor, so only - # validate it in that case - test -f "$VAULT_PASS_FILE" && test "$(cat "$VAULT_PASS_FILE")" || { - die "$PROGRAM_NAME: $VAULT_PASS_FILE: Missing or invalid password" - } - } || { - # For other flavors, undefine the variable so that Ansible - # will not try to read the file at all - VAULT_PASS_FILE= - } - - # Make sure the root password has been configured properly - test -f "$ROOT_PASS_FILE" && test "$(cat "$ROOT_PASS_FILE")" || { - die "$PROGRAM_NAME: $ROOT_PASS_FILE: Missing or invalid password" - } - - ROOT_HASH_FILE="$CONFIG_DIR/.root-password.hash" - - # Regenerate root password hash. Ansible expects passwords as hashes but - # doesn't provide a built-in facility to generate one from plain text - hash_file "$ROOT_PASS_FILE" >"$ROOT_HASH_FILE" || { - die "$PROGRAM_NAME: Failure while hashing root password" - } -} - -# ---------------------- -# User-visible actions -# ---------------------- - -do_help() { - echo "\ -Usage: $CALL_NAME ACTION [OPTIONS] - -Actions: - list List known guests - install GUEST Install GUEST - prepare GUEST|all Prepare or update GUEST. Can be run multiple times - update GUEST|all Alias for prepare - help Display this help" -} - -do_list() { - # List all guests present in the inventory. Skip group names, - # comments and empty lines - grep -vE '^#|^\[|^$' inventory | sort -u -} - -do_install() -{ - GUEST="$1" - - test "$GUEST" || { - die "$(do_help)" - } - do_list | grep -q "$GUEST" || { - die "$PROGRAM_NAME: $GUEST: Unknown guest" - } - test -f "host_vars/$GUEST/install.yml" || { - die "$PROGRAM_NAME: $GUEST: Missing configuration, guest must be installed manually" - } - - load_config - - # Load configuration files. Values don't get overwritten after being - # set the first time, so loading the host-specific configuration before - # the group configuration ensures overrides work as expected - load_install_config "host_vars/$GUEST/install.yml" - load_install_config "group_vars/all/install.yml" - - # Both memory size and disk size use GiB as unit, but virt-install wants - # disk size in GiB and memory size in *MiB*, so perform conversion here - INSTALL_MEMORY_SIZE=$(expr "$INSTALL_MEMORY_SIZE" \* 1024 2>/dev/null) - - # preseed files must use a well-known name to be picked up by d-i; - # for kickstart files, we can use whatever name we please but we need - # to point anaconda in the right direction through a kernel argument - case "$INSTALL_CONFIG" in - *kickstart*|*ks*) EXTRA_ARGS="ks=file:/${INSTALL_CONFIG##*/}" ;; - esac - - # Only configure autostart for the guest for the jenkins flavor - test "$FLAVOR" = jenkins && { - AUTOSTART="--autostart" - } - - virt-install \ - --name "$GUEST" \ - --location "$INSTALL_URL" \ - --virt-type "$INSTALL_VIRT_TYPE" \ - --arch "$INSTALL_ARCH" \ - --machine "$INSTALL_MACHINE" \ - --cpu "$INSTALL_CPU_MODEL" \ - --vcpus "$INSTALL_VCPUS" \ - --memory "$INSTALL_MEMORY_SIZE" \ - --disk "size=$INSTALL_DISK_SIZE,pool=$INSTALL_STORAGE_POOL,bus=virtio" \ - --network "network=$INSTALL_NETWORK,model=virtio" \ - --graphics none \ - --console pty \ - --sound none \ - --initrd-inject "$INSTALL_CONFIG" \ - --extra-args "console=ttyS0 $EXTRA_ARGS" \ - $AUTOSTART \ - --wait 0 -} - -do_prepare() { - GUEST="$1" - - test "$GUEST" || { - die "$(do_help)" - } - do_list | grep -q "$GUEST" || test "$GUEST" = all || { - die "$PROGRAM_NAME: $GUEST: Unknown guest" - } - - load_config - - EXTRA_VARS="flavor=$FLAVOR root_password_file=$ROOT_HASH_FILE" - - ansible-playbook \ - --vault-password-file "$VAULT_PASS_FILE" \ - --extra-vars "$EXTRA_VARS" \ - -l "$GUEST" \ - site.yml -} - -# --------------------- -# Program entry point -# --------------------- - -CALL_NAME="$0" -PROGRAM_NAME="${0##*/}" - -test -f "$PROGRAM_NAME" || { - die "$PROGRAM_NAME: Must be run from the source directory" -} - -case "$1" in - list) do_list ;; - install) do_install "$2" ;; - prepare|update) do_prepare "$2" ;; - *help) do_help ;; - *) die "$(do_help)" ;; -esac -- 2.17.1

Doesn't do much right now, but it's a start :) Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 guests/lcitool diff --git a/guests/lcitool b/guests/lcitool new file mode 100755 index 0000000..5ca885f --- /dev/null +++ b/guests/lcitool @@ -0,0 +1,46 @@ +#!/usr/bin/env python + +# lcitool - libvirt CI guest management tool +# Copyright (C) 2017-2018 Andrea Bolognani <abologna@redhat.com> +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see <https://www.gnu.org/licenses/>. + +import argparse +import sys + + +class Error(Exception): + + def __init__(self, message): + super(Error, self).__init__() + self.message = message + + +class Application: + + def __init__(self): + self._parser = argparse.ArgumentParser( + description="libvirt CI guest management tool", + ) + + def run(self): + self._parser.parse_args() + + +if __name__ == "__main__": + try: + Application().run() + except Error as err: + sys.stderr.write("{}: {}\n".format(sys.argv[0], err.message)) + sys.exit(1) -- 2.17.1

The on-disk configuration format and its behavior are fully backwards compatible with the previous implementation. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/guests/lcitool b/guests/lcitool index 5ca885f..70dcaff 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -17,6 +17,10 @@ # with this program. If not, see <https://www.gnu.org/licenses/>. import argparse +import crypt +import os +import random +import string import sys @@ -27,9 +31,120 @@ class Error(Exception): self.message = message +class Util: + + @staticmethod + def mksalt(): + alphabeth = string.ascii_letters + string.digits + salt = "".join(random.choice(alphabeth) for x in range(0, 16)) + return "$6${}$".format(salt) + + +class Config: + + @staticmethod + def _get_config_file(name): + try: + config_dir = os.environ["XDG_CONFIG_HOME"] + except KeyError: + config_dir = os.path.join(os.environ["HOME"], ".config/") + config_dir = os.path.join(config_dir, "lcitool/") + + # Create the directory if it doesn't already exist + if not os.path.exists(config_dir): + try: + os.mkdir(config_dir) + except Exception: + raise Error( + "Can't create configuration directory ({})".format( + config_dir, + ) + ) + + return os.path.join(config_dir, name) + + def get_flavor(self): + flavor_file = self._get_config_file("flavor") + + try: + with open(flavor_file, "r") as infile: + flavor = infile.readline().strip() + except Exception: + # If the flavor has not been configured, we choose the default + # and store it on disk to ensure consistent behavior from now on + flavor = "test" + try: + with open(flavor_file, "w") as infile: + infile.write("{}\n".format(flavor)) + except Exception: + raise Error( + "Can't write flavor file ({})".format( + flavor_file, + ) + ) + + if flavor not in ["test", "jenkins"]: + raise Error("Invalid flavor '{}'".format(flavor)) + + return flavor + + def get_vault_password_file(self): + vault_pass_file = None + + # The vault password is only needed for the jenkins flavor, but in + # that case we want to make sure there's *something* in there + if self.get_flavor() != "test": + vault_pass_file = self._get_config_file("vault-password") + + try: + with open(vault_pass_file, "r") as infile: + if not infile.readline().strip(): + raise ValueError + except Exception: + raise Error( + "Missing or invalid vault password file ({})".format( + vault_pass_file, + ) + ) + + return vault_pass_file + + def get_root_password_file(self): + root_pass_file = self._get_config_file("root-password") + root_hash_file = self._get_config_file(".root-password.hash") + + try: + with open(root_pass_file, "r") as infile: + root_pass = infile.readline().strip() + except Exception: + raise Error( + "Missing or invalid root password file ({})".format( + root_pass_file, + ) + ) + + # The hash will be different every time we run, but that doesn't + # matter - it will still validate the correct root password + root_hash = crypt.crypt(root_pass, Util.mksalt()) + + try: + with open(root_hash_file, "w") as infile: + infile.write("{}\n".format(root_hash)) + except Exception: + raise Error( + "Can't write hashed root password file ({})".format( + root_hash_file, + ) + ) + + return root_hash_file + + class Application: def __init__(self): + self._config = Config() + self._parser = argparse.ArgumentParser( description="libvirt CI guest management tool", ) -- 2.17.1

We use an actual YAML parser this time around, and bring the behavior more in line with what Ansible is doing, so interoperability should be more solid overall. New in this implementation is more flexibility in defining host lists, including support for explicit lists as well as glob patterns. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/guests/lcitool b/guests/lcitool index 70dcaff..ffea969 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -18,10 +18,18 @@ import argparse import crypt +import fnmatch import os import random import string import sys +import yaml + +# This is necessary to maintain Python 2.7 compatibility +try: + import configparser +except ImportError: + import ConfigParser as configparser class Error(Exception): @@ -39,6 +47,32 @@ class Util: salt = "".join(random.choice(alphabeth) for x in range(0, 16)) return "$6${}$".format(salt) + @staticmethod + def expand_pattern(pattern, source, name): + if pattern is None: + raise Error("Missing {} list".format(name)) + + if pattern == "all": + pattern = "*" + + # This works correctly for single items as well as more complex + # cases such as explicit lists, glob patterns and any combination + # of the above + matches = [] + for partial_pattern in pattern.split(","): + + partial_matches = [] + for item in source: + if fnmatch.fnmatch(item, partial_pattern): + partial_matches += [item] + + if not partial_matches: + raise Error("Invalid {} list '{}'".format(name, pattern)) + + matches += partial_matches + + return sorted(set(matches)) + class Config: @@ -140,10 +174,75 @@ class Config: return root_hash_file +class Inventory: + + def __init__(self): + try: + parser = configparser.SafeConfigParser() + parser.read("./ansible.cfg") + inventory_path = parser.get("defaults", "inventory") + except Exception: + raise Error("Can't find inventory location in ansible.cfg") + + self._facts = {} + try: + # We can only deal with trivial inventories, but that's + # all we need right now and we can expand support further + # later on if necessary + with open(inventory_path, "r") as infile: + for line in infile: + host = line.strip() + self._facts[host] = {} + except Exception: + raise Error( + "Missing or invalid inventory ({})".format( + inventory_path, + ) + ) + + for host in self._facts: + try: + self._facts[host] = self._read_all_facts(host) + self._facts[host]["inventory_hostname"] = host + except Exception: + raise Error("Can't load facts for '{}'".format(host)) + + @staticmethod + def _add_facts_from_file(facts, yaml_path): + with open(yaml_path, "r") as infile: + some_facts = yaml.load(infile) + for fact in some_facts: + facts[fact] = some_facts[fact] + + def _read_all_facts(self, host): + facts = {} + + # We load from group_vars/ first and host_vars/ second, sorting + # files alphabetically; doing so should result in our view of + # the facts matching Ansible's + for source in ["./group_vars/all/", "./host_vars/{}/".format(host)]: + for item in sorted(os.listdir(source)): + yaml_path = os.path.join(source, item) + if not os.path.isfile(yaml_path): + continue + if not yaml_path.endswith(".yml"): + continue + self._add_facts_from_file(facts, yaml_path) + + return facts + + def expand_pattern(self, pattern): + return Util.expand_pattern(pattern, self._facts, "host") + + def get_facts(self, host): + return self._facts[host] + + class Application: def __init__(self): self._config = Config() + self._inventory = Inventory() self._parser = argparse.ArgumentParser( description="libvirt CI guest management tool", -- 2.17.1

This replaces the 'list' action from the original implementation. We're going to list more than just hosts over time, so a more specific name is warranted. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/guests/lcitool b/guests/lcitool index ffea969..2df231a 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -23,6 +23,7 @@ import os import random import string import sys +import textwrap import yaml # This is necessary to maintain Python 2.7 compatibility @@ -245,11 +246,34 @@ class Application: self._inventory = Inventory() self._parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, description="libvirt CI guest management tool", + epilog=textwrap.dedent(""" + informational actions: + hosts list all known hosts + """), ) + self._parser.add_argument( + "-a", + metavar="ACTION", + required=True, + help="action to perform (see below)", + ) + + def _action_hosts(self): + for host in self._inventory.expand_pattern("all"): + print(host) def run(self): - self._parser.parse_args() + cmdline = self._parser.parse_args() + action = cmdline.a + + method = "_action_{}".format(action.replace("-", "_")) + + if hasattr(self, method): + getattr(self, method).__call__() + else: + raise Error("Invalid action '{}'".format(action)) if __name__ == "__main__": -- 2.17.1

Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 75 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/guests/lcitool b/guests/lcitool index 2df231a..5156869 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -22,6 +22,7 @@ import fnmatch import os import random import string +import subprocess import sys import textwrap import yaml @@ -246,11 +247,17 @@ class Application: self._inventory = Inventory() self._parser = argparse.ArgumentParser( + conflict_handler="resolve", formatter_class=argparse.RawDescriptionHelpFormatter, description="libvirt CI guest management tool", epilog=textwrap.dedent(""" + common actions: + install perform unattended host installation + informational actions: hosts list all known hosts + + glob patterns are supported for HOSTS """), ) self._parser.add_argument( @@ -259,19 +266,83 @@ class Application: required=True, help="action to perform (see below)", ) + self._parser.add_argument( + "-h", + metavar="HOSTS", + help="list of hosts to act on", + ) - def _action_hosts(self): + def _action_hosts(self, _hosts): for host in self._inventory.expand_pattern("all"): print(host) + def _action_install(self, hosts): + flavor = self._config.get_flavor() + + for host in self._inventory.expand_pattern(hosts): + facts = self._inventory.get_facts(host) + + # Both memory size and disk size are stored as GiB in the + # inventory, but virt-install expects the disk size in GiB + # and the memory size in *MiB*, so perform conversion here + memory_arg = str(int(facts["install_memory_size"]) * 1024) + + vcpus_arg = str(facts["install_vcpus"]) + + disk_arg = "size={},pool={},bus=virtio".format( + facts["install_disk_size"], + facts["install_storage_pool"], + ) + network_arg = "network={},model=virtio".format( + facts["install_network"], + ) + + # preseed files must use a well-known name to be picked up by + # d-i; for kickstart files, we can use whatever name we please + # but we need to point anaconda in the right direction through + # a kernel argument + extra_arg = "console=ttyS0 ks=file:/{}".format( + facts["install_config"], + ) + + cmd = [ + "virt-install", + "--name", host, + "--location", facts["install_url"], + "--virt-type", facts["install_virt_type"], + "--arch", facts["install_arch"], + "--machine", facts["install_machine"], + "--cpu", facts["install_cpu_model"], + "--vcpus", vcpus_arg, + "--memory", memory_arg, + "--disk", disk_arg, + "--network", network_arg, + "--graphics", "none", + "--console", "pty", + "--sound", "none", + "--initrd-inject", facts["install_config"], + "--extra-args", extra_arg, + "--wait", "0", + ] + + # Only configure autostart for the guest for the jenkins flavor + if flavor == "jenkins": + cmd += ["--autostart"] + + try: + subprocess.check_call(cmd) + except Exception: + raise Error("Failed to install '{}'".format(host)) + def run(self): cmdline = self._parser.parse_args() action = cmdline.a + hosts = cmdline.h method = "_action_{}".format(action.replace("-", "_")) if hasattr(self, method): - getattr(self, method).__call__() + getattr(self, method).__call__(hosts) else: raise Error("Invalid action '{}'".format(action)) -- 2.17.1

The 'prepare' alias was kinda redundant and offered dubious value, so it has been dropped. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/guests/lcitool b/guests/lcitool index 5156869..ba5ed5d 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -253,6 +253,7 @@ class Application: epilog=textwrap.dedent(""" common actions: install perform unattended host installation + update prepare hosts and keep them updated informational actions: hosts list all known hosts @@ -334,6 +335,35 @@ class Application: except Exception: raise Error("Failed to install '{}'".format(host)) + def _action_update(self, hosts): + flavor = self._config.get_flavor() + vault_pass_file = self._config.get_vault_password_file() + root_pass_file = self._config.get_root_password_file() + + ansible_hosts = ",".join(self._inventory.expand_pattern(hosts)) + + extra_vars = "flavor={} root_password_file={}".format( + flavor, + root_pass_file, + ) + + cmd = ["ansible-playbook"] + + # Provide the vault password if available + if vault_pass_file is not None: + cmd += ["--vault-password-file", vault_pass_file] + + cmd += [ + "--limit", ansible_hosts, + "--extra-vars", extra_vars, + "./site.yml", + ] + + try: + subprocess.check_call(cmd) + except Exception: + raise Error("Failed to update '{}'".format(hosts)) + def run(self): cmdline = self._parser.parse_args() action = cmdline.a -- 2.17.1

Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/README.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/guests/README.markdown b/guests/README.markdown index bc780f3..4a40619 100644 --- a/guests/README.markdown +++ b/guests/README.markdown @@ -6,16 +6,16 @@ of the guests used by the Jenkins-based libvirt CI environment. There are two steps to bringing up a guest: -* `./lcitool install $guest` will perform an unattended installation +* `./lcitool -a install -h $guest` will perform an unattended installation of `$guest`. Not all guests can be installed this way: see the "FreeBSD" section below; -* `./lcitool prepare $guest` will go through all the post-installation +* `./lcitool -a update -h $guest` will go through all the post-installation configuration steps required to make the newly-created guest usable; Once those steps have been performed, maintainance will involve running: -* `./lcitool update $guest` +* `./lcitool -a update -h $guest` periodically to ensure the guest configuration is sane and all installed packages are updated. @@ -40,7 +40,7 @@ you'll want to use the `libvirt_guest` variant of the plugin. To keep guests up to date over time, it's recommended to have an entry along the lines of - 0 0 * * * cd ~/libvirt-jenkins-ci/guests && ./lcitool update all + 0 0 * * * cd ~/libvirt-jenkins-ci/guests && ./lcitool -a update -h all in your crontab. -- 2.17.1

Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/host_vars/libvirt-centos-7/docker.yml | 2 ++ guests/host_vars/libvirt-debian-8/docker.yml | 2 ++ guests/host_vars/libvirt-debian-9/docker.yml | 2 ++ guests/host_vars/libvirt-debian-sid/docker.yml | 2 ++ guests/host_vars/libvirt-fedora-27/docker.yml | 2 ++ guests/host_vars/libvirt-fedora-28/docker.yml | 2 ++ guests/host_vars/libvirt-fedora-rawhide/docker.yml | 2 ++ guests/host_vars/libvirt-ubuntu-16/docker.yml | 2 ++ guests/host_vars/libvirt-ubuntu-18/docker.yml | 2 ++ 9 files changed, 18 insertions(+) create mode 100644 guests/host_vars/libvirt-centos-7/docker.yml create mode 100644 guests/host_vars/libvirt-debian-8/docker.yml create mode 100644 guests/host_vars/libvirt-debian-9/docker.yml create mode 100644 guests/host_vars/libvirt-debian-sid/docker.yml create mode 100644 guests/host_vars/libvirt-fedora-27/docker.yml create mode 100644 guests/host_vars/libvirt-fedora-28/docker.yml create mode 100644 guests/host_vars/libvirt-fedora-rawhide/docker.yml create mode 100644 guests/host_vars/libvirt-ubuntu-16/docker.yml create mode 100644 guests/host_vars/libvirt-ubuntu-18/docker.yml diff --git a/guests/host_vars/libvirt-centos-7/docker.yml b/guests/host_vars/libvirt-centos-7/docker.yml new file mode 100644 index 0000000..59f7f12 --- /dev/null +++ b/guests/host_vars/libvirt-centos-7/docker.yml @@ -0,0 +1,2 @@ +--- +docker_base: centos:centos7 diff --git a/guests/host_vars/libvirt-debian-8/docker.yml b/guests/host_vars/libvirt-debian-8/docker.yml new file mode 100644 index 0000000..235f0fd --- /dev/null +++ b/guests/host_vars/libvirt-debian-8/docker.yml @@ -0,0 +1,2 @@ +--- +docker_base: debian:8 diff --git a/guests/host_vars/libvirt-debian-9/docker.yml b/guests/host_vars/libvirt-debian-9/docker.yml new file mode 100644 index 0000000..0b4ccee --- /dev/null +++ b/guests/host_vars/libvirt-debian-9/docker.yml @@ -0,0 +1,2 @@ +--- +docker_base: debian:9 diff --git a/guests/host_vars/libvirt-debian-sid/docker.yml b/guests/host_vars/libvirt-debian-sid/docker.yml new file mode 100644 index 0000000..e20a37e --- /dev/null +++ b/guests/host_vars/libvirt-debian-sid/docker.yml @@ -0,0 +1,2 @@ +--- +docker_base: debian:sid diff --git a/guests/host_vars/libvirt-fedora-27/docker.yml b/guests/host_vars/libvirt-fedora-27/docker.yml new file mode 100644 index 0000000..dcada18 --- /dev/null +++ b/guests/host_vars/libvirt-fedora-27/docker.yml @@ -0,0 +1,2 @@ +--- +docker_base: fedora:27 diff --git a/guests/host_vars/libvirt-fedora-28/docker.yml b/guests/host_vars/libvirt-fedora-28/docker.yml new file mode 100644 index 0000000..a874018 --- /dev/null +++ b/guests/host_vars/libvirt-fedora-28/docker.yml @@ -0,0 +1,2 @@ +--- +docker_base: fedora:28 diff --git a/guests/host_vars/libvirt-fedora-rawhide/docker.yml b/guests/host_vars/libvirt-fedora-rawhide/docker.yml new file mode 100644 index 0000000..39dda1c --- /dev/null +++ b/guests/host_vars/libvirt-fedora-rawhide/docker.yml @@ -0,0 +1,2 @@ +--- +docker_base: fedora:rawhide diff --git a/guests/host_vars/libvirt-ubuntu-16/docker.yml b/guests/host_vars/libvirt-ubuntu-16/docker.yml new file mode 100644 index 0000000..2d4eb25 --- /dev/null +++ b/guests/host_vars/libvirt-ubuntu-16/docker.yml @@ -0,0 +1,2 @@ +--- +docker_base: ubuntu:16.04 diff --git a/guests/host_vars/libvirt-ubuntu-18/docker.yml b/guests/host_vars/libvirt-ubuntu-18/docker.yml new file mode 100644 index 0000000..13d6cc1 --- /dev/null +++ b/guests/host_vars/libvirt-ubuntu-18/docker.yml @@ -0,0 +1,2 @@ +--- +docker_base: ubuntu:18.04 -- 2.17.1

The original tool's limited scope meant loading this information was not needed, but we're going to start making use of it pretty soon. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/guests/lcitool b/guests/lcitool index ba5ed5d..95d16b3 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -240,11 +240,58 @@ class Inventory: return self._facts[host] +class Projects: + + def __init__(self): + try: + with open("./vars/mappings.yml", "r") as infile: + mappings = yaml.load(infile) + self._mappings = mappings["mappings"] + except Exception: + raise Error("Can't load mappings") + + self._packages = {} + source = "./vars/projects/" + for item in os.listdir(source): + yaml_path = os.path.join(source, item) + if not os.path.isfile(yaml_path): + continue + if not yaml_path.endswith(".yml"): + continue + + project = os.path.splitext(item)[0] + + try: + with open(yaml_path, "r") as infile: + packages = yaml.load(infile) + self._packages[project] = packages["packages"] + except Exception: + raise Error("Can't load packages for '{}'".format(project)) + + def expand_pattern(self, pattern): + projects = Util.expand_pattern(pattern, self._packages, "project") + + # Some projects are internal implementation details and should + # not be exposed to the user + for project in ["base", "blacklist", "jenkins"]: + if project in projects: + projects.remove(project) + + return projects + + def get_mappings(self): + return self._mappings + + def get_packages(self, project): + return self._packages[project] + + class Application: def __init__(self): self._config = Config() self._inventory = Inventory() + self._projects = Projects() self._parser = argparse.ArgumentParser( conflict_handler="resolve", -- 2.17.1

Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/guests/lcitool b/guests/lcitool index 95d16b3..cfea077 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -303,7 +303,8 @@ class Application: update prepare hosts and keep them updated informational actions: - hosts list all known hosts + hosts list all known hosts + projects list all known projects glob patterns are supported for HOSTS """), @@ -324,6 +325,10 @@ class Application: for host in self._inventory.expand_pattern("all"): print(host) + def _action_projects(self, _hosts): + for project in self._projects.expand_pattern("all"): + print(project) + def _action_install(self, hosts): flavor = self._config.get_flavor() -- 2.17.1

This is basically the exact same algorithm used by the Ansible playbooks to process package mappings, implemented in pure Python. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- guests/lcitool | 86 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/guests/lcitool b/guests/lcitool index cfea077..22b08dd 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -306,7 +306,10 @@ class Application: hosts list all known hosts projects list all known projects - glob patterns are supported for HOSTS + uncommon actions: + dockerfile generate Dockerfile (doesn't access the host) + + glob patterns are supported for HOSTS and PROJECTS """), ) self._parser.add_argument( @@ -320,16 +323,21 @@ class Application: metavar="HOSTS", help="list of hosts to act on", ) + self._parser.add_argument( + "-p", + metavar="PROJECTS", + help="list of projects to consider", + ) - def _action_hosts(self, _hosts): + def _action_hosts(self, _hosts, _projects): for host in self._inventory.expand_pattern("all"): print(host) - def _action_projects(self, _hosts): + def _action_projects(self, _hosts, _projects): for project in self._projects.expand_pattern("all"): print(project) - def _action_install(self, hosts): + def _action_install(self, hosts, _projects): flavor = self._config.get_flavor() for host in self._inventory.expand_pattern(hosts): @@ -387,7 +395,7 @@ class Application: except Exception: raise Error("Failed to install '{}'".format(host)) - def _action_update(self, hosts): + def _action_update(self, hosts, _projects): flavor = self._config.get_flavor() vault_pass_file = self._config.get_vault_password_file() root_pass_file = self._config.get_root_password_file() @@ -416,15 +424,81 @@ class Application: except Exception: raise Error("Failed to update '{}'".format(hosts)) + def _action_dockerfile(self, hosts, projects): + mappings = self._projects.get_mappings() + + hosts = self._inventory.expand_pattern(hosts) + if len(hosts) > 1: + raise Error("Can't generate Dockerfile for multiple hosts") + host = hosts[0] + + facts = self._inventory.get_facts(host) + package_format = facts["package_format"] + os_name = facts["os_name"] + os_full = os_name + str(facts["os_version"]) + + if package_format not in ["deb", "rpm"]: + raise Error("Host {} doesn't support Dockerfiles".format(host)) + + projects = self._projects.expand_pattern(projects) + for project in projects: + if project not in facts["projects"]: + raise Error( + "Host {} doesn't support project {}".format( + host, + project, + ) + ) + + temp = {} + + # We need to add the base project manually here: the standard + # machinery hides it because it's an implementation detail + for project in projects + ["base"]: + for package in self._projects.get_packages(project): + if "default" in mappings[package]: + temp[package] = mappings[package]["default"] + if package_format in mappings[package]: + temp[package] = mappings[package][package_format] + if os_name in mappings[package]: + temp[package] = mappings[package][os_name] + if os_full in mappings[package]: + temp[package] = mappings[package][os_full] + + flattened = [] + for item in temp: + if temp[item] is not None and temp[item] not in flattened: + flattened += [temp[item]] + + print("FROM {}".format(facts["docker_base"])) + + sys.stdout.write("ENV PACKAGES ") + sys.stdout.write(" \\\n ".join(sorted(flattened))) + + if package_format == "deb": + sys.stdout.write(textwrap.dedent(""" + RUN apt-get update && \\ + apt-get install -y ${PACKAGES} && \\ + apt-get autoremove -y && \\ + apt-get autoclean -y + """)) + elif package_format == "rpm": + sys.stdout.write(textwrap.dedent(""" + RUN yum install -y ${PACKAGES} && \\ + yum autoremove -y && \\ + yum clean all -y + """)) + def run(self): cmdline = self._parser.parse_args() action = cmdline.a hosts = cmdline.h + projects = cmdline.p method = "_action_{}".format(action.replace("-", "_")) if hasattr(self, method): - getattr(self, method).__call__(hosts) + getattr(self, method).__call__(hosts, projects) else: raise Error("Invalid action '{}'".format(action)) -- 2.17.1

On Tue, Jul 17, 2018 at 06:35:16PM +0200, Andrea Bolognani wrote:
pylint is reasonably happy with the script now:
$ pylint lcitool No config file found, using default configuration ************* Module lcitool C: 1, 0: Missing module docstring (missing-docstring) C: 37, 0: Missing class docstring (missing-docstring) C: 47, 4: Missing method docstring (missing-docstring) W:108,15: Catching too general exception Exception (broad-except) R:427, 4: Too many branches (14/12) (too-many-branches) R:289, 0: Too few public methods (1/2) (too-few-public-methods)
------------------------------------------------------------------- Your code has been rated at 9.21/10 (previous run: 9.21/10, +0.00)
The remaining issues are not considered blockers and will be addressed, if at all, later down the line.
Changes from [v2]:
* address review comments; * improve pycodestyle and pylint compliance; * replace FSF address with an URL.
Changes from [v1]:
* add Dockerfile generator; * rename the 'list' action to 'hosts' to better fit along with the additional 'projects' action; * always list items in alphabetical order; * move some generic functions to an Util class.
[v2] https://www.redhat.com/archives/libvir-list/2018-July/msg00795.html [v1] https://www.redhat.com/archives/libvir-list/2018-July/msg00717.html
Andrea Bolognani (12): lcitool: Drop shell implementation lcitool: Stub out Python implementation lcitool: Add tool configuration handling lcitool: Add inventory handling lcitool: Implement the 'hosts' action lcitool: Implement the 'install' action lcitool: Implement the 'update' action guests: Update documentation guests: Add Docker-related information to the inventory lcitool: Add projects information handling lcitool: Implement the 'projects' action lcitool: Implement the 'dockerfile' action
guests/README.markdown | 8 +- guests/host_vars/libvirt-centos-7/docker.yml | 2 + guests/host_vars/libvirt-debian-8/docker.yml | 2 + guests/host_vars/libvirt-debian-9/docker.yml | 2 + .../host_vars/libvirt-debian-sid/docker.yml | 2 + guests/host_vars/libvirt-fedora-27/docker.yml | 2 + guests/host_vars/libvirt-fedora-28/docker.yml | 2 + .../libvirt-fedora-rawhide/docker.yml | 2 + guests/host_vars/libvirt-ubuntu-16/docker.yml | 2 + guests/host_vars/libvirt-ubuntu-18/docker.yml | 2 + guests/lcitool | 730 ++++++++++++------ 11 files changed, 529 insertions(+), 227 deletions(-) create mode 100644 guests/host_vars/libvirt-centos-7/docker.yml create mode 100644 guests/host_vars/libvirt-debian-8/docker.yml create mode 100644 guests/host_vars/libvirt-debian-9/docker.yml create mode 100644 guests/host_vars/libvirt-debian-sid/docker.yml create mode 100644 guests/host_vars/libvirt-fedora-27/docker.yml create mode 100644 guests/host_vars/libvirt-fedora-28/docker.yml create mode 100644 guests/host_vars/libvirt-fedora-rawhide/docker.yml create mode 100644 guests/host_vars/libvirt-ubuntu-16/docker.yml create mode 100644 guests/host_vars/libvirt-ubuntu-18/docker.yml
-- 2.17.1
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
For all patches in this series. Reviewed-by: Katerina Koukiou <kkoukiou@redhat.com>
participants (2)
-
Andrea Bolognani
-
Katerina Koukiou