[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH] virt-sysprep:add logging feature



Hi Rich,

I did with your suggestions, please help reviewing.

I got a compiling error,
-
File "sysprep_operation.mli", line 23, characters 17-19:
Error: Unbound type parameter 'a
make: *** [sysprep_operation.cmi] Error 2
make: Leaving directory `/work/git/libguestfs/sysprep'
-

Thanks,
Wanlong Gao


Signed-off-by: Wanlong Gao <gaowanlong cn fujitsu com>
---
 sysprep/main.ml                                  |   12 +++++++++---
 sysprep/sysprep_operation.ml                     |    6 ++++--
 sysprep/sysprep_operation.mli                    |    6 ++++--
 sysprep/sysprep_operation_cron_spool.ml          |   10 ++++++++--
 sysprep/sysprep_operation_dhcp_client_state.ml   |    9 +++++++--
 sysprep/sysprep_operation_dhcp_server_state.ml   |    8 ++++++--
 sysprep/sysprep_operation_hostname.ml            |    4 +++-
 sysprep/sysprep_operation_logfiles.ml            |   11 +++++++++--
 sysprep/sysprep_operation_mail_spool.ml          |    9 +++++++--
 sysprep/sysprep_operation_net_hwaddr.ml          |    3 ++-
 sysprep/sysprep_operation_random_seed.ml         |    3 ++-
 sysprep/sysprep_operation_rhn_systemid.ml        |    8 ++++++--
 sysprep/sysprep_operation_script.ml              |    2 +-
 sysprep/sysprep_operation_smolt_uuid.ml          |    8 ++++++--
 sysprep/sysprep_operation_ssh_hostkeys.ml        |    8 ++++++--
 sysprep/sysprep_operation_udev_persistent_net.ml |    7 +++++--
 sysprep/sysprep_operation_utmp.ml                |    7 +++++--
 sysprep/sysprep_operation_yum_uuid.ml            |    8 ++++++--
 18 files changed, 96 insertions(+), 33 deletions(-)

diff --git a/sysprep/main.ml b/sysprep/main.ml
index d06b0d2..c456521 100644
--- a/sysprep/main.ml
+++ b/sysprep/main.ml
@@ -29,7 +29,7 @@ let () = Sysprep_operation.bake ()
 (* Command line argument parsing. *)
 let prog = Filename.basename Sys.executable_name
 
-let debug_gc, operations, g, selinux_relabel =
+let debug_gc, operations, g, selinux_relabel, show_log =
   let debug_gc = ref false in
   let domain = ref None in
   let dryrun = ref false in
@@ -40,6 +40,7 @@ let debug_gc, operations, g, selinux_relabel =
   let selinux_relabel = ref `Auto in
   let trace = ref false in
   let verbose = ref false in
+  let show_log = ref false in
 
   let display_version () =
     let g = new G.guestfs () in
@@ -112,6 +113,7 @@ let debug_gc, operations, g, selinux_relabel =
     "-V",        Arg.Unit display_version,  " Display version and exit";
     "--version", Arg.Unit display_version,  " -\"-";
     "-x",        Arg.Set trace,             " Enable tracing of libguestfs calls";
+    "--log",     Arg.Set show_log,          " Enable operation logging";
   ] @ Sysprep_operation.extra_args () in
   let anon_fun _ = raise (Arg.Bad "extra parameter on the command line") in
   let usage_msg =
@@ -162,6 +164,7 @@ read the man page virt-sysprep(1).
   let selinux_relabel = !selinux_relabel in
   let trace = !trace in
   let verbose = !verbose in
+  let show_log = !show_log in
 
   (* Connect to libguestfs. *)
   let g = new G.guestfs () in
@@ -170,7 +173,7 @@ read the man page virt-sysprep(1).
   add g dryrun;
   g#launch ();
 
-  debug_gc, operations, g, selinux_relabel
+  debug_gc, operations, g, selinux_relabel, show_log
 
 let () =
   (* Inspection. *)
@@ -194,7 +197,10 @@ let () =
         ) mps;
 
         (* Perform the operations. *)
-        let flags = Sysprep_operation.perform_operations ?operations g root in
+        let flags = Sysprep_operation.perform_operations ?operations ~debug g root in
+
+        let debug fs =
+          ksprintf (fun str -> if show_log then prerr_endline str) fs in
 
         (* Parse flags. *)
         let relabel = ref false in
