fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Fab-user] command timeouts via Connection.run


From: Jeff Forcier
Subject: Re: [Fab-user] command timeouts via Connection.run
Date: Tue, 8 Jan 2019 12:49:31 -0800

Hi Chris,

Was the former, at least in my case, apologies!

Looks like this is a missing feature in 2.x, we simply aren't providing any way for you to pass that timeout parameter yet :( Your use of 'env' is unfortunately a bit off the mark, that's for shell environment variables, which doesn't help here.

I searched the tracker and somebody made a ticket for this recently, which I just expanded on: https://github.com/fabric/fabric/issues/1922

The tl;dr is that the command runner level needs to understand timeouts, and then Fabric's use of that can subclass/extend such that it tickles the timeout param in paramiko.Client.exec_command. (See links in that ticket.)

Best,
Jeff



On Tue, Jan 8, 2019 at 11:42 AM Chris Satterthwaite <address@hidden> wrote:

Is this newsgroup active?  It’s been a week and I haven’t seen any activity.

 

Since I posted for the first time, I’m not sure if my message just hit bad timing over holidays, or if it’s stumped folks and I should post elsewhere.

 

Thanks!

 

From: Fab-user <fab-user-bounces+chris=address@hidden> On Behalf Of Chris Satterthwaite
Sent: Monday, December 31, 2018 1:06 PM
To: address@hidden
Subject: [Fab-user] command timeouts via Connection.run

 

Fab community,

 

I need some direction on getting command timeouts to work on remote SSH commands using fabric.connection.Connection.run.  I’m new to Fabric so I’m assuming I have missed something straightforward; I appreciate any guidance you can provide. 

 

I scripted a quick test case below, using Paramiko’s exec_command and Fabric’s Connection.run.  The command I’m sending from Windows over to a Linux remote endpoint is 'date; sleep 3; date', and attempting to force a timeout after 1 second.

 

The result of the script shows the following, which illustrates Paramiko timing out as expected, but Fabric not timing out:

=================================================

Calling useFabric with timeout 1:

  {'hide': True, 'env': {'command_timeout': '1'}}

  result is ok: True

  STDOUT: Mon Dec 31 12:38:19 CST 2018

  STDOUT: Mon Dec 31 12:38:22 CST 2018

  useFabric runtime in seconds: 3

 

Calling useParamiko with timeout 1:

  STDOUT: Mon Dec 31 12:38:22 CST 2018

 

  Timed out

  useParamiko runtime in seconds: 1

=================================================

 

 

Test script follows:

=================================================

import sys

import traceback

import socket

import time

from fabric import Connection

from paramiko.client import SSHClient, AutoAddPolicy

 

def useFabric():

    try:

        timeout = 1

        client = Connection(host='192.168.121.190', user='chris', connect_timeout='10', connect_kwargs={'password': 'changeMe!'})

        runtimeArgs = {'hide': True, 'env': {'command_timeout' : str(timeout)}}

        print('  {}'.format(runtimeArgs))

        result = client.run('date; sleep 3; date', **runtimeArgs)

        print('  result is ok: {}'.format(result.ok))

        for line in result.stdout.split('\n'):

            if len(line) > 1:

                print('  STDOUT: {}'.format(line))

        if len(result.stderr) > 1:

            print('  STDERR: {}'.format(result.stderr))

    except socket.timeout:

        print('  Timed out: {}'.format(str(sys.exc_info()[2])))

    except:

        stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])

        print('  Exception: {}'.format(stacktrace))

 

def useParamiko():

    try:

        timeout = 1

        client = SSHClient()

        client.load_system_host_keys()

        client.set_missing_host_key_policy(AutoAddPolicy)

        client.connect('192.168.121.190', username='chris', password='changeMe!')

        (stdin, stdout, stderr) = client.exec_command('date; sleep 3; date', timeout=timeout)

        for line in stdout:

            print('  STDOUT: {}'.format(line))

        for line in stderr:

            print('  STDERR: {}'.format(line))

    except socket.timeout:

        print('  Timed out')

    except:

        stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])

        print('  Exception: {}'.format(stacktrace))

 

def caller(func):

    print('Calling {} with timeout 1:'.format(str(func)))

    start = time.time()

    eval(func)()

    stop = time.time()

    print('  {} runtime in seconds: {}\n'.format(func, int(stop-start)))

 

def main():

    caller('useFabric')

    caller('useParamiko')

 

if __name__ == '__main__':

    main()

=================================================

 

 

Lastly, here are my platform versions in case they are needed:

  Base platform (where I ran the script below):  Windows 10 Pro

  Python version on the base platform:  Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]

  Target platform:  RedHat Linux 7.5

  Fabric version (from pip):  2.2.1

  Paramiko version (from pip):  2.4.1

 

Thanks!

_______________________________________________
Fab-user mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/fab-user


--
Jeff Forcier
Unix sysadmin; Python engineer
http://bitprophet.org

reply via email to

[Prev in Thread] Current Thread [Next in Thread]