Ezylryb
Ezylryb

Reputation: 673

Running Fabric with Python script together

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!

Upvotes: 11

Views: 14196

Answers (2)

Bekzot Asimov
Bekzot Asimov

Reputation: 421

You have to give fabric's execute command. At the very bottom execute(deploy)

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174718

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.

Upvotes: 19

Related Questions