[英]On an Ubuntu server, how do I call “sudo reload myproject” from a python script in Flask
I followed the tutorial here: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04 and created a Flask app hosted on an Ubuntu server on Digital Ocean. 我在这里遵循了该教程: https : //www.digitalocean.com/community/tutorials/how-to-serv-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04,并创建了Flask应用托管在Digital Ocean的Ubuntu服务器上。 To update the website, I need to ssh into my cloud server and call "sudo reload myproject" from the command line.
要更新网站,我需要ssh进入我的云服务器,并从命令行调用“ sudo reload myproject”。 I would like to automate this and have the python code in Flask call this command every time it gets a post request.
我想自动执行此操作,并在Flask中的python代码每次收到发布请求时都调用此命令。
I tried using os.system('sudo reload myproject') but that does not work. 我尝试使用os.system('sudo reload myproject'),但这不起作用。 How would I use Flask to reload the project?
我将如何使用Flask重新加载项目?
in /etc/sudoers.d/myOverrides.tmp, I have the following line: 在/etc/sudoers.d/myOverrides.tmp中,我有以下几行:
user ALL=NOPASSWD: ALL 用户ALL = NOPASSWD:ALL
the code in my flask module: 我的flask模块中的代码:
from flask import Flask, render_template, request
import os
@application.route("/", methods=['GET', 'POST'])
def hello():
os.system('sudo reload halo')
if (request.method == 'POST'):
print 'post request'
os.system('sudo reload halo')
return "<h1>reloaded</h1>"
else:
return "<h1>default</h1>"
When I type in "sudo reload halo" on the command line, it reloads without a problem. 当我在命令行中输入“ sudo reload halo”时,它将重新加载而没有问题。 I would like the Flask application to execute the same command.
我希望Flask应用程序执行相同的命令。
This seems like a bad idea to me. 对我来说,这似乎是个坏主意。 Providing your web app
sudo
access and making that call publicly available screams liability. 向您的Web应用程序提供
sudo
访问权限,并使该调用可公开获得尖叫责任。
Instead here are a couple of other options: 相反,这里有几个其他选项:
Automate it with a ssh command 使用ssh命令使其自动化
You can actually run a command over SSH without having to do a full login yourself. 实际上,您可以通过SSH运行命令,而无需亲自进行完整登录。 You can continue to use the user you are using at the moment, but set up passwordless login to use a public-private key pair.
您可以继续使用当前正在使用的用户,但是将无密码登录设置为使用公私钥对。
Then call ssh -i /path/to/keyfile username@server 'sudo reload halo'
然后调用
ssh -i /path/to/keyfile username@server 'sudo reload halo'
Auto reload with gunicorn 自动装满金枪鱼
gunicorn
has a --reload
option builtin you should consider using as well. gunicorn
内置了--reload
选项,您也应该考虑使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.