Commits

Sergey Generalov  committed 9372a34

initial commit

  • Participants

Comments (0)

Files changed (7)

+syntax: glob
+*.egg-info/*
+build
+dist
+*.pyc

File gbsite/bin/gbsite

+#!/usr/bin/env python
+
+import os
+
+
+def main():
+    print "Helper script for genbit's sites"
+
+if __name__ == "__main__":
+    main()

File gbsite/fabconfig.py.tpl

+# -*- coding:utf-8 -*-
+
+from fabric.api import env
+
+
+# globals
+env.project_name = ''
+
+"Use the local virtual server"
+# env.hosts = ['myhost'] 
+env.project_domain = ''
+env.sudo_user = ''
+env.group = ''
+env.user = ''
+env.path = '/home/' + env.user + '/' + env.project_domain
+env.virtualhost_path = "/"
+env.wsgi_threads = 10

File gbsite/fabfile.py

+# -*- coding:utf-8 -*-
+
+import os
+import sys
+
+PROJECT_ROOT = os.path.dirname(__file__)
+sys.path.append(PROJECT_ROOT)
+
+from fabric.api import *
+import fabconfig
+
+# tasks
+def test():
+    "Run the test suite and bail out if it fails"
+    local("cd %(project_name)s;../env/bin/python manage.py test" % env, capture=False)
+
+def setup_local():
+    local('virtualenv env', capture=False)
+    local('pip -E env install -r requirements.txt', capture=False)
+
+def setup_apache():
+    '''
+    Создает конфигурационный файл для apache2 и копирует его в /etc/apache2/sites-available
+    Включает сайт с помощью a2ensite
+    Вызывается только из deploy 
+    '''
+    apache_config = """<VirtualHost 127.0.0.1:8080>
+        ServerName %(project_domain)s
+        ServerAlias www.%(project_domain)s
+        WSGIDaemonProcess %(project_name)s-production user=%(user)s group=%(group)s threads=%(wsgi_threads)s python-path=%(path)s
+        WSGIProcessGroup %(project_name)s-production
+        WSGIScriptAlias / %(path)s/releases/current/%(project_name)s/index.wsgi
+        <Directory %(path)s/shared>
+            Order deny,allow
+            Allow from all
+        </Directory>
+        ErrorLog %(path)s/log/error.log
+        LogLevel warn
+        CustomLog %(path)s/log/access.log combined
+</VirtualHost>"""
+    f = open('apache2.config', 'w')
+    f.write(apache_config % env)
+    f.close()
+    put('apache2.config', '%(path)s/' % env)
+    with settings(
+        user = env.sudo_user
+    ):
+        sudo('cd %(path)s;cp apache2.config /etc/apache2/sites-available/%(project_domain)s;a2ensite %(project_domain)s;rm apache2.config' % env)
+
+def setup_nginx():
+    nginx_config = """server {
+    listen 80;
+    server_name *.%(project_domain)s %(project_domain)s
+    root %(path)s/shared;
+    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov|html|htm)$ {
+        if (-f $request_filename) {
+            expires max;
+            access_log  off;
+            break;
+        }
+    }
+    location / {
+        proxy_pass http://127.0.0.1:8080;
+        include /etc/nginx/conf/proxy.conf;
+        error_log %(path)s/log/nginx.error.log;
+        access_log %(path)s/log/nginx.access.log main;
+    }
+}
+"""
+    f = open('nginx.config', 'w')
+    f.write(nginx_config % env)
+    f.close()
+    put('nginx.config', '%(path)s/' % env)
+    with settings(
+        user = env.sudo_user
+    ):
+        sudo('cd %(path)s;cp nginx.config /etc/nginx/sites-available/%(project_domain)s;ln -s /etc/nginx/sites-available/%(project_domain)s /etc/nginx/sites-enabled/;rm nginx.config' % env)
+
+def setup():
+    """
+    Setup a fresh virtualenv as well as a few useful directories, then run
+    a full deployment
+    """
+    require('hosts')
+    require('path')
+    run('mkdir -p %(path)s;' % env)
+    run('cd %(path)s; virtualenv env;' % env)
+    run('cd %(path)s; mkdir releases; mkdir -p shared/uploads; mkdir packages;mkdir log;mkdir tmp' % env)
+    deploy()
+
+def deploy_settings():
+    for host in env.hosts:
+        settings_file = 'settings_%s' % (host,)
+        if os.path.exists(settings_file):
+            put(settings_file, '%(path)s/releases/current/%(project_name)s/settings_local.py' % env)
+
+def deploy():
+    """
+    Deploy the latest version of the site to the servers, install any
+    required third party modules, install the virtual host and
+    then restart the webserver
+    """
+    require('hosts')
+    require('path')
+    import time
+    f = open('release', 'r')
+    release = f.read().strip()
+    f.close()
+    env.release = release 
+    upload_tar_from_hg()
+    install_requirements()
+    symlink_current_release()
+    install_site()
+    deploy_settings()
+    migrate()
+def deploy_version(version):
+    "Specify a specific version to be made live"
+    f = open('release', 'r')
+    env.release = f.read().strip()
+    f.close()
+    with settings(
+        hide('warnings', 'running', 'stdout', 'stderr'),
+        warn_only=True
+    ):
+        run('cd %(path)s; rm releases/previous; mv releases/current releases/previous;' % env)
+    run('cd %(path)s; ln -s %(path)s/releases/%(release)s releases/current' % env)
+    deploy_settings()
+    restart_webserver()
+def rollback():
+    """
+    Limited rollback capability. Simple loads the previously current
+    version of the code. Rolling back again will swap between the two.
+    """
+    with settings(
+        hide('warnings', 'running', 'stdout', 'stderr'),
+        warn_only=True
+    ):
+        run('cd %(path)s; mv releases/current releases/_previous;' % env)
+        run('cd %(path)s; mv releases/previous releases/current;' % env)
+        run('cd %(path)s; mv releases/_previous releases/previous;' % env)
+    restart_webserver()
+# Helpers. These are called by other functions rather than directly
+def upload_tar_from_hg():
+    require('release', provided_by=[deploy, setup])
+    "Create an archive from the current Hg default branch and upload it"
+    local('hg archive --type=tgz %(release)s.tar.gz' % env)
+    #run('mkdir %(path)s/releases/' % env)
+    put('%(release)s.tar.gz' % env, '%(path)s/packages/' % env)
+    run('cd %(path)s/releases/ && tar zxf ../packages/%(release)s.tar.gz' % env)
+    local('rm %(release)s.tar.gz'% env)
+def install_site():
+    "Add the virtualhost file to apache"
+    #require('release', provided_by=[deploy, setup])
+    setup_apache()
+    setup_nginx()
+    restart_webserver()
+def install_requirements():
+    "Install the required packages from the requirements file using pip"
+    require('release', provided_by=[deploy, setup])
+    run('cd %(path)s; pip install -E env -r ./releases/%(release)s/requirements.txt' % env)
+def symlink_current_release():
+    "Symlink our current release"
+    #require('release', provided_by=[deploy, setup])
+    f = open('release', 'r')
+    release = f.read().strip()
+    f.close()
+    env.release = release
+    with settings(
+        hide('warnings', 'running', 'stdout', 'stderr'),
+        warn_only=True
+    ):
+        run('cd %(path)s; rm releases/previous; mv releases/current releases/previous;' % env)
+        run('cd %(path)s; ln -s %(path)s/releases/%(release)s releases/current' % env)
+
+def symlink_media():
+    f = open('release', 'r')
+    release = f.read().strip()
+    f.close()
+    env.release = release
+    # symlink to public
+    with settings(
+        hide('warnings', 'running', 'stdout', 'stderr'),
+        warn_only=True
+    ):
+        run('cd %(path)s;ln -s %(path)s/releases/current/%(project_name)s/media shared/media' % env)
+        run('cd %(path)s;ln -s %(path)s/env/src/cms/cms/media/cms shared/media/' % env)
+        run('cd %(path)s;ln -s %(path)s/env/src/django/django/contrib/admin/media shared/media/admin' % env)
+
+def migrate():
+    "Update the database"
+    require('project_name')
+    run('cd %(path)s/releases/current/%(project_name)s;  %(path)s/env/bin/python manage.py syncdb --noinput; %(path)s/env/bin/python manage.py migrate' % env)
+
+def touch_site():
+    run('touch %(path)s/releases/current/%(project_name)s/index.wsgi' % env)
+
+def restart_webserver():
+    "Restart the web server"
+    with settings(
+        user=env.sudo_user
+    ):
+        sudo('/etc/init.d/apache2 restart')
+        sudo('/etc/init.d/nginx restart')

File gbsite/requirements.txt

+#
+# This file contains global requirements for all genbit's sites base on django-cms2
+#
+
+# general libs
+South
+
+# django
+-e svn+http://code.djangoproject.com/svn/django/trunk/@13082#egg=django
+
+# django-cms 
+-e git+git://github.com/digi604/django-cms-2.0.git#egg=cms
+-e svn+http://django-tinymce.googlecode.com/svn/trunk/#egg=tinymce
+-e svn+http://django-template-utils.googlecode.com/svn/trunk/#egg=template_utils
+-e svn+http://django-reversion.googlecode.com/svn/trunk/src/#egg=reversion
+
+# for extend django-cms
+-e hg+http://bitbucket.org/izi/django-admin-tools#egg=admin-tools
+-e git+git://github.com/wardi/django-filebrowser-no-grappelli.git#egg=filebrowser
+
+# project libs
+#-e svn+http://django-photologue.googlecode.com/svn/trunk/#egg=photologue
+#-e hg+http://bitbucket.org/fivethreeo/django-videologue#egg=videologue
+#-e svn+http://django-simple-captcha.googlecode.com/svn/trunk/#egg=captcha
+#-e hg+http://bitbucket.org/genbit/cmsplugin_comments#egg=cmsplugin_comments
+#-e hg+http://bitbucket.org/genbit/cmsplugin-fbpicture#egg=cmsplugin_fbpicture
+3-e hg+http://bitbucket.org/genbit/cmsplugin-news#egg=cmsplugin_news

File gbsite/requirements_local.txt.tpl

+#
+# This is templtaee file that contains local requirements for all genbit's sites base on django-cms2
+#
+
+# project libs
+#-e svn+http://django-photologue.googlecode.com/svn/trunk/#egg=photologue
+#-e hg+http://bitbucket.org/fivethreeo/django-videologue#egg=videologue
+#-e svn+http://django-simple-captcha.googlecode.com/svn/trunk/#egg=captcha
+#-e hg+http://bitbucket.org/genbit/cmsplugin_comments#egg=cmsplugin_comments
+#-e hg+http://bitbucket.org/genbit/cmsplugin-fbpicture#egg=cmsplugin_fbpicture
+#-e hg+http://bitbucket.org/genbit/cmsplugin-news#egg=cmsplugin_news
+#/usr/bin/env python
+import os
+from setuptools import setup, find_packages
+
+ROOT_DIR = os.path.dirname(__file__)
+SOURCE_DIR = os.path.join(ROOT_DIR)
+
+# Dynamically calculate the version
+setup(
+    name = "gbsite",
+    version = '0.1',
+    description = "Application for deploy sites based on the django-cms2",
+    author = "Sergey Generalov",
+    author_email = "sergey@genbit.ru",
+    url = "http://bitbucket.org/genbit/site",
+    license='BSD License',
+    platforms=['OS Independent'],
+    scripts = ['gbsite/bin/gbsite'],
+    packages = find_packages(),
+    zip_safe = False
+)
+