Fabric Quick Tutorial 2014
enviroments
install pyenv
% brew install pyenv % echo 'if which pyenv >> /dev/null; then eval "$(pyenv init -)"; fi' % exec $SHELL -l
install python
% pyenv install 2.7.2 % pyenv global 2.7.2 % python --version [~] Python 2.7.2
install fabric
% pip install fabric % pip freeze | grep Fabric [~] Fabric==1.8.2
prepare
create practice directory.
% mkdir ~/testFabric % cd ~/testFabric
create local action
edit: fabfile.py
from fabric.api import env, lcd, local
def local_action():
""" local action """
with lcd("/tmp/"):
local("touch local.txt")
listing available commands.
% cd ~/testFabric
% fab -l [~/testFabric]
Available commands:
local_action local action
do it.
% fab local_action [~/testFabric] [localhost] local: touch local.txt Done.
see result.
% ls /tmp/local.txt [~/testFabric] /tmp/local.txt
remote action
edit: fabfile.py
from fabric.api import env, lcd, local, cd, run
from fabric.decorators import roles
env.roledefs = {
'www': ['centos.vm']
}
env.use_ssh_config = True
env.key_filename = '~/.vagrant.d/insecure_private_key'
def local_action():
""" local action """
with lcd("/tmp/"):
local("touch local.txt")
@roles("www")
def remote_action():
""" remote action """
with cd("/tmp/"):
run("touch remote.txt")
do it.
% fab remote_action
see result.
% ssh centos.vm ls /tmp/remote.txt [~/testFabric] /tmp/remote.txt
rsync action
edit: fabfile.py
from fabric.api import env, lcd, local, cd, run
from fabric.decorators import roles
from fabric.contrib.project import rsync_project
env.roledefs = {
'www': ['centos.vm']
}
env.use_ssh_config = True
env.key_filename = '~/.vagrant.d/insecure_private_key'
def local_action():
""" local action """
with lcd("/tmp/"):
local("touch local.txt")
@roles("www")
def remote_action():
""" remote action """
with cd("/tmp/"):
run("touch remote.txt")
@roles("www")
def rsync_action():
""" rsync action """
local("mkdir -p /tmp/sync")
with lcd("/tmp/sync"):
local("touch test1.txt test2.txt test3.txt exclude.txt")
run("mkdir -p /tmp/sync")
with cd("/tmp/sync/"):
run("touch dummy.txt")
rsync_project(remote_dir="/tmp", local_dir="/tmp/sync", exclude="exclude.txt", delete=True, extra_opts="", ssh_opts="-o StrictHostKeyChecking=no")
do it and see result.
% fab rsync_action % ssh centos.vm ls /tmp/sync [~/testFabric] test1.txt test2.txt test3.txt
enjoy!