29
Fabric workshop (1) Soshi Nemoto Mulodo Vietnam Co., Ltd.

Fabric workshop(1) - (MOSG)

Embed Size (px)

Citation preview

Fabric workshop (1)

Soshi Nemoto Mulodo Vietnam Co., Ltd.

Our Purpose Make ourselves ‘Business partner’ from ‘factory’.

Points Engineering knowledge <- * TODAY * Logical Thinking Co-operation work

Resources Meetup: http://www.meetup.com/Open-Study-Group-Saigon Slideshare: http://www.slideshare.net/nemo-mulodo/

— Study Group —

Previous study DevOps: What's DevOps

http://www.slideshare.net/nemo-mulodo/devops1-whats-devops-mosg

DevOps: Vagrant http://www.slideshare.net/nemo-mulodo/devops2-vagrant-mosg

DevOps: Ansible http://www.slideshare.net/nemo-mulodo/devops3-ansible-mosg

DevOps: Ansible(2) http://www.slideshare.net/nemo-mulodo/devops4-ansible2-mosg

Preparation for workshop (tools) http://www.slideshare.net/nemo-mulodo/instruction-dev-environment

Feedback of previous study

Goal

Server Apps

Apache

PHP

MySQL

Product Apps

Source

Data

Cron

Virtual server

Vagrant+ ansible+ fabric

automation

Goal

Server Apps

Apache

PHP

MySQL

Product Apps

Source

Data

Cron

Virtual server

Vagrant+ ansible+ fabric

automation

Goal

Server Apps

Apache

PHP

MySQL

Product Apps

Source

Data

Cron

Virtual server

Vagrant+ ansible+ fabric

automation

Today: learn Fabric

Fabric workshop A radically simple IT automation engine

Share GOAL

create fabfile

AGENDA

GOAL

Create deploy tool - deploy - dev/staging/production - tag name (any ascii) - rollback - use any tag deployed - tag list - remove tag.

GOAL$ fab help Usage: <Deploy> $ fab -R [ROLE] deploy $ fab -R [ROLE] deploy:mode=[MODE],tag=[TAG],force=[True] deploy source to servers which are defined as ROLE Options: mode : 'production', 'staging', ‘development(default)' tag : any ASCII tag. (default = Epoch time + microtime) force: set 'True' to overwrite. (default = False)

<Show existed tags> $ fab -R [ROLE] tags show existed tags

<Change version> $ fab -R [ROLE] change:tag=XXXXXX change working version to another tagged source tag should be exactly same as shown by Tags command

<Remove specified tag> $ fab -R [ROLE] remove:tag=XXXXXX remove exist tags tag should be exactly same as shown by Tags command

GOAL

DEMO

GOAL

Create deploy tool - deploy - dev/staging/production - tag name (any ascii) - rollback - use any tag deployed - tag list - remove tag.

Share GOAL

create fabfile

AGENDA

local / print remote(use Ansible inventory) cd / file put / conditions

Install fabric (useing pip)

$ brew install python --framework : $ pip install fabric

Local task

$ cd {YOUR VAGRANT_DIR}

fabfile.py --- from fabric import api

def localtest(): print "local test" api.local('ls -al')

Local task (1)$ fab -R {ROLE} -f {FILE} {COMMAND}

$ fab localtest local test [localhost] local: ls -al total 44 drwxr-xr-x 10 nemo staff 340 Jan 14 15:05 . drwxr-xr-x+ 427 nemo staff 14518 Jan 14 14:52 .. drwxr-xr-x 3 nemo staff 102 Nov 19 20:10 .vagrant -rw-r--r-- 1 nemo staff 3337 Jan 13 20:58 Vagrantfile drwxr-xr-x 18 nemo staff 612 Jan 14 15:26 fabfile.py -rw-r--r-- 1 nemo staff 29 Jan 7 18:34 hosts drwxr-xr-x 6 nemo staff 204 Dec 10 18:47 playbooks -rw-r--r-- 1 nemo staff 133 Dec 10 17:23 setup.yml

