Skip to content

Instantly share code, notes, and snippets.

@nMustaki
Created July 21, 2015 21:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nMustaki/507c336013bf15a9f289 to your computer and use it in GitHub Desktop.
Save nMustaki/507c336013bf15a9f289 to your computer and use it in GitHub Desktop.
Use of Fabric as library (for fab-user mailing list)
# With many thanks to pmuller for its streamlogger class : # https://gist.githubusercontent.com/pmuller/2376336/raw/cc455144c66b058d8de56c5d6c0a87dbd7c301d5/streamlogger.py
class FabricException(Exception):
pass
def fabric_task(host, command, **kwargs):
"""
Args:
host (string): eg : server-id
command (import path) : python import path to the task, eg sercie.process.status
"""
fabric.api.env.host_string = "root@{0}:22".format(host)
fabric.api.env.host = host
fabric.api.env.abort_on_prompts = True
fabric.api.env.colorize_errors = False
fabric.api.env.abort_exception = FabricException
fabric.api.env.warn_only = True
from app.lib.streamsilencer import StreamSilencer
with StreamSilencer('stdout'):
try:
with fabric.api.quiet():
return fabric.api.execute(command,
hosts=[fabric.api.env.host_string],
**kwargs)[fabric.api.env.host_string]
finally:
fabric.network.disconnect_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment