#!/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 subprocess import sys import time import ansible.inventory import ansible.runner import lib def cmdline (subparsers): p = subparsers.add_parser ( 'off', help='power off a node (or nodes)' ) p.add_argument ( 'wildcard', help='node name(s) to be powered off (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']) # Check the nodes have no guests. running, _ = lib.get_all_guests (c) for node in nodes: for vm in running.values(): if vm['node'] == node.name: sys.exit ("error: node %s has running guests, migrate them off or stop them first" % node.name) # Power them off. runner = ansible.runner.Runner ( remote_user = 'root', module_name = 'command', module_args = '/sbin/poweroff', inventory = inventory, pattern = c['nodes_group'], ) data = runner.run () # Wait for them to power off. pings = 60 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['contacted']) == 0: break pings -= 1 time.sleep (1) if pings == 0: sys.exit ('warning: some nodes did not power off')