Created
August 16, 2012 00:10
-
-
Save brianr/3364913 to your computer and use it in GitHub Desktop.
Simple deploy script with 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
import sys | |
from fabric.api import run, local, cd, env, roles, execute | |
import requests | |
env.roledefs = { | |
'web': ['web1', 'web2'] | |
} | |
def deploy(): | |
# pre-roll checks | |
check_user() | |
# do the roll. | |
# execute() will call the passed-in function, honoring host/role decorators. | |
execute(update_and_restart) | |
# post-roll tasks | |
ratchet_record_deploy() | |
@roles('web') | |
def update_and_restart(): | |
code_dir = '/home/deploy/www/mox' | |
with cd(code_dir): | |
run("git pull") | |
run("pip install -r requirements.txt") | |
run("supervisorctl restart web1") | |
run("supervisorctl restart web2") | |
def check_user(): | |
if local('whoami', capture=True) != 'deploy': | |
print "This command should be run as deploy. Run like: sudo -u deploy fab deploy" | |
sys.exit(1) | |
def ratchet_record_deploy(): | |
# read access_token from production.ini | |
access_token = local("grep 'ratchet.access_token' production.ini | sed 's/^.* = //g'", capture=True) | |
environment = 'production' | |
local_username = local('whoami', capture=True) | |
revision = local('git log -n 1 --pretty=format:"%H"', capture=True) | |
resp = requests.post('https://submit.ratchet.io/api/1/deploy/', { | |
'access_token': access_token, | |
'environment': environment, | |
'local_username': local_username, | |
'revision': revision | |
}, timeout=3) | |
if resp.status_code == 200: | |
print "Deploy recorded successfully. Deploy id:", resp.json['data']['deploy_id'] | |
else: | |
print "Error recording deploy:", resp.text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment