Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fabric for fun and profit
Search
Jair Trejo
September 14, 2013
Programming
4
940
Fabric for fun and profit
An introduction to Fabric presented at PyCon APAC 2013
Jair Trejo
September 14, 2013
Tweet
Share
More Decks by Jair Trejo
See All by Jair Trejo
Storybook & React Native
jairtrejo
0
670
Non Sequitur: An exploration of Python's random module
jairtrejo
0
740
Other Decks in Programming
See All in Programming
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
0
220
最近のVS Codeで気になるニュース 2025/01
74th
1
240
ISUCON14感想戦で85万点まで頑張ってみた
ponyo877
1
790
ErdMap: Thinking about a map for Rails applications
makicamel
1
1.1k
SRE、開発、QAが協業して挑んだリリースプロセス改革@SRE Kaigi 2025
nealle
1
3.3k
ecspresso, ecschedule, lambroll を PipeCDプラグインとして動かしてみた (プロトタイプ) / Running ecspresso, ecschedule, and lambroll as PipeCD Plugins (prototype)
tkikuc
2
2.3k
Kanzawa.rbのLT大会を支える技術の裏側を変更する Ruby on Rails + Litestream 編
muryoimpl
0
120
Compose でデザインと実装の差異を減らすための取り組み
oidy
1
240
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
3.7k
functionalなアプローチで動的要素を排除する
ryopeko
1
940
AWS Lambda functions with C# 用の Dev Container Template を作ってみた件
mappie_kochi
0
220
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2k
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
GitHub's CSS Performance
jonrohan
1030
460k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Site-Speed That Sticks
csswizardry
3
310
Designing Experiences People Love
moore
139
23k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Automating Front-end Workflow
addyosmani
1367
200k
Transcript
Fabric for fun and profit @ jairtrejo Pycon APAC 2013
@jairtrejo Operations Director at Vinco Orbis
We script everything
We ssh in and out servers all the time
Fabric is a Python tool for streamlining the use of
SSH for systems administration
@hosts(staging, production) @task def publish(): local('./build') project.rsync_project( remote_dir=DEST_PATH, local_dir=DEPLOY_PATH.rstrip('/') +
'/', delete=True ) run(‘./bin/restart-apache’)
Quick fabric introduction
$ pip install fabric
Tasks
# fabfile.py from fabric.api import task, local @task def hello():
local('echo "Hello, world"')
$ fab hello Hello, world
multi-host
# fabfile.py from fabric.api import run, task @task def host_type():
run('uname -s')
$ fab -H localhost,
[email protected]
host_type Darwin Linux
Environment dictionary
# fabfile.py from fabric.api import run, task, env @task def
development(): env.hosts = [ 'fake.jairtrejo.mx', 'demo.jairtrejo.mx'] @task def production(): env.hosts = ['jairtrejo.mx'] @task def host_type(): run('uname -s')
$ fab production host_type
Case study: Gift cards
Huge files
Download link
Random URLs
Environment configuration
@task def production(): """ Production server """ env.user = 'pdfs'
env.hosts = ['example.com'] # Pdf’s storage env.pdf_base_dir = \ '/var/www/vhosts/example.com/httpdocs/PDFS/'
It is just Python
@task def find_and_rename(listing, source, destination): pdfs = [] with open(listing,
'rb') as csvfile: reader = csv.DictReader(csvfile) for row in reader: name = ‘%s.pdf’ % row[“Client number”] if os.path.exists(os.path.join(source, name)): pdfs.append(name) else: logging.warning("Not found: %s" % name) import logging logging.basicConfig( filename='example.log', level=logging.DEBUG, format="%(asctime)s %(levelname)s: %(message)s", )
We can mix Python with system calls
rows = [] for original in pdfs: fname, ext =
os.path.splitext(original) hashed, salt = hash_name(fname) hashed += ext hfname = os.path.join(destination, hashed) local( 'cp "%s" "%s"' % (os.path.join(source, original), hfname) ) rows.append( {'original': original, 'salt': salt, 'archivo': hfname}) rows.sort(key=lambda row: row['original'])
Case study Vagrant development
SSH integration
@task def vagrant(): """ Local dev environment (local Vagrant machine)
""" env.user = "vagrant" env.hosts = ["127.0.0.1:2222"] # Vagrant machine's ssh key result = local( "vagrant ssh-config | grep IdentityFile", capture=True) env.key_filename = result.split()[1] # Project directory env.site_dir = "~/src" Via sysadminpy.com
@task def syncdb(): require("site_dir") with cd(env.site_dir): run("python manage.py syncdb --noinput")
run("python manage.py migrate myapp --noinput")
$ fab vagrant syncdb
$ fab vagrant -- sudo service couchdb restart
Task composition
@task def resetdb(): require("site_dir") run('mysql -u root -pf5dd8Xo -e "DROP
DATABASE mydb;" mydb') run('mysql -u root -pf5dd8Xo -e "CREATE DATABASE mydb;"') syncdb()
$ fab vagrant resetdb
Silly stuff
if result.failed: print red("Some tests failed", bold=True) bomb = r"""
,--.!, __/ -*- ,d08b. '|` 0088MM `9MMP' """ print red(bomb) else: print green("All tests passed", bold=True) owl = r""" ,___, [O.o] /)__) -"--"- """ print green(owl)
Fabric alternatives
Ansible Server configuration
Invoke Build tool
Image credits (1)http://www.flickr.com/photos/worldbank/5320646943/ (2)http://blog.trendmicro.com/trendlabs-security-intelligence/mehika-twitter-botnet- targets-twitter-users/ (3)http://junofitness.blogspot.jp/2012/11/jennifer-and-julie.html (4)http://favim.com/image/361336/ (5)http://projectwhy.blogspot.jp/2013/04/a-wonderful-gift-may-not-be-wrapped-as.html (6)http://www.marysrosaries.com/collaboration/index.php?title=File:Forklift_(PSF).png (7)http://commons.wikimedia.org/wiki/File:US_Navy_070207-
N-4973A-044_Sailors_aboard_guided_missile_destroyer_USS_O'Kane_(DDG_77)_unload_foo d_stores_and_refrigerated_items_during_a_connected_replenishment_with_Military_Sea lift_Command_(MSC)_combat_stores_ship_USNS_Conco.jpg (8)http://www.casinocenter.com/helping-luck-along/ (9)http://python.sg/ (10)http://bailtruth.com/2012/09/12/steampunk-gears-and-cogwheels-brooch-2/ (11)http://www.giglig.com/food/how-to-make-mojito (12)http://cnx.org/content/m42117/latest/?collection=col11325/latest (13)http://mother-care.net/leisure/hobby/fugu-fish-deadly-delight.html (14)http://www.tumblr.com/tagged/tinker-toys (15)http://www.deviantart.com/?view_mode=2&order=24&q=by%3Ajam-star (16)http://www.vivianpartnership.co.uk/choices-choices-choices/ (17)http://faculty.indwes.edu/bcupp/lookback/hist-08.htm (18)http://www.thebedroom.com.au/manchester/character-bedding/bob-the-builder.html
Thank you! http://vincoorbis.com http://jairtrejo.mx