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 =
|
|
|
|
# =========
|
|
|
|
|
2011-03-21 20:07:24 -04:00
|
|
|
env.user = 'sclay'
|
2010-07-30 23:50:49 -04:00
|
|
|
env.roledefs ={
|
2011-04-01 19:40:02 -04:00
|
|
|
'app': ['app01.newsblur.com'],
|
2011-04-03 10:54:20 -04:00
|
|
|
'web': ['www.newsblur.com'],
|
2011-03-27 20:56:21 -04:00
|
|
|
'db': ['db01.newsblur.com', 'db02.newsblur.com', 'db03.newsblur.com'],
|
2011-03-28 10:10:01 -04:00
|
|
|
'task': ['task01.newsblur.com', 'task02.newsblur.com'],
|
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
|
|
|
|
2010-12-16 13:54:09 -05:00
|
|
|
def app():
|
|
|
|
env.roles = ['app']
|
2011-04-03 10:54:20 -04:00
|
|
|
def web():
|
|
|
|
env.roles = ['web']
|
2010-12-16 13:54:09 -05:00
|
|
|
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 =
|
|
|
|
# ==========
|
|
|
|
|
2011-04-03 10:54:20 -04:00
|
|
|
@roles('web')
|
2010-12-16 13:54:09 -05:00
|
|
|
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`')
|
2011-04-03 11:08:42 -04:00
|
|
|
run('curl -s http://www.newsblur.com > /dev/null')
|
|
|
|
with cd('media/js'):
|
2011-04-07 17:00:28 -04:00
|
|
|
run('rm -f *.gz')
|
2011-04-03 11:08:42 -04:00
|
|
|
run('for js in *-compressed-*.js; do gzip -9 $js -c > $js.gz; done;')
|
|
|
|
with cd('media/css'):
|
2011-04-07 17:00:28 -04:00
|
|
|
run('rm -f *.gz')
|
2011-04-03 11:08:42 -04:00
|
|
|
run('for css in *-compressed-*.css; do gzip -9 $css -c > $css.gz; done;')
|
2010-12-16 13:54:09 -05:00
|
|
|
|
2011-04-03 10:54:20 -04:00
|
|
|
@roles('web')
|
2011-01-15 18:43:09 -05:00
|
|
|
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
|
|
|
|
2011-04-03 10:54:20 -04:00
|
|
|
@roles('web')
|
2010-12-16 16:25:13 -05:00
|
|
|
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-04-03 10:54:20 -04:00
|
|
|
@roles('web')
|
2011-01-15 18:43:09 -05:00
|
|
|
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')
|
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-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
|
|
|
|
|
|
|
@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-24 09:27:05 -04:00
|
|
|
def setup_common():
|
|
|
|
setup_installs()
|
|
|
|
setup_user()
|
|
|
|
setup_repo()
|
|
|
|
setup_local_files()
|
|
|
|
setup_libxml()
|
|
|
|
setup_python()
|
|
|
|
setup_supervisor()
|
|
|
|
setup_hosts()
|
|
|
|
config_pgbouncer()
|
|
|
|
setup_mongoengine()
|
|
|
|
setup_forked_mongoengine()
|
|
|
|
setup_pymongo_repo()
|
|
|
|
setup_logrotate()
|
|
|
|
setup_sudoers()
|
|
|
|
setup_nginx()
|
2011-03-31 18:51:23 -04:00
|
|
|
configure_nginx()
|
2011-03-24 09:27:05 -04:00
|
|
|
|
2011-02-09 15:45:41 -05:00
|
|
|
def setup_app():
|
|
|
|
setup_common()
|
2011-03-17 19:26:21 -04:00
|
|
|
setup_app_motd()
|
2011-02-09 15:45:41 -05:00
|
|
|
setup_gunicorn()
|
|
|
|
update_gunicorn()
|
|
|
|
|
|
|
|
def setup_db():
|
2011-03-15 10:02:13 -04:00
|
|
|
setup_common()
|
2011-03-19 18:35:44 -04:00
|
|
|
setup_db_firewall()
|
2011-03-17 19:26:21 -04:00
|
|
|
setup_db_motd()
|
2011-03-15 10:02:13 -04:00
|
|
|
setup_rabbitmq()
|
2011-02-09 15:45:41 -05:00
|
|
|
setup_postgres()
|
2011-03-14 21:44:30 -04:00
|
|
|
setup_mongo()
|
|
|
|
|
2011-02-09 15:45:41 -05:00
|
|
|
def setup_task():
|
2011-03-15 10:02:13 -04:00
|
|
|
setup_common()
|
2011-03-17 19:26:21 -04:00
|
|
|
setup_task_motd()
|
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()
|
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')
|
2011-03-28 10:07:45 -04:00
|
|
|
sudo('apt-get -y install build-essential gcc scons libreadline-dev 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 ntp monit')
|
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-03-15 10:02:13 -04:00
|
|
|
put('config/munin.conf', '/etc/munin/munin.conf', use_sudo=True)
|
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-15 10:02:13 -04:00
|
|
|
sudo('chsh sclay -s /bin/zsh')
|
2011-03-15 18:06:24 -04:00
|
|
|
|
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')
|
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")
|
|
|
|
run('mv authorized_keys ~/.ssh/')
|
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-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')
|
2011-03-19 18:35:44 -04:00
|
|
|
run('touch logs/newsblur.log')
|
2011-02-09 15:45:41 -05:00
|
|
|
|
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():
|
|
|
|
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-02-09 15:45:41 -05:00
|
|
|
|
2011-03-14 21:44:30 -04:00
|
|
|
def setup_python():
|
|
|
|
sudo('easy_install pip')
|
2011-03-15 10:02:13 -04:00
|
|
|
sudo('easy_install fabric django celery 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\'')
|
|
|
|
put('config/pystartup.py', '.pystartup')
|
2011-03-14 21:44:30 -04:00
|
|
|
|
|
|
|
def setup_supervisor():
|
|
|
|
sudo('apt-get -y install supervisor')
|
|
|
|
|
2011-03-15 18:06:24 -04:00
|
|
|
def setup_hosts():
|
|
|
|
put('config/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)
|
|
|
|
put('config/pgbouncer_userlist.txt', '/etc/pgbouncer/userlist.txt', use_sudo=True)
|
|
|
|
sudo('mkdir -p /var/run/postgresql')
|
|
|
|
sudo('chown postgres.postgres /var/run/postgresql')
|
|
|
|
sudo('echo "START=1" > /etc/default/pgbouncer')
|
|
|
|
|
2011-03-28 10:07:45 -04:00
|
|
|
def config_monit():
|
|
|
|
# sudo('apt-get install -y monit')
|
|
|
|
put('config/monit.conf', '/etc/monit/conf.d/celery.conf', use_sudo=True)
|
|
|
|
sudo('echo "startup=1" > /etc/default/monit')
|
|
|
|
sudo('/etc/init.d/monit restart')
|
|
|
|
|
2011-03-19 16:19:53 -04:00
|
|
|
def setup_mongoengine():
|
|
|
|
with cd('~/code'):
|
|
|
|
run('git clone https://github.com/hmarr/mongoengine.git')
|
|
|
|
sudo('ln -s ~/code/mongoengine/mongoengine /usr/local/lib/python2.6/dist-packages/mongoengine')
|
|
|
|
|
|
|
|
def setup_pymongo_repo():
|
|
|
|
with cd('~/code'):
|
|
|
|
run('git clone git://github.com/mongodb/mongo-python-driver.git pymongo')
|
|
|
|
with cd('~/code/pymongo'):
|
|
|
|
sudo('python setup.py install')
|
|
|
|
|
|
|
|
def setup_forked_mongoengine():
|
|
|
|
with cd('~/code/mongoengine'):
|
|
|
|
run('git remote add github http://github.com/samuelclay/mongoengine')
|
2011-03-23 15:43:15 -04:00
|
|
|
run('git checkout dev')
|
2011-03-19 16:19:53 -04:00
|
|
|
run('git pull github dev')
|
|
|
|
|
2011-03-23 15:43:15 -04:00
|
|
|
def setup_logrotate():
|
|
|
|
put('config/logrotate.conf', '/etc/logrotate.d/newsblur', use_sudo=True)
|
|
|
|
|
|
|
|
def setup_sudoers():
|
2011-03-23 17:18:57 -04:00
|
|
|
sudo('su - root -c "echo \\\\"sclay ALL=(ALL) NOPASSWD: ALL\\\\" >> /etc/sudoers"')
|
2011-02-09 15:45:41 -05:00
|
|
|
|
|
|
|
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')
|
2011-03-21 20:07:24 -04:00
|
|
|
with cd('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-21 20:07:24 -04:00
|
|
|
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")
|
|
|
|
put("config/newsblur.conf", "/usr/local/nginx/conf/sites-enabled/newsblur.conf", use_sudo=True)
|
|
|
|
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")
|
2011-03-24 09:27:05 -04:00
|
|
|
|
|
|
|
# ===============
|
|
|
|
# = Setup - App =
|
|
|
|
# ===============
|
|
|
|
|
|
|
|
def setup_app_motd():
|
|
|
|
put('config/motd_app.txt', '/etc/motd.tail', use_sudo=True)
|
|
|
|
|
|
|
|
def setup_gunicorn(supervisor=True):
|
|
|
|
if supervisor:
|
|
|
|
put('config/supervisor_gunicorn.conf', '/etc/supervisor/conf.d/gunicorn.conf', use_sudo=True)
|
|
|
|
with cd('~/code'):
|
|
|
|
sudo('rm -fr gunicorn')
|
|
|
|
run('git clone git://github.com/benoitc/gunicorn.git')
|
|
|
|
|
|
|
|
def update_gunicorn():
|
|
|
|
with cd('~/code/gunicorn'):
|
|
|
|
run('git pull')
|
|
|
|
sudo('python setup.py develop')
|
2011-03-14 21:44:30 -04:00
|
|
|
|
|
|
|
# ==============
|
|
|
|
# = Setup - DB =
|
|
|
|
# ==============
|
|
|
|
|
2011-03-19 18:35:44 -04:00
|
|
|
def setup_db_firewall():
|
|
|
|
sudo('ufw default deny')
|
2011-03-24 09:27:05 -04:00
|
|
|
sudo('ufw allow ssh') # SSH
|
|
|
|
sudo('ufw allow 5432') # PostgreSQL
|
|
|
|
sudo('ufw allow 27017') # MongoDB
|
|
|
|
sudo('ufw allow 5672') # RabbitMQ
|
2011-03-19 18:35:44 -04:00
|
|
|
sudo('ufw enable')
|
|
|
|
|
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-03-14 21:44:30 -04:00
|
|
|
def setup_postgres():
|
|
|
|
sudo('apt-get -y install postgresql-9.0 postgresql-client-9.0 postgresql-contrib-9.0 libpq-dev')
|
2011-03-15 18:06:24 -04: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-03-14 21:44:30 -04:00
|
|
|
sudo('echo "deb http://downloads.mongodb.org/distros/ubuntu 10.10 10gen" >> /etc/apt/sources.list.d/10gen.list')
|
2011-03-15 10:02:13 -04:00
|
|
|
sudo('apt-get update')
|
2011-03-14 21:44:30 -04:00
|
|
|
sudo('apt-get -y install mongodb')
|
|
|
|
|
|
|
|
# ================
|
|
|
|
# = Setup - Task =
|
|
|
|
# ================
|
|
|
|
|
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)
|
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()
|