env.password 在 fab 文件中设置,但进程仍然多次询问 sudo 密码
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要编写自己的代码来请求密码;尝试使用 Fabric 的
sudo
命令 代替。Don't write your own code to request a password; try using Fabric's
sudo
command instead.我认为你的问题可能是由换行符引起的,试试这个:
I think your problem probably cause by the newlines,try this: