Running Fabric with Python script together

Ezylryb picture Ezylryb · Nov 17, 2011 · Viewed 13.7k times · Source

I see most of the Fabric API are use together with function.

Example of file (sample.py):

from fabric.api import *
print "Hello"

def deploy():
    with settings(hosts_string="Remote", user = "ubuntu", key_filename="/home/ubuntu/key.pem"):
        put('/home/localuser/sample.sh', '/home/ubuntu/')
        run('bash /home/ubuntu/sample.sh')

I run the command to execute

fab deploy

Is it possible to running Fabric in Main Method. So when I run it as a python script, the fabric will be executed.

python ./sample.py

Thanks!

Answer

Burhan Khalid picture Burhan Khalid · Nov 17, 2011

To answer your question directly, you can add this snippet to your file:

from fabric.api import *
print "Hello"

def deploy():
    with settings(host_string="Remote", user = "ubuntu", key_filename="/home/ubuntu/key.pem"):
        put('/home/localuser/sample.sh', '/home/ubuntu/')
        run('bash /home/ubuntu/sample.sh')

if __name__ == '__main__':
   deploy()

Now when you run python ./sample.py, it will do the same thing as fab deploy

However, the fab allows you to do much more other than your simple example. See the fab documentation for more information on the flexibility of the fab command.