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?