24

I would like to automate the response for some question prompted by some programs, like mysql prompting for a password, or apt asking for a 'yes' or ... when I want to rebuild my haystack index with a ./manage.py rebuild_index.

For MySQL, I can use the --password= switch, and I'm sure that apt has a 'quiet' like option. But how can I pass the response to other programs ?

dzen
  • 6,923
  • 5
  • 28
  • 31

6 Answers6

39

If you are looking for a user to confirm an operation, use the confrim method.

if fabric.contrib.console.confirm("You tests failed do you want to continue?"):
  #continue processing

Or if you are looking for a way to get input from the user, use the prompt method.

password = fabric.operations.prompt("What is your password?")
buckley
  • 2,060
  • 1
  • 17
  • 12
  • 7
    Instead of using Fabric's prompt for a password, I would recommend importing getpass and using that so that you don't display the password on the terminal as it's being typed. For non-password input, then I agree that Fabric's prompt is good to use. – Matthew Rankin Mar 15 '10 at 21:42
  • In fabric2: `invocations.console.confirm("do you really want to do this?")` http://www.fabfile.org/upgrading.html#contrib (`invocations` is a standalone package now) – Marshall May 06 '19 at 21:08
13

Why can't you just use pipes?

For example, for an automated auto accept, just use yes, that just outputs a neverending stream of y.

yes | rm *.txt


(source: wikimedia.org)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
  • What if the command *sometimes* prompts and sometimes does not? How can I pipe yes only when I know there will be a prompt? – Kvass Jun 19 '13 at 13:54
  • and what if i have a complex command, asking 2-3 times yes/no and numerical value after ? – Alex Granovsky Jul 23 '15 at 16:36
1

Those both methods are valid and works.

I choose the first one, because I didn't want to have any interaction with my deployment system.

So here is the solution I used:

% yes | ./manage.py rebuild_index

WARNING: This will irreparably remove EVERYTHING from your search index. Your choices after this are to restore from backups or rebuild via the rebuild_index command. Are you sure you wish to continue? [y/N] Removing all documents from your index because you said so. All documents removed. Indexing 27 Items.

dzen
  • 6,923
  • 5
  • 28
  • 31
1

The development version of Fabric (1.0a) now supports interaction with remote programs. http://docs.fabfile.org/1.0a/usage/interactivity.html

akv
  • 3,693
  • 3
  • 17
  • 11
  • 2
    Fabric is now at 1.0.1 and the url is http://docs.fabfile.org/en/1.0.1/usage/interactivity.html – Julien Grenier Apr 14 '11 at 18:10
  • Fabric is now at 1.5 and the url is http://docs.fabfile.org/en/1.5/usage/interactivity.html. The documentation has not changed significantly. – lsh Feb 28 '13 at 12:02
  • This is fun. Ok, here's for 1.10: http://docs.fabfile.org/en/1.10/usage/interactivity.html. Next update shouldn't come any earlier than 2017, and then hopefully for Fabric 2! – lucianf May 11 '15 at 23:44
  • 1.13 is here [docs.fabfile.org/en/1.13/usage/interactivity.html](http://docs.fabfile.org/en/1.13/usage/interactivity.html). For you guys said that in 2011, 2013, 2015... I should add one...It's 2017 now. ;) – Yarco Jul 05 '17 at 03:47
0

Late answer, but hope this would help peoples having similar problem.

Different point:

  1. Answer two or more different input to console.
  2. parallel mode Support.
  3. Any type of input yes/no/y/n included.

Problem

[hostxxx] out: Type 'c' if you want to use the Commercial Edition.
[hostxxx] out: Type 'o' if you want to use the Open Source Edition.
[hostxxx] out: Type '3' to view the GNU General Public License version 3.
[hostxxx] out: Type 'L' to view the Lesser GNU General Public License version 2.1.
[hostxxx] out: Type 'yes' to accept this license offer.
[hostxxx] out: Type 'no' to decline this license offer.

Solution:

Use printf instead of yes to add more flexibility, meanwhile this works like a charm on parallel mode.

@parallel
def demo_multi_input():
    run('printf "o\nyes\n"|./configure --prefix=/home/work/bin/qt')
luoluo
  • 5,353
  • 3
  • 30
  • 41
-1

Use this code:

run("echo yes|./manage.py rebuild_index")
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40