builder: Use progress bar for large template downloads that are not in the cache.

This commit is contained in:
Richard W.M. Jones
2013-10-05 16:00:21 +01:00
parent 7b82580352
commit 80da8b19e0
4 changed files with 18 additions and 15 deletions

5
TODO
View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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. *)