bd7f11639379df5e10db252d8c8a3202480e1076
[mclu.git] / mclu_stop.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 libvirt
22
23 import lib
24
25 def cmdline (subparsers):
26     p = subparsers.add_parser (
27         'stop',
28         help='stop virtual machine(s)',
29     )
30     p.add_argument (
31         '--force', action='store_const', const=True,
32         help='power off the virtual machine(s) forcibly'
33     )
34     p.add_argument (
35         'wildcards', nargs='+',
36         help='virtual machine(s) to be stopped'
37     )
38     p.set_defaults (run=run)
39
40 def run (c, args, nodes):
41     running, _ = lib.get_all_guests (c, nodes.values ())
42
43     for vm in running.values():
44         node = vm['node']
45         dom = vm['dom']
46         # Form the name of this VM (eg. "ham0:vm") so we can match it
47         # against the wildcards (eg. "ham0:*")
48         name = node.name + ":" + dom.name()
49         for wc in args.wildcards:
50             if fnmatch.fnmatch (name, wc) or fnmatch.fnmatch (dom.name(), wc):
51                 if args.force:
52                     dom.destroy()
53                 else:
54                     dom.shutdown()