31ff86ce9d41c7d42a103f3381aa29d1c862cacc
[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 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         'on',
29         help='power on a node (or nodes)'
30     )
31     p.add_argument (
32         '--all', action='store_const', const=True,
33         help='wake up all nodes'
34     )
35     p.add_argument (
36         'nodes', nargs='*',
37         help='node name to be woken up'
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     # Wake them up.
45     up = 0
46     for node in nodes:
47         if node.ping (force=True):
48             up += 1
49         else:
50             node.wake()
51
52     # Wait for them to come up.
53     pings = 30
54     while pings > 0 and up < len (nodes):
55         for node in nodes:
56             if not node.up:
57                 if node.ping (force=True):
58                     up += 1
59         pings -= 1
60
61     if pings == 0:
62         sys.exit ('warning: some nodes did not wake up')