2012-11-01 7 views
6

Ich verwende Fabric, um eine Bereitstellung zu automatisieren. In diesem Prozess verwende ich die prompt Funktion, um dem Benutzer einige Eingaben zu stellen. Insbesondere muss ich nach einem Passwort fragen, und ich möchte den Wert, den der Benutzer eingibt, wie Python getpass verstecken. Ich möchte prompt wegen der Behandlung von key und validate args verwenden.Erhalte ein Passwort vom Benutzer in Fabric, nicht den Wert

Gibt es eine eingebaute Fabric-Möglichkeit, oder muss ich prompt source ändern (eventuell eine Pull-Anforderung senden)?

Antwort

6

Sie könnten in der Lage sein prompt_for_password verwenden in fabric.network

def prompt_for_password(prompt=None, no_colon=False, stream=None): 
    """ 
    Prompts for and returns a new password if required; otherwise, returns 
    None. 

    A trailing colon is appended unless ``no_colon`` is True. 

    If the user supplies an empty password, the user will be re-prompted until 
    they enter a non-empty password. 

    ``prompt_for_password`` autogenerates the user prompt based on the current 
    host being connected to. To override this, specify a string value for 
    ``prompt``. 

    ``stream`` is the stream the prompt will be printed to; if not given, 
    defaults to ``sys.stderr``. 
    """ 
    from fabric.state import env 
    handle_prompt_abort("a connection or sudo password") 
    stream = stream or sys.stderr 
    # Construct prompt 
    default = "[%s] Login password for '%s'" % (env.host_string, env.user) 
    password_prompt = prompt if (prompt is not None) else default 
    if not no_colon: 
     password_prompt += ": " 
    # Get new password value 
    new_password = getpass.getpass(password_prompt, stream) 
    # Otherwise, loop until user gives us a non-empty password (to prevent 
    # returning the empty string, and to avoid unnecessary network overhead.) 
    while not new_password: 
     print("Sorry, you can't enter an empty password. Please try again.") 
     new_password = getpass.getpass(password_prompt, stream) 
    return new_password 

Es sieht so aus, wie Stoff abruft Passwort für ssh, Sie dann diese auf env mit:

def set_password(password): 
    from fabric.state import env 
    env.password = env.passwords[env.host_string] = password 

Key ist leicht austauschbar durch Einstellung env, aber sieht aus, als ob Sie sich selbst validieren müssen ...

Verwandte Themen