2010-12-16 13:54:09 -05:00
|
|
|
from fabric.api import abort, cd, env, get, hide, hosts, local, prompt
|
|
|
|
from fabric.api import put, require, roles, run, runs_once, settings, show, sudo, warn
|
|
|
|
from fabric.colors import red, green, blue, cyan, magenta, white, yellow
|
2010-12-15 22:26:05 -05:00
|
|
|
from boto.s3.connection import S3Connection
|
|
|
|
from boto.s3.key import Key
|
2010-12-16 13:54:09 -05:00
|
|
|
from fabric.contrib import django
|
|
|
|
import os, sys
|
|
|
|
|
|
|
|
django.settings_module('settings')
|
2010-12-15 22:26:05 -05:00
|
|
|
from django.conf import settings as django_settings
|
2010-07-30 23:50:49 -04:00
|
|
|
|
|
|
|
# =========
|
|
|
|
# = Roles =
|
|
|
|
# =========
|
|
|
|
|
2010-08-29 12:35:09 -04:00
|
|
|
env.user = 'conesus'
|
2010-12-16 13:54:09 -05:00
|
|
|
# env.hosts = ['www.newsblur.com', 'db01.newsblur.com', 'db02.newsblur.com', 'db03.newsblur.com']
|
2010-07-30 23:50:49 -04:00
|
|
|
env.roledefs ={
|
2010-12-16 13:54:09 -05:00
|
|
|
'app': ['www.newsblur.com'],
|
2010-08-29 12:35:09 -04:00
|
|
|
'db': ['db01.newsblur.com'],
|
2010-11-11 20:05:53 -05:00
|
|
|
'task': ['db02.newsblur.com', 'db03.newsblur.com'],
|
2010-07-30 23:50:49 -04:00
|
|
|
}
|
|
|
|
|
2010-09-08 18:30:33 -07:00
|
|
|
"""
|
|
|
|
Base configuration
|
|
|
|
"""
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2010-09-08 18:30:33 -07:00
|
|
|
"""
|
|
|
|
Environments
|
|
|
|
"""
|
2010-12-16 13:54:09 -05:00
|
|
|
def app():
|
|
|
|
env.roles = ['app']
|
|
|
|
def db():
|
|
|
|
env.roles = ['db']
|
|
|
|
def task():
|
|
|
|
env.roles = ['task']
|
2010-09-08 18:30:33 -07:00
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
# ==========
|
|
|
|
# = Deploy =
|
|
|
|
# ==========
|
|
|
|
|
|
|
|
@roles('app')
|
|
|
|
def deploy():
|
2010-12-16 14:15:22 -05:00
|
|
|
with cd('~/newsblur'):
|
|
|
|
run('git pull')
|
2011-02-05 19:39:30 -05:00
|
|
|
run('./utils/restart')
|
2010-12-16 13:54:09 -05:00
|
|
|
|
2011-01-15 18:43:09 -05:00
|
|
|
@roles('app')
|
|
|
|
def deploy_full():
|
|
|
|
with cd('~/newsblur'):
|
|
|
|
run('git pull')
|
|
|
|
run('./manage.py migrate')
|
2011-02-05 19:37:15 -05:00
|
|
|
run('sudo supervisorctl restart gunicorn')
|
2011-01-15 18:43:09 -05:00
|
|
|
|
2010-12-16 16:25:13 -05:00
|
|
|
@roles('app')
|
|
|
|
def staging():
|
|
|
|
with cd('~/staging'):
|
|
|
|
run('git pull')
|
2011-01-07 18:44:36 -05:00
|
|
|
run('kill -HUP `cat /var/run/gunicorn/gunicorn_staging.pid`')
|
2010-12-16 16:25:13 -05:00
|
|
|
|
2011-01-15 18:43:09 -05:00
|
|
|
@roles('app')
|
|
|
|
def staging_full():
|
|
|
|
with cd('~/staging'):
|
|
|
|
run('git pull')
|
|
|
|
run('./manage.py migrate')
|
|
|
|
run('kill -HUP `cat /var/run/gunicorn/gunicorn_staging.pid`')
|
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
@roles('task')
|
|
|
|
def celery():
|
2010-12-16 14:15:22 -05:00
|
|
|
with cd('~/newsblur'):
|
|
|
|
run('git pull')
|
|
|
|
run('sudo supervisorctl restart celery')
|
|
|
|
run('tail logs/newsblur.log')
|
2010-12-16 13:54:09 -05:00
|
|
|
|
|
|
|
@roles('task')
|
|
|
|
def force_celery():
|
2010-12-16 14:15:22 -05:00
|
|
|
with cd('~/newsblur'):
|
|
|
|
run('git pull')
|
|
|
|
run('ps aux | grep celeryd | egrep -v grep | awk \'{print $2}\' | sudo xargs kill -9')
|
|
|
|
# run('sudo supervisorctl start celery && tail logs/newsblur.log')
|
2010-12-16 13:54:09 -05:00
|
|
|
|
|
|
|
# ===========
|
|
|
|
# = Backups =
|
|
|
|
# ===========
|
|
|
|
|
|
|
|
@roles('app')
|
|
|
|
def backup_mongo():
|
2010-12-16 14:15:22 -05:00
|
|
|
with cd('~/newsblur/utils/backups'):
|
|
|
|
run('./mongo_backup.sh')
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
@roles('db')
|
|
|
|
def backup_postgresql():
|
2010-12-16 14:15:22 -05:00
|
|
|
with cd('~/newsblur/utils/backups'):
|
|
|
|
run('./postgresql_backup.sh')
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
# ======
|
|
|
|
# = S3 =
|
|
|
|
# ======
|
2010-09-08 18:30:33 -07:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
ACCESS_KEY = django_settings.S3_ACCESS_KEY
|
|
|
|
SECRET = django_settings.S3_SECRET
|
|
|
|
BUCKET_NAME = django_settings.S3_BACKUP_BUCKET # Note that you need to create this bucket first
|
2010-09-08 18:30:33 -07:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
def save_file_in_s3(filename):
|
|
|
|
conn = S3Connection(ACCESS_KEY, SECRET)
|
|
|
|
bucket = conn.get_bucket(BUCKET_NAME)
|
|
|
|
k = Key(bucket)
|
|
|
|
k.key = filename
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
k.set_contents_from_filename(filename)
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
def get_file_from_s3(filename):
|
|
|
|
conn = S3Connection(ACCESS_KEY, SECRET)
|
|
|
|
bucket = conn.get_bucket(BUCKET_NAME)
|
|
|
|
k = Key(bucket)
|
|
|
|
k.key = filename
|
2010-09-08 18:30:33 -07:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
k.get_contents_to_filename(filename)
|
2010-09-08 18:30:33 -07:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
def list_backup_in_s3():
|
|
|
|
conn = S3Connection(ACCESS_KEY, SECRET)
|
|
|
|
bucket = conn.get_bucket(BUCKET_NAME)
|
2010-09-08 18:30:33 -07:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
for i, key in enumerate(bucket.get_all_keys()):
|
|
|
|
print "[%s] %s" % (i, key.name)
|
2010-09-08 18:30:33 -07:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
def delete_all_backups():
|
|
|
|
#FIXME: validate filename exists
|
|
|
|
conn = S3Connection(ACCESS_KEY, SECRET)
|
|
|
|
bucket = conn.get_bucket(BUCKET_NAME)
|
2010-09-08 18:30:33 -07:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
for i, key in enumerate(bucket.get_all_keys()):
|
|
|
|
print "deleting %s" % (key.name)
|
|
|
|
key.delete()
|