sebastien / cuisine

Chef-like functionality for Fabric

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

trapping fabric.exceptions.NetworkError

ilovetogetspamed opened this issue · comments

I get a "fabric.exceptions.NetworkError: Name lookup failed for raspberrypi" when cuisine tries to connect to my host (if the host ins't reachable of course).

I tried to trap the error by wrapping it in a try:except block:

import cuisine
from fabric.exceptions import NetworkError

...

try:
    cuisine.connect(host='raspberrypi', user='pi', password='raspberry')
except NetworkError as e:
    print e
    exit(2)

...
# do more cuisine stuff
...
# update the hostname
cuisine.run('echo {new_hostname} > /etc/hostname'.format(new_hostname=args.new_hostname))

but the error is never caught:

(cusine_env)iMac:fab-monitor *****$ python fab-monitor.py monitor01
[raspberrypi] sudo: DEBIAN_FRONTEND=noninteractive apt-get -q --yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -q --yes update
Traceback (most recent call last):
  File "fab-monitor.py", line 229, in <module>
    main(sys.argv[1:])
  File "fab-monitor.py", line 224, in main
    update_monitor(new_hostname=validated_name)
  File "fab-monitor.py", line 40, in update_monitor
    cuisine.package_update()
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/cuisine.py", line 158, in wrapper
    return function(*args, **kwargs)
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/cuisine.py", line 208, in wrapper
    return specific(*args, **kwargs)
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/cuisine.py", line 1009, in package_update_apt
    return apt_get("-q --yes update")
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/cuisine.py", line 999, in apt_get
    result = sudo(cmd)
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/cuisine.py", line 158, in wrapper
    return function(*args, **kwargs)
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/cuisine.py", line 463, in sudo
    return run(*args, **kwargs)
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/cuisine.py", line 441, in run
    return fabric.api.sudo(*args, **kwargs)
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/fabric/network.py", line 683, in host_prompting_wrapper
    return func(*args, **kwargs)
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/fabric/operations.py", line 1148, in sudo
    capture_buffer_size=capture_buffer_size,
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/fabric/operations.py", line 930, in _run_command
    channel=default_channel(), command=wrapped_command, pty=pty,
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/fabric/state.py", line 424, in default_channel
    chan = _open_session()
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/fabric/state.py", line 416, in _open_session
    return connections[env.host_string].get_transport().open_session()
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/fabric/network.py", line 159, in __getitem__
    self.connect(key)
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/fabric/network.py", line 151, in connect
    user, host, port, cache=self, seek_gateway=seek_gateway)
  File "/Users/*****/cusine_env/lib/python2.7/site-packages/fabric/network.py", line 577, in connect
    raise NetworkError('Name lookup failed for %s' % host, e)
fabric.exceptions.NetworkError: Name lookup failed for raspberrypi

This try/except works:

try:
    1/0
except:
    print "Stupid Git, you can't divide by Zero!"

>>> 
Stupid Git, you cant divide by Zero!

So I don't quite understand why the error trap doesn't work for the cusines' connect method.

I tried it using just except: instead of except NetworkError and it still failed.

Do you have a way of trapping the error that I'm unaware of? I'm sure I'm missing something simple, I'm relatively new to Python language.

Thank you.

@ilovetogetspamed The error is being raised on line 40 of fab-monitor.py during your call to cuisine.package_update(). You might want to look into using sys.excepthook to handle the uncaught exceptions.

Cuisine does not do any special error handling. I think that what @jenrzzz suggests is a good way to resolve your issue.

Thanks for the info, I will try that.