Skip to content

Instantly share code, notes, and snippets.

@gelendir
Last active December 23, 2015 16:39
Show Gist options
  • Save gelendir/6663930 to your computer and use it in GitHub Desktop.
Save gelendir/6663930 to your computer and use it in GitHub Desktop.

title: Fabric name: inverse layout: true class: center, middle, inverse

#Fabric

Lord Junior 6th Gregory Eric Sanderson Turcot Temlett MacDonnell of Glengary Forbes of Linden


layout: false

What is fabric ?


What about chef or puppet ?

  • Similar goals
  • Different approach
  • Chef/Puppet are full-fledged frameworks
  • Fabric feels more like a micro-framework
  • I <3 python

Hello world

fabfile.py

def hello():
    print("Hello world!")

running the code:

$ fab hello
Hello world!

Done.

Multiple tasks

fabfile.py

def hello():
    print("Hello world!")

def world():
    print("World, Hello!")

running the code:

$ fab hello
Hello world!

Done.


$ fab world
World, hello!

Done.

What system am I running ?

fabfile.py

from fabric.api import run

def system_type():
    run('unname -s')

running the code:

$ fab -H localhost system_type
[localhost] Executing task 'system_type'
[localhost] run: uname -s
[localhost] Login password for 'gregory': 
[localhost] out: Linux
[localhost] out: 


Done.
Disconnecting from localhost... done.

Deployment example

fabfile.py

from fabric.api import *

DEFAULT_PATH = '/home/gregory/opencode'
DEPLOY_BRANCH = 'deploy'


def deploy(path=DEFAULT_PATH):
    add(path)
    commit(path)
    push(path)


def add(path=DEFAULT_PATH):
    with cd(path):
        local('git add -i')


def commit(path=DEFAULT_PATH):
    with cd(path):
        local('git commit -v')


def push(path=DEFAULT_PATH):
    with cd(path):
        local('git push origin master:%s' % DEPLOY_BRANCH)

Deployment example

running the code:

[localhost] local: git add -i
[localhost] local: git commit -v
[master 001e95a] add hello world
 1 file changed, 5 insertions(+)
 create mode 100644 hello/fabfile.py
[localhost] local: git push origin master:deploy
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 394 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To [email protected]:gelendir/opencode-fabric.git
   3cc8181..001e95a  master -> deploy

Done.

Error checking

fabfile.py

from fabric.api import run, settings, cd

DEPLOY_PATH = '/tmp/opencode-fabric'


def deploy():
    check_deployment()
    update()


def check_deployment():
    with settings(warn_only=True):
        command = run("test -d %s" % DEPLOY_PATH)
        if command.failed:
            run("git clone git://github.com/gelendir/opencode-fabric %s" % DEPLOY_PATH)


def update():
    with cd(DEPLOY_PATH):
        run("git pull")

Error checking

running the code:

$ fab -H localhost deploy
[localhost] Executing task 'deploy'
[localhost] run: test -d /tmp/opencode-fabric
[localhost] Login password for 'gregory': 

Warning: run() received nonzero return code 1 while executing 'test -d /tmp/opencode-fabric'!

[localhost] run: git clone git://github.com/gelendir/opencode-fabric /tmp/opencode-fabric
[localhost] out: Cloning into '/tmp/opencode-fabric'...
[localhost] out: remote: Counting objects: 10, done.
[localhost] out: remote: Compressing objects: 100% (6/6), done.
[localhost] out: remote: Total 10 (delta 0), reused 10 (delta 0)
[localhost] out: Receiving objects: 100% (10/10), done.
[localhost] out: 

[localhost] run: git pull
[localhost] out: Already up-to-date.
[localhost] out: 


Done.
Disconnecting from localhost... done.

Other nice features

  • console.confirm
  • with sudo()
  • parallel execution
  • roles
  • hosts

Real world examples


Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment