Running Fabric with Python script together
Question: 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: 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](http://docs.fabfile.org/en/1.3.2/usage/fab.html)
for more information on the flexibility of the `fab` command.

