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