env.password在fab文件中设置,但进程仍然多次询问sudo密码

时间:2011-12-09 02:47:49

标签: python fabric

我的django项目有一个fabfile.py,我用它进行自动部署。我使用getpass在服务器上提示用户输入sudo密码,然后设置env.password,但是,当我运行其他命令时,比如run(' sudo mkdir foo / bar')我会收到sudo密码提示每次运行sudo命令时。

这是代码(抱歉可怕的代码块,仍然是网站的新海报):

 def initial_deploy(*args, **kwargs):
        args = dict([(x, True) for x in args] + kwargs.items())
        env.user = args.get('user','webiken')
        password = getpass('Please provide sudo password for user %(user)s ' %               dict(user=env.user))
        if password:
          env['password'] = password
          env['sudo_prompt'] = 'sending sudo password...'
         app_name = args.get('app','webiken-demo')
         now = datetime.now()
            year = now.year
        dt = now.strftime('%Y-%m-%d_%H:%M:%S')
        cmd = 'sudo rm -rf /opt/%(app_name)s' % dict(app_name=app_name)
        run(cmd)
        cmd = 'sudo mkdir -p /opt/%(app_name)s/%(year)s/' % dict(
    app_name = app_name,
    year = year
    )
        run(cmd)
        cmd = '/opt/%(app_name)s/%(year)s' % dict(
        app_name = app_name,
        year = year
    )
        with cd(cmd):
    run('sudo git clone git@localhost:%(app_name)s.git %(now)s' % dict(
        app_name = app_name,
        now = dt
    ))
        run('sudo ln -s /opt/%(app_name)s/%(year)s/%(now)s /opt/%(app_name)s/current' % dict(
    app_name = app_name,
    year = year,
    now = dt
    ))

2 个答案:

答案 0 :(得分:2)

不要编写自己的代码来申请密码;尝试使用Fabric的sudo command代替。

答案 1 :(得分:0)

我认为你的问题可能是由换行引起的,试试这个:

f = open("../pass.txt", 'r')
password=str(f.read()).strip()
f.close()

print password
env.user = 'user'
env.password = password