Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to suppress output of fabric run?

Tags:

python

fabric

I am running a command on the remote machine:

remote_output = run('mysqldump --no-data --user=username --password={0} database'.format(password)) 

I would like to capture the output, but not have it all printed to the screen. What's the easiest way to do this?

like image 693
Ben McCann Avatar asked Feb 26 '12 19:02

Ben McCann


2 Answers

It sounds like Managing output section is what you're looking for.

To hide the output from the console, try something like this:

from __future__ import with_statement from fabric.api import hide, run, get  with hide('output'):     run('mysqldump --no-data test | tee test.create_table')     get('~/test.create_table', '~/test.create_table') 

Belows is the sample results:

No hosts found. Please specify (single) host string for connection: 192.168.6.142 [192.168.6.142] run: mysqldump --no-data test | tee test.create_table [192.168.6.142] download: /home/quanta/test.create_table <- /home/quanta/test.create_table 
like image 143
quanta Avatar answered Sep 22 '22 16:09

quanta


Try this if you want to hide everything from log and avoid fabric throwing exceptions when command fails:

from __future__ import with_statement from fabric.api import env,run,hide,settings  env.host_string = 'username@servernameorip' env.key_filename = '/path/to/key.pem'  def exec_remote_cmd(cmd):     with hide('output','running','warnings'), settings(warn_only=True):         return run(cmd) 

After that, you can check commands result as shown in this example:

cmd_list = ['ls', 'lss'] for cmd in cmd_list:     result = exec_remote_cmd(cmd)     if result.succeeded:         sys.stdout.write('\n* Command succeeded: '+cmd+'\n')         sys.stdout.write(result+"\n")     else:         sys.stdout.write('\n* Command failed: '+cmd+'\n')         sys.stdout.write(result+"\n") 

This will be the console output of the program (observe that there aren't log messages from fabric):

 * Command succeeded: ls Desktop    espaiorgcats.sql  Pictures   Public     Videos Documents  examples.desktop  projectes  scripts Downloads  Music         prueba Templates  * Command failed: lss /bin/bash: lss: command not found 
like image 23
cfillol Avatar answered Sep 21 '22 16:09

cfillol