Keshav Agrawal
Keshav Agrawal

Reputation: 597

fexpect breaks fabric scripts

I hot upon an requirement where I needed to automatically answer the prompt on remote machine and then I found fexpect after reading different stackoverflow questions. But the moment I include fexpect in my script it breaks the whole script!

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 743, in main
    *args, **kwargs
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 368, in execute
    multiprocessing
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 264, in _execute
    return task.run(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 171, in run
    return self.wrapped(*args, **kwargs)
  File "/etc/puppet/fabfile.py", line 165, in edit_sudoers
    run('echo "Current Permission of the file /etc/sudoers - "`stat -c "%a %n" /etc/sudoers`')
  File "/usr/local/lib/python2.7/dist-packages/ilogue/fexpect/api.py", line 15, in run
    wrappedCmd = wrapExpectations(cmd)
  File "/usr/local/lib/python2.7/dist-packages/ilogue/fexpect/internals.py", line 15, in wrapExpectations
    script = createScript(cmd)
  File "/usr/local/lib/python2.7/dist-packages/ilogue/fexpect/internals.py", line 39, in createScript
    for e in fabric.state.env.expectations:
  File "/usr/local/lib/python2.7/dist-packages/fabric/utils.py", line 184, in __getattr__
    raise AttributeError(key)
AttributeError: expectations

The moment i write from ilogue.fexpect import expect, expecting, run fabric stops working with the above error message.

I asked in fabric irc as well but I got to know that this might be because of some version related issues. Has anyone else encountered this error before?

fexpect==0.2.post7 Fabric==1.8.0

Upvotes: 1

Views: 849

Answers (1)

Kevin
Kevin

Reputation: 2889

Just import fexpect's run as erun and its sudo as esudo.

When you use the fexpect run or sudo functions, you must wrap those calls in a with expecting(prompts): context. This is a known issue in fexpect, although there is a pull request, so it might be fixed by the time posterity reads this.

One solution is to import fexpect's run function with a different name, e.g. erun, and use it only when you need the automatic prompt handling functionality:

from fabric.api import run 
from ilogue.fexpect import expect, expecting, run as erun
run(a_cmd)   # Native Fabric run - should work fine
prompts = [...]
with expecting(prompts):
    erun(a_prompting_cmd)   # fexpect run - should with fine inside expecting context

Another thing that isn't explicitly stated in the fexpect documentation is that the pexpect package needs to be installed on the target system.

Yet another fexpect gotcha is that the prompt strings are regular expressions -- the fexpect sample code is misleading about this.

Upvotes: 3

Related Questions