hellojoshhhy
hellojoshhhy

Reputation: 958

python exception handling is not working with fabric.api

I am working on a code so that it can handle the error from fabric.local, but some how it always abort with the error and never go into except block. Here is my code, hopefully can get some idea from you guys

This snippet is trying to get Vagrant ssh port, if the vagrant is not up, bring it up

def findsshport():
    with settings(warn_only=True):
        try:
            print 'greping port'
            return (local('vagrant ssh-config {} | grep Port'.format(env.vmId), capture=True))
        except:
            print 'vagrant not up'
            with lcd('%s' % (buildfolder)):
                local('vagrant up ext4')
            return (local('vagrant ssh-config {} | grep Port'.format(env.vmId), capture=True))

env.user = 'root'
sshPort = findsshport()
env.hosts = ['127.0.0.1:' + sshPort.split()[1]]

Error

[localhost] local: vagrant ssh-config 22921a7 | grep Port

Warning: local() encountered an error (return code 1) while executing 'vagrant ssh-config 22921a7 | grep Port'
Traceback (most recent call last):
  File "/home/testing/local/lib/python2.7/site-packages/test123/fabriclogin.py", line 114, in sshlogin
    env.hosts = ['127.0.0.1:' + sshPort.split()[1]]
AttributeError: 'NoneType' object has no attribute 'split'

UPDATE Similar Question and Answer

Can I catch error codes when using Fabric to run() calls in a remote shell?

Upvotes: 0

Views: 500

Answers (3)

Shadow
Shadow

Reputation: 9427

Your problem is probably here.

with settings(warn_only=True)

Remove this line, and your local call will raise exceptions if the command exits with a non-zero return code.

def task_name():
    with settings(warn_only=True):
        try:
            local("invalid_command")
        except:
            print("This will never print!")

Lets compare that to;

def task_name():
    try:
        local("invalid_command")
    except:
        print("This will print")

Upvotes: 0

hellojoshhhy
hellojoshhhy

Reputation: 958

Martin is correct, that was a warning from fabric.api.local and python exception handling will not treat it as an error. Instead, the error that I seen was from another part of code which the above snippet had returned something invalid. Instead of using try and except, if else is used with return_code which checking the command exit status.

port = local('vagrant ssh-config {} | grep Port'.format(env.vmId), capture=True)
if port.return_code == 0:
    return port
else:
    with lcd('%s' % (buildfolder)):
            local('vagrant up {}'.format(env.vmId), capture=True)
        return (local('vagrant ssh-config {} | grep Port'.format(env.vmId), capture=True))

Upvotes: 0

Martin Liu
Martin Liu

Reputation: 107

It seems like it's just a warning from fabric. My understand if you encounter an error on ssh, it doesn't "translate" into a Python error, that's why the exception block doesn't work. Please provide error trace for further analysis.

Upvotes: 1

Related Questions