def add_server_to_nginx(nginx_host, user, key_file, new_server): login = '******' % (user, nginx_host) with settings(host_string=login, key_filename=key_file): if contains("/etc/nginx/conf.d/backends", new_server, exact=False): print "Error: Server already exists in nginx backends!" else: print "Server does not exist in nginx backends!" sed("/etc/nginx/conf.d/backends", before="}", after=" server %s max_fails=1 fail_timeout=15s;\\n}" % (new_server,), use_sudo=False) remote_run("/usr/sbin/nginx -s reload")
def add_server_to_nginx(nginx_host, user, key_file, new_server): login = '******' % (user, nginx_host) with settings(host_string=login, key_filename=key_file): if contains("/etc/nginx/conf.d/backends", new_server, exact=False): print "Error: Server already exists in nginx backends!" else: print "Server does not exist in nginx backends!" sed("/etc/nginx/conf.d/backends", before="}", after=" server %s max_fails=1 fail_timeout=15s;\\n}" % (new_server, ), use_sudo=False) remote_run("/usr/sbin/nginx -s reload")
def run(*args, **kwargs): if env.hosts: if 'capture' in kwargs: del kwargs['capture'] # default behavior return remote_run(*args, **kwargs) else: return local(*args, **kwargs)
$ fab local deploy $ fab prod deploy TODO: Give time for this code to mature, then submit it to fabric/contrib. Copyright (C) 2011 by Denis Ryzhkov <*****@*****.**> MIT License, see http://opensource.org/licenses/MIT ''' __all__ = 'run sudo cd'.split() from fabric.api import env env.is_local = False # default #### run from fabric.api import run as remote_run, local as local_run run = lambda command, capture=False: local_run(command, capture=capture) if env.is_local else remote_run(command) #### sudo from fabric.api import sudo as remote_sudo local_sudo = lambda command, capture=False: local_run('sudo ' + command, capture=capture) sudo = lambda command, capture=False: local_sudo(command, capture=capture) if env.is_local else remote_sudo(command) #### cd from fabric.api import cd as remote_cd, lcd as local_cd from contextlib import contextmanager cd = lambda path: (local_cd if env.is_local else remote_cd)(path)
def update_remote(): for dir in REMOTE_DIRS: with cd(dir): remote_run('git pull origin master')