Commits

sodas tsai committed 1353e41

Use custom task class to update fabric env

  • Participants
  • Parent commits c076cae

Comments (0)

Files changed (9)

File beanstalk/__init__.py

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

File beanstalk/bsjack

         print '    {0} --list'.format(command_name)
         print ''
         with hide('running'):
-            local('PYTHONPATH={python_path} fab -f {fab_file} -l -F nested'.format(**exec_context))
+            local('PYTHONPATH={python_path} fab -f {fab_file} -l'.format(**exec_context))
         print ''
         print 'Beanstalk-Stack is built with ' + green('Python-Fabric') + '.'
         print 'So you can pass all arguments for fabric to {0}.'.format(command_name)

File beanstalk/decorators.py

 from functools import wraps
-from fabric.api import env
+from fabric.api import env, task
+from beanstalk.tasks.base_class import BeanstalkTask
 
 
 def beanstalk_role(role):
             return fn(*args, **kwargs)
         return wrapped
     return wrapper
+
+
+def beanstalk_task(*dec_args, **dec_kwargs):
+    # INVOKED: This decorator is used with "()"
+    invoked = bool(not dec_args or dec_kwargs)
+
+    if invoked:
+        # If invoked, func is not passed as the first argument of this decorator
+        return task(task_class=BeanstalkTask, *dec_args, **dec_kwargs)
+    else:
+        # If not invoked, func is passed as the first argument of this decorator
+        return task(task_class=BeanstalkTask)(dec_args[0])

File beanstalk/tasks/app.py

 from beanstalk import (IDENTIFIER as beanstalk_identifier, BEANSTALK_LOCAL_BASE_PATH,
                        __version__ as beanstalk_version_string, VERSION as beanstalk_version_tuple)
 from beanstalk.tasks.utils import set_verbose_level, load_role_settings, load_web_servers, run_hooked_actions
-from beanstalk.decorators import beanstalk_role
+from beanstalk.decorators import beanstalk_task
 from beanstalk.console.titles import section_title, tool_title, separator
 from beanstalk.validator import validate_web_server, validate_file_existence, validate_project_type, supported_projects
 
         })
 
 
-@task
-@beanstalk_role('app')
+@beanstalk_task(beanstalk_role='app')
 def plant(VERBOSE=0):
     """plant beanstalk-stack for project in current work directory
     """
     print green('Planted. Grow up now!')
 
 
-@task
-@beanstalk_role('app')
+@beanstalk_task(beanstalk_role='app')
 def deploy(settings_path=None, **settings_patches):
     """Deploy project in current work folder to beanstalk-stack
     """
             print green('Deployed!')
 
 
-@roles('web_servers')
-@beanstalk_role('app')
+@beanstalk_task(beanstalk_role='app')
 def deploy_remote_core():
     """ The part to run on each server
     """
     print separator(' . ')
 
 
-@task
-@beanstalk_role('app')
+@beanstalk_task(beanstalk_role='app')
 def activate(settings_path=None, **settings_patches):
     """Activate apps in the remote
     """
     execute(core)
 
 
-@task
-@beanstalk_role('app')
+@beanstalk_task(beanstalk_role='app')
 def deactivate(settings_path=None, **settings_patches):
     """Deactivate apps in the remote
     """

File beanstalk/tasks/base_class.py

+from fabric.state import env
+from fabric.tasks import WrappedCallableTask
+
+
+class BeanstalkTask(WrappedCallableTask):
+    def __init__(self, func, *args, **kwargs):
+        self.local_env = {
+            'user': 'beanstalk',  # Override the value from fabricrc or other command line options
+        }
+        # shortcut for settiing beanstalk_role for settings
+        if kwargs.get('beanstalk_role', None):
+            self.local_env['beanstalk_role'] = kwargs.pop('beanstalk_role', None)
+        super(BeanstalkTask, self).__init__(func, *args, **kwargs)
+
+    def run(self, *args, **kwargs):
+        # Update env
+        env.update(self.local_env)
+        # Run
+        return super(BeanstalkTask, self).run(*args, **kwargs)

File beanstalk/tasks/daemon.py

 from fabric.api import *
 from fabric.colors import *
 from beanstalk import BEANSTALK_GLOBAL_BASE_PATH
