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

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