2012-07-30 13:25:44 -07:00
|
|
|
from fabric.api import cd, env, local, parallel, serial
|
2012-07-09 15:49:57 -07:00
|
|
|
from fabric.api import put, run, settings, sudo
|
|
|
|
# from fabric.colors import red, green, blue, cyan, magenta, white, yellow
|
2012-10-15 16:32:32 -07:00
|
|
|
from boto.s3.connection import S3Connection
|
|
|
|
from boto.s3.key import Key
|
|
|
|
from boto.ec2.connection import EC2Connection
|
2010-12-16 13:54:09 -05:00
|
|
|
from fabric.contrib import django
|
2012-07-09 15:49:57 -07:00
|
|
|
import os
|
2012-10-15 16:32:32 -07:00
|
|
|
import time
|
|
|
|
import sys
|
2013-01-07 11:33:24 -08:00
|
|
|
import re
|
2013-03-18 09:51:20 +00:00
|
|
|
try:
|
|
|
|
import dop.client
|
|
|
|
except ImportError:
|
|
|
|
print "Digital Ocean's API not loaded"
|
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
|
|
|
|
django.settings_module('settings')
|
2011-07-17 11:31:07 -07:00
|
|
|
try:
|
|
|
|
from django.conf import settings as django_settings
|
|
|
|
except ImportError:
|
|
|
|
print " ---> Django not installed yet."
|
2011-07-17 15:30:25 -07:00
|
|
|
django_settings = None
|
2011-07-17 11:31:07 -07:00
|
|
|
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2011-07-17 15:52:06 -07:00
|
|
|
# ============
|
|
|
|
# = DEFAULTS =
|
|
|
|
# ============
|
|
|
|
|
2011-07-17 15:56:28 -07:00
|
|
|
env.NEWSBLUR_PATH = "~/projects/newsblur"
|
|
|
|
env.VENDOR_PATH = "~/projects/code"
|
2011-07-17 15:52:06 -07:00
|
|
|
|
2010-07-30 23:50:49 -04:00
|
|
|
# =========
|
|
|
|
# = Roles =
|
|
|
|
# =========
|
|
|
|
|
2011-03-21 20:07:24 -04:00
|
|
|
env.user = 'sclay'
|
2010-07-30 23:50:49 -04:00
|
|
|
env.roledefs ={
|
2011-07-17 11:31:07 -07:00
|
|
|
'local': ['localhost'],
|
2012-03-29 13:57:38 -07:00
|
|
|
'app': ['app01.newsblur.com',
|
2012-05-21 09:40:44 -07:00
|
|
|
'app02.newsblur.com',
|
2013-02-24 17:20:11 -08:00
|
|
|
'app03.newsblur.com',
|
2012-10-01 13:07:45 -07:00
|
|
|
'app04.newsblur.com',
|
2013-03-20 22:00:03 -07:00
|
|
|
'198.211.109.197',
|
2013-03-14 19:26:22 -07:00
|
|
|
'198.211.110.131',
|
2013-03-20 22:00:03 -07:00
|
|
|
'198.211.110.230',
|
2013-03-14 19:26:22 -07:00
|
|
|
'192.34.61.227',
|
|
|
|
'198.211.109.155',
|
2013-03-15 11:11:47 -07:00
|
|
|
'198.211.107.87',
|
2013-03-15 12:11:29 -07:00
|
|
|
'198.211.105.155',
|
|
|
|
'198.211.104.133',
|
2013-03-15 12:35:10 -07:00
|
|
|
'198.211.103.214',
|
|
|
|
'198.211.106.22',
|
2013-03-15 17:05:44 -07:00
|
|
|
'198.211.110.189',
|
|
|
|
'198.211.106.215',
|
|
|
|
'192.81.209.42',
|
|
|
|
'198.211.102.245',
|
2013-03-18 11:32:24 -07:00
|
|
|
'198.211.109.236',
|
|
|
|
'198.211.113.54',
|
|
|
|
'198.211.113.206',
|
|
|
|
'198.211.113.86',
|
|
|
|
'198.211.113.196',
|
2012-10-01 13:07:45 -07:00
|
|
|
],
|
2012-05-02 19:00:17 -07:00
|
|
|
'dev': ['dev.newsblur.com'],
|
2013-03-17 14:06:17 -07:00
|
|
|
'debug': ['debug.newsblur.com'],
|
2012-07-31 15:53:16 -07:00
|
|
|
'web': ['app01.newsblur.com',
|
|
|
|
'app02.newsblur.com',
|
2012-10-01 13:07:45 -07:00
|
|
|
'app04.newsblur.com',
|
|
|
|
],
|
2012-03-29 13:57:38 -07:00
|
|
|
'db': ['db01.newsblur.com',
|
|
|
|
'db02.newsblur.com',
|
2013-03-14 10:53:50 -07:00
|
|
|
# 'db03.newsblur.com',
|
2012-06-26 11:19:53 -07:00
|
|
|
'db04.newsblur.com',
|
2012-10-01 13:07:45 -07:00
|
|
|
'db05.newsblur.com',
|
2013-03-14 12:41:24 -07:00
|
|
|
'db10.newsblur.com',
|
|
|
|
'db11.newsblur.com',
|
2013-03-14 18:43:01 -07:00
|
|
|
'db12.newsblur.com',
|
2013-03-20 15:05:52 -07:00
|
|
|
'db20.newsblur.com',
|
|
|
|
'db21.newsblur.com',
|
2012-10-01 13:07:45 -07:00
|
|
|
],
|
2013-03-20 15:05:52 -07:00
|
|
|
'dbdo':['198.211.115.113',
|
|
|
|
'198.211.115.153',
|
|
|
|
],
|
2012-03-26 17:24:57 -07:00
|
|
|
'task': ['task01.newsblur.com',
|
2013-02-07 15:40:13 -08:00
|
|
|
'task02.newsblur.com',
|
2012-03-26 17:24:57 -07:00
|
|
|
'task03.newsblur.com',
|
2012-05-17 12:42:15 -07:00
|
|
|
'task04.newsblur.com',
|
2012-11-07 12:50:38 -08:00
|
|
|
# 'task05.newsblur.com',
|
|
|
|
# 'task06.newsblur.com',
|
|
|
|
# 'task07.newsblur.com',
|
|
|
|
'task08.newsblur.com',
|
|
|
|
'task09.newsblur.com',
|
2013-03-18 13:14:37 -07:00
|
|
|
# 'task10.newsblur.com',
|
2012-11-07 12:50:38 -08:00
|
|
|
'task11.newsblur.com',
|
2012-10-01 13:07:45 -07:00
|
|
|
],
|
2013-03-15 17:05:44 -07:00
|
|
|
'ec2task': ['ec2-54-242-38-48.compute-1.amazonaws.com',
|
2012-10-25 14:20:36 -07:00
|
|
|
'ec2-184-72-214-147.compute-1.amazonaws.com',
|
|
|
|
'ec2-107-20-103-16.compute-1.amazonaws.com',
|
|
|
|
'ec2-50-17-12-16.compute-1.amazonaws.com',
|
2013-03-15 17:05:44 -07:00
|
|
|
'ec2-54-242-34-138.compute-1.amazonaws.com',
|
2012-12-13 12:12:00 -08:00
|
|
|
'ec2-184-73-2-61.compute-1.amazonaws.com',
|
2013-03-18 13:35:11 -07:00
|
|
|
|
|
|
|
'ec2-54-234-211-75.compute-1.amazonaws.com',
|
|
|
|
'ec2-50-16-97-13.compute-1.amazonaws.com',
|
2013-03-19 18:27:00 -07:00
|
|
|
'ec2-54-242-131-232.compute-1.amazonaws.com',
|
|
|
|
'ec2-75-101-195-131.compute-1.amazonaws.com',
|
|
|
|
'ec2-54-242-105-17.compute-1.amazonaws.com',
|
2012-10-25 14:20:36 -07:00
|
|
|
],
|
2012-07-20 11:44:42 -07:00
|
|
|
'vps': ['task01.newsblur.com',
|
|
|
|
'task03.newsblur.com',
|
|
|
|
'task04.newsblur.com',
|
2012-10-01 13:07:45 -07:00
|
|
|
'task08.newsblur.com',
|
|
|
|
'task09.newsblur.com',
|
|
|
|
'task10.newsblur.com',
|
|
|
|
'task11.newsblur.com',
|
2012-07-20 11:44:42 -07:00
|
|
|
'app01.newsblur.com',
|
|
|
|
'app02.newsblur.com',
|
2012-10-01 13:07:45 -07:00
|
|
|
],
|
2010-07-30 23:50:49 -04:00
|
|
|
}
|
|
|
|
|
2011-03-24 09:27:05 -04:00
|
|
|
# ================
|
|
|
|
# = Environments =
|
|
|
|
# ================
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2011-08-29 18:43:17 -07:00
|
|
|
def server():
|
2013-03-13 19:17:45 -07:00
|
|
|
env.NEWSBLUR_PATH = "/srv/newsblur"
|
|
|
|
env.VENDOR_PATH = "/srv/code"
|
2011-08-29 18:43:17 -07:00
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
def app():
|
2011-08-29 18:43:17 -07:00
|
|
|
server()
|
2010-12-16 13:54:09 -05:00
|
|
|
env.roles = ['app']
|
2012-03-29 13:57:38 -07:00
|
|
|
|
|
|
|
def dev():
|
|
|
|
server()
|
|
|
|
env.roles = ['dev']
|
2011-07-17 15:52:06 -07:00
|
|
|
|
2011-04-03 10:54:20 -04:00
|
|
|
def web():
|
2011-08-29 18:43:17 -07:00
|
|
|
server()
|
2011-04-03 10:54:20 -04:00
|
|
|
env.roles = ['web']
|
2011-07-17 15:52:06 -07:00
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
def db():
|
2011-08-29 18:43:17 -07:00
|
|
|
server()
|
2010-12-16 13:54:09 -05:00
|
|
|
env.roles = ['db']
|
2011-07-17 15:52:06 -07:00
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
def task():
|
2011-08-29 18:43:17 -07:00
|
|
|
server()
|
2010-12-16 13:54:09 -05:00
|
|
|
env.roles = ['task']
|
2011-08-16 19:55:44 -07:00
|
|
|
|
2012-10-25 14:20:36 -07:00
|
|
|
def ec2task():
|
|
|
|
ec2()
|
|
|
|
env.roles = ['ec2task']
|
|
|
|
|
2012-07-20 11:44:42 -07:00
|
|
|
def vps():
|
|
|
|
server()
|
|
|
|
env.roles = ['vps']
|
2012-09-18 13:08:25 -07:00
|
|
|
|
2013-03-14 13:41:46 -07:00
|
|
|
def do():
|
|
|
|
server()
|
|
|
|
env.roles = ['do']
|
|
|
|
|
2013-03-17 14:06:17 -07:00
|
|
|
def debug():
|
|
|
|
server()
|
|
|
|
env.roles = ['debug']
|
|
|
|
|
2012-09-18 13:08:25 -07:00
|
|
|
def ec2():
|
|
|
|
env.user = 'ubuntu'
|
|
|
|
env.key_filename = ['/Users/sclay/.ec2/sclay.pem']
|
2012-09-19 12:00:41 -07:00
|
|
|
server()
|
2012-07-20 11:44:42 -07:00
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
# ==========
|
|
|
|
# = Deploy =
|
|
|
|
# ==========
|
|
|
|
|
2012-02-24 12:08:11 -08:00
|
|
|
@parallel
|
2011-08-29 18:43:17 -07:00
|
|
|
def pull():
|
|
|
|
with cd(env.NEWSBLUR_PATH):
|
|
|
|
run('git pull')
|
2012-01-02 18:11:40 -08:00
|
|
|
|
|
|
|
def pre_deploy():
|
2012-02-28 12:17:38 -08:00
|
|
|
compress_assets(bundle=True)
|
2012-01-02 18:11:40 -08:00
|
|
|
|
2012-07-30 13:25:44 -07:00
|
|
|
@serial
|
2012-01-02 18:11:40 -08:00
|
|
|
def post_deploy():
|
|
|
|
cleanup_assets()
|
2011-08-29 18:43:17 -07:00
|
|
|
|
2012-01-13 22:25:06 -08:00
|
|
|
@parallel
|
2013-03-17 14:41:29 -07:00
|
|
|
def deploy(fast=False):
|
|
|
|
deploy_code(copy_assets=True, fast=fast)
|
2010-12-16 13:54:09 -05:00
|
|
|
|
2011-01-15 18:43:09 -05:00
|
|
|
def deploy_full():
|
2013-03-15 12:35:10 -07:00
|
|
|
deploy_code(copy_assets=True, full=True)
|
2012-01-02 18:11:40 -08:00
|
|
|
|
2012-02-23 15:39:39 -08:00
|
|
|
@parallel
|
2013-03-15 11:11:47 -07:00
|
|
|
def deploy_code(copy_assets=False, full=False, fast=False):
|
2011-07-17 15:56:28 -07:00
|
|
|
with cd(env.NEWSBLUR_PATH):
|
2011-01-15 18:43:09 -05:00
|
|
|
run('git pull')
|
2012-01-02 17:32:12 -08:00
|
|
|
run('mkdir -p static')
|
2012-01-02 18:11:40 -08:00
|
|
|
if full:
|
|
|
|
run('rm -fr static/*')
|
2012-02-23 15:39:39 -08:00
|
|
|
if copy_assets:
|
|
|
|
transfer_assets()
|
2013-03-15 11:11:47 -07:00
|
|
|
sudo('supervisorctl reload')
|
|
|
|
if fast:
|
|
|
|
with settings(warn_only=True):
|
|
|
|
if env.user == 'ubuntu':
|
|
|
|
sudo('./utils/kill_gunicorn.sh')
|
|
|
|
else:
|
|
|
|
run('./utils/kill_gunicorn.sh')
|
2013-03-13 20:31:40 -07:00
|
|
|
# run('curl -s http://%s > /dev/null' % env.host)
|
|
|
|
# run('curl -s http://%s/api/add_site_load_script/ABCDEF > /dev/null' % env.host)
|
2012-04-02 18:48:41 -07:00
|
|
|
|
2012-12-26 20:00:38 -08:00
|
|
|
@parallel
|
|
|
|
def kill():
|
|
|
|
sudo('supervisorctl reload')
|
2013-03-15 11:49:02 -07:00
|
|
|
with settings(warn_only=True):
|
|
|
|
if env.user == 'ubuntu':
|
|
|
|
sudo('./utils/kill_gunicorn.sh')
|
|
|
|
else:
|
|
|
|
run('./utils/kill_gunicorn.sh')
|
2012-04-02 18:48:41 -07:00
|
|
|
|
|
|
|
def deploy_node():
|
|
|
|
with cd(env.NEWSBLUR_PATH):
|
2012-04-07 15:33:41 -07:00
|
|
|
run('sudo supervisorctl restart node_unread')
|
2012-12-24 23:01:25 -08:00
|
|
|
run('sudo supervisorctl restart node_unread_ssl')
|
2013-03-17 10:28:26 -07:00
|
|
|
# run('sudo supervisorctl restart node_favicons')
|
2012-05-02 19:00:17 -07:00
|
|
|
|
|
|
|
def gunicorn_restart():
|
|
|
|
restart_gunicorn()
|
|
|
|
|
2011-09-21 17:49:26 -07:00
|
|
|
def restart_gunicorn():
|
2013-03-20 16:06:02 -07:00
|
|
|
with cd(env.NEWSBLUR_PATH), settings(warn_only=True):
|
|
|
|
run('sudo supervisorctl restart gunicorn')
|
2011-09-21 17:49:26 -07:00
|
|
|
|
2011-10-26 08:40:31 -07:00
|
|
|
def gunicorn_stop():
|
2013-03-20 16:06:02 -07:00
|
|
|
with cd(env.NEWSBLUR_PATH), settings(warn_only=True):
|
|
|
|
run('sudo supervisorctl stop gunicorn')
|
2011-10-26 08:40:31 -07:00
|
|
|
|
2010-12-16 16:25:13 -05:00
|
|
|
def staging():
|
|
|
|
with cd('~/staging'):
|
|
|
|
run('git pull')
|
2011-04-15 11:34:41 -04:00
|
|
|
run('kill -HUP `cat logs/gunicorn.pid`')
|
2011-04-19 09:41:39 -04:00
|
|
|
run('curl -s http://dev.newsblur.com > /dev/null')
|
2011-06-09 22:15:34 -04:00
|
|
|
run('curl -s http://dev.newsblur.com/m/ > /dev/null')
|
2010-12-16 16:25:13 -05:00
|
|
|
|
2011-01-15 18:43:09 -05:00
|
|
|
def staging_full():
|
|
|
|
with cd('~/staging'):
|
|
|
|
run('git pull')
|
|
|
|
run('./manage.py migrate')
|
2011-04-15 11:34:41 -04:00
|
|
|
run('kill -HUP `cat logs/gunicorn.pid`')
|
2011-04-19 09:41:39 -04:00
|
|
|
run('curl -s http://dev.newsblur.com > /dev/null')
|
2011-06-09 22:15:34 -04:00
|
|
|
run('curl -s http://dev.newsblur.com/m/ > /dev/null')
|
2011-01-15 18:43:09 -05:00
|
|
|
|
2012-01-14 18:54:59 -08:00
|
|
|
@parallel
|
2010-12-16 13:54:09 -05:00
|
|
|
def celery():
|
2012-11-26 10:39:10 -08:00
|
|
|
celery_slow()
|
|
|
|
|
|
|
|
def celery_slow():
|
2011-09-25 09:32:40 -07:00
|
|
|
with cd(env.NEWSBLUR_PATH):
|
|
|
|
run('git pull')
|
2011-10-11 09:46:13 -07:00
|
|
|
celery_stop()
|
|
|
|
celery_start()
|
2011-09-25 09:32:40 -07:00
|
|
|
|
2012-11-26 11:41:31 -08:00
|
|
|
@parallel
|
2012-11-26 10:39:10 -08:00
|
|
|
def celery_fast():
|
|
|
|
with cd(env.NEWSBLUR_PATH):
|
|
|
|
run('git pull')
|
|
|
|
celery_reload()
|
|
|
|
|
2012-02-29 12:15:01 -08:00
|
|
|
@parallel
|
2011-09-25 09:31:08 -07:00
|
|
|
def celery_stop():
|
2011-07-17 15:56:28 -07:00
|
|
|
with cd(env.NEWSBLUR_PATH):
|
2011-04-11 15:46:33 -04:00
|
|
|
run('sudo supervisorctl stop celery')
|
2011-04-12 11:02:02 -04:00
|
|
|
with settings(warn_only=True):
|
|
|
|
run('./utils/kill_celery.sh')
|
2011-09-25 09:31:08 -07:00
|
|
|
|
2012-02-29 12:15:01 -08:00
|
|
|
@parallel
|
2011-09-25 09:31:08 -07:00
|
|
|
def celery_start():
|
|
|
|
with cd(env.NEWSBLUR_PATH):
|
2011-04-11 15:46:33 -04:00
|
|
|
run('sudo supervisorctl start celery')
|
2010-12-16 14:15:22 -05:00
|
|
|
run('tail logs/newsblur.log')
|
2010-12-16 13:54:09 -05:00
|
|
|
|
2012-11-26 10:39:10 -08:00
|
|
|
@parallel
|
|
|
|
def celery_reload():
|
|
|
|
with cd(env.NEWSBLUR_PATH):
|
|
|
|
run('sudo supervisorctl reload celery')
|
|
|
|
run('tail logs/newsblur.log')
|
|
|
|
|
2011-09-25 09:32:40 -07:00
|
|
|
def kill_celery():
|
2011-07-17 15:56:28 -07:00
|
|
|
with cd(env.NEWSBLUR_PATH):
|
2010-12-16 14:15:22 -05:00
|
|
|
run('ps aux | grep celeryd | egrep -v grep | awk \'{print $2}\' | sudo xargs kill -9')
|
2010-12-16 13:54:09 -05:00
|
|
|
|
2012-02-28 12:17:38 -08:00
|
|
|
def compress_assets(bundle=False):
|
2012-01-02 17:32:12 -08:00
|
|
|
local('jammit -c assets.yml --base-url http://www.newsblur.com --output static')
|
2012-01-23 17:51:19 -08:00
|
|
|
local('tar -czf static.tgz static/*')
|
2012-01-02 18:11:40 -08:00
|
|
|
|
|
|
|
def transfer_assets():
|
2012-01-23 17:51:19 -08:00
|
|
|
put('static.tgz', '%s/static/' % env.NEWSBLUR_PATH)
|
|
|
|
run('tar -xzf static/static.tgz')
|
|
|
|
run('rm -f static/static.tgz')
|
2012-01-02 18:11:40 -08:00
|
|
|
|
|
|
|
def cleanup_assets():
|
2012-01-23 17:51:19 -08:00
|
|
|
local('rm -f static.tgz')
|
2012-01-02 18:11:40 -08:00
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
# ===========
|
|
|
|
# = Backups =
|
|
|
|
# ===========
|
|
|
|
|
|
|
|
def backup_mongo():
|
2011-07-17 20:53:30 -07:00
|
|
|
with cd(os.path.join(env.NEWSBLUR_PATH, 'utils/backups')):
|
2011-12-18 20:57:18 -08:00
|
|
|
# run('./mongo_backup.sh')
|
|
|
|
run('python backup_mongo.py')
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
def backup_postgresql():
|
2012-12-17 16:19:28 -08:00
|
|
|
# crontab for postgres master server
|
2013-02-25 19:01:04 -08:00
|
|
|
# 0 4 * * * python /srv/newsblur/utils/backups/backup_psql.py
|
2012-11-09 09:58:48 -08:00
|
|
|
# 0 * * * * sudo find /var/lib/postgresql/9.1/archive -mtime +1 -exec rm {} \;
|
2012-12-17 16:19:28 -08:00
|
|
|
|
2011-07-17 20:53:30 -07:00
|
|
|
with cd(os.path.join(env.NEWSBLUR_PATH, 'utils/backups')):
|
2011-12-18 20:57:18 -08:00
|
|
|
# run('./postgresql_backup.sh')
|
|
|
|
run('python backup_psql.py')
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2011-11-22 12:51:44 -05:00
|
|
|
# ===============
|
|
|
|
# = Calibration =
|
|
|
|
# ===============
|
|
|
|
|
|
|
|
def sync_time():
|
2012-04-24 10:38:23 -07:00
|
|
|
with settings(warn_only=True):
|
|
|
|
sudo("/etc/init.d/ntp stop")
|
|
|
|
sudo("ntpdate pool.ntp.org")
|
|
|
|
sudo("/etc/init.d/ntp start")
|
2012-04-23 13:18:50 -07:00
|
|
|
|
|
|
|
def setup_time_calibration():
|
2012-04-24 10:38:23 -07:00
|
|
|
sudo('apt-get -y install ntp')
|
|
|
|
put('config/ntpdate.cron', '%s/' % env.NEWSBLUR_PATH)
|
2012-10-14 17:33:07 -04:00
|
|
|
sudo('chown root.root %s/ntpdate.cron' % env.NEWSBLUR_PATH)
|
2012-04-23 13:18:50 -07:00
|
|
|
sudo('chmod 755 %s/ntpdate.cron' % env.NEWSBLUR_PATH)
|
|
|
|
sudo('mv %s/ntpdate.cron /etc/cron.hourly/ntpdate' % env.NEWSBLUR_PATH)
|
2012-05-17 12:42:15 -07:00
|
|
|
with settings(warn_only=True):
|
|
|
|
sudo('/etc/cron.hourly/ntpdate')
|
2011-11-22 12:51:44 -05:00
|
|
|
|
2011-02-08 22:07:59 -05:00
|
|
|
# =============
|
|
|
|
# = Bootstrap =
|
|
|
|
# =============
|
|
|
|
|
2011-03-24 09:27:05 -04:00
|
|
|
def setup_common():
|
|
|
|
setup_installs()
|
|
|
|
setup_user()
|
2011-12-13 17:50:33 -08:00
|
|
|
setup_sudoers()
|
2013-03-20 15:05:52 -07:00
|
|
|
setup_ulimit()
|
2011-03-24 09:27:05 -04:00
|
|
|
setup_repo()
|
2011-07-17 15:52:06 -07:00
|
|
|
setup_repo_local_settings()
|
2011-03-24 09:27:05 -04:00
|
|
|
setup_local_files()
|
|
|
|
setup_libxml()
|
|
|
|
setup_python()
|
2012-03-26 10:08:33 -07:00
|
|
|
# setup_psycopg()
|
2011-03-24 09:27:05 -04:00
|
|
|
setup_supervisor()
|
|
|
|
setup_hosts()
|
|
|
|
config_pgbouncer()
|
2012-11-06 11:28:36 -08:00
|
|
|
setup_mongoengine()
|
|
|
|
setup_forked_mongoengine()
|
|
|
|
setup_pymongo_repo()
|
2011-03-24 09:27:05 -04:00
|
|
|
setup_logrotate()
|
|
|
|
setup_nginx()
|
2011-03-31 18:51:23 -04:00
|
|
|
configure_nginx()
|
2012-12-24 20:24:45 -08:00
|
|
|
setup_munin()
|
2011-03-24 09:27:05 -04:00
|
|
|
|
2013-01-16 11:06:59 -08:00
|
|
|
def setup_all():
|
2011-02-09 15:45:41 -05:00
|
|
|
setup_common()
|
2013-01-16 11:06:59 -08:00
|
|
|
setup_app(skip_common=True)
|
|
|
|
setup_db(skip_common=True)
|
|
|
|
setup_task(skip_common=True)
|
|
|
|
|
|
|
|
def setup_app(skip_common=False):
|
|
|
|
if not skip_common:
|
|
|
|
setup_common()
|
2012-04-23 13:18:50 -07:00
|
|
|
setup_vps()
|
2011-09-07 22:35:20 -07:00
|
|
|
setup_app_firewall()
|
2011-03-17 19:26:21 -04:00
|
|
|
setup_app_motd()
|
2012-05-02 16:46:00 -07:00
|
|
|
copy_app_settings()
|
2012-05-21 09:40:44 -07:00
|
|
|
configure_nginx()
|
2011-07-17 20:53:30 -07:00
|
|
|
setup_gunicorn(supervisor=True)
|
2011-02-09 15:45:41 -05:00
|
|
|
update_gunicorn()
|
2012-03-29 17:24:23 -07:00
|
|
|
setup_node()
|
2012-04-07 14:44:52 -07:00
|
|
|
configure_node()
|
2012-05-21 09:40:44 -07:00
|
|
|
pre_deploy()
|
2012-05-21 09:44:17 -07:00
|
|
|
deploy()
|
2012-12-10 10:49:36 -08:00
|
|
|
config_monit_app()
|
2011-02-09 15:45:41 -05:00
|
|
|
|
2013-03-20 12:24:36 -07:00
|
|
|
def setup_db(skip_common=False, engine=None):
|
2013-01-16 11:06:59 -08:00
|
|
|
if not skip_common:
|
|
|
|
setup_common()
|
2012-04-23 13:18:50 -07:00
|
|
|
setup_baremetal()
|
2011-03-19 18:35:44 -04:00
|
|
|
setup_db_firewall()
|
2011-03-17 19:26:21 -04:00
|
|
|
setup_db_motd()
|
2012-05-02 16:46:00 -07:00
|
|
|
copy_task_settings()
|
2013-01-16 11:06:59 -08:00
|
|
|
setup_memcached()
|
2013-03-20 12:24:36 -07:00
|
|
|
if engine == "postgres":
|
2013-03-15 17:05:44 -07:00
|
|
|
setup_postgres(standby=False)
|
2013-03-20 12:24:36 -07:00
|
|
|
elif engine == "postgres_slave":
|
2013-03-15 17:05:44 -07:00
|
|
|
setup_postgres(standby=True)
|
2013-03-20 12:24:36 -07:00
|
|
|
elif engine == "mongo":
|
2013-03-15 17:05:44 -07:00
|
|
|
setup_mongo()
|
2013-03-20 12:24:36 -07:00
|
|
|
elif engine == "redis":
|
2013-03-15 17:05:44 -07:00
|
|
|
setup_redis()
|
2013-01-16 11:06:59 -08:00
|
|
|
setup_gunicorn(supervisor=False)
|
2012-03-30 14:56:16 -07:00
|
|
|
setup_db_munin()
|
2012-09-19 12:00:41 -07:00
|
|
|
|
2013-01-16 11:06:59 -08:00
|
|
|
# if env.user == 'ubuntu':
|
|
|
|
# setup_db_mdadm()
|
2011-03-14 21:44:30 -04:00
|
|
|
|
2013-01-16 11:06:59 -08:00
|
|
|
def setup_task(skip_common=False):
|
|
|
|
if not skip_common:
|
|
|
|
setup_common()
|
2012-04-23 13:18:50 -07:00
|
|
|
setup_vps()
|
2011-09-07 22:35:20 -07:00
|
|
|
setup_task_firewall()
|
2011-03-17 19:26:21 -04:00
|
|
|
setup_task_motd()
|
2012-05-02 16:46:00 -07:00
|
|
|
copy_task_settings()
|
2011-03-24 09:27:05 -04:00
|
|
|
enable_celery_supervisor()
|
2011-03-19 18:35:44 -04:00
|
|
|
setup_gunicorn(supervisor=False)
|
|
|
|
update_gunicorn()
|
2012-08-15 17:02:55 -07:00
|
|
|
config_monit_task()
|
2011-02-09 15:45:41 -05:00
|
|
|
|
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')
|
2013-03-14 22:39:58 -07:00
|
|
|
sudo('apt-get -y install build-essential gcc scons libreadline-dev sysstat iotop git zsh python-dev locate python-software-properties software-properties-common libpcre3-dev libncurses5-dev libdbd-pg-perl libssl-dev make pgbouncer python-psycopg2 libmemcache0 python-memcache libyaml-0-2 python-yaml python-numpy python-scipy python-imaging curl monit ufw')
|
2011-11-01 19:00:58 -07:00
|
|
|
# sudo('add-apt-repository ppa:pitti/postgresql')
|
2011-02-09 15:45:41 -05:00
|
|
|
sudo('apt-get -y update')
|
2011-11-01 19:00:58 -07:00
|
|
|
sudo('apt-get -y install postgresql-client')
|
2011-03-14 21:44:30 -04:00
|
|
|
sudo('mkdir -p /var/run/postgresql')
|
|
|
|
sudo('chown postgres.postgres /var/run/postgresql')
|
2011-08-24 12:31:47 -07:00
|
|
|
with settings(warn_only=True):
|
|
|
|
run('git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh')
|
2011-02-09 15:45:41 -05:00
|
|
|
run('curl -O http://peak.telecommunity.com/dist/ez_setup.py')
|
|
|
|
sudo('python ez_setup.py -U setuptools && rm ez_setup.py')
|
2011-12-13 17:50:33 -08:00
|
|
|
sudo('chsh %s -s /bin/zsh' % env.user)
|
2013-03-20 12:24:36 -07:00
|
|
|
with settings(warn_only=True):
|
|
|
|
sudo('mkdir -p %s' % env.VENDOR_PATH)
|
2013-03-20 15:05:52 -07:00
|
|
|
sudo('chown %s.%s %s' % (env.user, env.user, env.VENDOR_PATH))
|
2011-03-15 18:06:24 -04:00
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
def setup_user():
|
2012-09-18 13:08:25 -07:00
|
|
|
# run('useradd -c "NewsBlur" -m newsblur -s /bin/zsh')
|
2011-03-14 21:44:30 -04:00
|
|
|
# run('openssl rand -base64 8 | tee -a ~conesus/.password | passwd -stdin conesus')
|
|
|
|
run('mkdir -p ~/.ssh && chmod 700 ~/.ssh')
|
2011-03-15 10:02:13 -04:00
|
|
|
run('rm -fr ~/.ssh/id_dsa*')
|
2011-03-14 21:44:30 -04:00
|
|
|
run('ssh-keygen -t dsa -f ~/.ssh/id_dsa -N ""')
|
2011-03-15 10:02:13 -04:00
|
|
|
run('touch ~/.ssh/authorized_keys')
|
|
|
|
put("~/.ssh/id_dsa.pub", "authorized_keys")
|
2012-09-18 13:08:25 -07:00
|
|
|
run('echo `cat authorized_keys` >> ~/.ssh/authorized_keys')
|
|
|
|
run('rm authorized_keys')
|
2011-03-14 21:44:30 -04:00
|
|
|
|
2011-03-23 22:08:00 -04:00
|
|
|
def add_machine_to_ssh():
|
|
|
|
put("~/.ssh/id_dsa.pub", "local_keys")
|
|
|
|
run("echo `cat local_keys` >> .ssh/authorized_keys")
|
2011-12-13 17:50:33 -08:00
|
|
|
run("rm local_keys")
|
2011-03-23 22:08:00 -04:00
|
|
|
|
2011-02-09 15:45:41 -05:00
|
|
|
def setup_repo():
|
2011-08-24 12:31:47 -07:00
|
|
|
with settings(warn_only=True):
|
2013-03-17 12:39:05 -07:00
|
|
|
run('git clone https://github.com/samuelclay/NewsBlur.git ~/newsblur')
|
2012-10-15 16:32:32 -07:00
|
|
|
sudo('mkdir -p /srv')
|
2013-03-20 15:05:52 -07:00
|
|
|
# with settings(warn_only=True):
|
|
|
|
# sudo('ln -f -s /home/%s/code /srv/' % env.user)
|
2013-03-17 12:39:05 -07:00
|
|
|
sudo('ln -f -s /home/%s/newsblur /srv/' % env.user)
|
2011-07-17 15:52:06 -07:00
|
|
|
|
|
|
|
def setup_repo_local_settings():
|
2011-07-17 15:56:28 -07:00
|
|
|
with cd(env.NEWSBLUR_PATH):
|
2011-02-09 15:45:41 -05:00
|
|
|
run('cp local_settings.py.template local_settings.py')
|
|
|
|
run('mkdir -p logs')
|
2011-03-19 18:35:44 -04:00
|
|
|
run('touch logs/newsblur.log')
|
2011-02-09 15:45:41 -05:00
|
|
|
|
2012-02-08 20:33:20 -08:00
|
|
|
def copy_local_settings():
|
|
|
|
with cd(env.NEWSBLUR_PATH):
|
|
|
|
put('local_settings.py.server', 'local_settings.py')
|
|
|
|
|
2011-03-15 10:02:13 -04:00
|
|
|
def setup_local_files():
|
|
|
|
put("config/toprc", "./.toprc")
|
|
|
|
put("config/zshrc", "./.zshrc")
|
2011-03-19 19:24:14 -04:00
|
|
|
put('config/gitconfig.txt', './.gitconfig')
|
2011-04-02 00:31:43 -04:00
|
|
|
put('config/ssh.conf', './.ssh/config')
|
2011-03-15 10:02:13 -04:00
|
|
|
|
2011-02-09 15:45:41 -05:00
|
|
|
def setup_libxml():
|
|
|
|
sudo('apt-get -y install libxml2-dev libxslt1-dev python-lxml')
|
2011-03-24 09:27:05 -04:00
|
|
|
|
|
|
|
def setup_libxml_code():
|
2011-07-17 15:56:28 -07:00
|
|
|
with cd(env.VENDOR_PATH):
|
2011-03-24 09:27:05 -04:00
|
|
|
run('git clone git://git.gnome.org/libxml2')
|
|
|
|
run('git clone git://git.gnome.org/libxslt')
|
|
|
|
|
2011-07-17 20:53:30 -07:00
|
|
|
with cd(os.path.join(env.VENDOR_PATH, 'libxml2')):
|
2011-03-24 09:27:05 -04:00
|
|
|
run('./configure && make && sudo make install')
|
|
|
|
|
2011-07-17 20:53:30 -07:00
|
|
|
with cd(os.path.join(env.VENDOR_PATH, 'libxslt')):
|
2011-03-24 09:27:05 -04:00
|
|
|
run('./configure && make && sudo make install')
|
2011-07-17 12:08:24 -07:00
|
|
|
|
|
|
|
def setup_psycopg():
|
2012-02-23 13:20:10 -08:00
|
|
|
sudo('easy_install -U psycopg2')
|
2013-03-19 18:06:01 +00:00
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
def setup_python():
|
2012-05-17 12:42:15 -07:00
|
|
|
# sudo('easy_install -U pip')
|
2013-03-19 18:06:01 +00:00
|
|
|
sudo('easy_install -U $(<%s)' %
|
|
|
|
os.path.join(env.NEWSBLUR_PATH, 'config/requirements.txt'))
|
2011-07-17 15:30:25 -07:00
|
|
|
put('config/pystartup.py', '.pystartup')
|
2013-03-19 18:06:01 +00:00
|
|
|
|
2012-07-17 12:07:26 -07:00
|
|
|
# with cd(os.path.join(env.NEWSBLUR_PATH, 'vendor/cjson')):
|
|
|
|
# sudo('python setup.py install')
|
2013-03-19 18:06:01 +00:00
|
|
|
|
2011-09-07 22:35:20 -07:00
|
|
|
with settings(warn_only=True):
|
2012-05-17 12:42:15 -07:00
|
|
|
sudo('su -c \'echo "import sys; sys.setdefaultencoding(\\\\"utf-8\\\\")" > /usr/lib/python2.7/sitecustomize.py\'')
|
2011-07-17 21:29:25 -07:00
|
|
|
|
|
|
|
# PIL - Only if python-imaging didn't install through apt-get, like on Mac OS X.
|
|
|
|
def setup_imaging():
|
|
|
|
sudo('easy_install pil')
|
2011-03-14 21:44:30 -04:00
|
|
|
|
|
|
|
def setup_supervisor():
|
|
|
|
sudo('apt-get -y install supervisor')
|
2013-03-14 18:43:01 -07:00
|
|
|
|
|
|
|
@parallel
|
2011-03-15 18:06:24 -04:00
|
|
|
def setup_hosts():
|
2013-03-16 18:04:19 -07:00
|
|
|
put('../secrets-newsblur/configs/hosts', '/etc/hosts', use_sudo=True)
|
2011-03-19 16:19:53 -04:00
|
|
|
|
2011-03-23 15:43:15 -04:00
|
|
|
def config_pgbouncer():
|
|
|
|
put('config/pgbouncer.conf', '/etc/pgbouncer/pgbouncer.ini', use_sudo=True)
|
2012-09-19 12:00:41 -07:00
|
|
|
# put('config/pgbouncer_userlist.txt', '/etc/pgbouncer/userlist.txt', use_sudo=True)
|
2013-03-16 17:58:34 -07:00
|
|
|
put('../secrets-newsblur/configs/pgbouncer_auth.conf', '/etc/pgbouncer/userlist.txt', use_sudo=True)
|
2011-12-27 21:38:32 -08:00
|
|
|
sudo('echo "START=1" > /etc/default/pgbouncer')
|
2012-07-20 01:26:56 -07:00
|
|
|
sudo('su postgres -c "/etc/init.d/pgbouncer stop"', pty=False)
|
2011-09-09 17:43:56 -07:00
|
|
|
with settings(warn_only=True):
|
2012-09-19 12:00:41 -07:00
|
|
|
sudo('pkill -9 pgbouncer')
|
2012-05-15 15:25:01 -07:00
|
|
|
run('sleep 2')
|
2012-07-20 01:26:56 -07:00
|
|
|
sudo('/etc/init.d/pgbouncer start', pty=False)
|
|
|
|
|
|
|
|
def bounce_pgbouncer():
|
|
|
|
sudo('su postgres -c "/etc/init.d/pgbouncer stop"', pty=False)
|
|
|
|
run('sleep 4')
|
|
|
|
with settings(warn_only=True):
|
|
|
|
sudo('pkill pgbouncer')
|
|
|
|
run('sleep 4')
|
|
|
|
run('sudo /etc/init.d/pgbouncer start', pty=False)
|
|
|
|
run('sleep 2')
|
2011-03-23 15:43:15 -04:00
|
|
|
|
2012-08-15 17:02:55 -07:00
|
|
|
def config_monit_task():
|
|
|
|
put('config/monit_task.conf', '/etc/monit/conf.d/celery.conf', use_sudo=True)
|
|
|
|
sudo('echo "startup=1" > /etc/default/monit')
|
|
|
|
sudo('/etc/init.d/monit restart')
|
|
|
|
|
2012-12-10 10:49:36 -08:00
|
|
|
def config_monit_app():
|
|
|
|
put('config/monit_app.conf', '/etc/monit/conf.d/gunicorn.conf', use_sudo=True)
|
|
|
|
sudo('echo "startup=1" > /etc/default/monit')
|
|
|
|
sudo('/etc/init.d/monit restart')
|
|
|
|
|
2012-08-15 17:02:55 -07:00
|
|
|
def config_monit_db():
|
|
|
|
put('config/monit_db.conf', '/etc/monit/conf.d/celery.conf', use_sudo=True)
|
2011-03-28 10:07:45 -04:00
|
|
|
sudo('echo "startup=1" > /etc/default/monit')
|
|
|
|
sudo('/etc/init.d/monit restart')
|
|
|
|
|
2011-03-19 16:19:53 -04:00
|
|
|
def setup_mongoengine():
|
2013-03-20 16:06:02 -07:00
|
|
|
with cd(env.VENDOR_PATH), settings(warn_only=True):
|
|
|
|
run('rm -fr mongoengine')
|
|
|
|
run('git clone https://github.com/MongoEngine/mongoengine.git')
|
|
|
|
sudo('rm -fr /usr/local/lib/python2.7/dist-packages/mongoengine')
|
|
|
|
sudo('rm -fr /usr/local/lib/python2.7/dist-packages/mongoengine-*')
|
|
|
|
sudo('ln -s %s /usr/local/lib/python2.7/dist-packages/mongoengine' %
|
|
|
|
os.path.join(env.VENDOR_PATH, 'mongoengine/mongoengine'))
|
2011-03-19 16:19:53 -04:00
|
|
|
|
|
|
|
def setup_pymongo_repo():
|
2013-03-20 16:06:02 -07:00
|
|
|
with cd(env.VENDOR_PATH), settings(warn_only=True):
|
|
|
|
run('git clone git://github.com/mongodb/mongo-python-driver.git pymongo')
|
2012-11-06 11:28:36 -08:00
|
|
|
# with cd(os.path.join(env.VENDOR_PATH, 'pymongo')):
|
|
|
|
# sudo('python setup.py install')
|
|
|
|
sudo('rm -fr /usr/local/lib/python2.7/dist-packages/pymongo*')
|
|
|
|
sudo('rm -fr /usr/local/lib/python2.7/dist-packages/bson*')
|
|
|
|
sudo('rm -fr /usr/local/lib/python2.7/dist-packages/gridgs*')
|
2012-12-24 14:57:43 -08:00
|
|
|
sudo('ln -fs %s /usr/local/lib/python2.7/dist-packages/' %
|
2012-11-06 11:28:36 -08:00
|
|
|
os.path.join(env.VENDOR_PATH, 'pymongo/{pymongo,bson,gridfs}'))
|
2011-03-19 16:19:53 -04:00
|
|
|
|
|
|
|
def setup_forked_mongoengine():
|
2013-03-20 16:06:02 -07:00
|
|
|
with cd(os.path.join(env.VENDOR_PATH, 'mongoengine')), settings(warn_only=True):
|
|
|
|
run('git remote add clay https://github.com/samuelclay/mongoengine.git')
|
|
|
|
run('git pull')
|
|
|
|
run('git fetch clay')
|
|
|
|
run('git checkout -b clay_master clay/master')
|
2011-05-18 14:23:43 -04:00
|
|
|
|
|
|
|
def switch_forked_mongoengine():
|
2011-07-17 20:53:30 -07:00
|
|
|
with cd(os.path.join(env.VENDOR_PATH, 'mongoengine')):
|
2011-05-20 09:51:56 -04:00
|
|
|
run('git co dev')
|
2012-09-18 13:08:25 -07:00
|
|
|
run('git pull %s dev --force' % env.user)
|
2011-05-20 09:51:56 -04:00
|
|
|
# run('git checkout .')
|
|
|
|
# run('git checkout master')
|
|
|
|
# run('get branch -D dev')
|
|
|
|
# run('git checkout -b dev origin/dev')
|
2011-03-19 16:19:53 -04:00
|
|
|
|
2011-03-23 15:43:15 -04:00
|
|
|
def setup_logrotate():
|
|
|
|
put('config/logrotate.conf', '/etc/logrotate.d/newsblur', use_sudo=True)
|
2013-03-17 20:14:55 -07:00
|
|
|
|
|
|
|
def setup_ulimit():
|
|
|
|
# Increase File Descriptor limits.
|
2013-03-20 15:05:52 -07:00
|
|
|
run('export FILEMAX=`sysctl -n fs.file-max`', pty=False)
|
|
|
|
sudo('mv /etc/security/limits.conf /etc/security/limits.conf.bak', pty=False)
|
|
|
|
sudo('touch /etc/security/limits.conf', pty=False)
|
|
|
|
sudo('chmod 666 /etc/security/limits.conf', pty=False)
|
|
|
|
run('echo "root soft nofile $FILEMAX" >> /etc/security/limits.conf', pty=False)
|
|
|
|
run('echo "root hard nofile $FILEMAX" >> /etc/security/limits.conf', pty=False)
|
|
|
|
run('echo "* soft nofile $FILEMAX" >> /etc/security/limits.conf', pty=False)
|
|
|
|
run('echo "* hard nofile $FILEMAX" >> /etc/security/limits.conf', pty=False)
|
|
|
|
sudo('chmod 644 /etc/security/limits.conf', pty=False)
|
2013-03-17 20:14:55 -07:00
|
|
|
|
|
|
|
# run('touch /home/ubuntu/.bash_profile')
|
|
|
|
# run('echo "ulimit -n $FILEMAX" >> /home/ubuntu/.bash_profile')
|
|
|
|
|
|
|
|
# Increase Ephemeral Ports.
|
|
|
|
# sudo chmod 666 /etc/sysctl.conf
|
|
|
|
# echo "net.ipv4.ip_local_port_range = 1024 65535" >> /etc/sysctl.conf
|
|
|
|
# sudo chmod 644 /etc/sysctl.conf
|
2011-03-23 15:43:15 -04:00
|
|
|
|
2013-03-14 18:43:01 -07:00
|
|
|
def setup_sudoers(user=None):
|
|
|
|
sudo('su - root -c "echo \\\\"%s ALL=(ALL) NOPASSWD: ALL\\\\" >> /etc/sudoers"' % (user or env.user))
|
2011-02-09 15:45:41 -05:00
|
|
|
|
|
|
|
def setup_nginx():
|
2012-07-20 01:26:56 -07:00
|
|
|
NGINX_VERSION = '1.2.2'
|
2013-03-20 16:06:02 -07:00
|
|
|
with cd(env.VENDOR_PATH), settings(warn_only=True):
|
|
|
|
sudo("groupadd nginx")
|
|
|
|
sudo("useradd -g nginx -d /var/www/htdocs -s /bin/false nginx")
|
|
|
|
run('wget http://nginx.org/download/nginx-%s.tar.gz' % NGINX_VERSION)
|
|
|
|
run('tar -xzf nginx-%s.tar.gz' % NGINX_VERSION)
|
|
|
|
run('rm nginx-%s.tar.gz' % NGINX_VERSION)
|
|
|
|
with cd('nginx-%s' % NGINX_VERSION):
|
|
|
|
run('./configure --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module')
|
|
|
|
run('make')
|
|
|
|
sudo('make install')
|
2011-03-31 18:51:23 -04:00
|
|
|
|
|
|
|
def configure_nginx():
|
2011-03-21 20:07:24 -04:00
|
|
|
put("config/nginx.conf", "/usr/local/nginx/conf/nginx.conf", use_sudo=True)
|
|
|
|
sudo("mkdir -p /usr/local/nginx/conf/sites-enabled")
|
|
|
|
sudo("mkdir -p /var/log/nginx")
|
2011-08-26 09:34:30 -07:00
|
|
|
put("config/nginx.newsblur.conf", "/usr/local/nginx/conf/sites-enabled/newsblur.conf", use_sudo=True)
|
2011-03-21 20:07:24 -04:00
|
|
|
put("config/nginx-init", "/etc/init.d/nginx", use_sudo=True)
|
|
|
|
sudo("chmod 0755 /etc/init.d/nginx")
|
|
|
|
sudo("/usr/sbin/update-rc.d -f nginx defaults")
|
2011-03-31 18:51:23 -04:00
|
|
|
sudo("/etc/init.d/nginx restart")
|
2012-09-19 12:00:41 -07:00
|
|
|
copy_certificates()
|
2012-04-23 13:18:50 -07:00
|
|
|
|
|
|
|
def setup_vps():
|
|
|
|
# VPS suffer from severe time drift. Force blunt hourly time recalibration.
|
|
|
|
setup_time_calibration()
|
|
|
|
|
|
|
|
def setup_baremetal():
|
|
|
|
# Bare metal doesn't suffer from severe time drift. Use standard ntp slow-drift-calibration.
|
|
|
|
sudo('apt-get -y install ntp')
|
2011-03-24 09:27:05 -04:00
|
|
|
|
|
|
|
# ===============
|
|
|
|
# = Setup - App =
|
|
|
|
# ===============
|
|
|
|
|
2011-09-07 22:35:20 -07:00
|
|
|
def setup_app_firewall():
|
|
|
|
sudo('ufw default deny')
|
2012-12-24 12:55:14 -08:00
|
|
|
sudo('ufw allow ssh') # ssh
|
|
|
|
sudo('ufw allow 80') # http
|
2013-03-18 11:32:24 -07:00
|
|
|
sudo('ufw allow 8000') # gunicorn
|
2012-12-24 12:55:14 -08:00
|
|
|
sudo('ufw allow 8888') # socket.io
|
|
|
|
sudo('ufw allow 8889') # socket.io ssl
|
|
|
|
sudo('ufw allow 443') # https
|
2011-09-07 22:35:20 -07:00
|
|
|
sudo('ufw --force enable')
|
|
|
|
|
2011-03-24 09:27:05 -04:00
|
|
|
def setup_app_motd():
|
|
|
|
put('config/motd_app.txt', '/etc/motd.tail', use_sudo=True)
|
|
|
|
|
2011-09-07 22:35:20 -07:00
|
|
|
def setup_gunicorn(supervisor=True):
|
2011-03-24 09:27:05 -04:00
|
|
|
if supervisor:
|
|
|
|
put('config/supervisor_gunicorn.conf', '/etc/supervisor/conf.d/gunicorn.conf', use_sudo=True)
|
2011-07-17 20:53:30 -07:00
|
|
|
with cd(env.VENDOR_PATH):
|
2011-03-24 09:27:05 -04:00
|
|
|
sudo('rm -fr gunicorn')
|
|
|
|
run('git clone git://github.com/benoitc/gunicorn.git')
|
2011-07-17 20:53:30 -07:00
|
|
|
with cd(os.path.join(env.VENDOR_PATH, 'gunicorn')):
|
|
|
|
run('git pull')
|
|
|
|
sudo('python setup.py develop')
|
|
|
|
|
2011-03-24 09:27:05 -04:00
|
|
|
|
|
|
|
def update_gunicorn():
|
2011-07-17 20:53:30 -07:00
|
|
|
with cd(os.path.join(env.VENDOR_PATH, 'gunicorn')):
|
2011-03-24 09:27:05 -04:00
|
|
|
run('git pull')
|
|
|
|
sudo('python setup.py develop')
|
2011-03-14 21:44:30 -04:00
|
|
|
|
2011-04-15 11:34:41 -04:00
|
|
|
def setup_staging():
|
|
|
|
run('git clone https://github.com/samuelclay/NewsBlur.git staging')
|
|
|
|
with cd('~/staging'):
|
|
|
|
run('cp ../newsblur/local_settings.py local_settings.py')
|
|
|
|
run('mkdir -p logs')
|
|
|
|
run('touch logs/newsblur.log')
|
2012-01-02 18:22:06 -08:00
|
|
|
|
|
|
|
def setup_node():
|
2012-05-21 09:40:44 -07:00
|
|
|
sudo('add-apt-repository -y ppa:chris-lea/node.js')
|
2012-01-02 18:22:06 -08:00
|
|
|
sudo('apt-get update')
|
2012-03-29 17:24:23 -07:00
|
|
|
sudo('apt-get install -y nodejs')
|
2013-01-07 11:33:24 -08:00
|
|
|
run('curl -L https://npmjs.org/install.sh | sudo sh')
|
2012-01-02 18:22:06 -08:00
|
|
|
sudo('npm install -g supervisor')
|
2012-01-13 22:25:06 -08:00
|
|
|
sudo('ufw allow 8888')
|
2012-04-07 14:44:52 -07:00
|
|
|
|
|
|
|
def configure_node():
|
|
|
|
sudo('rm -fr /etc/supervisor/conf.d/node.conf')
|
|
|
|
put('config/supervisor_node_unread.conf', '/etc/supervisor/conf.d/node_unread.conf', use_sudo=True)
|
2012-12-24 11:53:00 -08:00
|
|
|
put('config/supervisor_node_unread_ssl.conf', '/etc/supervisor/conf.d/node_unread_ssl.conf', use_sudo=True)
|
2013-03-17 10:28:26 -07:00
|
|
|
# put('config/supervisor_node_favicons.conf', '/etc/supervisor/conf.d/node_favicons.conf', use_sudo=True)
|
2012-03-29 17:24:23 -07:00
|
|
|
sudo('supervisorctl reload')
|
2012-01-14 18:54:59 -08:00
|
|
|
|
2013-03-18 13:14:37 -07:00
|
|
|
@parallel
|
2012-05-02 16:46:00 -07:00
|
|
|
def copy_app_settings():
|
2013-03-16 17:58:34 -07:00
|
|
|
put('../secrets-newsblur/settings/app_settings.py', '%s/local_settings.py' % env.NEWSBLUR_PATH)
|
2012-05-02 16:46:00 -07:00
|
|
|
run('echo "\nSERVER_NAME = \\\\"`hostname`\\\\"" >> %s/local_settings.py' % env.NEWSBLUR_PATH)
|
2012-05-21 09:40:44 -07:00
|
|
|
|
|
|
|
def copy_certificates():
|
|
|
|
run('mkdir -p %s/config/certificates/' % env.NEWSBLUR_PATH)
|
2013-03-16 17:58:34 -07:00
|
|
|
put('../secrets-newsblur/certificates/comodo/newsblur.com.crt', '%s/config/certificates/' % env.NEWSBLUR_PATH)
|
|
|
|
put('../secrets-newsblur/certificates/comodo/newsblur.com.key', '%s/config/certificates/' % env.NEWSBLUR_PATH)
|
|
|
|
put('../secrets-newsblur/certificates/comodo/EssentialSSLCA_2.crt', '%s/config/certificates/intermediate.crt' % env.NEWSBLUR_PATH)
|
2012-06-26 11:19:53 -07:00
|
|
|
|
2012-12-25 15:09:46 -08:00
|
|
|
@parallel
|
2012-06-26 11:19:53 -07:00
|
|
|
def maintenance_on():
|
2012-12-21 15:05:38 -08:00
|
|
|
put('templates/maintenance_off.html', '%s/templates/maintenance_off.html' % env.NEWSBLUR_PATH)
|
2012-06-26 11:19:53 -07:00
|
|
|
with cd(env.NEWSBLUR_PATH):
|
2012-12-21 15:05:38 -08:00
|
|
|
run('mv templates/maintenance_off.html templates/maintenance_on.html')
|
2012-12-25 15:09:46 -08:00
|
|
|
|
2013-03-20 22:00:03 -07:00
|
|
|
@parallel
|
2012-06-26 11:19:53 -07:00
|
|
|
def maintenance_off():
|
2013-03-20 22:00:03 -07:00
|
|
|
with cd(env.NEWSBLUR_PATH):
|
2013-03-20 16:06:02 -07:00
|
|
|
run('mv templates/maintenance_on.html templates/maintenance_off.html')
|
2012-12-21 15:05:38 -08:00
|
|
|
run('git checkout templates/maintenance_off.html')
|
2013-03-14 21:40:14 -07:00
|
|
|
|
2013-03-17 10:28:26 -07:00
|
|
|
def setup_haproxy():
|
|
|
|
sudo('ufw allow 81') # nginx moved
|
|
|
|
with cd(env.VENDOR_PATH):
|
|
|
|
run('wget http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev17.tar.gz')
|
|
|
|
run('tar -xf haproxy-1.5-dev17.tar.gz')
|
|
|
|
with cd('haproxy-1.5-dev17'):
|
|
|
|
run('make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1')
|
|
|
|
sudo('make install')
|
2013-03-14 21:40:14 -07:00
|
|
|
put('config/haproxy-init', '/etc/init.d/haproxy', use_sudo=True)
|
|
|
|
sudo('chmod u+x /etc/init.d/haproxy')
|
2013-03-20 15:12:56 -07:00
|
|
|
put('../secrets-newsblur/configs/haproxy.conf', '/etc/haproxy/haproxy.cfg', use_sudo=True)
|
2013-03-14 22:39:58 -07:00
|
|
|
sudo('echo "ENABLED=1" > /etc/default/haproxy')
|
2013-03-17 10:28:26 -07:00
|
|
|
cert_path = "%s/config/certificates" % env.NEWSBLUR_PATH
|
2013-03-14 22:39:58 -07:00
|
|
|
run('cat %s/newsblur.com.crt > %s/newsblur.pem' % (cert_path, cert_path))
|
|
|
|
run('cat %s/intermediate.crt >> %s/newsblur.pem' % (cert_path, cert_path))
|
|
|
|
run('cat %s/newsblur.com.key >> %s/newsblur.pem' % (cert_path, cert_path))
|
2013-03-17 12:39:05 -07:00
|
|
|
put('config/haproxy_rsyslog.conf', '/etc/rsyslog.d/49-haproxy.conf', use_sudo=True)
|
|
|
|
sudo('restart rsyslog')
|
|
|
|
|
2013-03-14 22:39:58 -07:00
|
|
|
sudo('/etc/init.d/haproxy stop')
|
|
|
|
sudo('/etc/init.d/haproxy start')
|
2013-03-14 21:40:14 -07:00
|
|
|
|
2013-03-17 10:28:26 -07:00
|
|
|
def config_haproxy(debug=False):
|
|
|
|
if debug:
|
|
|
|
put('config/debug_haproxy.conf', '/etc/haproxy/haproxy.cfg', use_sudo=True)
|
|
|
|
else:
|
2013-03-20 15:12:56 -07:00
|
|
|
put('../secrets-newsblur/configs/haproxy.conf', '/etc/haproxy/haproxy.cfg', use_sudo=True)
|
2013-03-15 11:11:47 -07:00
|
|
|
sudo('/etc/init.d/haproxy reload')
|
2013-03-20 15:43:35 -07:00
|
|
|
|
|
|
|
def upgrade_django():
|
|
|
|
with cd(env.NEWSBLUR_PATH), settings(warn_only=True):
|
2013-03-20 16:13:10 -07:00
|
|
|
sudo('supervisorctl stop gunicorn')
|
2013-03-20 15:43:35 -07:00
|
|
|
run('./utils/kill_gunicorn.sh')
|
2013-03-20 16:13:10 -07:00
|
|
|
sudo('easy_install -U django gunicorn')
|
2013-03-20 15:43:35 -07:00
|
|
|
pull()
|
2013-03-20 16:13:10 -07:00
|
|
|
sudo('supervisorctl reload')
|
2013-03-20 15:43:35 -07:00
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
# ==============
|
|
|
|
# = Setup - DB =
|
|
|
|
# ==============
|
|
|
|
|
2013-03-14 19:26:22 -07:00
|
|
|
@parallel
|
2011-03-19 18:35:44 -04:00
|
|
|
def setup_db_firewall():
|
2012-12-17 18:42:55 -08:00
|
|
|
ports = [
|
|
|
|
5432, # PostgreSQL
|
|
|
|
27017, # MongoDB
|
2013-01-07 10:48:42 -08:00
|
|
|
28017, # MongoDB web
|
2012-12-17 18:42:55 -08:00
|
|
|
6379, # Redis
|
|
|
|
11211, # Memcached
|
|
|
|
3060, # Node original page server
|
2013-01-07 10:48:42 -08:00
|
|
|
9200, # Elasticsearch
|
2012-12-17 18:42:55 -08:00
|
|
|
]
|
2011-03-19 18:35:44 -04:00
|
|
|
sudo('ufw default deny')
|
2011-09-07 22:35:20 -07:00
|
|
|
sudo('ufw allow ssh')
|
|
|
|
sudo('ufw allow 80')
|
2013-01-07 10:48:42 -08:00
|
|
|
|
2013-01-07 11:33:24 -08:00
|
|
|
sudo('ufw allow proto tcp from 199.15.248.0/21 to any port %s ' % ','.join(map(str, ports)))
|
2013-03-14 12:41:24 -07:00
|
|
|
|
|
|
|
# DigitalOcean
|
2013-03-20 15:05:52 -07:00
|
|
|
for ip in set(env.roledefs['app'] + env.roledefs['dbdo']):
|
2013-03-18 13:35:11 -07:00
|
|
|
if 'newsblur.com' in ip: continue
|
2013-03-14 12:41:24 -07:00
|
|
|
sudo('ufw allow proto tcp from %s to any port %s' % (
|
|
|
|
ip,
|
|
|
|
','.join(map(str, ports))
|
|
|
|
))
|
|
|
|
|
2012-09-18 13:08:25 -07:00
|
|
|
# EC2
|
2013-03-15 17:05:44 -07:00
|
|
|
for host in set(env.roledefs['ec2task']):
|
2013-01-07 11:33:24 -08:00
|
|
|
ip = re.search('ec2-(\d+-\d+-\d+-\d+)', host).group(1).replace('-', '.')
|
2012-12-17 18:42:55 -08:00
|
|
|
sudo('ufw allow proto tcp from %s to any port %s' % (
|
2013-01-07 11:33:24 -08:00
|
|
|
ip,
|
|
|
|
','.join(map(str, ports))
|
2012-12-17 18:42:55 -08:00
|
|
|
))
|
2013-01-07 10:48:42 -08:00
|
|
|
|
2011-08-24 21:41:44 -07:00
|
|
|
sudo('ufw --force enable')
|
2011-03-19 18:35:44 -04:00
|
|
|
|
2011-03-17 19:26:21 -04:00
|
|
|
def setup_db_motd():
|
|
|
|
put('config/motd_db.txt', '/etc/motd.tail', use_sudo=True)
|
2011-03-15 10:02:13 -04:00
|
|
|
|
|
|
|
def setup_rabbitmq():
|
2011-03-19 19:24:14 -04:00
|
|
|
sudo('echo "deb http://www.rabbitmq.com/debian/ testing main" >> /etc/apt/sources.list')
|
|
|
|
run('wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc')
|
|
|
|
sudo('apt-key add rabbitmq-signing-key-public.asc')
|
|
|
|
run('rm rabbitmq-signing-key-public.asc')
|
|
|
|
sudo('apt-get update')
|
2011-03-15 10:02:13 -04:00
|
|
|
sudo('apt-get install -y rabbitmq-server')
|
2011-03-17 19:26:21 -04:00
|
|
|
sudo('rabbitmqctl add_user newsblur newsblur')
|
|
|
|
sudo('rabbitmqctl add_vhost newsblurvhost')
|
|
|
|
sudo('rabbitmqctl set_permissions -p newsblurvhost newsblur ".*" ".*" ".*"')
|
2011-03-15 10:02:13 -04:00
|
|
|
|
2011-11-08 14:46:30 -08:00
|
|
|
def setup_memcached():
|
|
|
|
sudo('apt-get -y install memcached')
|
|
|
|
|
2012-05-15 15:25:01 -07:00
|
|
|
def setup_postgres(standby=False):
|
2013-03-15 17:05:44 -07:00
|
|
|
shmmax = 599585856
|
2012-06-26 11:19:53 -07:00
|
|
|
sudo('apt-get -y install postgresql postgresql-client postgresql-contrib libpq-dev')
|
2012-05-15 15:25:01 -07:00
|
|
|
put('config/postgresql%s.conf' % (
|
|
|
|
('_standby' if standby else ''),
|
2012-06-26 11:19:53 -07:00
|
|
|
), '/etc/postgresql/9.1/main/postgresql.conf', use_sudo=True)
|
2012-05-15 15:25:01 -07:00
|
|
|
sudo('echo "%s" > /proc/sys/kernel/shmmax' % shmmax)
|
|
|
|
sudo('echo "\nkernel.shmmax = %s" > /etc/sysctl.conf' % shmmax)
|
2012-05-04 13:37:01 -07:00
|
|
|
sudo('sysctl -p')
|
2012-05-15 15:25:01 -07:00
|
|
|
|
|
|
|
if standby:
|
2012-06-26 11:19:53 -07:00
|
|
|
put('config/postgresql_recovery.conf', '/var/lib/postgresql/9.1/recovery.conf', use_sudo=True)
|
2012-05-15 15:25:01 -07:00
|
|
|
|
2012-05-04 13:37:01 -07:00
|
|
|
sudo('/etc/init.d/postgresql stop')
|
|
|
|
sudo('/etc/init.d/postgresql start')
|
2011-03-15 18:06:24 -04:00
|
|
|
|
2012-07-20 01:26:56 -07:00
|
|
|
def copy_postgres_to_standby():
|
2013-03-14 18:43:01 -07:00
|
|
|
slave = 'db12.newsblur.com'
|
2012-07-20 01:26:56 -07:00
|
|
|
# Make sure you can ssh from master to slave and back.
|
|
|
|
# Need to give postgres accounts keys in authroized_keys.
|
|
|
|
|
2012-10-29 14:58:43 -07:00
|
|
|
# sudo('su postgres -c "psql -c \\"SELECT pg_start_backup(\'label\', true)\\""', pty=False)
|
2012-07-20 01:26:56 -07:00
|
|
|
sudo('su postgres -c \"rsync -a --stats --progress /var/lib/postgresql/9.1/main postgres@%s:/var/lib/postgresql/9.1/ --exclude postmaster.pid\"' % slave, pty=False)
|
2013-03-14 18:43:01 -07:00
|
|
|
# sudo('su postgres -c "psql -c \\"SELECT pg_stop_backup()\\""', pty=False)
|
2012-07-20 01:26:56 -07:00
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
def setup_mongo():
|
2011-03-15 10:02:13 -04:00
|
|
|
sudo('apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10')
|
2011-08-27 13:22:56 -07:00
|
|
|
# sudo('echo "deb http://downloads.mongodb.org/distros/ubuntu 10.10 10gen" >> /etc/apt/sources.list.d/10gen.list')
|
|
|
|
sudo('echo "deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen" >> /etc/apt/sources.list')
|
2011-03-15 10:02:13 -04:00
|
|
|
sudo('apt-get update')
|
2013-03-20 15:05:52 -07:00
|
|
|
sudo('apt-get -y install mongodb-10gen')
|
2012-09-18 13:08:25 -07:00
|
|
|
put('config/mongodb.%s.conf' % ('prod' if env.user != 'ubuntu' else 'ec2'),
|
|
|
|
'/etc/mongodb.conf', use_sudo=True)
|
2012-06-27 00:23:17 -07:00
|
|
|
sudo('/etc/init.d/mongodb restart')
|
2011-11-06 15:18:44 -08:00
|
|
|
|
|
|
|
def setup_redis():
|
2013-03-13 19:17:45 -07:00
|
|
|
redis_version = '2.6.11'
|
2011-11-06 15:18:44 -08:00
|
|
|
with cd(env.VENDOR_PATH):
|
2012-05-03 18:33:29 -07:00
|
|
|
run('wget http://redis.googlecode.com/files/redis-%s.tar.gz' % redis_version)
|
|
|
|
run('tar -xzf redis-%s.tar.gz' % redis_version)
|
|
|
|
run('rm redis-%s.tar.gz' % redis_version)
|
|
|
|
with cd(os.path.join(env.VENDOR_PATH, 'redis-%s' % redis_version)):
|
2011-11-18 10:13:39 -08:00
|
|
|
sudo('make install')
|
2011-11-06 15:18:44 -08:00
|
|
|
put('config/redis-init', '/etc/init.d/redis', use_sudo=True)
|
|
|
|
sudo('chmod u+x /etc/init.d/redis')
|
|
|
|
put('config/redis.conf', '/etc/redis.conf', use_sudo=True)
|
|
|
|
sudo('mkdir -p /var/lib/redis')
|
|
|
|
sudo('update-rc.d redis defaults')
|
2012-05-03 18:33:29 -07:00
|
|
|
sudo('/etc/init.d/redis stop')
|
2011-11-18 10:13:39 -08:00
|
|
|
sudo('/etc/init.d/redis start')
|
2012-03-05 09:24:32 -08:00
|
|
|
|
2012-12-24 20:24:45 -08:00
|
|
|
def setup_munin():
|
|
|
|
sudo('apt-get update')
|
|
|
|
sudo('apt-get install -y munin munin-node munin-plugins-extra spawn-fcgi')
|
|
|
|
put('config/munin.conf', '/etc/munin/munin.conf', use_sudo=True)
|
|
|
|
put('config/spawn_fcgi_munin_graph.conf', '/etc/init.d/spawn_fcgi_munin_graph', use_sudo=True)
|
|
|
|
sudo('chmod u+x /etc/init.d/spawn_fcgi_munin_graph')
|
|
|
|
sudo('/etc/init.d/spawn_fcgi_munin_graph start')
|
|
|
|
sudo('update-rc.d spawn_fcgi_munin_graph defaults')
|
|
|
|
|
|
|
|
|
2012-03-05 09:24:32 -08:00
|
|
|
def setup_db_munin():
|
2012-07-20 08:19:44 -07:00
|
|
|
sudo('cp -frs %s/config/munin/mongo* /etc/munin/plugins/' % env.NEWSBLUR_PATH)
|
|
|
|
sudo('cp -frs %s/config/munin/pg_* /etc/munin/plugins/' % env.NEWSBLUR_PATH)
|
2013-03-20 16:06:02 -07:00
|
|
|
with cd(env.VENDOR_PATH), settings(warn_only=True):
|
|
|
|
run('git clone git://github.com/samuel/python-munin.git')
|
2012-09-19 12:00:41 -07:00
|
|
|
with cd(os.path.join(env.VENDOR_PATH, 'python-munin')):
|
|
|
|
run('sudo python setup.py install')
|
2012-12-24 14:57:43 -08:00
|
|
|
sudo('/etc/init.d/munin-node restart')
|
2012-03-05 09:24:32 -08:00
|
|
|
|
2012-07-17 00:06:41 -07:00
|
|
|
def enable_celerybeat():
|
|
|
|
with cd(env.NEWSBLUR_PATH):
|
|
|
|
run('mkdir -p data')
|
|
|
|
put('config/supervisor_celerybeat.conf', '/etc/supervisor/conf.d/celerybeat.conf', use_sudo=True)
|
|
|
|
put('config/supervisor_celeryd_beat.conf', '/etc/supervisor/conf.d/celeryd_beat.conf', use_sudo=True)
|
2013-03-14 12:55:10 -07:00
|
|
|
put('config/supervisor_celeryd_beat_feeds.conf', '/etc/supervisor/conf.d/celeryd_beat_feeds.conf', use_sudo=True)
|
2012-07-17 00:06:41 -07:00
|
|
|
sudo('supervisorctl reread')
|
|
|
|
sudo('supervisorctl update')
|
2011-03-14 21:44:30 -04:00
|
|
|
|
2012-09-19 12:00:41 -07:00
|
|
|
def setup_db_mdadm():
|
|
|
|
sudo('apt-get -y install xfsprogs mdadm')
|
|
|
|
sudo('yes | mdadm --create /dev/md0 --level=0 -c256 --raid-devices=4 /dev/xvdf /dev/xvdg /dev/xvdh /dev/xvdi')
|
|
|
|
sudo('mkfs.xfs /dev/md0')
|
|
|
|
sudo('mkdir -p /srv/db')
|
|
|
|
sudo('mount -t xfs -o rw,nobarrier,noatime,nodiratime /dev/md0 /srv/db')
|
|
|
|
sudo('mkdir -p /srv/db/mongodb')
|
|
|
|
sudo('chown mongodb.mongodb /srv/db/mongodb')
|
|
|
|
sudo("echo 'DEVICE /dev/xvdf /dev/xvdg /dev/xvdh /dev/xvdi' | sudo tee -a /etc/mdadm/mdadm.conf")
|
|
|
|
sudo("mdadm --examine --scan | sudo tee -a /etc/mdadm/mdadm.conf")
|
|
|
|
sudo("echo '/dev/md0 /srv/db xfs rw,nobarrier,noatime,nodiratime,noauto 0 0' | sudo tee -a /etc/fstab")
|
|
|
|
sudo("sudo update-initramfs -u -v -k `uname -r`")
|
2012-12-17 18:42:55 -08:00
|
|
|
|
|
|
|
def setup_original_page_server():
|
|
|
|
setup_node()
|
2013-01-07 11:16:52 -08:00
|
|
|
sudo('mkdir -p /srv/originals')
|
|
|
|
sudo('chown sclay.sclay -R /srv/originals')
|
2012-12-17 18:42:55 -08:00
|
|
|
put('config/supervisor_node_original.conf',
|
|
|
|
'/etc/supervisor/conf.d/node_original.conf', use_sudo=True)
|
|
|
|
sudo('supervisorctl reread')
|
|
|
|
sudo('supervisorctl reload')
|
|
|
|
|
2012-12-20 16:19:27 -08:00
|
|
|
def setup_elasticsearch():
|
|
|
|
ES_VERSION = "0.20.1"
|
|
|
|
sudo('apt-get update')
|
|
|
|
sudo('apt-get install openjdk-7-jre -y')
|
2012-09-19 12:00:41 -07:00
|
|
|
|
2012-12-20 16:19:27 -08:00
|
|
|
with cd(env.VENDOR_PATH):
|
|
|
|
run('mkdir elasticsearch')
|
|
|
|
with cd(os.path.join(env.VENDOR_PATH, 'elasticsearch-%s' % ES_VERSION)):
|
|
|
|
run('wget http://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-%s.deb' % ES_VERSION)
|
|
|
|
sudo('dpkg -i elasticsearch-%s.deb' % ES_VERSION)
|
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
# ================
|
|
|
|
# = Setup - Task =
|
|
|
|
# ================
|
|
|
|
|
2011-09-07 22:35:20 -07:00
|
|
|
def setup_task_firewall():
|
|
|
|
sudo('ufw default deny')
|
|
|
|
sudo('ufw allow ssh')
|
|
|
|
sudo('ufw allow 80')
|
|
|
|
sudo('ufw --force enable')
|
|
|
|
|
2011-03-17 19:26:21 -04:00
|
|
|
def setup_task_motd():
|
|
|
|
put('config/motd_task.txt', '/etc/motd.tail', use_sudo=True)
|
2011-03-15 10:02:13 -04:00
|
|
|
|
2011-03-24 09:27:05 -04:00
|
|
|
def enable_celery_supervisor():
|
2011-03-17 19:26:21 -04:00
|
|
|
put('config/supervisor_celeryd.conf', '/etc/supervisor/conf.d/celeryd.conf', use_sudo=True)
|
2012-12-26 02:41:13 -08:00
|
|
|
sudo('supervisorctl reread')
|
|
|
|
sudo('supervisorctl update')
|
|
|
|
|
2013-03-18 13:14:37 -07:00
|
|
|
@parallel
|
2012-05-02 16:46:00 -07:00
|
|
|
def copy_task_settings():
|
2012-07-06 20:53:27 -07:00
|
|
|
with settings(warn_only=True):
|
2013-03-16 17:58:34 -07:00
|
|
|
put('../secrets-newsblur/settings/task_settings.py', '%s/local_settings.py' % env.NEWSBLUR_PATH)
|
2012-07-06 20:53:27 -07:00
|
|
|
run('echo "\nSERVER_NAME = \\\\"`hostname`\\\\"" >> %s/local_settings.py' % env.NEWSBLUR_PATH)
|
2012-05-02 16:46:00 -07:00
|
|
|
|
2013-03-14 18:43:01 -07:00
|
|
|
# =========================
|
|
|
|
# = Setup - Digital Ocean =
|
|
|
|
# =========================
|
|
|
|
|
2013-03-20 12:24:36 -07:00
|
|
|
def setup_do(name, size=2):
|
|
|
|
INSTANCE_SIZE = "%sGB" % size
|
2013-03-14 18:43:01 -07:00
|
|
|
IMAGE_NAME = "Ubuntu 12.04 x64 Server"
|
|
|
|
doapi = dop.client.Client(django_settings.DO_CLIENT_KEY, django_settings.DO_API_KEY)
|
|
|
|
sizes = dict((s.name, s.id) for s in doapi.sizes())
|
|
|
|
size_id = sizes[INSTANCE_SIZE]
|
|
|
|
ssh_key_id = doapi.all_ssh_keys()[0].id
|
|
|
|
region_id = doapi.regions()[0].id
|
|
|
|
images = dict((s.name, s.id) for s in doapi.images())
|
|
|
|
image_id = images[IMAGE_NAME]
|
|
|
|
instance = doapi.create_droplet(name=name,
|
|
|
|
size_id=size_id,
|
|
|
|
image_id=image_id,
|
|
|
|
region_id=region_id,
|
|
|
|
ssh_key_ids=[str(ssh_key_id)])
|
|
|
|
print "Booting droplet: %s/%s (size: %s)" % (instance.id, IMAGE_NAME, INSTANCE_SIZE)
|
|
|
|
|
|
|
|
instance = doapi.show_droplet(instance.id)
|
|
|
|
i = 0
|
|
|
|
while True:
|
|
|
|
if instance.status == 'active':
|
|
|
|
print "...booted: %s" % instance.ip_address
|
|
|
|
time.sleep(5)
|
|
|
|
break
|
|
|
|
elif instance.status == 'new':
|
|
|
|
print ".",
|
|
|
|
sys.stdout.flush()
|
|
|
|
instance = doapi.show_droplet(instance.id)
|
|
|
|
i += 1
|
|
|
|
time.sleep(i)
|
|
|
|
else:
|
|
|
|
print "!!! Error: %s" % instance.status
|
|
|
|
return
|
|
|
|
|
|
|
|
host = instance.ip_address
|
|
|
|
env.host_string = host
|
2013-03-14 20:38:24 -07:00
|
|
|
add_user_to_do()
|
2013-03-14 18:43:01 -07:00
|
|
|
|
2013-03-14 20:38:24 -07:00
|
|
|
def add_user_to_do():
|
2013-03-14 18:43:01 -07:00
|
|
|
env.user = "root"
|
|
|
|
with settings(warn_only=True):
|
|
|
|
run('useradd -m sclay')
|
|
|
|
setup_sudoers("sclay")
|
|
|
|
run('mkdir -p ~sclay/.ssh && chmod 700 ~sclay/.ssh')
|
|
|
|
run('rm -fr ~sclay/.ssh/id_dsa*')
|
|
|
|
run('ssh-keygen -t dsa -f ~sclay/.ssh/id_dsa -N ""')
|
|
|
|
run('touch ~sclay/.ssh/authorized_keys')
|
|
|
|
put("~/.ssh/id_dsa.pub", "authorized_keys")
|
|
|
|
run('echo `cat authorized_keys` >> ~sclay/.ssh/authorized_keys')
|
|
|
|
run('rm authorized_keys')
|
|
|
|
run('chown sclay.sclay -R ~sclay/.ssh')
|
|
|
|
env.user = "sclay"
|
|
|
|
|
2012-10-15 16:32:32 -07:00
|
|
|
# ===============
|
|
|
|
# = Setup - EC2 =
|
|
|
|
# ===============
|
2012-05-03 18:33:29 -07:00
|
|
|
|
2013-03-13 19:17:45 -07:00
|
|
|
def setup_ec2():
|
2012-10-15 17:34:08 -07:00
|
|
|
AMI_NAME = 'ami-834cf1ea' # Ubuntu 64-bit 12.04 LTS
|
2012-10-15 16:32:32 -07:00
|
|
|
# INSTANCE_TYPE = 'c1.medium'
|
2012-10-22 11:22:28 -07:00
|
|
|
INSTANCE_TYPE = 'c1.medium'
|
2012-10-15 16:32:32 -07:00
|
|
|
conn = EC2Connection(django_settings.AWS_ACCESS_KEY_ID, django_settings.AWS_SECRET_ACCESS_KEY)
|
|
|
|
reservation = conn.run_instances(AMI_NAME, instance_type=INSTANCE_TYPE,
|
|
|
|
key_name='sclay',
|
|
|
|
security_groups=['db-mongo'])
|
|
|
|
instance = reservation.instances[0]
|
|
|
|
print "Booting reservation: %s/%s (size: %s)" % (reservation, instance, INSTANCE_TYPE)
|
2012-10-22 16:25:36 -07:00
|
|
|
i = 0
|
2012-10-15 16:32:32 -07:00
|
|
|
while True:
|
|
|
|
if instance.state == 'pending':
|
|
|
|
print ".",
|
|
|
|
sys.stdout.flush()
|
|
|
|
instance.update()
|
2012-10-22 16:25:36 -07:00
|
|
|
i += 1
|
|
|
|
time.sleep(i)
|
2012-10-15 16:32:32 -07:00
|
|
|
elif instance.state == 'running':
|
|
|
|
print "...booted: %s" % instance.public_dns_name
|
|
|
|
time.sleep(5)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print "!!! Error: %s" % instance.state
|
|
|
|
return
|
|
|
|
|
|
|
|
host = instance.public_dns_name
|
|
|
|
env.host_string = host
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-03 18:33:29 -07:00
|
|
|
# ==============
|
|
|
|
# = Tasks - DB =
|
|
|
|
# ==============
|
|
|
|
|
2012-08-17 23:29:17 -07:00
|
|
|
def restore_postgres(port=5432):
|
|
|
|
backup_date = '2012-08-17-08-00'
|
|
|
|
# run('PYTHONPATH=%s python utils/backups/s3.py get backup_postgresql_%s.sql.gz' % (env.NEWSBLUR_PATH, backup_date))
|
|
|
|
# sudo('su postgres -c "createuser -p %s -U newsblur"' % (port,))
|
|
|
|
sudo('su postgres -c "createdb newsblur -p %s -O newsblur"' % (port,))
|
|
|
|
sudo('su postgres -c "pg_restore -p %s --role=newsblur --dbname=newsblur backup_postgresql_%s.sql.gz"' % (port, backup_date))
|
2012-05-03 18:33:29 -07:00
|
|
|
|
2012-07-24 23:36:20 -07:00
|
|
|
def restore_mongo():
|
|
|
|
backup_date = '2012-07-24-09-00'
|
2012-09-18 13:08:25 -07:00
|
|
|
run('PYTHONPATH=/home/%s/newsblur python s3.py get backup_mongo_%s.tgz' % (env.user, backup_date))
|
2012-07-24 23:36:20 -07:00
|
|
|
run('tar -xf backup_mongo_%s.tgz' % backup_date)
|
|
|
|
run('mongorestore backup_mongo_%s' % backup_date)
|
2012-05-03 18:33:29 -07:00
|
|
|
|
2010-12-15 22:26:05 -05:00
|
|
|
# ======
|
|
|
|
# = S3 =
|
|
|
|
# ======
|
2010-09-08 18:30:33 -07:00
|
|
|
|
2011-07-17 11:31:07 -07:00
|
|
|
if django_settings:
|
2011-07-17 15:30:25 -07:00
|
|
|
try:
|
|
|
|
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
|
|
|
|
except:
|
|
|
|
print " ---> You need to fix django's settings. Enter python and type `import settings`."
|
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)
|
2013-03-20 12:24:36 -07:00
|
|
|
key.delete()
|
|
|
|
|
|
|
|
def add_revsys_keys():
|
|
|
|
put("~/Downloads/revsys-keys.pub", "revsys_keys")
|
|
|
|
run('cat revsys_keys >> ~/.ssh/authorized_keys')
|
|
|
|
run('rm revsys_keys')
|