2 * Copyright (C) 2014-2015 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 try Sys.getenv "MCLU_PATH"
27 with Not_found -> Config.pkgdatadir // "templates"
30 let files = Sys.readdir template_dir in
31 let files = Array.to_list files in
32 let files = List.map ((//) template_dir) files in
34 List.filter (fun name -> Filename.check_suffix name ".template") files in
38 let name = Filename.basename filename in
39 let name = Filename.chop_suffix name ".template" in
42 List.sort compare templates
44 let template_names () = List.map fst (templates ())
46 let run_template ~verbose filename subcmd args =
48 sprintf "%s %s %s" (quote filename) (quote subcmd)
49 (String.concat " " (List.map quote args)) in
50 if verbose then printf "%s\n%!" cmd;
51 let chan = Unix.open_process_in cmd in
53 (try while true do lines := input_line chan :: !lines done
54 with End_of_file -> ());
55 let lines = List.rev !lines in
56 let stat = Unix.close_process_in chan in
58 | Unix.WEXITED 0 -> Some lines
59 | Unix.WEXITED 2 -> None
61 eprintf "mclu: template '%s' subcmd '%s' exited with error %d\n"
65 eprintf "mclu: template '%s' subcmd '%s' killed by signal %d\n"
69 eprintf "mclu: template '%s' subcmd '%s' stopped by signal %d\n"
74 type template_info = {
76 minimum_memory : int64 option;
77 recommended_memory : int64 option;
78 minimum_size : int64 option;
79 disk_bus : string option;
80 network_model : string option;
81 has_xml_target : bool;
84 let probe ?(verbose = false) filename =
85 (* Check the template is a template. *)
86 (match run_template ~verbose filename "probe" [] with
87 | Some ["hello"] -> ()
89 eprintf "mclu: file %s is not an mclu template\n" filename;
93 (* Probe for various properties. *)
95 match run_template ~verbose filename "base-image" [] with
96 | Some [answer] -> answer
98 eprintf "mclu: cannot parse '%s base-image'\n" filename;
101 match run_template ~verbose filename "minimum-memory" [] with
103 (try Some (bytes_of_human_size memory)
105 eprintf "mclu: cannot parse output of '%s minimum-memory'\n"
110 let recommended_memory =
111 match run_template ~verbose filename "recommended-memory" [] with
113 (try Some (bytes_of_human_size memory)
115 eprintf "mclu: cannot parse output of '%s recommended-memory'\n"
121 match run_template ~verbose filename "minimum-size" [] with
123 (try Some (bytes_of_human_size size)
125 eprintf "mclu: cannot parse output of '%s minimum-size'\n"
131 match run_template ~verbose filename "disk-bus" [] with
132 | Some [answer] -> Some answer
135 match run_template ~verbose filename "network-model" [] with
136 | Some [answer] -> Some answer
139 let has_xml_target = run_template ~verbose filename "xml" [] <> None in
141 { base_image = base_image;
142 minimum_memory = minimum_memory;
143 recommended_memory = recommended_memory;
144 minimum_size = minimum_size;
146 network_model = network_model;
147 has_xml_target = has_xml_target }