Getting Started with Fabric

JAWS-UG Kyoto #4

12th June 2013
by SOMEDA Takashi (id:tksmd / @tksmd)

Work

Cacoo AWS Stencil

AWS Simple Icons v2.1 released

Old version is still available, too.

Community

Fabric

  • fabfile.org
  • written in Python (Python 2.5 or higher)
  • Simple framework for
    • application deployment
    • systems administration tasks
  • Latest version is 1.6.1

Example

fabfile.py


from fabric.api import task,local

@task
def whoami():
    local("who am i")
					

Execute!


$ fab whoami
[localhost] local: who am i
someda   ttys000  Jun 11 23:30 

Done.
					

Feature

  • simple
  • small (few dependencies)
  • installation NOT required on server

Similar Tools

How we use

Application deployment/Continuous Delivery

Why we choose it

  • Easy to start
  • Easy migration from shell script
  • Easy integration with boto
  • We love Python.

Before to start

Are you familier with Python?

Amazon Linux AMI release 2013.03 premised

  • Yes →
  • No ↓

Install Python

Setup pyenv beforehand


$ cd
$ git clone git://github.com/yyuu/pyenv.git .pyenv
$ echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

reload shell and install Python


$ pyenv install 2.7.5
$ pyenv rehash
$ pyenv global 2.7.5
$ pyenv versions

Troubleshooting

Build tools


$ sudo yum install gcc make patch -y
$ sudo yum install subversion git -y

Prerequisites


$ sudo yum install readline-devel-y
$ sudo yum install sqlite-devel -y
$ sudo yum install zlib-devel bzip2-devel -y
$ sudo yum install openssl-devel -y

Install Fabric


$ pip install fabric
$ pyenv rehash
$ which fab

to upgrade

					
$ pip install --upgrade fabric					
$ fab -V
Fabric 1.6.1
Paramiko 1.10.1

Define Tasks

Create fabfile.py and add tasks


from fabric.api import task,local,run

@task
def localtime():
	local("date")

@task
def remotetime():
	run("date")

def private():
	print("private method")

use new style task to distinguish tasks and other private functions clearly

Execute Task

List your tasks on fabfile.py


$ fab -l
Available commands:

    localtime
    remotetime

Execute specified task by passing its name as argument to fab


$ fab localtime
[localhost] local: date
2013年 6月12日 水曜日 03時50分53秒 JST

Done.

Run Command

only 3 methods to run command

execute on remote host
run("uname -a")
execute on remote host with sudo
sudo("/etc/init.d/httpd graceful")
execute on local
local("ls -la")

Define remote hosts

command line agument


# globally
$ fab remotetime -H ec2-user@ec2-XX.ap-northeast-1.compute.amazonaws.com
# per task
$ fab remotetime:hosts="ec2-user@ec2-XX.ap-northeast-1.compute.amazonaws.com"

global definition on fabfile


from fabric.api import env

env.hosts = ['ec2-user@ec2-XX.ap-northeast-1.compute.amazonaws.com']

per task definition on fabfile


from fabric.api import task,run,hosts

@hosts('ec2-user@ec2-XX.ap-northeast-1.compute.amazonaws.com')
@task
def remotetime():
	run("date")

Simplify host definition

prepare ssh config


# saved as ssh.config
Host web01
  User ec2-user
  HostName ec2-XX.ap-northeast-1.compute.amazonaws.com						

Available options are limited compared to actual ssh, see details

enable ssh config loading


from fabric.api import task,local,run,hosts,env

env.use_ssh_config = True
env.ssh_config_path = 'ssh.config'

execute with shortened name


$ fab remotetime -H web01						

Work with boto

integrate with AWS API like

  • Start/Stop instances
  • Attach/Detach EBS
  • Create/Delete Snapshot

Demo

para para manga

Image movie (not implemented)

jawsug04-demo-img

Powered by Cacoo art

References

Thanks!