فهرست منبع

Add support for 'networks' directive in task block

Bertrand Chenal 6 سال پیش
والد
کامیت
98fc1a26e9
3فایلهای تغییر یافته به همراه13 افزوده شده و 11 حذف شده
  1. 2 2
      byrd/config.py
  2. 10 8
      byrd/main.py
  3. 1 1
      byrd/utils.py

+ 2 - 2
byrd/config.py

@@ -138,6 +138,8 @@ class Task(Node):
         'env': EnvNode,
         'multi': MultiList,
         'fmt': Atom,
+        'networks': AtomList,
+        # TODO add support for auth here
     }
 
     @classmethod
@@ -175,5 +177,3 @@ class ConfigRoot(Node):
         'env': EnvNode,
         'load': LoadList,
     }
-
-

+ 10 - 8
byrd/main.py

@@ -62,11 +62,11 @@ def connect(host, auth):
 
     private_key_file = password = None
     if auth and auth.get('ssh_private_key'):
-        private_key_file = auth.ssh_private_key
-        if not os.path.exists(auth.ssh_private_key):
-            msg = 'Private key file "%s" not found' % auth.ssh_private_key
+        private_key_file = os.path.expanduser(auth.ssh_private_key)
+        if not os.path.exists(private_key_file):
+            msg = 'Private key file "%s" not found' % private_key_file
             raise ByrdException(msg)
-        password = get_passphrase(auth.ssh_private_key)
+        password = get_passphrase(private_key_file)
     else:
         password = get_password(host)
 
@@ -331,6 +331,10 @@ def run_batch(task, hosts, cli, global_env=None):
     out = None
     export_env = {}
     task_env = global_env.fmt(task.get('env', {}))
+    if not hosts and task.networks:
+        hosts = list(chain(*(spellcheck(cli.cfg.networks, n).hosts
+                             for n in task.networks)))
+
     if task.get('multi'):
         parent_env = Env(export_env, task_env, global_env)
         parent_sudo = task.sudo
@@ -339,8 +343,7 @@ def run_batch(task, hosts, cli, global_env=None):
             if task_name:
                 # _cfg contain "local" config wrt the task
                 siblings = task._cfg.tasks
-                spellcheck(siblings, task_name)
-                sub_task = siblings[task_name]
+                sub_task = spellcheck(siblings, task_name)
                 sudo = step.sudo or sub_task.sudo or parent_sudo
             else:
                 # reify a task out of attributes
@@ -351,8 +354,7 @@ def run_batch(task, hosts, cli, global_env=None):
             sub_task.sudo = sudo
             network = step.get('network')
             if network:
-                spellcheck(cli.cfg.networks, network)
-                hosts = cli.cfg.networks[network].hosts
+                hosts = spellcheck(cli.cfg.networks, network).hosts
             child_env = step.get('env', {})
             child_env = parent_env.fmt(child_env)
             out = run_batch(sub_task, hosts, cli, Env(child_env, parent_env))

+ 1 - 1
byrd/utils.py

@@ -82,7 +82,7 @@ def spell(candidates,  word):
 
 def spellcheck(objdict, word):
     if word in objdict:
-        return
+        return objdict[word]
 
     candidates = objdict.get('_candidates')
     if not candidates: