sodas tsai avatar sodas tsai committed 6a34f46

Refine code and process YUM dependency

Comments (0)

Files changed (8)

beanstalk/__init__.py

 
 # Pacakge information
 #-----------------------------------------------------------------------------------------------------------------------
-__version__ = '0.1.87'
+__version__ = '0.1.92'
 VERSION = tuple(map(lambda x: int(x), __version__.split('.')))
 BEANSTALK_ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
 

beanstalk/default_settings/app_settings.py

 import os
-import sys
 from six import callable
 from beanstalk.tasks.action import check_comments
 from beanstalk.tasks.action.django import collect_static, sync_db, south_migrate, switch_settings
 
 # Hooked actions
 #-----------------------------------------------------------------------------------------------------------------------
+
+
+def _get_action(settings, action_set):
+    project_actions = action_set.get(_(settings['PROJECT_TYPE']), [])
+    default_actions = action_set.get('default', [])
+
+    actions = []
+    for action_set in [default_actions, project_actions]:
+        for action in action_set:
+            if action not in actions:
+                actions.append(action)
+    return actions
+
+
 default_source_check_actions = {
-    'django': [check_comments],
+    'default': [check_comments],
 }
 default_pre_deploy_actions = {
     'django': [collect_static, switch_settings],
 }
-default_pre_install_actions = {}
+default_pre_install_actions = {
+
+}
 default_pre_send_actions = {}
 default_post_send_actions = {}
 default_post_install_actions = {
 
 # Before confirmation of deploy
 # Show issues or other warnings before doing someting to remote
-SOURCE_CHECK_ACTIONS = lambda x: default_source_check_actions.get(_(x['PROJECT_TYPE']), [])
+SOURCE_CHECK_ACTIONS = lambda x: _get_action(x, default_source_check_actions)
 # Start to work.
 # Before connect to remote. Actions will be performed once.
-PRE_DEPLOY_ACTIONS = lambda x: default_pre_deploy_actions.get(_(x['PROJECT_TYPE']), [])
+PRE_DEPLOY_ACTIONS = lambda x: _get_action(x, default_pre_deploy_actions)
 # We connected to the remote
 # But we have done nothing yet.
-PRE_INSTALL_ACTIONS = lambda x: default_pre_install_actions.get(_(x['PROJECT_TYPE']), [])
+PRE_INSTALL_ACTIONS = lambda x: _get_action(x, default_pre_install_actions)
 # We have remote app info now.
-PRE_SEND_ACTIONS = lambda x: default_pre_send_actions.get(_(x['PROJECT_TYPE']), [])
+PRE_SEND_ACTIONS = lambda x: _get_action(x, default_pre_send_actions)
 # The source code has been just delivered.
 # Beanstalk modifies your code to fit its settings after this stage.
 # (For example, env information related to the rmote like uwsgi port.
-POST_SEND_ACTIONS = lambda x: default_post_send_actions.get(_(x['PROJECT_TYPE']), [])
+POST_SEND_ACTIONS = lambda x: _get_action(x, default_post_send_actions)
 # The code has been cleared, modified, and normalized. You can perform db-migration here
-POST_INSTALL_ACTIONS = lambda x: default_post_install_actions.get(_(x['PROJECT_TYPE']), [])
+POST_INSTALL_ACTIONS = lambda x: _get_action(x, default_post_install_actions)
 # Things are almost finished. You can do clean job here. They will be performed only once
-POST_DEPLOY_ACTIONS = lambda x: default_post_deploy_actions.get(_(x['PROJECT_TYPE']), [])
+POST_DEPLOY_ACTIONS = lambda x: _get_action(x, default_post_deploy_actions)

beanstalk/default_settings/server_settings.py

-from beanstalk.tasks.action.server import create_database
+from beanstalk.tasks.action.server import (create_database, install_pip_requirements, compile_python_code,
+                                           create_venv, initialize_git, install_yum_packages, clean_old_items)
 
 
 def _(var):
 # Beanstalk tool behavior
 #-----------------------------------------------------------------------------------------------------------------------
 VERBOSE = 3
+ALLOW_INSTALL_YUM_PACKAGES = True
 
 # Beanstalk Installation
 #-----------------------------------------------------------------------------------------------------------------------
 # Hooked Actions
 #-----------------------------------------------------------------------------------------------------------------------
 PRE_SETUP_ACTIONS = []  # Only beanstalk settings
-POST_SETUP_ACTIONS = []  # Only beanstalk settings
+POST_SETUP_ACTIONS = [
+    clean_old_items,
+]  # Only beanstalk settings
 
 PRE_CREATE_ACTIONS = []  # project_name
 POST_CREATE_ACTIONS = [
     create_database,
+    create_venv,
+    initialize_git,
 ]  # project_name, uwsgi_port
 
 PRE_COMMIT_ACTIONS = []  # project_name
 PRE_RELOAD_ACTIONS = []  # project_name
 POST_RELOAD_ACTIONS = []  # project_name
 
-PRE_INSTALL_ACTIONS = []  # project_name
+PRE_INSTALL_ACTIONS = [
+    install_yum_packages,
+    install_pip_requirements,
+    compile_python_code
+]  # project_name
 POST_INSTALL_ACTIONS = []  # project_name
 
 PRE_COMMIT_ACTION = []  # project_name
 POST_COMMIT_ACTION = []  # project_name
+
+PRE_DELETE_ACTION = []  # project_name
+POST_DELETE_ACTION = []  # project_name
+
+PRE_ACTIVATE_ACTION = []  # project_name
+POST_ACTIVATE_ACTION = []  # project_name
+
+PRE_DEACTIVATE_ACTION = []  # project_name
+POST_DEACTIVATE_ACTION = []  # project_name

beanstalk/static/beanstalk_sudoer

+Cmnd_Alias RELOAD_HTTPD = /sbin/service httpd reload
+Cmnd_Alias INSTALL_YUM = /usr/bin/yum install *
+beanstalk ALL=(root) NOPASSWD: RELOAD_HTTPD, INSTALL_YUM

beanstalk/static/httpd_reload.sh

-#!/bin/sh
-service httpd reload

beanstalk/tasks/action/server.py

+import os
 from fabric.api import *
+from ground_soil.fabric import virtual_env
+from beanstalk.console.titles import action_title
+from beanstalk.paths.server import project_venv_path, project_source_path, project_base_path
 
 
 def create_database(db_name=None, db_user=None, db_password=None, permissions=None):
-    pass
-    # TODO: Create database
 #    if permissions is None:
 #        permissions = env.get(
 #            'db_permissions',
 #
 #    for sql_command in sql_commands:
 #        print sql_command.format(**db_info)
+    pass
+
+
+def install_pip_requirements():
+    print action_title('Install/Update packages in virtualenv')
+
+    project_name = env.project_name
+    project_source = project_source_path(project_name)
+    project_venv = project_venv_path(project_name)
+
+    with settings(virtual_env(project_venv)):
+        local('pip install -r %s' % os.path.join(project_source, 'requirements.txt'))
+
+
+def compile_python_code():
+    print action_title('Compile python code')
+
+    project_name = env.project_name
+    project_source = project_source_path(project_name)
+
+    local('find %s -name \'*.pyc\' -exec rm -rf {} \;' % project_source)
+    local('find %s -name \'*.pyo\' -exec rm -rf {} \;' % project_source)
+    local('python -m compileall -q %s' % project_source)
+
+
+def create_venv():
+    project_name = env.project_name
+
+    project_base = project_base_path(project_name)
+    project_venv = project_venv_path(project_name)
+
+    if not os.path.exists(project_venv):
+        print action_title('Initialize virtual env')
+        with lcd(project_base):
+            local('virtualenv venv')
+
+
+def initialize_git():
+    project_name = env.project_name
+    project_base = project_base_path(project_name)
+
+    # Initialize git
+    if not os.path.exists(os.path.join(project_base, '.git')):
+        print action_title('Make git repository')
+        with lcd(project_base):
+            local('git init')
+            local('touch .gitignore')
+            local('git add .gitignore')
+            local('git commit -m "Init commit"')
+
+
+def install_yum_packages():
+    beanstalk_settings = env.get('beanstalk_settings', None)
+    if beanstalk_settings is None or not beanstalk_settings['ALLOW_INSTALL_YUM_PACKAGES']:
+        return
+
+    project_name = env.project_name
+    project_source = project_source_path(project_name)
+    yum_package_list_path = os.path.join(project_source, 'yum_packages.txt')
+    if not os.path.exists(yum_package_list_path):
+        return
+
+    yum_packages = []
+    with open(yum_package_list_path, 'r') as yum_package_list:
+        for yum_package in yum_package_list:
+            yum_packages.append(yum_package[:-1])
+
+    yum_package_string = ' '.join(yum_packages)
+    yum_command = 'sudo yum install {0}'.format(yum_package_string)
+    local(yum_command)
+
+
+def clean_old_items():
+    print action_title('Clean old items')
+
+    def _exist_and_clean(path):
+        if os.path.exists(path):
+            local('rm -rf {0}'.format(path))
+
+    _exist_and_clean('/etc/sudoers.d/beanstalk_reload_httpd')
+    _exist_and_clean('/usr/sbin/httpd_reload')

beanstalk/tasks/app.py

     if not os.path.exists(beanstalk_local_base_path):
         print cyan('Make beanstalk_stack local base: ./{path}'.format(path=os.path.relpath(beanstalk_local_base_path)))
         local('mkdir -p {path}'.format(path=beanstalk_local_base_path))
+        local('touch {0}'.format(os.path.join(beanstalk_local_base_path, '__init__.py')))
 
     print magenta('Beanstalk-Stack app plant tool')
     print separator('=-')
     def core():
         run('bsjack server.deactivate_app:{project_name}'.format(project_name=project_name))
     execute(core)
+
+
+@beanstalk_task(beanstalk_role='app')
+def reload_remote(settings_path=None, **settings_patches):
+    """Deactivate apps in the remote
+    """
+    beanstalk_settings = load_role_settings(additional_settings_path=settings_path, **settings_patches)
+    set_verbose_level()
+    load_web_servers()
+
+    project_name = beanstalk_settings['PROJECT_NAME']
+
+    @roles('web_servers')
+    def core():
+        run('bsjack server.reload_app:{0}'.format(project_name))
+    execute(core)

beanstalk/tasks/server.py

         with open(global_uwsgi_ini_path, 'w') as f:
             f.write(global_uwsgi_ini_content)
 
-    # Apache command
-    httpd_cmd_path = '/usr/sbin/httpd_reload'
-    if not os.path.exists(httpd_cmd_path):
-        print section_title('Create apache-reload command')
-        httpd_cmd_source = os.path.join(BEANSTALK_ROOT_PATH, 'static/httpd_reload.sh')
-        local('/bin/cp -f %s %s' % (httpd_cmd_source, httpd_cmd_path))
-        local('chmod +x %s' % httpd_cmd_path)
-
-    # Give beanstalk permission
-    sudoer_file = '/etc/sudoers.d/beanstalk_reload_httpd'
+    # Give beanstalk permission for some commands
+    sudoer_file = '/etc/sudoers.d/beanstalk'
     if not os.path.exists(sudoer_file):
-        print section_title('Give beanstalk permission for apache-reload command')
-        local('echo "beanstalk ALL=(root) NOPASSWD: %s" > %s' % (httpd_cmd_path, sudoer_file))
+        print section_title('Give beanstalk permission for commands: yum-install and apache-reload')
+        sudoer_source = os.path.join(BEANSTALK_ROOT_PATH, 'static/beanstalk_sudoer')
+        local('/bin/cp -f %s %s' % (sudoer_source, sudoer_file))
         local('chmod 440 %s' % sudoer_file)
 
     # Make beanstalk access ssh
     project_base = project_base_path(project_name)
     project_source = project_source_path(project_name)
     project_logs = project_logs_path(project_name)
-    project_venv = project_venv_path(project_name)
     project_run = project_run_path(project_name)
 
     if os.path.exists(project_base):
     local('setfacl -m u:apache:rwx %s' % project_logs)
     local('setfacl -m u:apache:rwx %s' % project_run)
 
-    # Initialize venv
-    if not os.path.exists(project_venv):
-        print section_title('Initialize virtual env')
-        with lcd(project_base):
-            local('virtualenv venv')
-
-    # Initialize git
-    if not os.path.exists(os.path.join(project_base, '.git')):
-        print section_title('Make git repository')
-        with lcd(project_base):
-            local('git init')
-            local('touch .gitignore')
-            local('git add .gitignore')
-            local('git commit -m "Init commit"')
-
-    execute(activate_app, project_name, settings_path=settings_path, **settings_patches)
-
     # Post-create actions
     run_hooked_actions('POST_CREATE_ACTIONS')
 
+    # Activate it
+    execute(activate_app, project_name, settings_path=settings_path, **settings_patches)
+
 
 @beanstalk_task(beanstalk_role='server')
 def delete_app(project_name, settings_path=None, **settings_patches):
 
     # delete
     if remove:
+        run_hooked_actions('PRE_DELETE_ACTION')
+
         print section_title('Remove app @ %s' % project_base)
         execute(deactivate_app, project_name, settings_path=settings_path, **settings_patches)
         local('rm -rf %s' % project_base)
 
+        run_hooked_actions('POST_DELETE_ACTION')
+
 
 @beanstalk_task(beanstalk_role='server')
 def install_app(project_name, settings_path=None, **settings_patches):
 
     run_hooked_actions('PRE_INSTALL_ACTIONS')
 
-    project_source = project_source_path(project_name)
-    project_venv = project_venv_path(project_name)
     project_run = project_run_path(project_name)
 
-    print section_title('Install/Update packages in virtualenv')
-    with settings(virtual_env(project_venv)):
-        local('pip install -r %s' % os.path.join(project_source, 'requirements.txt'))
-
-    print section_title('Compile python code')
-    local('find %s -name \'*.pyc\' -exec rm -rf {} \;' % project_source)
-    local('find %s -name \'*.pyo\' -exec rm -rf {} \;' % project_source)
-    local('python -m compileall -q %s' % project_source)
-
     print section_title('Setup uwsgi')
     # edit ini
     uwsgi_path = project_uwsgi_path(project_name)
     run_hooked_actions('PRE_COMMIT_ACTION')
 
     project_base = project_base_path(project_name)
-    with settings(lcd(project_base), hide('warnings'), warning=True):
+    with settings(lcd(project_base), hide('warnings'), warn_only=True):
         local('git add -A && git commit -m "state at %s"' % datetime.datetime.now().strftime('%s'))
 
     run_hooked_actions('POST_COMMIT_ACTION')
     load_role_settings(additional_settings_path=settings_path, **settings_patches)
     set_verbose_level()
     project_run = project_run_path(project_name)
+    env.project_name = project_name
+
+    run_hooked_actions('PRE_ACTIVATE_ACTION')
 
     with open(apache_conf_path(), 'r') as f:
         apache_conf = f.read()
         ]
         sed_command = '\\n'.join(commands)
         local(sed('$ a\\{command}'.format(command=sed_command), apache_conf_path()))
-        local('sudo /usr/sbin/httpd_reload')
+        local('sudo service httpd reload')
+
+    run_hooked_actions('POST_ACTIVATE_ACTION')
 
 
 @beanstalk_task(beanstalk_role='server')
 def deactivate_app(project_name, settings_path=None, **settings_patches):
     """Remove app from apache httpd
     """
+    env.project_name = project_name
+
+    run_hooked_actions('PRE_DEACTIVATE_ACTION')
+
     load_role_settings(additional_settings_path=settings_path, **settings_patches)
     set_verbose_level()
     local(sed('/# {name}/,+7d'.format(name=project_name), apache_conf_path()))
-    local('sudo /usr/sbin/httpd_reload')
+    local('sudo service httpd reload')
+
+    run_hooked_actions('POST_DEACTIVATE_ACTION')
 
 
 @beanstalk_task(beanstalk_role='server')
 def add_user(tmp_key_path, clean_tmp_key=False):
     """Add a user's SSH public key to beanstalk
     """
+    # NOTE: Maybe add action hooks
     tmp_key_path = os.path.expanduser(tmp_key_path)
 
     with open(tmp_key_path, 'r') as f:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.