I have a django app and I wrote a fabric script that installs my app on deployment server (Cent OS 5).
Now I want to run the same fabric script locally on the deployment server.
Is there a way to do it without supplying ssh user and password?
I mean just with "-H localhost"?
Thanks, Alex A.
run (fabric. Fabric's run procedure is used for executing a shell command on one or more remote hosts. The output results of run can be captured using a variable.
Python. Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Fabric is very simple and powerful and can help to automate repetitive command-line tasks. This approach can save time by automating your entire workflow.
More specifically, Fabric is: A tool that lets you execute arbitrary Python functions via the command line; A library of subroutines (built on top of a lower-level library) to make executing shell commands over SSH easy and Pythonic.
Yes, you can run fab locally by using method local instead of run. What I do typically is have methods for setting up the environment, and call these methods first before calling the actual task. Let me illustrate this with an example for your specific question
from fabric.operations import local as lrun, run from fabric.api import task from fabric.state import env @task def localhost(): env.run = lrun env.hosts = ['localhost'] @task def remote(): env.run = run env.hosts = ['some.remote.host'] @task def install(): env.run('deploymentcmd')
And based on the environment, you can do the following
Install on localhost:
fab localhost install
Install on remote machine:
fab remote install
I am using another trick for executing remote task locally:
from fabric.api import run, sudo, local from contextlib import contextmanager @contextmanager def locally(): global run global sudo global local _run, _sudo = run, sudo run = sudo = local yield run, sudo = _run, _sudo def local_task(): with locally(): run("ls -la")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With