Skip to content

Instantly share code, notes, and snippets.

@jersub
Created September 26, 2011 21:43
Show Gist options
  • Save jersub/1243492 to your computer and use it in GitHub Desktop.
Save jersub/1243492 to your computer and use it in GitHub Desktop.
Management of multiple gallery3 instances with Fabric
import os
import re
from fabric.api import lcd, hide, local, puts
CONTRIB_DIR = 'gallery-contrib'
GALLERY_REPO = 'git://github.com/gallery/gallery3.git'
GALLERY_CONTRIB_REPO = 'git://github.com/gallery/gallery3-contrib.git'
DATABASE_PREFIX = 'gallery_'
# PRIVATE UTILITY FUNCTIONS
def _get_tags():
output = local('git ls-remote --tags ' + GALLERY_REPO, True)
it = re.finditer('/([^/]+)$', output, re.MULTILINE)
return [m.group(1) for m in it]
def _get_last_version():
return max(_get_tags())
def _get_gallery_dirs():
current_dir = os.getcwd()
entries = os.listdir(current_dir)
dirs = filter(os.path.isdir, entries)
if CONTRIB_DIR in dirs:
dirs.remove(CONTRIB_DIR)
dirs.remove('.git')
return dirs
# TAGS TASKS
def list_tags():
map(puts, _get_tags())
def last_version():
puts(_get_last_version())
# INSTANCES TASKS
def deploy(name, database_password=None):
create_instance(name)
create_database(name, database_password)
def create_instance(name):
local('git clone ' + GALLERY_REPO + ' ' + name)
with lcd(name):
tag = _get_last_version()
local('git checkout ' + tag)
local('mkdir var')
local('chown www-data:www-data var')
path = os.path.abspath(name)
puts('New gallery instance deployed in ' + path)
def create_database(site, password=None):
if not password:
password = local('pwgen -s 8 1')
database_name = DATABASE_PREFIX + site
database_user = database_name + '@localhost'
local('mysqladmin -u root -p create ' + database_name)
local('mysql -u root -p -e "CREATE USER ' + database_user + ' IDENTIFIED BY \'' + password + '\';"')
local('mysql -u root -p -e "GRANT ALL ON ' + database_name + '.* TO ' + database_user + ';"')
puts('Database ' + database_name + ' successfully created with password ' + password)
def upgrade_site(site, tag=None):
if not tag:
tag = _get_last_version()
puts('Upgrade site ' + site + ' to version ' + tag)
with lcd(site):
local('git fetch origin')
local('git checkout ' + tag)
def upgrade(site=None, tag=None):
if not tag:
tag = _get_last_version()
if site:
upgrade_site(site, tag)
return
for site in _get_gallery_dirs():
upgrade_site(site, tag)
puts('')
# CONTRIB REPO TASKS
def setup_contrib():
local('git clone ' + GALLERY_CONTRIB_REPO + ' ' + CONTRIB_DIR)
path = os.path.abspath(CONTRIB_DIR)
puts('Contrib repository fetched in ' + path)
def upgrade_contrib():
with lcd(CONTRIB_DIR):
local('git pull origin master')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment