From nobody Mon Feb 9 19:39:18 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1531845354810820.0088180998558; Tue, 17 Jul 2018 09:35:54 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7ACD1307D85A; Tue, 17 Jul 2018 16:35:52 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3C79F20155E0; Tue, 17 Jul 2018 16:35:52 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B3B1024F64; Tue, 17 Jul 2018 16:35:51 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6HGZbxD021044 for ; Tue, 17 Jul 2018 12:35:37 -0400 Received: by smtp.corp.redhat.com (Postfix) id 06D5416875; Tue, 17 Jul 2018 16:35:37 +0000 (UTC) Received: from inaba.usersys.redhat.com (unknown [10.43.2.44]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9B4331687B for ; Tue, 17 Jul 2018 16:35:36 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Tue, 17 Jul 2018 18:35:19 +0200 Message-Id: <20180717163528.4591-4-abologna@redhat.com> In-Reply-To: <20180717163528.4591-1-abologna@redhat.com> References: <20180717163528.4591-1-abologna@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Subject: [libvirt] [jenkins-ci PATCH v3 03/12] lcitool: Add tool configuration handling X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Tue, 17 Jul 2018 16:35:53 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The on-disk configuration format and its behavior are fully backwards compatible with the previous implementation. Signed-off-by: Andrea Bolognani --- 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 . =20 import argparse +import crypt +import os +import random +import string import sys =20 =20 @@ -27,9 +31,120 @@ class Error(Exception): self.message =3D message =20 =20 +class Util: + + @staticmethod + def mksalt(): + alphabeth =3D string.ascii_letters + string.digits + salt =3D "".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 =3D os.environ["XDG_CONFIG_HOME"] + except KeyError: + config_dir =3D os.path.join(os.environ["HOME"], ".config/") + config_dir =3D 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 =3D self._get_config_file("flavor") + + try: + with open(flavor_file, "r") as infile: + flavor =3D 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 =3D "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 =3D 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() !=3D "test": + vault_pass_file =3D 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 =3D self._get_config_file("root-password") + root_hash_file =3D self._get_config_file(".root-password.hash") + + try: + with open(root_pass_file, "r") as infile: + root_pass =3D 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 =3D 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: =20 def __init__(self): + self._config =3D Config() + self._parser =3D argparse.ArgumentParser( description=3D"libvirt CI guest management tool", ) --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list