Last active
October 1, 2015 08:27
-
-
Save yukihirai0505/5964130f78d6fc09414e to your computer and use it in GitHub Desktop.
fabricでデプロイ自動化
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# -*- coding:utf-8 -*- | |
from fabric.api import * | |
from fabric.decorators import runs_once | |
from datetime import datetime | |
env.hosts = ['ホスト名'] | |
env.user = 'ユーザー名' | |
env.key_filename = '鍵の場所' | |
env.project_name = 'プロジェクトネーム' | |
env.project_root = 'プロジェクトのルート' | |
env.build_path = "ビルドコマンドのパス" | |
env.branch = 'master' | |
env.archive = '' | |
env.server_dir = 'サーバー上のファイルパス' | |
@task | |
@runs_once | |
def build(): | |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
env.archive = "%(build)-%(timestamp)s.tar.gz" % {"build": env.project_name, "timestamp": timestamp} | |
with lcd(env.project_root): | |
local("tar cvfz %(archive)s [圧縮したいファイル]" % {"archive": env.archive}) | |
put("%(archive)s" % {"archive":env.archive}, "%(dir)s/" % {"dir" : env.server_dir}) | |
@task | |
def start(): | |
with cd(env.server_dir): | |
run("nohup %(bin)s &>> /dev/null 2>&1 &" % {"bin": env.build_path}) | |
@task | |
def stop(): | |
if run('ps -ef | grep "%(root)s" | grep -v grep | wc -l' % {"root": env.project_name}) == "1": | |
run("kill `cat %(root)s[走っているプロセスのIDを記録してあるパス]`" % {"root": env.server_dir}) | |
@task | |
@runs_once | |
def unpack(): | |
run("rm -Rf %(dir)s[削除したいファイル]" % {"dir" : env.server_dir}) | |
run("tar zxvf %(dir)s/%(archive)s -C %(dir)s" % {"dir" : env.server_dir, "archive": env.archive}) | |
@task | |
def restart(): | |
execute(stop) | |
execute(start) | |
@task | |
@runs_once | |
def deploy(branch=env.branch): | |
execute(build) | |
execute(stop) | |
execute(unpack) | |
execute(start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment