3 # Copyright (C) 2014 Red Hat Inc.
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.
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.
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.
25 import ansible.inventory
30 def cmdline (subparsers):
31 p = subparsers.add_parser (
33 help='power off a node (or nodes)'
37 help='node name(s) to be powered off (wildcard may be used)'
39 p.set_defaults (run=run)
42 inventory = ansible.inventory.Inventory ()
43 inventory.subset (subset_pattern = args.wildcard)
44 nodes = inventory.get_hosts (c['nodes_group'])
46 # Check the nodes have no guests.
47 running, _ = lib.get_all_guests (c)
49 for vm in running.values():
50 if vm['node'] == node.name:
51 sys.exit ("error: node %s has running guests, migrate them off or stop them first" % node.name)
54 runner = ansible.runner.Runner (
56 module_name = 'command',
57 module_args = '/sbin/poweroff',
58 inventory = inventory,
59 pattern = c['nodes_group'],
63 # Wait for them to power off.
65 runner = ansible.runner.Runner (
68 inventory = inventory,
69 pattern = c['nodes_group'],
73 if len (data['contacted']) == 0:
79 sys.exit ('warning: some nodes did not power off')