Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fabric plugin for juju
# Mostly from django-fab-deploy
import os
import sys
from datetime import datetime
from subprocess import Popen, PIPE
import yaml
from fabric.api import env, run, sudo, task
from fabric.context_managers import cd
from fabric.contrib import files
# Initialisation
env.user = 'ubuntu'
d = yaml.safe_load(Popen(['juju','status'],stdout=PIPE).stdout)
services = d.get("services", {})
if services is None:
env.roledefs = {}
for service in services.items():
if service is None:
units = services.get(service[0], {}).get("units", {})
if units is None:
for unit in units.items():
if 'public-address' in unit[1].keys():
env.roledefs.setdefault(service[0], []).append(unit[1]['public-address'])
# helpers
def _config_get():
conf = Popen('jitsu run-as-hook %s/0 config-get' % (env.roles[0]),shell=True, stdout=PIPE).stdout
return eval(
def _relation_get(relation, relation_type):
res = Popen('jitsu run-as-hook %s/0 relation-ids %s' % (env.roles[0], relation_type),shell=True, stdout=PIPE).stdout
rel_id =
conf = Popen('jitsu run-as-hook %s/0 relation-get -r %s' % (relation, rel_id),shell=True, stdout=PIPE).stdout
return eval(
# Debian
def apt_install(packages):
sudo('apt-get install -y %s' % packages)
def apt_update():
sudo('apt-get update')
def apt_dist_upgrade():
sudo('apt-get dist-upgrade -y')
def apt_install_r():
sudo("apt-get install -y $(cat requirements.apt | tr '\\n' ' '")
def pip_install(packages):
sudo("pip install %s" % packages)
def pip_install_r():
sudo("pip install -r requirements.txt")
# Users
def adduser(username):
sudo('adduser %s --disabled-password --gecos ""' % username)
def ssh_add_key(pub_key_file, username=None):
with open(os.path.normpath(pub_key_file), 'rt') as f:
ssh_key =
if username is None:
run('mkdir -p .ssh')
files.append('.ssh/authorized_keys', ssh_key)
run('mkdir -p /home/%s/.ssh' % username)
files.append('/home/%s/.ssh/authorized_keys' % username, ssh_key)
run('chown -R %s:%s /home/%s/.ssh' % (username, username, username))
def pull():
conf = _config_get()
with cd('/srv/%s/' % env.roles[0]):
if conf['vcs'] is 'bzr':
run('bzr pull %s' % conf['repos_url'])
if conf['vcs'] is 'git':
run('git pull %s' % conf['repos_url'])
if conf['vcs'] is 'hg':
run('hg pull -u %s' % conf['repos_url'])
if conf['vcs'] is 'svn':
run('svn up %s' % conf['repos_url'])
# Gunicorn
def reload():
sudo('invoke-rc.d gunicorn reload')
# Django
def manage(command):
run('python ' + command)
def migrate(params='', do_backup=True):
if do_backup is True:
manage('migrate --noinput %s' % params)
def syncdb(params=''):
""" Runs syncdb management command. """
manage('syncdb --noinput %s' % params)
def collectstatic(params=''):
manage('collectstatic --noinput %s' % params)
# DB
def _get_dump_filename(backup_dir):
now ='%Y.%m.%d-%H.%M')
filename = '%s%s.sql' % (backup_dir, now)
return filename
def db_list():
run('find /srv/%s/backups/ -name *.gz' % env.roles[0])
def db_backup(name):
backup_dir = '/srv/%s/backups/%s/' % (env.roles[0], name)
run('mkdir -p ' + backup_dir)
conf = _relation_get('postgresql', 'db')
conf['filename'] = _get_dump_filename(backup_dir)
if 'port' not in conf.keys(): conf['port'] = '5432'
cmd = 'PGPASSWORD="%(password)s" pg_dump --username=%(user)s --host=%(host)s --port=%(port)s %(database)s | gzip -3 > %(filename)s.gz' % conf
def db_restore(name):
backup_file = '/srv/%s/backups/%s' % (env.roles[0], name)
conf = _relation_get('postgresql', 'db')
conf['filename'] = backup_file
if 'port' not in conf.keys(): conf['port'] = '5432'
cmd = 'gunzip -c %(filename)s | PGPASSWORD="%(password)s" psql --username=%(user)s --host=%(host)s --port=%(port)s %(database)s' % conf
# Utils
def delete_pyc():
""" Deletes *.pyc files from project source dir """
run("find . -name '*.pyc' -delete")

This comment has been minimized.

Copy link

@yarbelk yarbelk commented Apr 5, 2013

Awesome gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.