snipt/fabfile.py

146 lines
3.4 KiB
Python
Raw Normal View History

2013-04-14 20:35:05 -07:00
#!/usr/bin/env python
# -- coding: utf-8 --
2012-09-18 16:22:55 -07:00
from fabric.api import cd, local, env, run, sudo
2011-10-23 20:13:37 -07:00
2012-04-29 13:25:56 -07:00
from boto.s3.connection import S3Connection
from boto.s3.key import Key
2013-04-14 20:35:05 -07:00
import datetime, hashlib, sys
2012-04-26 19:49:43 -07:00
2013-03-25 18:41:29 -07:00
from settings_local import AMAZON_API_KEY, AMAZON_API_SECRET, ENV_HOST
2011-10-23 20:07:59 -07:00
2012-09-18 13:45:00 -07:00
env.hosts = [ENV_HOST]
2012-03-05 13:48:03 -08:00
env.site_path = '/var/www/snipt'
2012-03-05 13:57:18 -08:00
env.venv_path = '/home/nick/.virtualenvs/snipt'
2012-01-25 07:20:52 -08:00
2013-04-14 20:35:05 -07:00
def _display_message(message, extra_line=True):
if extra_line:
msg = '\n{}\n========================\n\n'.format(message)
else:
msg = '{}\n========================\n\n'.format(message)
try:
from fabric.colors import cyan
sys.stderr.write(cyan(msg))
except ImportError:
print(msg)
2012-03-05 13:57:18 -08:00
def _python(cmd):
return env.venv_path.rstrip('/') + '/bin/python ' + cmd
2013-04-14 20:35:05 -07:00
2012-04-29 08:03:17 -07:00
def dep():
2012-04-29 08:28:27 -07:00
2013-02-18 06:37:22 -08:00
_display_message('Collect static (local)')
2012-04-29 08:28:27 -07:00
################
2012-06-05 12:47:07 -07:00
local('python manage.py collectstatic --ignore cache --noinput')
2012-03-05 13:57:18 -08:00
2012-04-29 08:28:27 -07:00
_display_message('Git push')
################
2012-04-07 16:17:27 -07:00
try:
2012-04-29 08:28:27 -07:00
local('git push')
_display_message('Get last commit info')
################
2012-04-07 16:17:27 -07:00
except:
pass
2012-03-05 13:57:18 -08:00
2012-04-29 08:28:27 -07:00
print('')
2012-03-05 13:48:03 -08:00
with cd(env.site_path):
2012-04-29 08:28:27 -07:00
_display_message('Git pull')
################
2012-04-29 08:05:07 -07:00
run('git pull')
2012-04-29 08:28:27 -07:00
2013-02-18 06:37:22 -08:00
_display_message('Collect static (remote)', False)
2012-04-29 08:28:27 -07:00
################
2012-06-05 12:47:07 -07:00
run(_python('manage.py collectstatic --ignore cache --noinput'))
2012-03-05 13:57:18 -08:00
2013-04-14 20:35:05 -07:00
def db_backup():
filename = datetime.datetime.now().strftime('%h-%d-%y__%I-%M-%S_%p.pgdump')
local('pg_dump snipt > {}'.format(filename))
conn = S3Connection(AMAZON_API_KEY, AMAZON_API_SECRET)
snipt_bucket = conn.get_bucket('snipt')
k = Key(snipt_bucket)
k.key = filename
k.set_contents_from_filename(filename)
local('rm {}'.format(filename))
2012-05-24 20:21:56 -07:00
def db():
with cd(env.site_path):
_display_message('Sync DB and migrate')
################
run(_python('manage.py syncdb'))
run(_python('manage.py migrate'))
2013-04-14 20:35:05 -07:00
def gravatars():
2012-04-29 08:28:27 -07:00
2013-04-14 20:35:05 -07:00
from fabric.contrib import django
django.settings_module('settings')
2012-04-29 08:28:27 -07:00
2013-04-14 20:35:05 -07:00
from django.contrib.auth.models import User
2012-04-29 08:28:27 -07:00
2013-04-14 20:35:05 -07:00
import requests
_display_message('Updating all users\' Gravatar flags')
################
for user in User.objects.all().order_by('id'):
_display_message('{}. {}'.format(user.pk, user.username.encode('ascii', 'ignore')))
2012-04-29 08:28:27 -07:00
################
2013-04-14 20:35:05 -07:00
email_md5 = hashlib.md5(user.email.lower()).hexdigest()
2012-04-26 19:49:43 -07:00
2013-04-14 20:35:05 -07:00
print 'Email MD5: {}'.format(email_md5)
2012-04-26 19:49:43 -07:00
greq = requests.get('https://secure.gravatar.com/avatar/{}?s=50&d=404'.format(email_md5))
2012-04-26 19:49:43 -07:00
if greq.status_code == 404:
has_gravatar = False
2013-04-14 20:35:05 -07:00
else:
has_gravatar = True
2012-04-29 13:25:56 -07:00
profile = user.profile
profile.has_gravatar = has_gravatar
profile.save()
2012-04-29 08:28:27 -07:00
2013-04-14 20:35:05 -07:00
try:
from fabric.colors import green, red
if has_gravatar:
print 'Has Gravatar: {}'.format(green(has_gravatar))
else:
print 'Has Gravatar: {}'.format(red(has_gravatar))
except ImportError:
print 'Has Gravatar: {}'.format(has_gravatar)
def re():
with cd(env.site_path):
_display_message('Kill gunicorn process')
################
sudo('supervisorctl stop snipt')
_display_message('Restart gunicorn process')
################
sudo('supervisorctl start snipt')
2012-04-29 08:28:27 -07:00