Rewrite to use ansible.
[mclu.git] / mclu_viewer.py
1 #!/usr/bin/python
2 # mclu (mini cluster)
3 # Copyright (C) 2014 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 import argparse
20 import re
21 import sys
22 import subprocess
23
24 import config
25 import lib
26
27 def cmdline (subparsers):
28     p = subparsers.add_parser (
29         'viewer',
30         help='connect to graphical console of named VM',
31     )
32     p.add_argument (
33         'vm',
34         help='vm (or node:vm) to connect to'
35     )
36     p.set_defaults (run=run)
37
38 def run (c, args):
39     running, _ = lib.get_all_guests (c)
40
41     m = re.match (r'^(.*):(.*)$', args.vm)
42     if m:
43         # We don't actually care about the node, but we check it
44         # is the expected one below.
45         node_name_check = m.group (1)
46         vm_name = m.group (2)
47     else:
48         node_name_check = None
49         vm_name = args.vm
50
51     if vm_name not in running:
52         sys.exit ("error: vm %s not found or not running" % vm_name)
53
54     vm = running[vm_name]['vm']
55     node_name = running[vm_name]['node']
56
57     if node_name_check and node_name != node_name_check:
58         sys.exit ("error: vm %s is not running on node %s, did you mean %s:%s ?" %
59                   (vm_name, node_name_check, node_name, vm_name))
60
61     # Run the virsh console command.
62     subprocess.call ([config.VIRT_VIEWER,
63                       "-c", lib.uri_of_node (node_name),
64                       vm_name])