mclu list: Sort lists of guests when displaying them.
[mclu.git] / mclu_on.py
index 31ff86c..7183f93 100644 (file)
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-import sys
+import argparse
 import os
+import sys
+import time
 import subprocess
 
-import argparse
+import ansible.inventory
+import ansible.runner
+
+import config
 import lib
 
 def cmdline (subparsers):
@@ -29,34 +34,40 @@ def cmdline (subparsers):
         help='power on a node (or nodes)'
     )
     p.add_argument (
-        '--all', action='store_const', const=True,
-        help='wake up all nodes'
-    )
-    p.add_argument (
-        'nodes', nargs='*',
-        help='node name to be woken up'
+        'wildcard',
+        help='node name(s) to be woken up (wildcard may be used)'
     )
     p.set_defaults (run=run)
 
-def run (c, args, all_nodes):
-    nodes = lib.get_nodes_by_name (all_nodes, args.nodes, args.all)
+def run (c, args):
+    inventory = ansible.inventory.Inventory ()
+    inventory.subset (subset_pattern = args.wildcard)
+    nodes = inventory.get_hosts (c['nodes_group'])
 
     # Wake them up.
-    up = 0
     for node in nodes:
-        if node.ping (force=True):
-            up += 1
+        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:
-            node.wake()
+            print "warning: no mac= line in ansible hosts file for %s (ignored)" % node.name
 
     # Wait for them to come up.
     pings = 30
-    while pings > 0 and up < len (nodes):
-        for node in nodes:
-            if not node.up:
-                if node.ping (force=True):
-                    up += 1
+    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')