Fix error message.
[mclu.git] / mclu_on.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 os
21 import sys
22 import time
23 import subprocess
24
25 import ansible.inventory
26 import ansible.runner
27
28 import config
29 import lib
30
31 def cmdline (subparsers):
32     p = subparsers.add_parser (
33         'on',
34         help='power on a node (or nodes)'
35     )
36     p.add_argument (
37         'wildcard',
38         help='node name(s) to be woken up (wildcard may be used)'
39     )
40     p.set_defaults (run=run)
41
42 def run (c, args):
43     inventory = ansible.inventory.Inventory ()
44     inventory.subset (subset_pattern = args.wildcard)
45     nodes = inventory.get_hosts (c['nodes_group'])
46
47     # Wake them up.
48     for node in nodes:
49         vars = node.get_variables()
50         if 'mac' in vars:
51             devnull = open (os.devnull, "w")
52             subprocess.check_call ([config.WOL, vars['mac']],
53                                    stdout = devnull, stderr = devnull)
54         else:
55             print "warning: no mac= line in ansible hosts file for %s (ignored)" % node.name
56
57     # Wait for them to come up.
58     pings = 30
59     runner = ansible.runner.Runner (
60         remote_user = 'root',
61         module_name = 'ping',
62         inventory = inventory,
63         pattern = c['nodes_group'],
64     )
65     while pings > 0:
66         data = runner.run ()
67         if len (data['dark']) == 0:
68             break
69         pings -= 1
70         time.sleep (1)
71
72     if pings == 0:
73         sys.exit ('warning: some nodes did not wake up')