Commits

Benoit C committed 1e1cbe1

adding some factory, to automagically instanciate specialized classes

Comments (0)

Files changed (11)

 from fabulator.core import south, haystack
 from fabulator.core.db import mysql
 
+from fabulator.core.db import database_factory
+from fabulator.core.vc import vc_factory
+from fabulator.core.http import http_factory
+
 import yaml
 import time
 
     # sure we can do more there
     env.yaml = yaml.load(file(conf).read()).get(env_type)
 
+
+def build(conf):
+    """instanciate the backend"""
+    env.vc = vc_factory(conf["vc"].get("type"), conf)
+    env.db = database_factory(conf["database"].get("type"), conf)
+    env.httpd = http_factory(conf["httpd"].get("type"), conf)
+
 def preprod(conf="conf.yaml"):
     """Preprod tasks"""
     load_yaml('preprod', conf)
     env.user = env.yaml.get('project').get('sudo_user')
     env.path = env.yaml.get('project').get('root_dir')
     env.virtualhost_path = env.path
+    build(env.yaml)
 
 def prod(conf="conf.yaml"):
     """Prod tasks"""
     env.user = env.yaml.get('project').get('sudo_user')
     env.path = env.yaml.get('project').get('root_dir')
     env.virtualhost_path = env.path
+    build(env.yaml)
 
 
 @Permissions("normal_user", env)
 
 def install():
     """for the first install onto the machines"""
-    # install required packages 
-    distro_install()
 
-    # update the hg repos
-    update_hg()
-    
-    # install or update the virtualenv
-    virtualenv()
-
-    # install the vhost required for the django application
-    install_httpd_vhost()
-
-
-    # install django configuration
-    django_settings()
-    
-    # install the database schema
-    install_database()
-
-    # start/restart the wsgi/fcgi process
-    wsgi_restart()
-
-    # restart other services
-    services_reload()
-    services_restart()
+    print env.httpd
+    print env.db
+    print env.vc
+#    # install required packages 
+#    distro_install()
+#
+#    # update the hg repos
+#    update_hg()
+#    
+#    # install or update the virtualenv
+#    virtualenv()
+#
+#    # install the vhost required for the django application
+#    install_httpd_vhost()
+#
+#
+#    # install django configuration
+#    django_settings()
+#    
+#    # install the database schema
+#    install_database()
+#
+#    # start/restart the wsgi/fcgi process
+#    wsgi_restart()
+#
+#    # restart other services
+#    services_reload()
+#    services_restart()
 
 @Permissions("sudo_user", env)
 def nginx_logs():

src/fabulator/core/db/__init__.py

+from mysql import MySQL
+from pgsql import PgSQL
+
+databases = {
+    "mysql" : MySQL,
+    "pgsql" : PgSQL,
+}
+
+def database_factory(database, conf):
+    return databases[database.lower()](conf)
+    

src/fabulator/core/db/pgsql.py

+"""
+    psql management functions
+"""
+import datetime
+import time
+
+from fabric.api import run
+
+from base import BaseDB
+
+class PgSQL(BaseDB):
+    """Base class for database"""
+
+    def create_database(self, database_name=None):
+        """create database"""
+        run("echo 'CREATE DATABASE IF NOT EXISTS %(db)s' | psql --user=%(user)s --password=%(pw)s" % self.conf)
+
+    def backup(self, filename="plop"):
+        """Backups your psql database"""
+        date = datetime.datetime.now()
+
+        conf['backup_fname'] = "%s/backup-%s-%d-%d-%d-%s.sql" % \
+            (self.conf.get('backup'), self.conf.get('db'), date.day, date.month,
+             date.year, time.time())
+        run("mkdir -p %(backup)s" % self.conf)
+        run("""psqldump --host=%(host)s --user=%(user)s --password=%(pw)s %(db)s > %(backup_fname)s""" % self.conf)
+
+    def migrate(self, revision):
+        """migrate the database"""
+        raise NotImplementedError
+
+    def fallback(self, revision):
+        """fallback to an old revision"""
+        raise NotImplementedError
+
+    def drop_table(self, table_name):
+        """Drop a table"""
+        raise NotImplementedError
+
+    def drop_database(self, database_name):
+        run("echo 'DROP DATABASE %(db)s' | psql --user=%(user)s --password=%(pw)s" % self.conf)
+
+    def list_revision(self):
+        """Return a list of available database revision"""
+        run("ls -l %(backup)s" % self.conf)
+        raise NotImplementedError
+
+
+
+

