やりたかったこと
コマンド一発で複数台サーバにGitデプロイ
参考URL
http://fabric-ja.readthedocs.io/ja/latest/tutorial.html
このページ読めば大体理解できます
インストール
sudo yum install fabric
# 確認
fab --version
python --version
fabfile.py作成
cd project/path
vim fabfile.py
fabfile.py
# coding: utf-8
from __future__ import with_statement
from fabric.api import run, env, abort, cd
from fabric.contrib.console import confirm
# デプロイ先のサーバ設定(この場合は2か所にデプロイ)
env.hosts = [
'hoge_username@hoge.com',
'fuga_username@fuga.com'
]
env.passwords = {
'hoge_username@hoge.com:22': 'hoge_password',
'fuga_username@fuga.com:22': 'fuga_password'
}
# env.hostsの数だけ実行される
def deploy():
hostname = run('hostname')
# 確認
if not confirm(hostname + " <- このサーバにデプロイします、よろしいですか?"):
abort("中止しました")
# デプロイ
project_path = '/project/path/html'
with cd(project_path):
run("git pull origin master")
Point
-
run('any command')
で外部サーバ上でコマンドが実行できる -
cd('path/to')
で外部サーバ上でディレクトリ移動ができる
デプロイ実行
fab deploy
fabコマンドはカレントディレクトリのfabfile.py
を処理対象とします