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

发布于 2024-12-20 10:54:52 字数 1388 浏览 1 评论 0原文

我的 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
    ))

I have a fabfile.py for my django project that I'm using for auto-deploy. I use getpass to prompt the user for sudo password on the server, then set env.password, however, when I run other commands, like run('sudo mkdir foo/bar') I get prompted for sudo password with each time a sudo command is run.

Here is the code (sorry for the horrible code block, still a new poster to the site):

 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
    ))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕头说它不想醒 2024-12-27 10:54:52

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

Don't write your own code to request a password; try using Fabric's sudo command instead.

甜味超标? 2024-12-27 10:54:52

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

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

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

I think your problem probably cause by the newlines,try this:

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

print password
env.user = 'user'
env.password = password
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文