-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reboot() snippet for 2.x #1904
Labels
Comments
I use this in my fabric-2.x scripts: import sys
import time
import paramiko.ssh_exception
# related links for reboot with fabric-2.x
# * fabric-1.x:
# https://github.com/fabric/fabric/blob/c0224a52df59821f21a8c0bd47ce15e42c2046a4/fabric/operations.py#L1244
# * snippet: https://github.com/fabric/fabric/issues/1904
# * timings: https://github.com/fabric/fabric/issues/1488
def reboot(c):
'''Reboot remote host and wait until the remote host is up again.
Also print out how long the reboot has taken.
NoValidConnectionsError after timeout or any unexpected Exception will be raised.
'''
start_time = time.time()
c.run('reboot', warn=True)
time.sleep(5)
# wait until the reboot has been finished
timeout = time.time() + 120
while True:
try:
c.run(
'whoami', # use `whoami` as a no-op
echo=False, hide=True,
)
break
except paramiko.ssh_exception.NoValidConnectionsError as exc:
sys.stdout.write('.')
sys.stdout.flush()
if time.time() > timeout:
raise exc
time.sleep(1)
print('reboot took {:.0f} seconds'.format(time.time() - start_time)) |
We use following approach with fabric.api.settings(host_string=server_ip, user="root"):
connect_attempts = 10
for _ in range(connect_attempts):
try:
fabric.api.execute('uname -a')
break
except BaseException as e: # use paramiko exceptions here
time.sleep(10) |
This helped me solve my problem. I went with this approach
Then I call it like so
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The official docs states:
I'm totally happy with that approach, however could you give a snippet for replicating the 1.x's reboot-and-wait-reconnect functionality? I guess it has to be some kind of
time.sleep(1)
loop, but I'm not clear about the connection management part.The text was updated successfully, but these errors were encountered: