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'],
|
2011-02-11 19:05:16 -05:00
|
|
|
'task': ['db02.newsblur.com', 'app02.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:40:07 -05:00
|
|
|
run('kill -HUP `cat logs/gunicorn.pid`')
|
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
|
|
|
|
2011-02-08 22:07:59 -05:00
|
|
|
# =============
|
|
|
|
# = Bootstrap =
|
|
|
|
# =============
|
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
def setup_common():
|
|
|
|
setup_installs()
|
|
|
|
setup_user()
|
|
|
|
setup_server()
|
|
|
|
setup_repo()
|
|
|
|
setup_libxml()
|
|
|
|
setup_python()
|
|
|
|
setup_mongoengine()
|
|
|
|
setup_supervisor()
|
|
|
|
|
2011-02-09 15:45:41 -05:00
|
|
|
def setup_app():
|
|
|
|
setup_common()
|
|
|
|
setup_gunicorn()
|
|
|
|
update_gunicorn()
|
|
|
|
setup_nginx()
|
|
|
|
|
|
|
|
def setup_db():
|
|
|
|
setup_postgres()
|
2011-03-14 21:44:30 -04:00
|
|
|
setup_mongo()
|
|
|
|
|
2011-02-09 15:45:41 -05:00
|
|
|
def setup_task():
|
|
|
|
setup_celery()
|
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
# ==================
|
|
|
|
# = Setup - Common =
|
|
|
|
# ==================
|
2011-02-08 22:07:59 -05:00
|
|
|
|
|
|
|
def setup_installs():
|
2011-02-09 15:45:41 -05:00
|
|
|
sudo('apt-get -y update')
|
|
|
|
sudo('apt-get -y upgrade')
|
2011-03-14 21:44:30 -04:00
|
|
|
sudo('apt-get -y install build-essential gcc scons sysstat iotop git zsh python-dev locate python-software-properties libpcre3-dev libssl-dev make pgbouncer python-psycopg2 libmemcache0 memcached python-memcache libyaml-0-2 python-yaml python-numpy python-scipy python-imaging munin munin-node munin-plugins-extra curl')
|
2011-02-09 15:45:41 -05:00
|
|
|
sudo('add-apt-repository ppa:pitti/postgresql')
|
|
|
|
sudo('apt-get -y update')
|
|
|
|
sudo('apt-get -y install postgresql-client-9.0')
|
2011-03-14 21:44:30 -04:00
|
|
|
sudo('mkdir -p /var/run/postgresql')
|
|
|
|
sudo('chown postgres.postgres /var/run/postgresql')
|
2011-02-09 15:45:41 -05:00
|
|
|
run('git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh')
|
|
|
|
run('curl -O http://peak.telecommunity.com/dist/ez_setup.py')
|
|
|
|
sudo('python ez_setup.py -U setuptools && rm ez_setup.py')
|
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
def setup_user():
|
|
|
|
# run('useradd -c "NewsBlur" -m conesus -s /bin/zsh')
|
|
|
|
# run('openssl rand -base64 8 | tee -a ~conesus/.password | passwd -stdin conesus')
|
|
|
|
run('mkdir -p ~/.ssh && chmod 700 ~/.ssh')
|
|
|
|
run('ssh-keygen -t dsa -f ~/.ssh/id_dsa -N ""')
|
|
|
|
put("~/.ssh/id_dsa.pub", "~/.ssh/authorized_keys")
|
|
|
|
|
|
|
|
def setup_server():
|
|
|
|
sudo('hostname app02')
|
|
|
|
|
2011-02-09 15:45:41 -05:00
|
|
|
def setup_repo():
|
|
|
|
run('mkdir -p ~/code')
|
|
|
|
run('git clone https://github.com/samuelclay/NewsBlur.git newsblur')
|
|
|
|
with cd('~/newsblur'):
|
|
|
|
run('cp local_settings.py.template local_settings.py')
|
|
|
|
run('mkdir -p logs')
|
|
|
|
|
|
|
|
def setup_libxml():
|
|
|
|
sudo('apt-get -y install libxml2-dev libxslt1-dev python-lxml')
|
|
|
|
# with cd('~/code'):
|
|
|
|
# run('git clone git://git.gnome.org/libxml2')
|
|
|
|
# run('git clone git://git.gnome.org/libxslt')
|
|
|
|
#
|
|
|
|
# with cd('~/code/libxml2'):
|
|
|
|
# run('./configure && make && sudo make install')
|
|
|
|
#
|
|
|
|
# with cd('~/code/libxslt'):
|
|
|
|
# run('./configure && make && sudo make install')
|
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
def setup_python():
|
|
|
|
sudo('easy_install pip')
|
|
|
|
sudo('easy_install fabric django django-celery django-compress South django-devserver django-extensions guppy psycopg2 pymongo BeautifulSoup pyyaml nltk lxml oauth2 pytz boto')
|
|
|
|
sudo('su -c \'echo "import sys; sys.setdefaultencoding(\"utf-8\")" > /usr/lib/python2.6/sitecustomize.py\'')
|
|
|
|
|
|
|
|
def setup_mongoengine():
|
|
|
|
with cd('~/code'):
|
|
|
|
run('git clone -f https://github.com/hmarr/mongoengine.git')
|
|
|
|
sudo('ln -s ~/code/mongoengine/mongoengine /usr/local/lib/python2.6/dist-packages/mongoengine')
|
|
|
|
|
|
|
|
def setup_supervisor():
|
|
|
|
sudo('apt-get -y install supervisor')
|
|
|
|
|
|
|
|
# ===============
|
|
|
|
# = Setup - App =
|
|
|
|
# ===============
|
|
|
|
|
|
|
|
def setup_app_installs():
|
|
|
|
sudo('apt-get install -y mongodb-clients')
|
|
|
|
|
2011-02-09 15:45:41 -05:00
|
|
|
def setup_gunicorn():
|
|
|
|
with cd('~/code'):
|
2011-03-14 21:44:30 -04:00
|
|
|
run('git clone -f git://github.com/benoitc/gunicorn.git')
|
2011-02-09 15:45:41 -05:00
|
|
|
sudo('ln -s ~/code/gunicorn/gunicorn /usr/local/lib/python2.6/dist-packages/gunicorn')
|
|
|
|
|
|
|
|
def update_gunicorn():
|
|
|
|
with cd('~/code/gunicorn'):
|
|
|
|
run('git pull')
|
|
|
|
sudo('python setup.py install')
|
|
|
|
|
|
|
|
def setup_nginx():
|
|
|
|
with cd('~/code'):
|
2011-03-14 21:44:30 -04:00
|
|
|
sudo("groupadd nginx")
|
|
|
|
sudo("useradd -g nginx -d /var/www/htdocs -s /bin/false nginx")
|
|
|
|
run('wget http://sysoev.ru/nginx/nginx-0.9.5.tar.gz')
|
|
|
|
run('tar -xzf nginx-0.9.5.tar.gz')
|
|
|
|
run('rm nginx-0.9.5.tar.gz')
|
|
|
|
with cd('~/code/nginx-0.9.5'):
|
2011-02-09 15:45:41 -05:00
|
|
|
run('./configure --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module')
|
|
|
|
run('make')
|
2011-03-14 21:44:30 -04:00
|
|
|
run('sudo make install')
|
|
|
|
put("config/nginx.conf", "/usr/local/nginx/conf/nginx.conf", use_sudo=True)
|
|
|
|
sudo("mkdir -p /usr/local/nginx/conf/sites-enabled")
|
|
|
|
put("config/newsblur.conf", "/usr/local/nginx/conf/site-enabled/newsblur.conf", use_sudo=True)
|
|
|
|
put("config/nginx-init", "/etc/init.d/nginx", mode=0755, use_sudo=True)
|
|
|
|
sudo("/usr/sbin/update-rc.d -f nginx defaults")
|
2011-02-09 15:45:41 -05:00
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
|
|
|
|
# ==============
|
|
|
|
# = Setup - DB =
|
|
|
|
# ==============
|
|
|
|
|
|
|
|
def setup_postgres():
|
|
|
|
sudo('apt-get -y install postgresql-9.0 postgresql-client-9.0 postgresql-contrib-9.0 libpq-dev')
|
2011-02-09 15:45:41 -05:00
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
def setup_mongo():
|
|
|
|
sudo('sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10')
|
|
|
|
sudo('echo "deb http://downloads.mongodb.org/distros/ubuntu 10.10 10gen" >> /etc/apt/sources.list.d/10gen.list')
|
|
|
|
sudo('apt-get -y install mongodb')
|
|
|
|
|
|
|
|
# ================
|
|
|
|
# = Setup - Task =
|
|
|
|
# ================
|
|
|
|
|
|
|
|
def setup_celery():
|
|
|
|
pass
|
2011-02-08 22:07:59 -05: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()
|