Done. air:nemo@~/TEST_STUDY$

$ fab localtest

Share GOAL

create fabfile

AGENDA

local / print remote(use Ansible inventory) cd / file put / conditions

remote taskfabfile.py --- from fabric import api

def remotetest(): print "remote test" api.run('ls -al’) api.sudo('ls -al’)

Remote task (2)

$ fab remotetest No hosts found. Please specify (single) host string for connection: {IP_ADDRESS}

[192.168.33.50] run: ls -al [192.168.33.50] out: total 44 [192.168.33.50] out: drwx------. 6 vagrant vagrant 4096 Jan 14 05:58 . : [192.168.33.50] out: drwxrwxr-x 3 vagrant vagrant 4096 Jan 13 11:47 src [192.168.33.50] out:

total 44

$ fab remotetest

target host required...

$ fab -H {IP_ADDRESS} remotetest

Remote task (trouble?)~/.ssh/config --- Host {IP_ADDRESS_OF_YOUR_VAGRANT_MACHINE} User vagrant TCPKeepAlive yes IdentityFile {PRIVATE_KEY_OF_YOUR_VAGRANT_MACHINE} IdentitiesOnly yes ControlPersist 2h

Where is the private key??$ vagrant ssh-config

api.env.use_ssh_config = Trueset option in your fabfile.py

Using ROLE instead of -H$ fab -R httpd-server remotetest

: from fabric.api import env from fabric.decorators import roles

env.user = user env.roledefs.update({ ‘httpd-server': [‘{IP_ADDRESS}’, ‘{IP_ADDRESS}’], 'toolservers': [‘{IP_ADDRESS}’], })

@roles(‘httpd-server') def YOUR_COMMAND: pass

Using ROLE (2)

But...

We want to use Ansible inventory (hosts) file.

* Target host definition should be only one. * The definition of targets in Fabric is in code,

so we should use Ansible’s inventory file.

Using Ansible inventoryfabfile.py --- from ansible import inventory from fabric.api import env, run, local from fabric import api

inventory = inventory.Inventory('./hosts') env.roledefs = inventory.groups_list() env.use_ssh_config = True

def remotetest(): run('ls -al')

hosts --- [httpd-server] 192.168.33.50

see) Ansible(1-3)

Using Ansible inventory

$ fab -R httpd-server remotetest

OK!!

Ansible inventory (Trouble?)

ex) /usr/local/Cellar/ansible/1.9.4/libexec/lib/python2.7/site-packages /usr/local/Cellar/ansible/1.9.4/libexec/vendor/lib/python2.7/site-packages

fabfile.py --- import sys sys.path.append({YOUR_ANSIBLE_PATH})

If you couldn’t load Ansible libraries, please set your ansible library path.

Share GOAL

create fabfile

AGENDA

local / print remote(use Ansible inventory) cd / file put / conditions

mkdir and cdfabfile.py --- from ansible import inventory from fabric.api import env, run, local, cd from fabric import api

inventory = inventory.Inventory('./hosts') env.roledefs = inventory.groups_list() env.use_ssh_config = True

def mkdir_and_cd(): run(‘mkdir -p TMP’) with cd(‘TMP’): run(‘pwd’)

$ fab -R httpd-server mkdir_and_cd

mkdir and cd (2)fabfile.py --- : from fabric.contrib import files : : def mkdir_and_cd():

if not files.exists(‘TMP’): run(‘mkdir -p TMP’) with cd(‘TMP’): run(‘ls -al’)

$ fab -R httpd-server mkdir_and_cd

mkdir, cd and putfabfile.py --- : from fabric.api import env, run, local, cd, put from fabric.contrib import files : : def mkdir_cd_and_put():

if not files.exists(‘TMP’): run(‘mkdir -p TMP’) with cd(‘TMP’): if not files.exists({DEST_FILE}): put({SRC_FILE}, {DEST_FILE})

$ fab -R httpd-server mkdir_cd_and_put