Last active
December 25, 2015 23:09
-
-
Save omerucel/7054935 to your computer and use it in GitHub Desktop.
fabfile deployment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #-*- coding: utf-8 -*- | |
| from fabric.api import * | |
| from fabric.contrib.files import exists | |
| from rjsmin import jsmin | |
| from rcssmin import cssmin | |
| import time | |
| timestamp = int(time.time()) | |
| projectname = 'projectname' | |
| development_ip_address = '192.168.56.24' | |
| production_ip_addresses = ['1.2.3.4'] | |
| static_dir = '%s/static' %(projectname) | |
| css_files = [ | |
| 'flatui/bootstrap/css/bootstrap.css', | |
| 'flatui/bootstrap/css/prettify.css', | |
| 'flatui/css/flat-ui.css', | |
| 'site/style.css' | |
| ] | |
| js_files =[ | |
| 'flatui/js/jquery-1.8.3.min.js', | |
| 'flatui/js/jquery-ui-1.10.3.custom.min.js', | |
| 'flatui/js/jquery.ui.touch-punch.min.js', | |
| 'flatui/js/bootstrap.min.js', | |
| 'flatui/js/bootstrap-select.js', | |
| 'flatui/js/bootstrap-switch.js', | |
| 'flatui/js/flatui-checkbox.js', | |
| 'flatui/js/flatui-radio.js', | |
| 'flatui/js/jquery.tagsinput.js', | |
| 'flatui/js/jquery.placeholder.js', | |
| 'site/app.js' | |
| ] | |
| ### Profiles ### | |
| def development(): | |
| env.django_settings_module = '%s.settings_development' %(projectname) | |
| def production(): | |
| env.user = 'ubuntu' | |
| env.hosts = production_ip_addresses | |
| env.key_filename = '/Users/omer/linux.pem' | |
| env.release_dir = '/home/ubuntu/%s' %(projectname) | |
| env.current_release_dir = '%s/%s' %(env.release_dir, timestamp) | |
| env.project_env_home = '/home/ubuntu/%senv' %(projectname) | |
| env.project_env_bin = '%s/bin' %(env.project_env_home) | |
| env.pip_bin = '%s/pip-2.7' %(env.project_env_bin) | |
| env.uwsgi_bin = '%s/uwsgi' %(env.project_env_bin) | |
| env.python_bin = '%s/python' %(env.project_env_bin) | |
| env.uwsgi_pid_file = '%s/uwsgi.pid' %(env.release_dir) | |
| env.uwsgi_log_file = '%s/uwsgi.log' %(env.release_dir) | |
| env.module_name = '%s.wsgi:application' %(projectname) | |
| env.django_settings_module = '%s.settings' %(projectname) | |
| env.nginx_vhost_file = '%s/current/production_vhost.conf' %(env.release_dir) | |
| ### TOOLS ### | |
| def remove_pyc(): | |
| local('find . -name "*.pyc" -exec rm -rf {} \;') | |
| def create_archive(): | |
| local('git archive --format=zip --output=/tmp/%s.zip master' %(timestamp)) | |
| ### DEVELOPMENT ### | |
| def dev_collectstatic(): | |
| local('cd static && python ../manage.py collectstatic') | |
| def dev_runserver(): | |
| local('DJANGO_SETTINGS_MODULE=%s.settings_development python manage.py runserver %s:8080' %( | |
| projectname, | |
| development_ip_address | |
| )) | |
| def dev_syncdb(): | |
| local('DJANGO_SETTINGS_MODULE=%s.settings_development python manage.py syncdb' %( | |
| projectname | |
| )) | |
| def dev_initial_migration(): | |
| local('DJANGO_SETTINGS_MODULE=%s.settings_development python manage.py schemamigration %s --initial' %( | |
| projectname, | |
| projectname | |
| )) | |
| def dev_check_migration(): | |
| local('DJANGO_SETTINGS_MODULE=%s.settings_development python manage.py schemamigration %s --auto' %( | |
| projectname, | |
| projectname | |
| )) | |
| def dev_migrate(): | |
| local('DJANGO_SETTINGS_MODULE=%s.settings_development python manage.py migrate %s' %( | |
| projectname, | |
| projectname | |
| )) | |
| ### DEPLOYMENT ### | |
| def minify(): | |
| minify_files( | |
| css_files, | |
| '%s/all.min.css' %(static_dir), | |
| '%s/css_version.py' %(projectname) | |
| ) | |
| minify_files( | |
| js_files, | |
| '%s/all.min.js' %(static_dir), | |
| '%s/js_version.py' %(projectname), | |
| is_css=False | |
| ) | |
| def minify_files(files, output_file, version_file, is_css=True): | |
| minified = '' | |
| for file in files: | |
| with open('%s/%s' %(static_dir, file)) as inputFile: | |
| srcText = inputFile.read() | |
| minified += "\n\n/*%s*/\n\n" %(file) | |
| if is_css: | |
| minified += cssmin(srcText) | |
| else: | |
| minified += jsmin(srcText) | |
| output = open(output_file, 'w') | |
| output.write(minified) | |
| output.close() | |
| version_file = open(version_file, 'w') | |
| version_file.write('VERSION = %s' %(timestamp)) | |
| version_file.close() | |
| def deploy(): | |
| minify() | |
| # TODO : Burada yeni versiyon dosyaları oluştuğu için git commit metodu çalışmalı ya da uyarı verilmeli :/ | |
| # Sunucuda gerekli kurulumları yap. | |
| sudo('apt-get -y install nginx python-pip python-virtualenv unzip gcc python-dev libmysqlclient-dev') | |
| if exists(env.project_env_home) == False: | |
| run('virtualenv --distribute %s' %(env.project_env_home)) | |
| # Projeyi arşivle ve sunucuya gönder. | |
| create_archive() | |
| archive_file = '/tmp/%s.zip' %(timestamp) | |
| put(archive_file, archive_file) | |
| local('rm -rf %s' %(archive_file)) | |
| # Proje dizinini oluştur. | |
| run('mkdir -p %s' %(env.current_release_dir)) | |
| with cd(env.current_release_dir): | |
| run('unzip %s -d %s' %( | |
| archive_file, | |
| env.current_release_dir | |
| )) | |
| run('rm -rf %s' %(archive_file)) | |
| run('%s install -r requirements.txt' %(env.pip_bin)) | |
| current_dir = '%s/current' %(env.release_dir) | |
| run('rm -f %s' %(current_dir)) | |
| run('ln -s %s %s' %(env.current_release_dir, current_dir)) | |
| restart() | |
| def restart(): | |
| current_dir = '%s/current' %(env.release_dir) | |
| if exists(current_dir) == False: | |
| raise SystemExit('current directory not found.') | |
| with cd(current_dir), shell_env(DJANGO_SETTINGS_MODULE=env.django_settings_module): | |
| if exists(env.uwsgi_pid_file): | |
| if run('ls %s' %(env.uwsgi_pid_file), quiet=True).succeeded: | |
| try: | |
| run('kill -INT `cat %s`' %(env.uwsgi_pid_file)) | |
| time.sleep(3) | |
| except SystemExit: | |
| pass | |
| run('rm -f %s' %(env.uwsgi_pid_file)) | |
| run('%s manage.py syncdb' %(env.python_bin)) | |
| run('%s manage.py migrate %s' %(env.python_bin, projectname)) | |
| run('%s --chdir=%s --module=%s --env DJANGO_SETTINGS_MODULE=%s --master --pidfile=%s --socket=127.0.0.1:8000 --processes=5 --daemonize=%s' %( | |
| env.uwsgi_bin, | |
| current_dir, | |
| env.module_name, | |
| env.django_settings_module, | |
| env.uwsgi_pid_file, | |
| env.uwsgi_log_file | |
| )) | |
| if exists('/etc/nginx/sites-enabled/default', use_sudo=True): | |
| sudo('rm -rf /etc/nginx/sites-enabled/default') | |
| sudo('rm -f /etc/nginx/sites-enabled/vhost.conf') | |
| sudo('ln -s %s /etc/nginx/sites-enabled/vhost.conf' %(env.nginx_vhost_file)) | |
| sudo('service nginx restart') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| upstream django { | |
| server 127.0.0.1:8000; | |
| } | |
| server { | |
| listen 80; | |
| charset utf-8; | |
| access_log /var/log/nginx/projectname.access.log; | |
| error_log /var/log/nginx/projectname.error.log; | |
| location / { | |
| uwsgi_pass django; | |
| include /home/ubuntu/projectname/current/uwsgi_params; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| uwsgi_param QUERY_STRING $query_string; | |
| uwsgi_param REQUEST_METHOD $request_method; | |
| uwsgi_param CONTENT_TYPE $content_type; | |
| uwsgi_param CONTENT_LENGTH $content_length; | |
| uwsgi_param REQUEST_URI $request_uri; | |
| uwsgi_param PATH_INFO $document_uri; | |
| uwsgi_param DOCUMENT_ROOT $document_root; | |
| uwsgi_param SERVER_PROTOCOL $server_protocol; | |
| uwsgi_param HTTPS $https if_not_empty; | |
| uwsgi_param REMOTE_ADDR $remote_addr; | |
| uwsgi_param REMOTE_PORT $remote_port; | |
| uwsgi_param SERVER_PORT $server_port; | |
| uwsgi_param SERVER_NAME $server_name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment