mclu initial commit.
[mclu.git] / mclu_shutdown.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 sys
20 import os
21 import subprocess
22
23 import argparse
24 import lib
25
26 def cmdline (subparsers):
27     p = subparsers.add_parser (
28         'shutdown',
29         help='power off a node (or nodes)'
30     )
31     p.add_argument (
32         '--all', action='store_const', const=True,
33         help='power off all nodes'
34     )
35     p.add_argument (
36         'nodes', nargs='*',
37         help='node name to be powered off'
38     )
39     p.set_defaults (run=run)
40
41 def run (c, args, all_nodes):
42     nodes = lib.get_nodes_by_name (all_nodes, args.nodes, args.all)
43
44     # Check the nodes have no guests.
45     for node in nodes:
46         guests = node.guests ()
47         if guests != []:
48             names = map (lambda x : x.name(), guests)
49             sys.exit ("error: node %s has running guests %s, migrate them off or stop them first" % (node.name, names))
50
51     # Power them off.
52     up = 0
53     for node in nodes:
54         if node.ping (force=True):
55             node.shutdown ()
56             up += 1
57
58     pings = 60
59     while pings > 0 and up > 0:
60         if not node.up:
61             if not node.ping (force=True):
62                 up -= 1
63         pings -= 1
64
65     if pings == 0:
66         sys.exit ('warning: some nodes did not power off')