Update fabfile to user global environment configuration, abstracted tasks for platform, webserver and database manager

This commit is contained in:
Roberto Rosario
2012-06-01 01:55:09 -04:00
parent ccf41ae67d
commit 9f0d7966bc
21 changed files with 374 additions and 196 deletions

32
fabfile.py vendored
View File

@@ -1,32 +0,0 @@
import os
from fabric.api import run, sudo, cd, env, task
from fabfiles.databases import mysql
from fabfiles.webservers import apache
from fabfiles.platforms import ubuntu
from fabfiles.conf import setup_paths
@task(default=True)
def install(**kwargs):
setup_paths(**kwargs)
ubuntu.install_dependencies()
ubuntu.install_mayan()
mysql.install_database_manager()
mysql.create_database()
ubuntu.fix_permissions()
apache.install()
apache.install_site()
apache.restart()
@task
def uninstall(**kwargs):
setup_paths(**kwargs)
ubuntu.uninstall()
apache.remove_site()

35
fabfile/__init__.py Normal file
View File

@@ -0,0 +1,35 @@
import sys
import os
from fabric.api import run, sudo, cd, env, task
from fabric.main import load_settings
import databases
import webservers
import platforms
from conf import setup_environment
setup_environment()
@task(default=True)
def install():
platforms.install_dependencies()
platforms.install_mayan()
platform.install_database_manager()
databases.create_database()
platforms.fix_permissions()
platforms.install_webserver()
webservers.install_site()
webservers.restart()
@task
def uninstall():
platforms.uninstall()
webservers.remove_site()
if env.drop_database:
databases.drop()

31
fabfile/conf.py Normal file
View File

@@ -0,0 +1,31 @@
import os
from fabric.api import env
from literals import (DEFAULT_INSTALL_PATH, DEFAULT_VIRTUALENV_NAME,
DEFAULT_REPOSITORY_NAME, DEFAULT_OS, OS_CHOICES,
DEFAULT_DATABASE_MANAGER, DB_CHOICES, DEFAULT_DATABASE_NAME,
DEFAULT_WEBSERVER, WEB_CHOICES)
def setup_environment():
env['os'] = getattr(env, 'os', DEFAULT_OS)
env['os_name'] = OS_CHOICES[env.os]
env['install_path'] = getattr(env, 'install_path', DEFAULT_INSTALL_PATH[env.os])
env['virtualenv_name'] = getattr(env, 'virtualenv_name', DEFAULT_VIRTUALENV_NAME[env.os])
env['repository_name'] = getattr(env, 'repository_name', DEFAULT_REPOSITORY_NAME[env.os])
env['virtualenv_path'] = os.path.join(env.install_path, env.virtualenv_name)
env['repository_path'] = os.path.join(env.virtualenv_path, env.repository_name)
env['database_manager'] = getattr(env, 'database_manager', DEFAULT_DATABASE_MANAGER)
env['database_manager_name'] = DB_CHOICES[env.database_manager]
if not getattr(env, 'database_manager_admin_password', None):
print('Must set the database_manager_admin_password entry in the fabric settings file (~/.fabricrc by default)')
exit(1)
env['database_name'] = getattr(env, 'database_name', DEFAULT_DATABASE_NAME)
env['webserver'] = getattr(env, 'webserver', DEFAULT_WEBSERVER)
env['webserver_name'] = WEB_CHOICES[env.webserver]

View File

@@ -0,0 +1,25 @@
from fabric.api import run, sudo, cd, env, task
from ..literals import DB_MYSQL
import mysql
@task
def create_database():
"""
Create the Mayan EDMS database
"""
if env.database_manager == DB_MYSQL:
mysql.create_database()
@task
def drop_database():
"""
Drop Mayan EDMS's database
"""
if env.database_manager == DB_MYSQL:
mysql.drop_database()

View File

@@ -0,0 +1,18 @@
from fabric.api import run, sudo, cd, env, task
def create_database():
"""
Create the MySQL Mayan EDMS database
"""
run('echo "create database %(database_name)s;" | mysql -u root --password=%(database_manager_admin_password)s' % env)
#TODO: create DB and mayan user
#TODO: custom settings_local
def drop_database():
"""
Drop MySQL's Mayan EDMS's database
"""
run('echo "drop database %(database_name)s;" | mysql -u root --password=%(database_manager_admin_password)s' % env)

50
fabfile/literals.py Normal file
View File

@@ -0,0 +1,50 @@
OS_UBUNTU = 'ubuntu'
OS_REDHAT = 'redhat'
OS_CENTOS = 'centos'
OS_FEDORA = 'fedora'
OS_WINDOWS = 'windows'
OS_FREEBSD = 'freebds',
OS_CHOICES = {
OS_UBUNTU: 'Ubuntu',
OS_REDHAT: 'RedHat',
OS_CENTOS: 'CentOS',
OS_FEDORA: 'Fedora',
OS_WINDOWS: 'MS Windows',
OS_FREEBSD: 'FreeBSD',
}
DEFAULT_INSTALL_PATH = {
OS_UBUNTU: '/usr/share'
}
DEFAULT_VIRTUALENV_NAME = {
OS_UBUNTU: 'mayan'
}
DEFAULT_REPOSITORY_NAME = {
OS_UBUNTU: 'mayan'
}
DB_MYSQL = 'mysql'
DB_PGSQL = 'pgsql'
DB_SQLITE = 'sqlite'
DB_CHOICES = {
DB_MYSQL: 'MySQL',
DB_PGSQL: 'PostgreSQL',
DB_SQLITE: 'SQLite'
}
WEB_APACHE = 'apache'
WEB_NGINX = 'nginx'
WEB_CHOICES = {
WEB_APACHE: 'Apache',
WEB_NGINX: 'Nginx',
}
DEFAULT_OS = OS_UBUNTU
DEFAULT_DATABASE_MANAGER = DB_MYSQL
DEFAULT_DATABASE_NAME = 'mayan'
DEFAULT_WEBSERVER = WEB_APACHE

View File

@@ -0,0 +1,64 @@
from fabric.api import run, sudo, cd, env, task
from ..literals import OS_UBUNTU
import ubuntu
@task
def install_dependencies():
"""
Install OS dependencies
"""
print('Installing dependencies for %s' % env.os_name)
if env.os == OS_UBUNTU:
ubuntu.install_dependencies()
@task
def install_mayan():
"""
Install Mayan EDMS
"""
print('Installing Mayan EDMS from git repository')
if env.os == OS_UBUNTU:
ubuntu.install_mayan()
@task
def install_database_manager():
"""
Install the selected database manager
"""
print('Installing database manager: %s' % env.database_manager_name)
if env.os == OS_UBUNTU:
ubuntu.install_database_manager()
@task
def fix_permissions():
"""
Fix installation files' permissions
"""
print('Fixing installation files\' permissions')
if env.os == OS_UBUNTU:
ubuntu.fix_permissions()
@task
def install_webserver():
"""
Installing the OS packages for the webserver
"""
print('Installing webserver: %s' % env.webserver_name)
if env.os == OS_UBUNTU:
ubuntu.install_webserver()

View File

@@ -0,0 +1,68 @@
from fabric.api import run, sudo, cd, env, task
from ..literals import DB_MYSQL, WEB_APACHE
def install_dependencies():
"""
Install Ubuntu dependencies
"""
sudo('apt-get install -y git-core gcc tesseract-ocr unpaper python-virtualenv ghostscript libjpeg-dev libpng-dev poppler-utils')
def uninstall():
"""
Uninstall Mayan EDMS from an Ubuntu system
"""
sudo('rm %s -Rf' % env.virtualenv_path)
def fix_permissions():
"""
Fix installation files' permissions on an Ubuntu system
"""
sudo('chmod 777 %s -R' % env.virtualenv_path)
sudo('chgrp www-data %s -R' % env.virtualenv_path)
def install_mayan():
"""
Install Mayan EDMS on an Ubuntu system
"""
with cd(env.install_path):
sudo('virtualenv --no-site-packages %s' % env.virtualenv_name)
with cd(env.virtualenv_path):
sudo('git clone http://www.github.com/rosarior/mayan %s' % env.repository_name)
sudo('source bin/activate; pip install -r %s/requirements/production.txt' % env.repository_name)
def syncdb():
with cd(env.virtualenv_path):
sudo('source bin/activate; %(repository_name)s/manage.py syncdb --noinput; %(repository_name)s/manage.py migrate' % (env))
def install_database_manager():
"""
Install the database manager on an Ubuntu system
"""
if env.database_manager == DB_MYSQL:
sudo('apt-get install -y mysql-server libmysqlclient-dev')
with cd(env.virtualenv_path):
sudo('source bin/activate; pip install MySQL-python')
@task
def install_webserver():
"""
Installing the Ubuntu packages for the webserver
"""
if env.webserver == WEB_APACHE:
sudo('apt-get install -y apache2 libapache2-mod-wsgi')
with settings(warn_only=True):
# Get rid of Apache's default site
sudo('a2dissite default')

View File

@@ -0,0 +1,49 @@
from fabric.api import run, sudo, cd, env, task
from ..literals import WEB_APACHE
import apache
@task
def install_site():
"""
Install Mayan EDMS site in the webserver configuration files
"""
print('Adding Mayan EDMS\'s site files to: %s' % os.webserver_name)
if os.webserver == WEB_APACHE:
apache.install_site()
@task
def remove_site():
"""
Install Mayan EDMS's site file from the webserver's configuration
"""
print('Removing Mayan EDMS\s site file from %s configuration' % os.webserver_name)
if os.webserver == WEB_APACHE:
apache.remove_site()
@task
def restart():
"""
Restart the webserver
"""
print('Restarting the web server: %s' % os.webserver_name)
if os.webserver == WEB_APACHE:
apache.restart()
@task
def reload():
"""
Reload webserver configuration files
"""
print('Reloading the web server configuration files')
if os.webserver == WEB_APACHE:
apache.reload()

View File

@@ -0,0 +1,34 @@
import os
from fabric.api import run, sudo, cd, env, task
def install_site():
"""
Install Mayan EDMS's site file in Apache configuration
"""
#TODO: mod site with paths
sudo('cp %s /etc/apache2/sites-available/' % os.path.join(env.repository_path, 'contrib/apache/mayan'))
sudo('a2ensite mayan')
def remove_site():
"""
Install Mayan EDMS's site file from Apache's configuration
"""
sudo('a2dissite mayan')
def restart():
"""
Restart Apache
"""
sudo('/etc/init.d/apache2 restart')
def reload():
"""
Reload Apache configuration files
"""
sudo('/etc/init.d/apache2 reload')

View File

View File

@@ -1,14 +0,0 @@
import os
from fabric.api import run, sudo, cd, env, task
from literals import DEFAULT_INSTALL_PATH, DEFAULT_VIRTUALENV_NAME, DEFAULT_REPOSITORY_NAME, DEFAULT_OS
def setup_paths(**kwargs):
env['os'] = kwargs.pop('os', DEFAULT_OS)
env['install_path'] = kwargs.pop('path', DEFAULT_INSTALL_PATH[env.os])
env['virtualenv_name'] = kwargs.pop('virtualenv_name', DEFAULT_VIRTUALENV_NAME[env.os])
env['repository_name'] = kwargs.pop('repository_name', DEFAULT_REPOSITORY_NAME[env.os])
env['virtualenv_path'] = os.path.join(env.install_path, env.virtualenv_name)
env['repository_path'] = os.path.join(env.virtualenv_path, env.repository_name)

View File

@@ -1,20 +0,0 @@
from fabric.api import run, sudo, cd, env, task
@task
def install_database_manager():
print('Installing MySQL')
sudo('apt-get install -y mysql-server libmysqlclient-dev')
with cd(env.virtualenv_path):
sudo('source bin/activate; pip install MySQL-python')
@task
def create_database(*args, **kwargs):
print('Setting up Mayan EDMS\'s MySQL database')
#TODO: create DB and mayan user
#TODO: custom settings_local

View File

@@ -1,15 +0,0 @@
OS_UBUNTU = 'ubuntu'
DEFAULT_INSTALL_PATH = {
OS_UBUNTU: '/usr/share'
}
DEFAULT_VIRTUALENV_NAME = {
OS_UBUNTU: 'mayan'
}
DEFAULT_REPOSITORY_NAME = {
OS_UBUNTU: 'mayan'
}
DEFAULT_OS = OS_UBUNTU

View File

@@ -1,50 +0,0 @@
from fabric.api import run, sudo, cd, env, task
from ..conf import setup_paths
@task
def install_dependencies():
print('Installing dependencies')
sudo('apt-get install -y git-core gcc tesseract-ocr unpaper python-virtualenv ghostscript libjpeg-dev libpng-dev poppler-utils')
@task
def uninstall(**kwargs):
drop_database=kwargs.pop('drop_database', False)
setup_paths(**kwargs)
print('Uninstalling Mayan EDMS from: %s' % env.virtualenv_path)
sudo('rm %s -Rf' % env.virtualenv_path)
if drop_database:
#TODO: drop database
pass
@task
def fix_permissions(**kwargs):
setup_paths(**kwargs)
sudo('chmod 777 %s -R' % env.virtualenv_path)
sudo('chgrp www-data %s -R' % env.virtualenv_path)
@task
def install_mayan():
print('Installing Mayan EDMS from git repository')
with cd(env.install_path):
sudo('virtualenv --no-site-packages %s' % env.virtualenv_name)
with cd(env.virtualenv_path):
sudo('git clone http://www.github.com/rosarior/mayan %s' % env.repository_name)
sudo('source bin/activate; pip install -r %s/requirements/production.txt' % env.repository_name)
@task
def syncdb(**kwargs):
setup_paths(**kwargs)
with cd(env.virtualenv_path):
sudo('source bin/activate; %(repository_name)s/manage.py syncdb --noinput; %(repository_name)s/manage.py migrate' % (env))

View File

@@ -1,20 +0,0 @@
import os
TEMPLATE_DIR = 'fabfiles/templates'
class Template(object):
def open(self, filename):
self.descriptor = open(os.path.join(TEMPLATE_DIR, filename), 'r')
def load(self, filename, context=None):
self.open()
self.content = self.descriptor.read() % (context if context else {})
def save_as(self, filename):
output_descriptor = open(filename, 'w')
output_descriptor.write(self.content)
output_descriptor.close()
def close(self):
self.descriptor.close()

View File

@@ -1,45 +0,0 @@
import os
from fabric.api import run, sudo, cd, env, task
from ..templates import Template
from ..conf import setup_paths
@task
def install():
print('Installing apache and mod-wsgi')
sudo('apt-get install -y apache2 libapache2-mod-wsgi')
# Get rid of Apache's default site
sudo('a2dissite default')
reload_webserver()
@task
def install_site(**kwargs):
print('Adding Mayan EDMS\'s virtualhost file to apache')
setup_paths(**kwargs)
#TODO: mod site with paths
sudo('cp %s /etc/apache2/sites-available/' % os.path.join(env.repository_path, 'contrib/apache/mayan'))
sudo('a2ensite mayan')
@task
def remove_site():
sudo('a2dissite mayan')
@task
def restart():
print('Restarting the web server')
sudo('/etc/init.d/apache2 restart')
@task
def reload():
print('Reloading the web server')
sudo('/etc/init.d/apache2 reload')