Commits

sodas tsai committed da8ec62

Fix deployment issues

Comments (0)

Files changed (4)

eggplant/fabfile/default_deploy_settings.py

 CLEAN_PY_IN_REMOTE = False
 CHECK_PIP_REQUIREMENTS = True
 COMPILE_PYC = True
+REMOTE_ENVIRONMENTS = {}
+UPDATE_YUM_PACKAGES = False
+YUM_PACKAGES = []
 
 # Apache
 APACHE_CONF_FILE = lambda: env.project_name
 SERVE_ROBOTS = False
 SERVE_STATIC = True
 URL_PREFIX = '/'
+WSGI_SCRIPT_DIR_NAME = lambda: env.project_name
 WSGI_SCRIPT_FILE_NAME = 'wsgi.py'
 WSGI_PROCESS_GROUP = 'apache'
 WSGI_PROCESS_NAME = lambda: env.project_name

eggplant/fabfile/deploy.py

 import datetime
 import sys
 import os
-from fabric.api import (hide, local, env, execute, abort, lcd, sudo, run, cd, puts, settings as fab_settings)
+from fabric.api import (hide, local, env, execute, abort, lcd, sudo, run, cd, puts, settings as fab_settings, quiet)
 from fabric.colors import red, green, yellow, cyan
 from fabric.contrib import django as fab_django
 from fabric.contrib.console import confirm
 from fabric.decorators import task, roles
 from fabric.state import output as output_level
 from fabric.utils import error
+import yajl as json
 
 from eggplant.utils import SettingsDict
 from eggplant.utils.fab import lsudo, virtual_env, fab_argument
         # Original git-ignore is used for SCM repository.
         # Now we need here is for remote base, so we copied/replaced the SCM one with IGNORE_FILE_LIST
         if ignore_file_list is not None and os.path.exists(ignore_file_list):
-            target_gitignore = '%s/.gitignore' % tmp_directory
+            target_gitignore = os.path.join(tmp_directory, '.gitignore')
+            copy_gitignore = False
+            if not os.path.exists(target_gitignore):
+                copy_gitignore = True
             # Copy only if they are different
-            with fab_settings(hide('everything'), warn_only=True):
+            with quiet():
                 diff_result = local('diff %s %s' % (ignore_file_list, target_gitignore))
                 if diff_result.return_code == 1:
-                    local(rsync(ignore_file_list, target_gitignore))
+                    copy_gitignore = True
+            if copy_gitignore:
+                local(rsync(ignore_file_list, target_gitignore))
 
         # Set project source root and append to PYTHONPATH
         env.project_src_root = project_src_root = tmp_directory
             if deploy_settings.get('COLLECT_STATIC'):
                 puts(cyan('Compile static files'))
                 with lcd(project_src_root):
-                    if 'collectstatic' in get_commands():
+                    if 'collectstatic' in get_commands() and \
+                       'django.contrib.staticfiles' in django_settings.INSTALLED_APPS:
                         local('python manage.py collectstatic --noinput -v 0')
-                    if 'compress' in get_commands():
+                    if 'compress' in get_commands() and 'compressor' in django_settings.INSTALLED_APPS:
                         # django-compressor
                         local('python manage.py compress')
                 if verbose != 0:
     project_settings_path = env.project_settings_path
     update_apache_conf = env.update_apache_conf
 
+    # Update environment
+    env['shell_env'] = deploy_settings.get('REMOTE_ENVIRONMENTS')
+
     # Define function to call
     if host_name in ('127.0.0.1', 'localhost'):
         is_remote = False
     with hide('everything'):
         remote_user = target_run('whoami', **capture_output)
         remote_group = target_run('id -g -n %s' % remote_user, **capture_output)
+    # Get eggplant settings in server
+    eggplant_server_setting = {}
+    with fab_settings(hide('everything'), warn_only=True):
+        eggplant_server_settings = target_run('cat /etc/eggplant.conf', **capture_output)
+        if eggplant_server_settings.return_code != 0:
+            eggplant_server_settings = '{}'
+        try:
+            eggplant_server_setting.update(json.loads(eggplant_server_settings))
+        except ValueError:
+            raise ValueError('Error eggplant configuration')
 
     print(yellow('Deploy to %s' % host_string, bold=True))
 
+    # Update YUM?
+    if deploy_settings.get('UPDATE_YUM_PACKAGES'):
+        print_title('Update Yum Packages')
+        allow_yum = eggplant_server_setting.get('ALLOW_YUM_UPDATE', True)
+        with fab_settings(hide('everything'), warn_only=True):
+            has_yum = target_run('which yum', **capture_output).return_code == 0
+        yum_packages = deploy_settings.get('YUM_PACKAGES')
+        if allow_yum and has_yum and len(yum_packages) != 0:
+            yum_package_string = ' '.join(yum_packages)
+            print(yellow('Updating yum packages'))
+            target_sudo('yum install -y %s' % yum_package_string)
+
     # Make room for deploy target
     puts('Deploy target is: %s' % remote_base_path)
     if not target_file_existence(remote_base_path):
             target_run('virtualenv %s' % venv_name)
 
     # Install apache conf
-    wsgi_script_dir_name = deploy_settings.get(
-        'WSGI_SCRIPT_DIR_NAME', os.path.join(remote_project_source_root, project_name)
+    wsgi_script_dir_name = os.path.abspath(
+        os.path.join(remote_project_source_root, deploy_settings.get('WSGI_SCRIPT_DIR_NAME'))
     )
     wsgi_script_file_name = deploy_settings.get('WSGI_SCRIPT_FILE_NAME')
     apache_conf_tmp_name = 'apache~%s.conf' % host_name
 Django==1.4.2
-Fabric==1.4.3
+Fabric==1.5.1
 Jinja2==2.6
 MySQL-python==1.2.4b5
 South==0.7.6
 django-fields==0.2.0
 django-storages==1.1.5
 netaddr==0.7.10
+paramiko==1.9.0
 pycrypto==2.6
 python-dateutil==2.1
 pytz==2012g
 six==1.2.0
-ssh==1.8.0
 wsgiref==0.1.2
 yajl==0.3.5
 
 setup(
     name='django-eggplant',
-    version='0.5.3',
+    version='0.5.5',
     author='sodas tsai',
     author_email='sodas@sodas.tw',
     packages=find_packages(exclude=('django_eggplant', 'eggplant_web')),
     include_package_data = True,
     install_requires = [
         'Django>=1.3',
-        'Fabric>=1.4.3',
+        'Fabric>=1.5',
         'Jinja2>=2.6',
         'django-crispy-forms>=1.2',
         'django-fields>=0.2.0',