Commits

Benoit C committed 9f6381b

virtualenv: Class to manage the virtualenv, using a list of egg to install, or a requirement file. use pip.
mercurial: fix typo

Comments (0)

Files changed (3)

 from fabulator.core.vc import vc_factory
 from fabulator.core.http import http_factory
 
+from fabulator.core.virtualenv import VirtualEnv
+
 import yaml
 import time
 
-def uname():
-    run("uname -a")
-
 def load_yaml(env_type, conf):
     # sure we can do more there
     env.yaml = yaml.load(file(conf).read()).get(env_type)
     env.vc = vc_factory(conf["vc"].get("type"), conf.get("vc"), env)
     env.db = database_factory(conf["database"].get("type"), conf.get("database"), env)
     env.httpd = http_factory(conf["httpd"].get("type"), conf.get("httpd"), env)
+    env.virtualenv = VirtualEnv(conf.get("virtualenv"), env)
 
 def preprod(conf="conf.yaml"):
     """Preprod tasks"""
 
 
 @Permissions("normal_user", env)
-@ConfChecker("wsgi", env)
-def wsgi_restart():
-    """Restarts the wsgi process"""
-    conf = env.yaml.get("wsgi")
-    django = env.yaml.get("django")
-    if isinstance(conf.get('port'), list):
-        for port in conf.get('port'):
-            pidfile = "%s.%d" % (conf.get('pidfile'), port)
-            if exists(pidfile):
-                run("kill -0 `cat %s`" % pidfile)
-                run("kill -HUP `head -1 %s`" % pidfile)
-            else:
-                with cd(django.get('src_dir')):
-                    run("%s/gunicorn_django --workers=%d --bind=127.0.0.1:%d --pid=%s --log-file=%s -D" % \
-                            (conf.get('path'), conf.get('workers'),
-                             port, pidfile, conf.get('logfile')))
-
-@Permissions("normal_user", env)
 @ConfChecker("httpd", env)
 def install_httpd_vhost():
     """Install the vhost for your favorite httpd"""
         sed("%s/settings_local.py" % conf.get('src_dir'), "DOCUMENT_ROOT", "%s" % conf.get('src_dir'))
 
 
-@Permissions("normal_user", env)
-@ConfChecker("hg", env)
-def update_hg():
-    """update your sources"""
-    conf = env.yaml.get('hg')
-    proj = env.yaml.get('project')
-    if exists(conf.get('dest')):
-        with cd(conf.get('dest')):
-            run("hg pull -u")
-            run("hg update -C")
-    else:
-        conf = env.yaml.get("hg")
-        run("mkdir -p %s" % os.path.dirname(env.path))
-        run("hg clone http://%(user)s:%(pw)s@%(url)s/%(repos)s %(dest)s" % conf)
-
-@Permissions("normal_user", env)
-@ConfChecker("virtualenv", env)
-def virtualenv():
-    """Create or update your virtualenv"""
-    conf = env.yaml.get('virtualenv')
-    deploy_dir = env.path + "/" + env.project_name + "/deploy"
-    if not exists(conf.get('dir')):
-        ret = run("virtualenv %(dir)s" % conf)
-        run("%(bin_dir)s/easy_install pip" % conf)
-
-    run("%s/pip install -r %s/requirements.txt" % (conf.get('bin_dir'), deploy_dir))
-
 
 @Permissions("normal_user", env)
 @ConfChecker("database", env)
     #apply the change
 
 
-@Permissions("normal_user", env)
-@ConfChecker("database", env)
-def install_database():
-    """Install your database schema"""
-    conf = env.yaml.get("database")
-    django = env.yaml.get("django")
-    venv = env.yaml.get("virtualenv")
-    
-    mysql.create_database(conf)
-    if django.get('south'):
-        south.install_south(env)
-    
-    with cd("%(src_dir)s" % django):
-        if django.get('south'):
-            run("%(bin_dir)s/python manage.py migrate --all" % venv)
-        else:
-            run("%(bin_dir)s/python manage.py syncdb --noinput" % venv)
-
-
 @Permissions("sudo_user", env)
 @ConfChecker("services", env)
 def services_reload():
     services = env.yaml.get('services').get('restart')
     for service in services:
         sudo("/etc/init.d/%s restart" % service)
-    print services
 
-
-@Permissions("sudo_user", env)
-@ConfChecker("distro", env)
-def distro_install():
-    """Installs required packages"""
-    # install packages specified in the yaml conf
-    dist_conf = env.yaml.get('distro')
-    if dist_conf.get('name') in ('ubuntu', 'debian'):
-        for package in dist_conf.get('packages'):
-            sudo("apt-get install %s" % package)
-
-@Permissions("sudo_user", env)
-@ConfChecker("distro", env)
-def delete_database():
-    db = env.yaml.get('database')
-    mysql.drop_database(db)
-
-@Permissions("sudo_user", env)
-@ConfChecker("distro", env)
-def delete_repos():
-    conf = env.yaml.get('project')
-    run("rm -Rf %(doc_root)s" % conf)
     
 
 def update():
     print env.vc
     
     env.vc.clone_repository()
