readthedocs.org/fabfile.py

215 lines
6.4 KiB
Python
Raw Normal View History

2013-04-07 02:40:30 +00:00
from fabric.api import cd, env, lcd, local, hosts, prompt, run
from fabric.decorators import runs_once
2013-10-24 00:46:17 +00:00
import os
import time
2010-08-28 19:21:36 +00:00
env.runtime = 'production'
2013-09-04 20:19:02 +00:00
env.hosts = ['newchimera.readthedocs.com',
'newbuild.readthedocs.com',
'newasgard.readthedocs.com']
2010-08-28 19:21:36 +00:00
env.user = 'docs'
2012-12-18 05:15:50 +00:00
env.code_dir = '/home/docs/checkouts/readthedocs.org'
env.virtualenv = '/home/docs/'
env.rundir = '/home/docs/run'
2010-08-28 19:21:36 +00:00
2013-10-24 00:46:17 +00:00
fabfile_dir = os.path.dirname(__file__)
2013-04-07 02:40:30 +00:00
2013-09-04 20:19:02 +00:00
@hosts(['newchimera.readthedocs.com', 'newasgard.readthedocs.com'])
2012-10-20 19:42:21 +00:00
def remove_project(project):
run('rm -rf %s/user_builds/%s' % (env.code_dir, project))
def ntpdate():
run('ntpdate-debian')
2013-04-07 02:40:30 +00:00
2013-10-27 21:22:47 +00:00
@hosts(['newasgard.readthedocs.com', 'newchimera.readthedocs.com'])
2012-10-18 03:17:51 +00:00
def nginx_logs():
env.user = "root"
run("tail -f /var/log/nginx/*.log")
2013-10-27 21:22:47 +00:00
@hosts(['newbuild.readthedocs.com'])
def celery_logs():
env.user = "docs"
run("tail -f tail -f ~/log/celery.err")
2013-10-27 21:05:00 +00:00
@hosts(['newasgard.readthedocs.com', 'newchimera.readthedocs.com'])
def logs():
env.user = "docs"
run("tail -f %s/logs/*.log" % env.code_dir)
2013-04-07 02:40:30 +00:00
2012-10-14 00:32:10 +00:00
@hosts(['localhost'])
2012-10-14 00:19:26 +00:00
def i18n():
with lcd('readthedocs'):
2012-10-14 05:57:12 +00:00
local('rm -rf rtd_tests/tests/builds/')
2012-10-14 00:19:26 +00:00
local('tx pull')
2012-10-14 05:57:12 +00:00
local('./manage.py makemessages --all')
2012-10-15 23:48:38 +00:00
local('tx push -s')
2012-10-14 00:19:26 +00:00
local('./manage.py compilemessages')
2013-04-07 02:40:30 +00:00
2010-08-28 19:21:36 +00:00
def push():
"Push new code, but don't restart/reload."
local('git push origin master')
with cd(env.code_dir):
2011-09-19 02:22:57 +00:00
run('git fetch')
2012-02-24 04:24:35 +00:00
run('git reset --hard origin/master')
2010-08-28 19:21:36 +00:00
2013-04-07 02:40:30 +00:00
def update_requirements():
"Update requirements in the virtualenv."
2013-10-27 03:53:08 +00:00
run(("%s/bin/pip install -i http://simple.crate.io/ -r "
2013-04-07 02:40:30 +00:00
"%s/deploy_requirements.txt") % (env.virtualenv, env.code_dir))
2013-09-04 20:19:02 +00:00
@hosts(['newchimera.readthedocs.com'])
def migrate(project=None):
if project:
run('django-admin.py migrate %s' % project)
else:
run('django-admin.py migrate')
2011-01-08 22:36:38 +00:00
2013-09-04 20:19:02 +00:00
@hosts(['newchimera.readthedocs.com'])
def syncdb(project=None):
run('django-admin.py syncdb')
2013-04-07 02:40:30 +00:00
2013-09-04 20:19:02 +00:00
@hosts(['newchimera.readthedocs.com', 'newasgard.readthedocs.com'])
2012-10-18 03:14:15 +00:00
def static():
"Restart (or just start) the server"
run('django-admin.py collectstatic --noinput')
2013-04-07 02:40:30 +00:00
2013-09-04 20:19:02 +00:00
@hosts(['newchimera.readthedocs.com', 'newasgard.readthedocs.com'])
2010-08-28 19:21:36 +00:00
def restart():
"Restart (or just start) the server"
2012-12-18 05:18:41 +00:00
env.user = "docs"
run("supervisorctl restart web")
#so it has time to reload
time.sleep(3)
2010-08-28 22:47:47 +00:00
2013-04-07 02:40:30 +00:00
2013-09-04 20:19:02 +00:00
@hosts(['newchimera.readthedocs.com', 'newasgard.readthedocs.com'])
def reload():
"Reload (or just start) the server"
2012-12-18 05:18:41 +00:00
run("supervisorctl update")
2013-04-07 02:40:30 +00:00
2013-09-04 20:19:02 +00:00
@hosts(['newbuild.readthedocs.com'])
2011-01-10 02:36:43 +00:00
def celery():
"Restart (or just start) the server"
2012-12-18 05:18:41 +00:00
run("supervisorctl restart celery")
2011-01-10 02:36:43 +00:00
2013-04-07 02:40:30 +00:00
def pull():
"Pull new code"
with cd(env.code_dir):
run('git pull origin master')
2013-04-07 02:40:30 +00:00
@runs_once
def spider():
local('patu.py -d1 readthedocs.org')
2013-04-07 02:40:30 +00:00
def _aws_wrapper(f, *args, **kwargs):
"get AWS credentials if not defined"
#these are normally defined in ~/.fabricrc
2013-04-07 02:40:30 +00:00
@hosts('run_once') # so fab doesn't go crazy
def wrapped(*args, **kwargs):
from boto.cloudfront.exception import CloudFrontServerError
from boto.cloudfront import CloudFrontConnection
2011-09-19 00:46:51 +00:00
c = CloudFrontConnection(env.aws_access_key_id,
env.aws_secret_access_key)
if not hasattr(env, 'aws_access_key_id'):
prompt('AWS Access Key ID: ', key='aws_access_key_id')
if not hasattr(env, 'aws_secret_access_key'):
prompt('AWS Secret Access Key: ', key='aws_secret_access_key')
try:
return f(c, *args, **kwargs)
except CloudFrontServerError as e:
print "Error: \n", e.error_message
return wrapped
2013-04-07 02:40:30 +00:00
@_aws_wrapper
def to_cdn(c, slug):
"Create a new Distribution object on CloudFront"
from boto.cloudfront import CloudFrontConnection
from boto.cloudfront.origin import CustomOrigin
2011-09-19 00:46:51 +00:00
c = CloudFrontConnection(env.aws_access_key_id,
env.aws_secret_access_key)
d = c.create_distribution(
origin=CustomOrigin(slug + '.cdn.readthedocs.org',
origin_protocol_policy='http-only'),
2011-09-19 00:46:51 +00:00
enabled=True,
comment='Slug: ' + slug,
cnames=[slug + '.readthedocs.org']
2013-04-07 02:40:30 +00:00
)
print "Created: " + d.domain_name + " for " + slug
list_cdn()
2013-04-07 02:40:30 +00:00
@_aws_wrapper
def list_cdn(c):
"List Distributions on CloudFront"
distributions = c.get_all_distributions()
for d in distributions:
2011-09-19 00:46:51 +00:00
print "%3s %4s %40s %30s" % ('Ena' if d.enabled else 'Dis',
d.status[:4], d.origin.dns_name,
d.domain_name)
2013-04-07 02:40:30 +00:00
@_aws_wrapper
def disable_cdn(c, *args):
"Sets a Distribution entry to disabled. Required before deletion."
distributions = c.get_all_distributions()
for distro in distributions:
dist_slug = distro.origin.dns_name.split('.')[0]
if dist_slug in args:
print "Disabling:", dist_slug
#this is broken as of boto 2.0b4.
#fix is to comment out lines 347-352 in cloudfront/distribution.py
distro.get_distribution().disable()
2013-04-07 02:40:30 +00:00
@_aws_wrapper
def delete_cdn(c):
"Deletes all Distributions in the 'Disabled' state."
distributions = c.get_all_distributions()
for distro in distributions:
2013-04-07 02:40:30 +00:00
if not distro.enabled and distro.status == "Deployed":
print "Deleting", distro.origin.dns_name
distro.get_distribution().delete()
def full_deploy():
2011-09-19 00:50:01 +00:00
#HACK
#Call this again at the top-level so the hosts decorator
#effects the hosts it runs against for each command.
run('fab push update_requirements migrate restart celery')
#push()
#update_requirements()
#migrate()
#restart()
#celery()
2011-06-19 23:52:26 +00:00
2013-04-07 02:40:30 +00:00
2013-09-04 20:19:02 +00:00
@hosts(['newchimera.readthedocs.com'])
2011-06-19 23:52:26 +00:00
def uptime():
run('uptime')
2011-09-19 02:22:57 +00:00
2013-04-07 02:40:30 +00:00
2013-09-04 20:19:02 +00:00
@hosts(['newchimera.readthedocs.com'])
2011-09-19 02:22:57 +00:00
def update_index():
run('django-admin.py update_index')
2013-10-24 00:46:17 +00:00
@hosts('None')
def update_theme():
with lcd(os.path.join(fabfile_dir, 'readthedocs', 'templates', 'sphinx')):
local('rm -rf theme_update_dir')
local('git clone https://github.com/snide/sphinx_rtd_theme.git theme_update_dir')
2013-10-24 00:46:17 +00:00
local('rm -rf sphinx_rtd_theme')
local('mv theme_update_dir/sphinx_rtd_theme .')
local('cp sphinx_rtd_theme/static/theme.css %s' % os.path.join(fabfile_dir, 'media', 'css', 'sphinx_rtd_theme.css'))
local('rm -rf theme_update_dir')