-from beanstalk.decorators import beanstalk_role
+from beanstalk.decorators import beanstalk_task
 from beanstalk.tasks.utils import load_role_settings
 
 _screen_width = 67
     return ' ' * spaces + '[{status}]'.format(status=status)
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 @with_settings(hide('running', 'status'))
 def start():
     """Start the uwsgi as a daemon
     sys.stdout.write('\n')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 @with_settings(hide('running', 'status'))
 def stop():
     """Stop the uwsgi daemon
     sys.stdout.write('\n')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 @with_settings(hide('running', 'status'))
 def reload():
     """Reload the uwsgi daemon
     sys.stdout.write('\n')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 @with_settings(hide('running', 'status'))
 def restart():
     """Restart the uwsgi daemon

File beanstalk/tasks/info.py

 import json
-from fabric.api import *
 from ground_soil.fabric import eval_kwargs
 from beanstalk import __version__ as version_str, VERSION as version_list
+from beanstalk.decorators import beanstalk_task
 
 
-@task
+@beanstalk_task
 def version(**kwargs):
     """Get version of beanstalk-stack
     """

File beanstalk/tasks/server.py

 from ground_soil.fabric import eval_kwargs, virtual_env
 from ground_soil.filesystem import temporary_directory, render_file, sed
 from beanstalk import IDENTIFIER, BEANSTALK_ROOT_PATH, BEANSTALK_GLOBAL_BASE_PATH
-from beanstalk.decorators import beanstalk_role
+from beanstalk.decorators import beanstalk_task
 from beanstalk.paths.server import (project_base_path, project_source_path, project_logs_path, project_venv_path,
                                     project_uwsgi_path, apache_conf_path, project_run_path)
 from beanstalk.tasks.utils import load_role_settings, set_verbose_level, run_hooked_actions
 #-----------------------------------------------------------------------------------------------------------------------
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def setup(settings_path=None, **settings_patches):
     """Setup remote base of beanstalk-stack in this server
     """
     run_hooked_actions('POST_SETUP_ACTIONS')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def create_app(project_name, settings_path=None, **settings_patches):
     """Create an app
     """
     run_hooked_actions('POST_CREATE_ACTIONS')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def delete_app(project_name, settings_path=None, **settings_patches):
     """Delete an app
     """
         local('rm -rf %s' % project_base)
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def install_app(project_name, settings_path=None, **settings_patches):
     """Install an app (call when u updated the code)
     """
     run_hooked_actions('POST_INSTALL_ACTIONS')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def reload_app(project_name, settings_path=None, **settings_patches):
     """Update an app (call when u updated the code)
     """
     run_hooked_actions('POST_RELOAD_ACTIONS')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def commit_app(project_name, settings_path=None, **settings_patches):
     """Commit app
     """
     run_hooked_actions('POST_COMMIT_ACTION')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def activate_app(project_name, settings_path=None, **settings_patches):
     """Add app to apache httpd
     """
         local('sudo /usr/sbin/httpd_reload')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def deactivate_app(project_name, settings_path=None, **settings_patches):
     """Remove app from apache httpd
     """
     local('sudo /usr/sbin/httpd_reload')
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def build_venv(project_name, settings_path=None, **settings_patches):
     """Build virtual env of target app.
     """
             local('pip intall -r %s' % requirments_path)
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def app_info(project_name, settings_path=None, **settings_patches):
     """Get info of an app
     """
         print separator('-', 40)
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def list_apps(settings_path=None, **settings_patches):
     """Get list of apps
     """
         print app
 
 
-@task
-@beanstalk_role('server')
+@beanstalk_task(beanstalk_role='server')
 def add_user(tmp_key_path, clean_tmp_key=False):
     """Add a user's SSH public key to beanstalk
     """

File beanstalk/tasks/users.py

 import hashlib
 from fabric.api import *
 from ground_soil.filesystem import rsync
-from beanstalk.decorators import beanstalk_role
+from beanstalk.decorators import beanstalk_task
 from beanstalk.tasks.utils import load_role_settings, load_web_servers
 from beanstalk.console.titles import tool_title
 from beanstalk.validator import validate_file_existence
 
 
-@task
-@beanstalk_role('app')
+@beanstalk_task(beanstalk_role='app')
 def add(settings_path=None, **settings_patches):
     """Add current user to beanstalk-stack server
     """