Skip to content
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

Open
hyperknot opened this issue Nov 23, 2018 · 3 comments
Open

reboot() snippet for 2.x #1904

hyperknot opened this issue Nov 23, 2018 · 3 comments

Comments

@hyperknot
Copy link

The official docs states:

No equivalent has been written for modern Fabric; now that the connection/client objects are made explicit, one can simply instantiate a new object with the same parameters (potentially with sufficient timeout parameters to get past the reboot, if one doesn’t want to manually call something like time.sleep.)

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.

@theno
Copy link

theno commented Sep 11, 2019

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))

@vsilent
Copy link

vsilent commented Sep 12, 2019

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)

@alias454
Copy link

This helped me solve my problem. I went with this approach

def cmd_wait(con, run_cmd):
    # May take up to 5 minutes
    sleep(5)
    ret = 'Command took too long to finish'
    for _ in range(25):
        try:
            ret = con.run(run_cmd, hide=True)
            break
        except (ConnectionError, NoValidConnectionsError):
            sleep(10)

    return ret

Then I call it like so

session = connect(hostname, username, password)

is_rebooted = session.sudo('nohup sudo -b bash -c "sleep 5 && reboot"', hide=True)
if is_rebooted.return_code == 0:
    is_running = cmd_wait(session, 'uptime')
    if is_running.return_code == 0:
        print('[OK] Reboot successful: ' + hostname)
    else:
        print('[FAIL] Status Check: ' + hostname)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants