Blame SOURCES/0005-customize-rebase-to-the-common-mlcustomize-Guest_pac.patch

7b180b
From 2014844107fc356e945fb637ef9179bc29656864 Mon Sep 17 00:00:00 2001
7b180b
From: Laszlo Ersek <lersek@redhat.com>
7b180b
Date: Mon, 6 Jun 2022 16:20:42 +0200
7b180b
Subject: [PATCH] customize: rebase to the common/mlcustomize/Guest_packages
7b180b
 interface
7b180b
7b180b
Replace the "guest_install_command", "guest_update_command" and
7b180b
"guest_uninstall_command" helper functions with the corresponding
7b180b
functions from libguestfs-common, interface mlcustomize/Guest_packages.
7b180b
7b180b
Add a wrapper function for (a) dealing with the exceptions uniformly
7b180b
(keeping the original behavior of virt-customize), (b) centralizing the
7b180b
[g#inspect_get_package_management root] call. Regarding (b), the wrapper
7b180b
function fills in the last argument [package_management] of the
7b180b
Guest_packages functions; thus, pass partially applied functions to the
7b180b
wrapper at the original call sites.
7b180b
7b180b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2028764
7b180b
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
7b180b
Message-Id: <20220606142042.16680-1-lersek@redhat.com>
7b180b
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
7b180b
(cherry picked from commit 7eb1ecf467e86374d72b23994d435139e302bca5)
7b180b
---
7b180b
 common                     |   2 +-
7b180b
 customize/customize_run.ml | 106 ++++---------------------------------
7b180b
 2 files changed, 10 insertions(+), 98 deletions(-)
7b180b
7b180b
Submodule common f8de5508f..9e990f3e4:
7b180b
diff --git a/common/mlcustomize/Makefile.am b/common/mlcustomize/Makefile.am
7b180b
index cd7d897..4e26064 100644
7b180b
--- a/common/mlcustomize/Makefile.am
7b180b
+++ b/common/mlcustomize/Makefile.am
7b180b
@@ -38,10 +38,12 @@ generator_built = \
7b180b
 
7b180b
 SOURCES_MLI = \
7b180b
 	firstboot.mli \
7b180b
+	guest_packages.mli \
7b180b
 	SELinux_relabel.mli
7b180b
 
7b180b
 SOURCES_ML = \
7b180b
 	firstboot.ml \
7b180b
+	guest_packages.ml \
7b180b
 	SELinux_relabel.ml
7b180b
 
7b180b
 if HAVE_OCAML
7b180b
diff --git a/common/mlcustomize/guest_packages.ml b/common/mlcustomize/guest_packages.ml
7b180b
new file mode 100644
7b180b
index 0000000..4c3c34e
7b180b
--- /dev/null
7b180b
+++ b/common/mlcustomize/guest_packages.ml
7b180b
@@ -0,0 +1,132 @@
7b180b
+(* virt-customize
7b180b
+ * Copyright (C) 2012-2021 Red Hat Inc.
7b180b
+ *
7b180b
+ * This program is free software; you can redistribute it and/or modify
7b180b
+ * it under the terms of the GNU General Public License as published by
7b180b
+ * the Free Software Foundation; either version 2 of the License, or
7b180b
+ * (at your option) any later version.
7b180b
+ *
7b180b
+ * This program is distributed in the hope that it will be useful,
7b180b
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7b180b
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7b180b
+ * GNU General Public License for more details.
7b180b
+ *
7b180b
+ * You should have received a copy of the GNU General Public License along
7b180b
+ * with this program; if not, write to the Free Software Foundation, Inc.,
7b180b
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
7b180b
+ *)
7b180b
+
7b180b
+open Printf
7b180b
+
7b180b
+open Common_gettext.Gettext
7b180b
+open Std_utils
7b180b
+
7b180b
+exception Unknown_package_manager of string
7b180b
+exception Unimplemented_package_manager of string
7b180b
+
7b180b
+(* Windows has package_management == "unknown". *)
7b180b
+let error_unknown_package_manager flag =
7b180b
+  let msg = sprintf (f_"cannot use ‘%s’ because no package manager has been \
7b180b
+                        detected for this guest OS.\n\nIf this guest OS is a \
7b180b
+                        common one with ordinary package management then this \
7b180b
+                        may have been caused by a failure of libguestfs \
7b180b
+                        inspection.\n\nFor OSes such as Windows that lack \
7b180b
+                        package management, this is not possible.  Try using \
7b180b
+                        one of the ‘--firstboot*’ flags instead (described in \
7b180b
+                        the virt-customize(1) manual).") flag in
7b180b
+  raise (Unknown_package_manager msg)
7b180b
+
7b180b
+let error_unimplemented_package_manager flag pm =
7b180b
+  let msg = sprintf (f_"sorry, ‘%s’ with the ‘%s’ package manager has not \
7b180b
+                        been implemented yet.\n\nYou can work around this by \
7b180b
+                        using one of the ‘--run*’ or ‘--firstboot*’ options \
7b180b
+                        instead (described in the virt-customize(1) manual).")
7b180b
+                    flag pm in
7b180b
+  raise (Unimplemented_package_manager msg)
7b180b
+
7b180b
+(* http://distrowatch.com/dwres.php?resource=package-management *)
7b180b
+let install_command packages package_management =
7b180b
+  let quoted_args = String.concat " " (List.map quote packages) in
7b180b
+  match package_management with
7b180b
+  | "apk" ->
7b180b
+     sprintf "
7b180b
+       apk update
7b180b
+       apk add %s
7b180b
+     " quoted_args
7b180b
+  | "apt" ->
7b180b
+    (* http://unix.stackexchange.com/questions/22820 *)
7b180b
+    sprintf "
7b180b
+      export DEBIAN_FRONTEND=noninteractive
7b180b
+      apt_opts='-q -y -o Dpkg::Options::=--force-confnew'
7b180b
+      apt-get $apt_opts update
7b180b
+      apt-get $apt_opts install %s
7b180b
+    " quoted_args
7b180b
+  | "dnf" ->
7b180b
+     sprintf "dnf%s -y install %s"
7b180b
+             (if verbose () then " --verbose" else "")
7b180b
+             quoted_args
7b180b
+  | "pisi" ->   sprintf "pisi it %s" quoted_args
7b180b
+  | "pacman" -> sprintf "pacman -S --noconfirm %s" quoted_args
7b180b
+  | "urpmi" ->  sprintf "urpmi %s" quoted_args
7b180b
+  | "xbps" ->   sprintf "xbps-install -Sy %s" quoted_args
7b180b
+  | "yum" ->    sprintf "yum -y install %s" quoted_args
7b180b
+  | "zypper" -> sprintf "zypper -n in -l %s" quoted_args
7b180b
+
7b180b
+  | "unknown" ->
7b180b
+    error_unknown_package_manager (s_"--install")
7b180b
+  | pm ->
7b180b
+    error_unimplemented_package_manager (s_"--install") pm
7b180b
+
7b180b
+let update_command package_management =
7b180b
+  match package_management with
7b180b
+  | "apk" ->
7b180b
+     "
7b180b
+       apk update
7b180b
+       apk upgrade
7b180b
+     "
7b180b
+  | "apt" ->
7b180b
+    (* http://unix.stackexchange.com/questions/22820 *)
7b180b
+    "
7b180b
+      export DEBIAN_FRONTEND=noninteractive
7b180b
+      apt_opts='-q -y -o Dpkg::Options::=--force-confnew'
7b180b
+      apt-get $apt_opts update
7b180b
+      apt-get $apt_opts upgrade
7b180b
+    "
7b180b
+  | "dnf" ->
7b180b
+     sprintf "dnf%s -y --best upgrade"
7b180b
+             (if verbose () then " --verbose" else "")
7b180b
+  | "pisi" ->   "pisi upgrade"
7b180b
+  | "pacman" -> "pacman -Su"
7b180b
+  | "urpmi" ->  "urpmi --auto-select"
7b180b
+  | "xbps" ->   "xbps-install -Suy"
7b180b
+  | "yum" ->    "yum -y update"
7b180b
+  | "zypper" -> "zypper -n update -l"
7b180b
+
7b180b
+  | "unknown" ->
7b180b
+    error_unknown_package_manager (s_"--update")
7b180b
+  | pm ->
7b180b
+    error_unimplemented_package_manager (s_"--update") pm
7b180b
+
7b180b
+let uninstall_command packages package_management =
7b180b
+  let quoted_args = String.concat " " (List.map quote packages) in
7b180b
+  match package_management with
7b180b
+  | "apk" -> sprintf "apk del %s" quoted_args
7b180b
+  | "apt" ->
7b180b
+    (* http://unix.stackexchange.com/questions/22820 *)
7b180b
+    sprintf "
7b180b
+      export DEBIAN_FRONTEND=noninteractive
7b180b
+      apt_opts='-q -y -o Dpkg::Options::=--force-confnew'
7b180b
+      apt-get $apt_opts remove %s
7b180b
+    " quoted_args
7b180b
+  | "dnf" ->    sprintf "dnf -y remove %s" quoted_args
7b180b
+  | "pisi" ->   sprintf "pisi rm %s" quoted_args
7b180b
+  | "pacman" -> sprintf "pacman -R %s" quoted_args
7b180b
+  | "urpmi" ->  sprintf "urpme %s" quoted_args
7b180b
+  | "xbps" ->   sprintf "xbps-remove -Sy %s" quoted_args
7b180b
+  | "yum" ->    sprintf "yum -y remove %s" quoted_args
7b180b
+  | "zypper" -> sprintf "zypper -n rm %s" quoted_args
7b180b
+
7b180b
+  | "unknown" ->
7b180b
+    error_unknown_package_manager (s_"--uninstall")
7b180b
+  | pm ->
7b180b
+    error_unimplemented_package_manager (s_"--uninstall") pm
7b180b
diff --git a/common/mlcustomize/guest_packages.mli b/common/mlcustomize/guest_packages.mli
7b180b
new file mode 100644
7b180b
index 0000000..7504a6a
7b180b
--- /dev/null
7b180b
+++ b/common/mlcustomize/guest_packages.mli
7b180b
@@ -0,0 +1,44 @@
7b180b
+(* virt-customize
7b180b
+ * Copyright (C) 2012-2021 Red Hat Inc.
7b180b
+ *
7b180b
+ * This program is free software; you can redistribute it and/or modify
7b180b
+ * it under the terms of the GNU General Public License as published by
7b180b
+ * the Free Software Foundation; either version 2 of the License, or
7b180b
+ * (at your option) any later version.
7b180b
+ *
7b180b
+ * This program is distributed in the hope that it will be useful,
7b180b
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7b180b
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7b180b
+ * GNU General Public License for more details.
7b180b
+ *
7b180b
+ * You should have received a copy of the GNU General Public License along
7b180b
+ * with this program; if not, write to the Free Software Foundation, Inc.,
7b180b
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
7b180b
+ *)
7b180b
+
7b180b
+exception Unknown_package_manager of string
7b180b
+exception Unimplemented_package_manager of string
7b180b
+(** For all three functions below, [package_management] determines the package
7b180b
+    management system in use by the guest; commonly it should be filled in from
7b180b
+    [Guestfs.inspect_get_package_management], or the equivalent guestfs object
7b180b
+    method.
7b180b
+
7b180b
+    If [package_management] is unknown or unimplemented, the functions raise
7b180b
+    [Unknown_package_manager "error message"] or [Unimplemented_package_manager
7b180b
+    "error message"], correspondingly. *)
7b180b
+
7b180b
+val install_command : string list -> string -> string
7b180b
+(** [install_command packages package_management] produces a properly quoted
7b180b
+    shell command string suitable for execution in the guest (directly or via a
7b180b
+    Firstboot script) for installing the OS packages listed in [packages]. *)
7b180b
+
7b180b
+val update_command : string -> string
7b180b
+(** [update_command package_management] produces a properly quoted shell command
7b180b
+    string suitable for execution in the guest (directly or via a Firstboot
7b180b
+    script) for updating the OS packages that are currently installed in the
7b180b
+    guest. *)
7b180b
+
7b180b
+val uninstall_command : string list -> string -> string
7b180b
+(** [uninstall_command packages package_management] produces a properly quoted
7b180b
+    shell command string suitable for execution in the guest (directly or via a
7b180b
+    Firstboot script) for uninstalling the OS packages listed in [packages]. *)
7b180b
diff --git a/customize/customize_run.ml b/customize/customize_run.ml
7b180b
index 99b5fe14d..bb2ba2a03 100644
7b180b
--- a/customize/customize_run.ml
7b180b
+++ b/customize/customize_run.ml
7b180b
@@ -67,99 +67,11 @@ let run (g : G.guestfs) root (ops : ops) =
7b180b
         error (f_"%s: command exited with an error") display
7b180b
   in
7b180b
 
7b180b
-  (* http://distrowatch.com/dwres.php?resource=package-management *)
7b180b
-  let rec guest_install_command packages =
7b180b
-    let quoted_args = String.concat " " (List.map quote packages) in
7b180b
-    match g#inspect_get_package_management root with
7b180b
-    | "apk" ->
7b180b
-       sprintf "
7b180b
-         apk update
7b180b
-         apk add %s
7b180b
-       " quoted_args
7b180b
-    | "apt" ->
7b180b
-      (* http://unix.stackexchange.com/questions/22820 *)
7b180b
-      sprintf "
7b180b
-        export DEBIAN_FRONTEND=noninteractive
7b180b
-        apt_opts='-q -y -o Dpkg::Options::=--force-confnew'
7b180b
-        apt-get $apt_opts update
7b180b
-        apt-get $apt_opts install %s
7b180b
-      " quoted_args
7b180b
-    | "dnf" ->
7b180b
-       sprintf "dnf%s -y install %s"
7b180b
-               (if verbose () then " --verbose" else "")
7b180b
-               quoted_args
7b180b
-    | "pisi" ->   sprintf "pisi it %s" quoted_args
7b180b
-    | "pacman" -> sprintf "pacman -S --noconfirm %s" quoted_args
7b180b
-    | "urpmi" ->  sprintf "urpmi %s" quoted_args
7b180b
-    | "xbps" ->   sprintf "xbps-install -Sy %s" quoted_args
7b180b
-    | "yum" ->    sprintf "yum -y install %s" quoted_args
7b180b
-    | "zypper" -> sprintf "zypper -n in -l %s" quoted_args
7b180b
-
7b180b
-    | "unknown" ->
7b180b
-      error_unknown_package_manager (s_"--install")
7b180b
-    | pm ->
7b180b
-      error_unimplemented_package_manager (s_"--install") pm
7b180b
-
7b180b
-  and guest_update_command () =
7b180b
-    match g#inspect_get_package_management root with
7b180b
-    | "apk" ->
7b180b
-       "
7b180b
-         apk update
7b180b
-         apk upgrade
7b180b
-       "
7b180b
-    | "apt" ->
7b180b
-      (* http://unix.stackexchange.com/questions/22820 *)
7b180b
-      "
7b180b
-        export DEBIAN_FRONTEND=noninteractive
7b180b
-        apt_opts='-q -y -o Dpkg::Options::=--force-confnew'
7b180b
-        apt-get $apt_opts update
7b180b
-        apt-get $apt_opts upgrade
7b180b
-      "
7b180b
-    | "dnf" ->
7b180b
-       sprintf "dnf%s -y --best upgrade"
7b180b
-               (if verbose () then " --verbose" else "")
7b180b
-    | "pisi" ->   "pisi upgrade"
7b180b
-    | "pacman" -> "pacman -Su"
7b180b
-    | "urpmi" ->  "urpmi --auto-select"
7b180b
-    | "xbps" ->   "xbps-install -Suy"
7b180b
-    | "yum" ->    "yum -y update"
7b180b
-    | "zypper" -> "zypper -n update -l"
7b180b
-
7b180b
-    | "unknown" ->
7b180b
-      error_unknown_package_manager (s_"--update")
7b180b
-    | pm ->
7b180b
-      error_unimplemented_package_manager (s_"--update") pm
7b180b
-
7b180b
-  and guest_uninstall_command packages =
7b180b
-    let quoted_args = String.concat " " (List.map quote packages) in
7b180b
-    match g#inspect_get_package_management root with
7b180b
-    | "apk" -> sprintf "apk del %s" quoted_args
7b180b
-    | "apt" ->
7b180b
-      (* http://unix.stackexchange.com/questions/22820 *)
7b180b
-      sprintf "
7b180b
-        export DEBIAN_FRONTEND=noninteractive
7b180b
-        apt_opts='-q -y -o Dpkg::Options::=--force-confnew'
7b180b
-        apt-get $apt_opts remove %s
7b180b
-      " quoted_args
7b180b
-    | "dnf" ->    sprintf "dnf -y remove %s" quoted_args
7b180b
-    | "pisi" ->   sprintf "pisi rm %s" quoted_args
7b180b
-    | "pacman" -> sprintf "pacman -R %s" quoted_args
7b180b
-    | "urpmi" ->  sprintf "urpme %s" quoted_args
7b180b
-    | "xbps" ->   sprintf "xbps-remove -Sy %s" quoted_args
7b180b
-    | "yum" ->    sprintf "yum -y remove %s" quoted_args
7b180b
-    | "zypper" -> sprintf "zypper -n rm %s" quoted_args
7b180b
-
7b180b
-    | "unknown" ->
7b180b
-      error_unknown_package_manager (s_"--uninstall")
7b180b
-    | pm ->
7b180b
-      error_unimplemented_package_manager (s_"--uninstall") pm
7b180b
-
7b180b
-  (* Windows has package_management == "unknown". *)
7b180b
-  and error_unknown_package_manager flag =
7b180b
-    error (f_"cannot use ‘%s’ because no package manager has been detected for this guest OS.\n\nIf this guest OS is a common one with ordinary package management then this may have been caused by a failure of libguestfs inspection.\n\nFor OSes such as Windows that lack package management, this is not possible.  Try using one of the ‘--firstboot*’ flags instead (described in the manual).") flag
7b180b
-
7b180b
-  and error_unimplemented_package_manager flag pm =
7b180b
-      error (f_"sorry, ‘%s’ with the ‘%s’ package manager has not been implemented yet.\n\nYou can work around this by using one of the ‘--run*’ or ‘--firstboot*’ options instead (described in the manual).") flag pm
7b180b
+  let guest_pkgs_command f =
7b180b
+    try f (g#inspect_get_package_management root) with
7b180b
+    | Guest_packages.Unknown_package_manager msg
7b180b
+    | Guest_packages.Unimplemented_package_manager msg ->
7b180b
+      error "%s" msg
7b180b
   in
7b180b
 
7b180b
   (* Set the random seed. *)
7b180b
@@ -255,7 +167,7 @@ let run (g : G.guestfs) root (ops : ops) =
7b180b
     | `FirstbootPackages pkgs ->
7b180b
       message (f_"Installing firstboot packages: %s")
7b180b
         (String.concat " " pkgs);
7b180b
-      let cmd = guest_install_command pkgs in
7b180b
+      let cmd = guest_pkgs_command (Guest_packages.install_command pkgs) in
7b180b
       let name = String.concat " " ("install" :: pkgs) in
7b180b
       Firstboot.add_firstboot_script g root name cmd
7b180b
 
7b180b
@@ -271,7 +183,7 @@ let run (g : G.guestfs) root (ops : ops) =
7b180b
 
7b180b
     | `InstallPackages pkgs ->
7b180b
       message (f_"Installing packages: %s") (String.concat " " pkgs);
7b180b
-      let cmd = guest_install_command pkgs in
7b180b
+      let cmd = guest_pkgs_command (Guest_packages.install_command pkgs) in
7b180b
       do_run ~display:cmd ~warn_failed_no_network:true cmd
7b180b
 
7b180b
     | `Link (target, links) ->
7b180b
@@ -365,12 +277,12 @@ let run (g : G.guestfs) root (ops : ops) =
7b180b
 
7b180b
     | `UninstallPackages pkgs ->
7b180b
       message (f_"Uninstalling packages: %s") (String.concat " " pkgs);
7b180b
-      let cmd = guest_uninstall_command pkgs in
7b180b
+      let cmd = guest_pkgs_command (Guest_packages.uninstall_command pkgs) in
7b180b
       do_run ~display:cmd cmd
7b180b
 
7b180b
     | `Update ->
7b180b
       message (f_"Updating packages");
7b180b
-      let cmd = guest_update_command () in
7b180b
+      let cmd = guest_pkgs_command Guest_packages.update_command in
7b180b
       do_run ~display:cmd ~warn_failed_no_network:true cmd
7b180b
 
7b180b
     | `Upload (path, dest) ->
7b180b
-- 
7b180b
2.31.1
7b180b