From 80da8b19e0d9745df8774e9cf78e087abbda6064 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 5 Oct 2013 16:00:21 +0100 Subject: [PATCH] builder: Use progress bar for large template downloads that are not in the cache. --- TODO | 5 ----- builder/builder.ml | 3 ++- builder/downloader.ml | 17 ++++++++++------- builder/downloader.mli | 8 ++++++-- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/TODO b/TODO index 6fb0da875..b2cccba8d 100644 --- a/TODO +++ b/TODO @@ -554,14 +554,9 @@ virt-builder - set default timezone and language - - the initial download of the template can take quite a long time - and needs a progress bar - - how can we debug it? at least document how to call virt-rescue guestfish etc to look at files - how can we give users a shell? - - use a single log file and dump it out on command failure - - Ubuntu templates diff --git a/builder/builder.ml b/builder/builder.ml index 6ee7358b9..dd6bce4fa 100644 --- a/builder/builder.ml +++ b/builder/builder.ml @@ -415,7 +415,8 @@ let template = let { Index_parser.revision = revision; file_uri = file_uri } = entry in let template = arg, revision in msg (f_"Downloading: %s") file_uri; - Downloader.download downloader ~template file_uri in + let progress_bar = not quiet in + Downloader.download downloader ~template ~progress_bar file_uri in if delete_on_exit then unlink_on_exit template; template diff --git a/builder/downloader.ml b/builder/downloader.ml index cb76b75a6..b8020b1ed 100644 --- a/builder/downloader.ml +++ b/builder/downloader.ml @@ -39,19 +39,19 @@ let create ~debug ~curl ~cache = { cache = cache; } -let rec download t ?template uri = +let rec download t ?template ?progress_bar uri = match template with | None -> (* no cache, simple download *) (* Create a temporary name. *) let tmpfile = Filename.temp_file "vbcache" ".txt" in - download_to t uri tmpfile; + download_to t ?progress_bar uri tmpfile; (tmpfile, true) | Some (name, revision) -> match t.cache with | None -> (* Not using the cache at all? *) - download t uri + download t ?progress_bar uri | Some cachedir -> let filename = cachedir // sprintf "%s.%d" name revision in @@ -60,14 +60,16 @@ let rec download t ?template uri = * If not, download it. *) if not (Sys.file_exists filename) then - download_to t uri filename; + download_to t ?progress_bar uri filename; (filename, false) -and download_to t uri filename = +and download_to t ?(progress_bar = false) uri filename = (* Get the status code first to ensure the file exists. *) let cmd = sprintf "%s%s -g -o /dev/null -I -w '%%{http_code}' %s" - t.curl (if t.debug then "" else " -s -S") (quote uri) in + t.curl + (if t.debug then "" else " -s -S") + (quote uri) in let chan = open_process_in cmd in let status_code = input_line chan in let stat = close_process_in chan in @@ -100,7 +102,8 @@ and download_to t uri filename = (* Now download the file. *) let filename_new = filename ^ ".new" in let cmd = sprintf "%s%s -g -o %s %s" - t.curl (if t.debug then "" else " -s -S") + t.curl + (if t.debug then "" else if progress_bar then " -#" else " -s -S") (quote filename_new) (quote uri) in if t.debug then eprintf "%s\n%!" cmd; let r = Sys.command cmd in diff --git a/builder/downloader.mli b/builder/downloader.mli index ed91fc0be..106744d65 100644 --- a/builder/downloader.mli +++ b/builder/downloader.mli @@ -27,7 +27,7 @@ type t val create : debug:bool -> curl:string -> cache:string option -> t (** Create the abstract type. *) -val download : t -> ?template:(string*int) -> uri -> (filename * bool) +val download : t -> ?template:(string*int) -> ?progress_bar:bool -> uri -> (filename * bool) (** Download the URI, returning the downloaded filename and a temporary file flag. The temporary file flag is [true] iff the downloaded file is temporary and should be deleted by the @@ -35,4 +35,8 @@ val download : t -> ?template:(string*int) -> uri -> (filename * bool) For templates, you must supply [~template:(name, revision)]. This causes the cache to be used (if possible). Name and revision are - used for cache control (see the man page for details). *) + used for cache control (see the man page for details). + + If [~progress_bar:true] then display a progress bar if the file + doesn't come from the cache. In debug mode, progress messages + are always displayed. *)