def hello(name="World"):
print("Hello %s!" % name)
Calling fab hello will display the familiar message "Hello World!" However, we can personalize it by issuing:
$fab hello:name=Jay
The message shown will be: "Hello Jay!"
Below is another small fabfile that allows us to find kernel release as well as machine hardware of a remote system by using SSH:
from fabric.api import run
def host_type():
run("uname -srm")
Since there is no remote host defined in the fabfile, we need to specify it (hostname) using option -H:
$fab -H hostname host_type
Fabric provides many command-execution functions, the following five are frequently used:
- run(command) -- Run a shell command on a remote host.
- sudo(comand) -- Run (with superuser privileges) a shell command on a remote host.
- local(command) -- Run a command on the local host.
- get(remote_path, local_path) -- Download one or more files from a remote host.
- put(local_path, remote_path) -- Upload one or more files to a remote host.
- settings(*args, **kwargs) -- Nest context managers and/or override env variables.
- lcd(path) -- Update local current working directory.
- cd(path) -- Update remote current working directory.
- testing the project -- python manage.py test apps
- packing the project -- tar czf /tmp/project.tgz .
- moving the packed file to server -- put("/tmp/project.tgz", "/path/to/serv/tmp/project.tgz")
- unpacking the file -- run("tar xzf /path/to/serv/tmp/project.tgz"), and
- deploying the project on server -- run("touch manage.py")
$fab install
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
env.hosts = ['serv1', 'serv2', 'serv3']
env.user= "admin"
def test():
"""
Run test; if it fails prompt the user for action.
"""
src_dir = "/path/to/local/src/directory"
with settings(warn_only=True), lcd(src_dir):
result = local("python manage.py test apps", capture=True)
if result.failed and not confirm("Tests failed. Continue anyway?"):
abort("Aborting at user request.")
def pack_move():
"""
Archive our current code and upload to servers.
"""
src_dir = "/path/to/local/src/directory"
with settings(warn_only=True), lcd(src_dir):
local( "tar czf /tmp/project.tgz .")
put( "/tmp/project.tgz", "/path/to/serv/tmp/project.tgz" )
local( "rm /tmp/project.tgz" )
def install():
"""
Deploy the project on servers.
"""
test()
pack_move()
dst_dir = "/path/to/serv/dst/directory"
with settings(hide("warnings"), warn_only=True):
if run("test -d %s" % dst_dir).failed:
run("mkdir -p %s" % dst_dir)
with cd(dst_dir):
run("tar xzf /path/to/serv/tmp/project.tgz")
run("touch manage.py")
run("rm -f /path/to/serv/tmp/project.tgz")