5a1d3cbb5741230b74cf92561ac9210637567a3c
[mclu.git] / mclu_console.ml
1 (* mclu: Mini Cloud
2  * Copyright (C) 2014-2015 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  *)
18
19 (* Implement 'mclu console'. *)
20
21 open Printf
22
23 open Utils
24
25 let get_arg_speclist () = []
26
27 let console ~verbose ?host name =
28   let nodes = Mclu_conf.nodes () in
29   let node =
30     match host with
31     | Some host ->
32       (try List.find (fun n -> host = n.Mclu_conf.hostname) nodes
33        with Not_found ->
34          eprintf "mclu: host '%s' not found\n" host;
35          exit 1)
36     | None ->
37       (* No 'host:' prefix given, so we need to find the host. *)
38       let guests = Mclu_list.active_guests ~verbose ~nodes () in
39       let node, _ =
40         try
41           List.find (
42             fun (node, doms) ->
43               List.exists (
44                 fun dom ->
45                   name = dom.Mclu_list.dom_name
46               ) doms
47           ) guests
48         with
49           Not_found ->
50             eprintf "mclu: guest '%s' not found\n" name;
51             exit 1 in
52       node in
53
54   let uri = node.Mclu_conf.libvirt_uri in
55
56   let cmd = sprintf "virsh -c %s console %s" (quote uri) (quote name) in
57   if verbose then printf "%s\n%!" cmd;
58   if Sys.command cmd <> 0 then (
59     eprintf "mclu: %s: command failed\n" cmd;
60     exit 1
61   )
62
63 let run ~verbose = function
64   | [ name ] ->
65     let host, name = name_parse name in
66     console ~verbose ?host name
67   | _ ->
68     eprintf "Usage: mclu boot <template> <[host:]name>\n";
69     exit 1