50d58c96b301e54378c19751b93ac5659038e75e
[mclu.git] / mclu_start.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 fnmatch
21 import re
22 import subprocess
23 import sys
24 import libvirt
25
26 import config
27 import lib
28
29 def cmdline (subparsers):
30     p = subparsers.add_parser (
31         'start',
32         help='start virtual machine(s)',
33     )
34     p.add_argument (
35         '--viewer', action='store_const', const=True,
36         help='start virt-viewer to show the graphical console'
37     )
38     p.add_argument (
39         'vms', nargs='+',
40         help='virtual machine(s) to be started'
41     )
42     p.set_defaults (run=run)
43
44 def run (c, args, nodes):
45     _, inactive = lib.get_all_guests (c, nodes.values ())
46
47     # User supplied a list of node:VMs.
48     for a in args.vms:
49         m = re.match (r'^(.*):(.*)$', a)
50         if m:
51             node_name = m.group (1)
52             wc = m.group (2)
53             if node_name not in nodes:
54                 sys.exit ("error: node %s does not exist" % node_name)
55             node = nodes[node_name]
56         else:
57             wc = a
58             node = lib.pick_any_node_which_is_up (nodes)
59         started = []
60         for vm_name in inactive:
61             if fnmatch.fnmatch (vm_name, wc):
62                 if args.viewer:
63                     subprocess.Popen ([config.VIRT_VIEWER, "-c",
64                                        node.uri, vm_name],
65                                       close_fds=True)
66                 lib.start_guest (c, node, vm_name)
67                 started.append (vm_name)
68
69         if not started:
70             sys.exit ("error: no VMs matched pattern %s" % a)
71
72         # Make sure we don't start the same VMs again the next
73         # time around the loop:
74         for vm_name in started:
75             del inactive[vm_name]