Use new progress when connecting to hosts.
import gevent
import six
+from pyinfra.progress import progress_spinner
-def connect_all(state, progress=None):
+
+def connect_all(state):
'''
Connect to all the configured servers in parallel. Reads/writes state.inventory.
@@ -14,8 +16,6 @@ def connect_all(state, progress=None):
state (``pyinfra.api.State`` obj): the state containing an inventory to connect to
'''
- greenlets = {}
-
# Don't connect to anything within our (top level, --limit) limit
hosts = [
host for host in state.inventory
@@ -23,22 +23,21 @@ def connect_all(state, progress=None):
or host in state.limit_hosts
]
- for host in hosts:
- greenlets[host] = state.pool.spawn(host.connect, state)
+ greenlet_to_host = {
+ state.pool.spawn(host.connect, state): host
+ for host in hosts
+ }
- # Wait for all the connections to complete
- for client in gevent.iwait(greenlets.values()):
- # Trigger CLI progress if provided
- if progress:
- progress()
+ with progress_spinner(greenlet_to_host.values()) as progress:
+ for greenlet in gevent.iwait(greenlet_to_host.keys()):
+ host = greenlet_to_host[greenlet]
+ progress(host)
# Get/set the results
failed_hosts = set()
- for host, greenlet in six.iteritems(greenlets):
- client = greenlet.get()
-
- if client:
+ for greenlet, host in six.iteritems(greenlet_to_host):
+ if host.connection:
state.activate_host(host)
else:
failed_hosts.add(host)
