From nobody Mon Feb 9 16:01:03 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 1531408822682234.94653018625775; Thu, 12 Jul 2018 08:20:22 -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 3F781308124B; Thu, 12 Jul 2018 15:20:20 +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 EFC03308BDB0; Thu, 12 Jul 2018 15:20:19 +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 A257C4BB78; Thu, 12 Jul 2018 15:20:19 +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 w6CFJga1027544 for ; Thu, 12 Jul 2018 11:19:42 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6BEFD20389E1; Thu, 12 Jul 2018 15:19:42 +0000 (UTC) Received: from inaba.usersys.redhat.com (unknown [10.43.2.44]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0CE0120389E0 for ; Thu, 12 Jul 2018 15:19:41 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Thu, 12 Jul 2018 17:19:29 +0200 Message-Id: <20180712151929.22789-13-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 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.49]); Thu, 12 Jul 2018 15:20:21 +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 d42b7e7..61cae97 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -299,7 +299,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( @@ -313,16 +316,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_list(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): @@ -380,7 +388,7 @@ class Application: except: 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() @@ -409,15 +417,81 @@ class Application: except: 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 !=3D "deb" and package_format !=3D "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] !=3D 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