src/fabulator/core/http/__init__.py

+from nginx import Nginx
+
+webservers = {
+    "nginx" : Nginx,
+}
+
+def http_factory(webserver, conf):
+    return webservers[webserver.lower()](conf)

src/fabulator/core/http/base.py

 # -*- coding: latin-1 -*-
 """
+    base class for web servers management
+"""
 
 
-
-"""
-from interface import BaseHTTP
-
 class BaseHTTP(object):
-    """Nginx class for web servers management"""
+
+    def __init__(self, conf):
+        self.conf = conf
 
     def install_vhost(self, vhost_name):
         """Install the vhost for your http webserver"""

src/fabulator/core/http/nginx.py

 # -*- coding: latin-1 -*-
 """
-
-
+    Specialized class to manage nginx :
+    - create the vhost (mostly)
 
 """
 
 from base import BaseHTTP
 
 class Nginx(BaseHTTP):
-    """
-
-    """
-
-    def install_vhost(self):
-        raise NotImplementedError
- 
-
-def main():
-    """some quick mains"""
-    pass
-
-if __name__ == '__main__':
-    main()
+    
+    def install_vhost(self, vhost_name):
+        # need to template the vhost
+        # replace the values
+        # install the vhost in the desired folder
+        # create the symbolic link
+        # restart nginx
+        pass

src/fabulator/core/mysql.py

-"""
-    mysql management functions
-"""
-import datetime
-import time
-
-from fabric.api import run
-
-def create_database(conf):
-    run("echo 'CREATE DATABASE IF NOT EXISTS %(db)s' | mysql --user=%(user)s --password=%(pw)s" % conf)
-    
-def drop_database(conf):
-    run("echo 'DROP DATABASE %(db)s' | mysql --user=%(user)s --password=%(pw)s" % conf)
-    
-    
-def backup_database(conf):
-    """Backups your mysql database"""
-    date = datetime.datetime.now()
-
-    conf['backup_fname'] = "%s/backup-%s-%d-%d-%d-%s.sql" % \
-        (conf.get('backup'), conf.get('db'), date.day, date.month, date.year, time.time())
-    run("mkdir -p %(backup)s" % conf)
-    run("""mysqldump --host=%(host)s --user=%(user)s --password=%(pw)s %(db)s > %(backup_fname)s""" % conf)
-
-    

src/fabulator/core/vc/__init__.py

+from mercurial import Mercurial
+
+vcs = {
+    "hg" : Mercurial,
+    "mercurial" : Mercurial
+}
+
+def vc_factory(vc, conf):
+    return vcs[vc.lower()](conf)

src/fabulator/core/vc/base.py

+"""
+    Base class for versionning control system
+"""
+class BaseVC(object):
+
+    def __init__(self, conf):
+        self.conf = conf
+        pass
+

src/fabulator/core/vc/interface.py

-# -*- coding: latin-1 -*-
-"""
-
-
-
-"""
-
-class BaseVC(object):
-    """Base class for versionning control system"""
-
-    def __init__(self):
-        pass
-

src/fabulator/core/vc/mercurial.py

+# -*- coding: latin-1 -*-
+"""
+
+
+"""
+
+from base import BaseVC
+
+class Mercurial(BaseVC):
+    """base class for controlling your Mercurial"""
+
+    pass