diff --git a/sysprep/sysprep_operation.ml b/sysprep/sysprep_operation.ml
index 3fd8afc..5bb870f 100644
--- a/sysprep/sysprep_operation.ml
+++ b/sysprep/sysprep_operation.ml
@@ -20,11 +20,13 @@ open Printf
 
 type flag = [ `Created_files ]
 
+type debug_fn = ('a, unit, string, unit) format4 -> 'a
+
 type operation = {
   name : string;
   pod_description : string;
   extra_args : ((Arg.key * Arg.spec * Arg.doc) * string) list;
-  perform : Guestfs.guestfs -> string -> flag list;
+  perform : Guestfs.guestfs -> debug:debug_fn -> string -> flag list;
 }
 
 let ops = ref []
@@ -169,7 +171,7 @@ let list_operations () =
    *)
   List.iter (fun op -> print_endline op.name ) !ops
 
-let perform_operations ?operations g root =
+let perform_operations ?operations ~debug g root =
   assert !baked;
 
   let ops =
diff --git a/sysprep/sysprep_operation.mli b/sysprep/sysprep_operation.mli
index 5fe035f..64976a9 100644
--- a/sysprep/sysprep_operation.mli
+++ b/sysprep/sysprep_operation.mli
@@ -20,6 +20,8 @@
 
 type flag = [ `Created_files ]
 
