From nobody Mon Feb 9 10:28:00 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 1531408798453555.4062624060552; Thu, 12 Jul 2018 08:19:58 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1EBA575710; Thu, 12 Jul 2018 15:19:56 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D37D75C886; Thu, 12 Jul 2018 15:19:55 +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 8AA1318037EF; Thu, 12 Jul 2018 15:19:55 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6CFJa3Z027474 for ; Thu, 12 Jul 2018 11:19:36 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9946320389E1; Thu, 12 Jul 2018 15:19:36 +0000 (UTC) Received: from inaba.usersys.redhat.com (unknown [10.43.2.44]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 39ABA20389E0 for ; Thu, 12 Jul 2018 15:19:36 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Thu, 12 Jul 2018 17:19:20 +0200 Message-Id: <20180712151929.22789-4-abologna@redhat.com> In-Reply-To: <20180712151929.22789-1-abologna@redhat.com> References: <20180712151929.22789-1-abologna@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-loop: libvir-list@redhat.com Subject: [libvirt] [jenkins-ci PATCH v2 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.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 12 Jul 2018 15:19:56 +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 | 112 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/guests/lcitool b/guests/lcitool index 1cba8ad..e03b388 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -18,6 +18,10 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. =20 import argparse +import crypt +import os +import random +import string import sys import textwrap =20 @@ -32,9 +36,117 @@ class Error(Exception): def __init__(self, message): self.message =3D message =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: + + def _get_config_file(self, 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: + 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 f: + flavor =3D f.readline().strip() + except: + # 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 f: + f.write("{}\n".format(flavor)) + except: + raise Error( + "Can't write flavor file ({})".format( + flavor_file, + ) + ) + + if flavor !=3D "test" and flavor !=3D "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 f: + if len(f.readline().strip()) =3D=3D 0: + raise ValueError + except: + 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 f: + root_pass =3D f.readline().strip() + except: + 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 f: + f.write("{}\n".format(root_hash)) + except: + 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( conflict_handler =3D "resolve", formatter_class =3D argparse.RawDescriptionHelpFormatter, --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list