+    env.virtualenv.create_virtualenv()
+    env.virtualenv.install_requirements(False)
+
+
+
 #    # install required packages 
 #    distro_install()
 #
     sudo("sudo tail -f /var/log/nginx/*.log")
 
 def reset():
-    delete_repos()
-    delete_database()
+    env.vc.remove_repository()
+
+
+@Permissions("sudo_user", env)
+@ConfChecker("distro", env)
+def distro_install():
+    """Installs required packages"""
+    # install packages specified in the yaml conf
+    dist_conf = env.yaml.get('distro')
+    if dist_conf.get('name') in ('ubuntu', 'debian'):
+        for package in dist_conf.get('packages'):
+            sudo("apt-get install %s" % package)
+
+@Permissions("sudo_user", env)
+@ConfChecker("distro", env)
+def delete_database():
+    db = env.yaml.get('database')
+    mysql.drop_database(db)
+
+@Permissions("sudo_user", env)
+@ConfChecker("distro", env)
+def delete_repos():
+    conf = env.yaml.get('project')
+    run("rm -Rf %(doc_root)s" % conf)
+
+
+
+
+@Permissions("normal_user", env)
+@ConfChecker("database", env)
+def install_database():
+    """Install your database schema"""
+    conf = env.yaml.get("database")
+    django = env.yaml.get("django")
+    venv = env.yaml.get("virtualenv")
+
+    mysql.create_database(conf)
+    if django.get('south'):
+        south.install_south(env)
+
+    with cd("%(src_dir)s" % django):
+        if django.get('south'):
+            run("%(bin_dir)s/python manage.py migrate --all" % venv)
+        else:
+            run("%(bin_dir)s/python manage.py syncdb --noinput" % venv)
+
+
+@Permissions("normal_user", env)
+@ConfChecker("wsgi", env)
+def wsgi_restart():
+    """Restarts the wsgi process"""
+    conf = env.yaml.get("wsgi")
+    django = env.yaml.get("django")
+    if isinstance(conf.get('port'), list):
+        for port in conf.get('port'):
+            pidfile = "%s.%d" % (conf.get('pidfile'), port)
+            if exists(pidfile):
+                run("kill -0 `cat %s`" % pidfile)
+                run("kill -HUP `head -1 %s`" % pidfile)
+            else:
+                with cd(django.get('src_dir')):
+                    run("%s/gunicorn_django --workers=%d --bind=127.0.0.1:%d --pid=%s --log-file=%s -D" % \
+                            (conf.get('path'), conf.get('workers'),
+                             port, pidfile, conf.get('logfile')))

src/fabulator/core/vc/mercurial.py

 
     def remove_repository(self):
         """remove the repository (yeah sometimes we expect some cleanups)"""
-        run("rm -Rf %(dest)s/%(repos)s" % self.conf):
-        raise NotImplementedError
+        run("rm -Rf %(dest)s/%(repos)s" % self.conf)
 
     def update_to_revision(self, revision):
         """update the code to a specified revision"""

src/fabulator/core/virtualenv.py

     virtualenv management functions
 
 """
+import os
 
-def virtualenv():
-    """Create or update your virtualenv"""
-    conf = env.yaml.get('virtualenv')
-    deploy_dir = env.path + "/" + env.project_name + "/deploy"
-    if not exists(conf.get('dir')):
-        ret = run("virtualenv %(dir)s" % conf)
-        run("%(bin_dir)s/easy_install pip" % conf)
+from fabric.api import run, sudo, cd
+from fabric.contrib.files import exists
 
-    run("%s/pip install -r %s/requirements.txt" % (conf.get('bin_dir'), deploy_dir))
+class VirtualEnv(object):
+    """Virtualenv class"""
+    
+    def __init__(self, conf, env):
+        self.conf = conf
+        self.env = env
+        self.dir = conf.get("dir")
+        self.bin_dir = self.dir + os.sep + "bin"
+        self.bin = conf.get("bin") or "virtualenv"
+        self.requirements = conf.get("requirements")
+    
+    def create_virtualenv(self, folder=None, pip=True):
+        """creates the virtualenv"""
+        run("%s %s" % (self.bin, self.dir))
+        if pip:
+            self.install_pip()
+        
+    def install_pip(self):
+        """checks if pip exists or install it using easy_install"""
+        if not exists("%s/pip" % self.bin_dir):
+            run("source %s/activate && easy_install pip" % (self.bin_dir))
+
+    def install_requirements(self, use_file=True):
+        if use_file:
+            run("pip install -E %s -r %s" % (self.dir, self.conf.get("requirements-file")))
+        else:
+            for requirement in self.requirements:
+                run("pip install -E %s %s" % (self.dir, requirement))
+        
+    def update_requirements(self, use_file=True):
+        if use_file:
+            run("pip install -E %s -r %s --upgrade" % (self.dir, self.conf.get("requirements-file")))
+        else:
+            for requirement in self.requirements:
+                run("pip install -E %s %s --upgrade" % (self.dir, requirement))
+
+    def remove_virtualenv(self):
+        run("rm -Rf %s" % self.dir)
+