#!/usr/bin/python # mclu (mini cluster) # Copyright (C) 2014 Red Hat Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import argparse import os import sys import time import subprocess import ansible.inventory import ansible.runner import config import lib def cmdline (subparsers): p = subparsers.add_parser ( 'on', help='power on a node (or nodes)' ) p.add_argument ( 'wildcard', help='node name(s) to be woken up (wildcard may be used)' ) p.set_defaults (run=run) def run (c, args): inventory = ansible.inventory.Inventory () inventory.subset (subset_pattern = args.wildcard) nodes = inventory.get_hosts (c['nodes_group']) # Wake them up. for node in nodes: vars = node.get_variables() if 'mac' in vars: devnull = open (os.devnull, "w") subprocess.check_call ([config.WOL, vars['mac']], stdout = devnull, stderr = devnull) else: print "warning: no mac= line in ansible hosts file for %s (ignored)" % node.name # Wait for them to come up. pings = 30 runner = ansible.runner.Runner ( remote_user = 'root', module_name = 'ping', inventory = inventory, pattern = c['nodes_group'], ) while pings > 0: data = runner.run () if len (data['dark']) == 0: break pings -= 1 time.sleep (1) if pings == 0: sys.exit ('warning: some nodes did not wake up')