More Related Content
Similar to 2日間Fabricを触った俺が
色々解説してみる
Similar to 2日間Fabricを触った俺が
色々解説してみる (20)
2日間Fabricを触った俺が
色々解説してみる
- 5. #!/usr/bin/env
python
#
-‐*-‐
coding:
utf-‐8
-‐*-‐
from
fabric.api
import
env,
local,
run
import
difflib
env.hosts
=
[
‘mydevelopment.jp'
]
env.key_filename
=
[
'~/.ssh/mydevelopment.rsa'
]
env.user
=
'airtoxin'
env.port
=
9724
def
get_date():
remote_date
=
run(
'date'
)
local_date
=
local(
'date',
capture=True
)
for
line
in
difflib.ndiff(
[
remote_date
],
[
local_date
]
):
print
line
Example of Fabric
fabfile.py
サーバー実行コマンド
タスク名
ローカル実行コマンド
Pythonで実行結果を処理
- 8. コマンド
• リモートでコマンドを実行 → run(‘command’)
• ローカルでコマンドを実行 → local(‘command’)
• sudoでコマンドを実行 → sudo(‘command’)
• ディレクトリの移動 → with cd(‘dirname’):
• ファイルアップロード → put(‘localpath’, ‘remotepath’)
• ファイルダウンロード → get(‘remotepath’, ‘localpath’)
- 16. Shell vs Fabric
Shell Fabric
導入コスト
-
プリインストール
低∼中程度
Pythonのpipによる
可読性
低い
文法, ワンライナー
高い
Pythonのコード
文字列操作
難∼普通程度
perl, awk等を利用
楽
ライブラリ等を利用
学習コスト
無し∼高い
普段使い/初心者
低い∼普通
リスト内包表記, decorator
情報量
多い
言わずもがな
中
ほぼ枯れている
- 17. Shell vs Fabric
Shell Fabric
導入コスト
-
プリインストール
低∼中程度
Pythonのpipによる
可読性
低い
文法, ワンライナー
高い
Pythonのコード
文字列操作
難∼普通程度
perl, awk等を利用
楽
ライブラリ等を利用
学習コスト
無し∼高い
普段使い/初心者
低い∼普通
リスト内包表記, decorator
情報量
多い
言わずもがな
中
ほぼ枯れている
- 26. Example of Fabric
fabfile.py
サーバー実行コマンド
タスク名
ローカル実行コマンド
Pythonで実行結果を処理
#!/usr/bin/env
python
#
-‐*-‐
coding:
utf-‐8
-‐*-‐
from
fabric.api
import
env,
local,
run
import
difflib
env.hosts
=
[
‘mydevelopment.jp'
]
env.key_filename
=
[
'~/.ssh/mydevelopment.rsa'
]
env.user
=
'airtoxin'
env.port
=
9724
def
get_date():
remote_date
=
run(
'date'
)
local_date
=
local(
'date',
capture=True
)
for
line
in
difflib.ndiff(
[
remote_date
],
[
local_date
]
):
print
line
- 29. ホストの設定のみ
hosts.py
.
├── .gitignore
└── fabfile
├── __init__.py
├── deploy.py
├── hosts.py
├── monitor.py
└── test.py
@task
def staging():
env.hosts = [
'mystaging.jp'
]
@task
def production():
env.hosts = [
'production.com',
'myhost.jp',
'myapp.jp'
]
- 30. 実行タスク
.
├── .gitignore
└── fabfile
├── __init__.py
├── deploy.py
├── hosts.py
├── monitor.py
└── test.py
deploy.py
@task( default=True )
def deploy():
with cd( '/path/to/myapp' ):
run( 'git checkout .' )
run( 'git pull' )
run( 'npm install' )
run( 'npm run compile' )
run( 'forever restart app.js' )
@task
def tagging():
hostname = local( 'hostname' )
run( 'git tag {host}-
{datetime}'.format( host=hostname,
datetime=now ) )
run( 'git push --tags' )
- 31. .
├── .gitignore
└── fabfile
├── __init__.py
├── deploy.py
├── hosts.py
├── monitor.py
└── test.py
__init__.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hosts
import deploy
import monitor
import test
- 32. Task List
fu:fabric_test$ fab -l
Available commands:
deploy
deploy.deploy
deploy.tagging
hosts.production
hosts.staging
monitor
monitor.all
monitor.disc_capacity
monitor.load_average
@task( default=True )
def deploy():
- 33. 実行コマンド
$ fab [task1] [task2] [task3]…
$ fab hosts.staging deploy
stagingサーバー群に対してdeployコマンド
が実行される
hostを直接指定するなら
$ fab -H myhost [tasks…]
- 35. 直列タスク
from fabric.decorators
import task, parallel
@task
def deploy():
print env.host
fu:fabric_test$ fab hosts.production deploy
[production.com] Executing task 'deploy'
production.com
[myhost.jp] Executing task 'deploy'
myhost.jp
[myapp.jp] Executing task 'deploy'
myapp.jp
Done.
host毎に直列実行
されている
- 36. 並列タスク
from fabric.decorators
import task, parallel
@task
@parallel
def deploy():
print env.host
fu:fabric_test$ fab hosts.production deploy
[production.com] Executing task 'deploy'
[myhost.jp] Executing task 'deploy'
[myapp.jp] Executing task 'deploy'
myapp.jp
production.com
myhost.jp
Done.
これだけ
並列実行されている