From nobody Fri Dec 19 20:35:43 2025 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 1531845361118259.9895794948761; Tue, 17 Jul 2018 09:36:01 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 84D4D8110F; Tue, 17 Jul 2018 16:35:58 +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 22732308BDB2; Tue, 17 Jul 2018 16:35:58 +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 9F90E18037EC; Tue, 17 Jul 2018 16:35:57 +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 w6HGZgJ2021116 for ; Tue, 17 Jul 2018 12:35:43 -0400 Received: by smtp.corp.redhat.com (Postfix) id C100B16875; Tue, 17 Jul 2018 16:35:42 +0000 (UTC) Received: from inaba.usersys.redhat.com (unknown [10.43.2.44]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 61D5D16878 for ; Tue, 17 Jul 2018 16:35:42 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Tue, 17 Jul 2018 18:35:28 +0200 Message-Id: <20180717163528.4591-13-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 12/12] lcitool: Implement the 'dockerfile' action 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.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 17 Jul 2018 16:36:00 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 --- 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 =20 - 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=3D"HOSTS", help=3D"list of hosts to act on", ) + self._parser.add_argument( + "-p", + metavar=3D"PROJECTS", + help=3D"list of projects to consider", + ) =20 - def _action_hosts(self, _hosts): + def _action_hosts(self, _hosts, _projects): for host in self._inventory.expand_pattern("all"): print(host) =20 - def _action_projects(self, _hosts): + def _action_projects(self, _hosts, _projects): for project in self._projects.expand_pattern("all"): print(project) =20 - def _action_install(self, hosts): + def _action_install(self, hosts, _projects): flavor =3D self._config.get_flavor() =20 for host in self._inventory.expand_pattern(hosts): @@ -387,7 +395,7 @@ class Application: except Exception: raise Error("Failed to install '{}'".format(host)) =20 - def _action_update(self, hosts): + def _action_update(self, hosts, _projects): flavor =3D self._config.get_flavor() vault_pass_file =3D self._config.get_vault_password_file() root_pass_file =3D self._config.get_root_password_file() @@ -416,15 +424,81 @@ class Application: except Exception: raise Error("Failed to update '{}'".format(hosts)) =20 + def _action_dockerfile(self, hosts, projects): + mappings =3D self._projects.get_mappings() + + hosts =3D self._inventory.expand_pattern(hosts) + if len(hosts) > 1: + raise Error("Can't generate Dockerfile for multiple hosts") + host =3D hosts[0] + + facts =3D self._inventory.get_facts(host) + package_format =3D facts["package_format"] + os_name =3D facts["os_name"] + os_full =3D os_name + str(facts["os_version"]) + + if package_format not in ["deb", "rpm"]: + raise Error("Host {} doesn't support Dockerfiles".format(host)) + + projects =3D 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 =3D {} + + # 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] =3D mappings[package]["default"] + if package_format in mappings[package]: + temp[package] =3D mappings[package][package_format] + if os_name in mappings[package]: + temp[package] =3D mappings[package][os_name] + if os_full in mappings[package]: + temp[package] =3D mappings[package][os_full] + + flattened =3D [] + for item in temp: + if temp[item] is not None and temp[item] not in flattened: + flattened +=3D [temp[item]] + + print("FROM {}".format(facts["docker_base"])) + + sys.stdout.write("ENV PACKAGES ") + sys.stdout.write(" \\\n ".join(sorted(flattened))) + + if package_format =3D=3D "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 =3D=3D "rpm": + sys.stdout.write(textwrap.dedent(""" + RUN yum install -y ${PACKAGES} && \\ + yum autoremove -y && \\ + yum clean all -y + """)) + def run(self): cmdline =3D self._parser.parse_args() action =3D cmdline.a hosts =3D cmdline.h + projects =3D cmdline.p =20 method =3D "_action_{}".format(action.replace("-", "_")) =20 if hasattr(self, method): - getattr(self, method).__call__(hosts) + getattr(self, method).__call__(hosts, projects) else: raise Error("Invalid action '{}'".format(action)) =20 --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list