Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Fabric with Python script together

Tags:

python

ssh

fabric

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!

like image 351
Ezylryb Avatar asked Nov 17 '11 10:11

Ezylryb


1 Answers

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.

like image 51
Burhan Khalid Avatar answered Oct 03 '22 07:10

Burhan Khalid