fabfile用户信息要求

fabfile user information requirement

远程服务器正在禁用通过 ssh 密钥的身份验证,因此当我部署新版本时,我需要输入我的密码(一些 LDAP 身份验证)。

但是,我的 fabfile 脚本将被许多开发人员使用。所以,每个人都必须以某种方式向脚本提供他的用户名。

我想到了这个:

def authenticate(login=None):
   if login is None:
      abort('You must provide your username')
   ...

@task
def deploy(username=None):
   authenticate(username)
   ...

@task
def init(username=None):
   authenticate(username)
   ...

@task
def rollback(username=None):
   authenticate(username)
   ...

@task
def restart_services(username=None, service=None):
   authenticate(username)
   ...

这很好用,但 DRY

是否有一种干净的方法来 authenticate 脚本用户?

如果你的问题是干的,你可以使用装饰器

def authenticate(f):
    @functools.wraps(f) 
    def wrapper(login, *args, **kwargs):
       if login is None:
          abort('You must provide your username')
       return f(*args, **kwargs)
    return wrapper

然后

@task
@authenticate
def deploy(whatever):
    ....