def selectively_attach(func): if not env.roles and not env.hosts: return roles(*role_list)(func) else: if env.hosts: func = hosts(*env.hosts)(func) if env.roles: func = roles(*env.roles)(func) return func
def test_decorator_incompatibility_on_task(): from fabric.decorators import task, hosts, runs_once, roles def foo(): return "foo" foo = task(foo) # since we aren't setting foo to be the newly decorated thing, its cool hosts('me@localhost')(foo) runs_once(foo) roles('www')(foo)
def selectively_attach(func): """Only decorate if nothing specified on command line""" # pylint: disable=W0142 if not env.roles and not env.hosts: return roles(*role_list)(func) else: if env.hosts: func = hosts(*env.hosts)(func) if env.roles: func = roles(*env.roles)(func) return func
def chef_query(query, api=None, hostname_attr=DEFAULT_HOSTNAME_ATTR, environment=_default_environment): """A decorator to use an arbitrary Chef search query to find nodes to execute on. This is used like Fabric's ``roles()`` decorator, but accepts a Chef search query. Example:: from chef.fabric import chef_query @chef_query('roles:web AND tags:active') @task def deploy(): pass .. versionadded:: 0.2.1 """ api = _api(api) if api.version_parsed < Environment.api_version_parsed and environment is not None: raise ChefAPIVersionError( 'Environment support requires Chef API 0.10 or greater') rolename = 'query_' + query env.setdefault('roledefs', {})[rolename] = Roledef(query, api, hostname_attr, environment) return lambda fn: roles(rolename)(fn)
def chef_query(query, api=None, hostname_attr=DEFAULT_HOSTNAME_ATTR, environment=_default_environment): api = _api(api) if api.version_parsed < Environment.api_version_parsed and environment is not None: raise ChefAPIVersionError('Environment support requires Chef API 0.10 or greater') rolename = 'query_'+query env.setdefault('roledefs', {})[rolename] = Roledef(query, api, hostname_attr, environment) return lambda fn: roles(rolename)(fn)
def setup_local(): with cd('%s' %(WWW_DIR_PATH)): base_dir = "%s-%s" %(SITE_NAME,SITE_EXTENSION) local('mkdir %s-%s' base_dir) with cd(base_dir): local('virtualenv env-%s --no-site-packages' %(SITE_NAME)) #Sanity check: pip is not global pip pip_location = local('which pip') if not re.match(r'env-%s' %(SITE_NAME), pip_location): raise Exception #Setup log files local('mkdir logs') with cd('logs'): local('touch error.log access.log') #Pull latest from repo local('svn co %s %s' %(SVN_REPO, SITE_NAME) local('pip install django south MySQL-python') @roles('staging') def setup_staging(name, extension): with cd('%s' %(WWW_DIR_PATH)): sudo('mkdir -p C_%s-%s/www/http_docs' %(SITE_NAME, SITE_EXTENSION)) with cd('www'): sudo('virtualenv env-%s --no-site-packages' %(SITE_NAME)) pip_location = sudo('which pip') if not re.match(r'env-%s' %(SITE_NAME), pip_location): raise Exception sudo('mkdir logs') sudo('touch error.log access.log') with cd('http_docs'): sudo('svn co %s %s' %(SVN_REPO, SITE_NAME) @roles('staging') def reload_staging(): with cd('%s/C_%s-%s/www/http_docs/%s' %(WWW_DIR_PATH, SITE_NAME, SITE_EXTENSION)): sudo('svn update') sudo('touch -c ../../django.wsgi') def deploy(): with cd('%s/C_%s-%s/www' %(WWW_DIR_PATH, SITE_NAME, SITE_EXTENSION)): sudo('touch -c django.wsgi')
def func_wrapper(f): f = roles(*roles_list)(f) @functools.wraps(f) def inner(*args, **kwargs): s = set(itertools.chain(*[x for k, x in env.roledefs.iteritems() if k in role_set])) if env.host_string not in s: return None else: return f(*args, **kwargs) return inner
def getTasks(self, role=None): """ Get all tasks of this L{Service} object. Intended to be used like:: globals().update(Service('name').getTasks()) at the module level of a fabfile. @returns: L{dict} of L{fabric.tasks.Task} """ tasks = prefixedMethods(self, TASK_PREFIX) tasks = ((_stripPrefix(t), t) for t in tasks) tasks = ((name, task(name=name)(t)) for name, t in tasks) if role: tasks = ((name, roles(role)(t)) for name, t in tasks) return dict(tasks)
def __init__(self, hosts=(), roles=(), runs_once=False, **kw): super(ManagedTask, self).__init__() if 'name' in kw: self.name = kw['name'] else: if hasattr(self.__class__, 'name'): self.name = getattr(self.__class__, 'name') if self.name == 'undefined': self.name = self.__class__.__name__.lower() self._decorator = lambda x: x if roles: self._decorator = lambda x: fab_api.roles(roles)(x) if hosts: self._decorator = lambda x: fab_api.hosts(hosts)(x) if runs_once: self._decorator = lambda x: fab_api.runs_once()(x) for _, f in self.getCommands().items(): setattr(self, f, self._decorator(getattr(self, f))) self.__createTasks()
def chef_query(query, api=None, hostname_attr=DEFAULT_HOSTNAME_ATTR, environment=_default_environment): """A decorator to use an arbitrary Chef search query to find nodes to execute on. This is used like Fabric's ``roles()`` decorator, but accepts a Chef search query. Example:: from chef.fabric import chef_query @chef_query('roles:web AND tags:active') @task def deploy(): pass .. versionadded:: 0.2.1 """ api = _api(api) if api.version_parsed < Environment.api_version_parsed and environment is not None: raise ChefAPIVersionError("Environment support requires Chef API 0.10 or greater") rolename = "query_" + query env.setdefault("roledefs", {})[rolename] = Roledef(query, api, hostname_attr, environment) return lambda fn: roles(rolename)(fn)
def _pull_live_media_from_staging(): """ Marginally different from _pull_media; uses remote variables for local paths etc., as the local environment is presumed to be staging server. """ if env['host'] == PRODUCTION_HOST_2: # No need to pull media twice return non_env_remote_media_path = _fetch_remote_variable(REMOTE_MEDIA_DIR) local_media_dir = os.getenv(ENV_MEDIA_DIR_VARIABLE) local('rsync -avz {}:{}/ {}'.format(env['host_string'], non_env_remote_media_path, local_media_dir)) deploy_staging = roles('staging')(_deploy) deploy_live = roles('live')(_deploy) pull_staging_data = roles('staging')(_pull_data) pull_live_data = roles('live')(_pull_data) pull_live_data_from_staging = roles('live')(_pull_live_data_from_staging) pull_staging_media = roles('staging')(_pull_media) pull_live_media = roles('live')(_pull_media) pull_live_media_from_staging = roles('live')(_pull_live_media_from_staging) @roles('live') def purge_cache():
from fabric.api import env, roles from fusionbox.fabric.django.new import stage, deploy def dev(): env.project_name = '{{ project_name }}.dev' env.vassal_name = '{{ project_name }}_dev' return ['fusionbox@{{ project_name }}.dev.fusionbox.com'] # def live(): # env.project_name = '{{ project_name }}.com' # env.vassal_name = '{{ project_name }}_com' # return ['fusionbox@demo.{{ project_name }}.com'] env.roledefs['dev'] = dev #env.roledefs['live'] = live stage = roles('dev')(stage) #deploy = roles('live')(deploy)
def test_roles_taskcall(self, fn): t = roles('foo')(task()(fn)) assert visitor.unwrap(t) is fn
def test_task_roles(self, fn): t = task(roles('foo')(fn)) assert visitor.unwrap(t) is fn
def wait_for_file(path, roles=roles()): wait('cat {}'.format(path), message="Waiting for a file")
from fabric.api import env, roles from fusionbox.fabric.django.new import stage, deploy def dev(): env.project_name = 'airmozilla.dev' env.vassal_name = 'airmozilla_dev' return ['*****@*****.**'] # def live(): # env.project_name = 'airmozilla.com' # env.vassal_name = 'airmozilla_com' # return ['*****@*****.**'] env.roledefs['dev'] = dev #env.roledefs['live'] = live stage = roles('dev')(stage) #deploy = roles('live')(deploy)