+type debug_fn = ('a, unit, string, unit) format4 -> 'a
+
 type operation = {
   name : string;
   (** Operation name, also used to enable the operation on the command
@@ -39,7 +41,7 @@ type operation = {
       You can decide the types of the arguments, whether they are
       mandatory etc. *)
 
-  perform : Guestfs.guestfs -> string -> flag list;
+  perform : Guestfs.guestfs -> debug:debug_fn -> string -> flag list;
   (** The function which is called to perform this operation, when
       enabled.
 
@@ -95,5 +97,5 @@ val add_to_set : string -> set -> set
     Note that this will raise [Not_found] if [name] is not
     a valid operation name. *)
 
-val perform_operations : ?operations:set -> Guestfs.guestfs -> string -> flag list
+val perform_operations : ?operations:set -> Guestfs.guestfs -> debug:debug_fn ->  string -> flag list
 (** Perform all operations, or the subset listed in the [operations] set. *)
diff --git a/sysprep/sysprep_operation_cron_spool.ml b/sysprep/sysprep_operation_cron_spool.ml
index e67688b..b822be7 100644
--- a/sysprep/sysprep_operation_cron_spool.ml
+++ b/sysprep/sysprep_operation_cron_spool.ml
@@ -17,11 +17,17 @@
  *)
 
 open Sysprep_operation
+open Utils
 
 module G = Guestfs
 
-let cron_spool_perform g root =
-  Array.iter g#rm_rf (g#glob_expand "/var/spool/cron/*");
+let cron_spool_perform ~debug g root =
+  let files = g#glob_expand "/var/spool/cron/*" in
+  Array.iter (
+    fun glob ->
+      g#rm glob
+      debug "Deleted %s" glob
+  ) files;
   []
 
 let cron_spool_op = {
diff --git a/sysprep/sysprep_operation_dhcp_client_state.ml b/sysprep/sysprep_operation_dhcp_client_state.ml
index e3e87cb..9048edb 100644
--- a/sysprep/sysprep_operation_dhcp_client_state.ml
+++ b/sysprep/sysprep_operation_dhcp_client_state.ml
@@ -20,11 +20,16 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let dhcp_client_state_perform g root =
+let dhcp_client_state_perform ~debug g root =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
     List.iter (
-      fun glob -> Array.iter g#rm_rf (g#glob_expand glob)
+      fun glob ->
+      Array.iter (
+        fun glob2 ->
+          g#rm_rf glob2
+          debug "Deleted %s" glob2
+      ) (g#glob_expand glob)
     ) [ "/var/lib/dhclient/*"; "/var/lib/dhcp/*" (* RHEL 3 *) ]
   );
   []
diff --git a/sysprep/sysprep_operation_dhcp_server_state.ml b/sysprep/sysprep_operation_dhcp_server_state.ml
index c5251ce..2d72ab7 100644
--- a/sysprep/sysprep_operation_dhcp_server_state.ml
+++ b/sysprep/sysprep_operation_dhcp_server_state.ml
@@ -20,8 +20,12 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let dhcp_server_state_perform g root =
-  Array.iter g#rm_rf (g#glob_expand "/var/lib/dhcpd/*");
+let dhcp_server_state_perform ~debug g root =
+  Array.iter (
+    fun glob ->
+    g#rm_rf glob
+    debug "Deleted %s" glob
+  ) (g#glob_expand "/var/lib/dhcpd/*");
   []
 
 let dhcp_server_state_op = {
diff --git a/sysprep/sysprep_operation_hostname.ml b/sysprep/sysprep_operation_hostname.ml
index 1472a1c..26c14a3 100644
--- a/sysprep/sysprep_operation_hostname.ml
+++ b/sysprep/sysprep_operation_hostname.ml
@@ -25,7 +25,7 @@ module G = Guestfs
 
 let hostname = ref "localhost.localdomain"
 
-let hostname_perform g root =
+let hostname_perform ~debug g root =
   let typ = g#inspect_get_type root in
   let distro = g#inspect_get_distro root in
   match typ, distro with
@@ -42,10 +42,12 @@ let hostname_perform g root =
       String.concat "\n" lines ^
       sprintf "\nHOSTNAME=%s\n" !hostname in
     g#write filename file;
+    debug "Modified HOSTNAME to %s" !hostname
     [ `Created_files ]
 
   | "linux", ("debian"|"ubuntu") ->
     g#write "/etc/hostname" !hostname;
+    debug "Modified HOSTNAME to %s" !hostname
     [ `Created_files ]
 
   | _ -> []
diff --git a/sysprep/sysprep_operation_logfiles.ml b/sysprep/sysprep_operation_logfiles.ml
index b4c6ea9..8bf5838 100644
--- a/sysprep/sysprep_operation_logfiles.ml
+++ b/sysprep/sysprep_operation_logfiles.ml
@@ -50,10 +50,17 @@ let globs = List.sort compare [
 ]
 let globs_as_pod = String.concat "\n" (List.map ((^) " ") globs)
 
-let logfiles_perform g root =
+let logfiles_perform ~debug g root =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
-    List.iter (fun glob -> Array.iter g#rm_rf (g#glob_expand glob)) globs
+    List.iter (
+      fun glob ->
+        Array.iter (
+          fun glob2 ->
+            g#rm_rf glob2
+            debug "Deleted %s\n" glob2
+        ) (g#glob_expand glob);
+    ) globs;
   );
   []
 
diff --git a/sysprep/sysprep_operation_mail_spool.ml b/sysprep/sysprep_operation_mail_spool.ml
index 74f2d94..76cc40b 100644
--- a/sysprep/sysprep_operation_mail_spool.ml
+++ b/sysprep/sysprep_operation_mail_spool.ml
@@ -20,9 +20,14 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let mail_spool_perform g root =
+let mail_spool_perform ~debug g root =
   List.iter (
-    fun glob -> Array.iter g#rm_rf (g#glob_expand glob)
+    fun glob ->
+      Array.iter (
+        fun glob2 ->
+          g#rm_rf glob2
+          debug "Deleted %s" glob2
+      ) (g#glob_expand glob)
   ) [
     "/var/spool/mail/*";
     "/var/mail/*";
diff --git a/sysprep/sysprep_operation_net_hwaddr.ml b/sysprep/sysprep_operation_net_hwaddr.ml
index 8aa102a..86840c1 100644
--- a/sysprep/sysprep_operation_net_hwaddr.ml
+++ b/sysprep/sysprep_operation_net_hwaddr.ml
@@ -21,7 +21,7 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let net_hwaddr_perform g root =
+let net_hwaddr_perform ~debug g root =
   let typ = g#inspect_get_type root in
   let distro = g#inspect_get_distro root in
   match typ, distro with
@@ -36,6 +36,7 @@ let net_hwaddr_perform g root =
         ) lines in
         let file = String.concat "\n" lines ^ "\n" in
         g#write filename file
+        debug "Replaced HWADDR in %s\n" filename
     ) filenames;
 
     if filenames <> [||] then [ `Created_files ] else []
diff --git a/sysprep/sysprep_operation_random_seed.ml b/sysprep/sysprep_operation_random_seed.ml
index 989ffe8..1302918 100644
--- a/sysprep/sysprep_operation_random_seed.ml
+++ b/sysprep/sysprep_operation_random_seed.ml
@@ -20,7 +20,7 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let random_seed_perform g root =
+let random_seed_perform ~debug g root =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
     let files = [
@@ -37,6 +37,7 @@ let random_seed_perform g root =
           close_in chan;
 
           g#write file buf
+          debug "Modified %s\n" file
         )
     ) files;
     [ `Created_files ]
diff --git a/sysprep/sysprep_operation_rhn_systemid.ml b/sysprep/sysprep_operation_rhn_systemid.ml
index 35849e4..c00f262 100644
--- a/sysprep/sysprep_operation_rhn_systemid.ml
+++ b/sysprep/sysprep_operation_rhn_systemid.ml
@@ -20,13 +20,17 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let rhn_systemid_perform g root =
+let rhn_systemid_perform ~debug g root =
   let typ = g#inspect_get_type root in
   let distro = g#inspect_get_distro root in
 
   match typ, distro with
   | "linux", "rhel" ->
-    (try g#rm "/etc/sysconfig/rhn/systemid" with G.Error _ -> ());
+    (try
+       let file = "/etc/sysconfig/rhn/systemid" in
+       g#rm file
+       debug "Deleted %s\n" file
+     with G.Error _ -> ());
     []
   | _ -> []
 
diff --git a/sysprep/sysprep_operation_script.ml b/sysprep/sysprep_operation_script.ml
index 977d41f..6364a54 100644
--- a/sysprep/sysprep_operation_script.ml
+++ b/sysprep/sysprep_operation_script.ml
@@ -35,7 +35,7 @@ let set_scriptdir dir =
 let scripts = ref []
 let add_script script = scripts := script :: !scripts
 
-let rec script_perform (g : Guestfs.guestfs) root =
+let rec script_perform ~debug (g : Guestfs.guestfs) root =
   let scripts = List.rev !scripts in
   if scripts <> [] then (
     (* Create a temporary directory? *)
diff --git a/sysprep/sysprep_operation_smolt_uuid.ml b/sysprep/sysprep_operation_smolt_uuid.ml
index a85aa9b..aad87a5 100644
--- a/sysprep/sysprep_operation_smolt_uuid.ml
+++ b/sysprep/sysprep_operation_smolt_uuid.ml
@@ -20,14 +20,18 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let smolt_uuid_perform g root =
+let smolt_uuid_perform ~debug g root =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
     let files = [ "/etc/sysconfig/hw-uuid";
                   "/etc/smolt/uuid";
                   "/etc/smolt/hw-uuid" ] in
     List.iter (
-      fun file -> try g#rm file with G.Error _ -> ()
+      fun file ->
+        try
+          g#rm file
+          debug "Deleted %s\n" file
+        with G.Error _ -> ()
     ) files;
 
     []
diff --git a/sysprep/sysprep_operation_ssh_hostkeys.ml b/sysprep/sysprep_operation_ssh_hostkeys.ml
index 8da405b..4f3b569 100644
--- a/sysprep/sysprep_operation_ssh_hostkeys.ml
+++ b/sysprep/sysprep_operation_ssh_hostkeys.ml
@@ -20,11 +20,15 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let ssh_hostkeys_perform g root =
+let ssh_hostkeys_perform ~debug g root =
   let typ = g#inspect_get_type root in
   if typ <> "windows" then (
     let files = g#glob_expand "/etc/ssh/*_host_*" in
-    Array.iter g#rm files;
+    Array.iter (
+      fun file ->
+      g#rm file
+      debug "Deleted %s\n" file
+    ) files;
     []
   )
   else []
diff --git a/sysprep/sysprep_operation_udev_persistent_net.ml b/sysprep/sysprep_operation_udev_persistent_net.ml
index e54e140..5b4941e 100644
--- a/sysprep/sysprep_operation_udev_persistent_net.ml
+++ b/sysprep/sysprep_operation_udev_persistent_net.ml
@@ -20,10 +20,13 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let udev_persistent_net_perform g root =
+let udev_persistent_net_perform ~debug g root =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
-    (try g#rm "/etc/udev/rules.d/70-persistent-net.rules"
+    (try
+       let file = "/etc/udev/rules.d/70-persistent-net.rules" in
+       g#rm file
+       debug "Deleted %s\n" file
      with G.Error _ -> ());
     []
   )
diff --git a/sysprep/sysprep_operation_utmp.ml b/sysprep/sysprep_operation_utmp.ml
index 69867e1..4d4950a 100644
--- a/sysprep/sysprep_operation_utmp.ml
+++ b/sysprep/sysprep_operation_utmp.ml
@@ -20,10 +20,13 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let utmp_perform g root =
+let utmp_perform ~debug g root =
   let typ = g#inspect_get_type root in
   if typ <> "windows" then (
-    try g#rm "/var/run/utmp"
+    try
+      let file = "/var/run/utmp" in
+      g#rm file
+      debug "Deleted %s\n" file
     with G.Error _ -> ()
   );
   []
diff --git a/sysprep/sysprep_operation_yum_uuid.ml b/sysprep/sysprep_operation_yum_uuid.ml
index 396cac5..3b6738c 100644
--- a/sysprep/sysprep_operation_yum_uuid.ml
+++ b/sysprep/sysprep_operation_yum_uuid.ml
@@ -20,10 +20,14 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let yum_uuid_perform g root =
+let yum_uuid_perform ~debug g root =
   let packager = g#inspect_get_package_management root in
   if packager = "yum" then (
-    (try g#rm "/var/lib/yum/uuid" with G.Error _ -> ());
+    (try
+       let file = "/var/lib/yum/uuid" in
+       g#rm file
+       debug "Deleted %s\n" file
+     with G.Error _ -> ());
     []
   )
   else []
-- 
1.7.10.rc3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]