简体   繁体   English

在Ubuntu服务器上,如何从Flask中的python脚本调用“ sudo reload myproject”

[英]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.

相关问题 如何从 ZC5FD214CDD0D2B3B4272E 中的 python 容器脚本连接到 Ubuntu 上的本地 SQL 服务器 - How do I connect to the local SQL server on Ubuntu from a python script in Docker container 如何使用 sudo 运行 Python 脚本? (苹果电脑) - How do I run a Python script with sudo? (MAC) 如何在服务器上托管 function,我可以从我的 python 脚本调用它? - How do I host a function on a server, which I can call from my python script? 如何从 Flask 服务器中执行 Python 程序? - How do I execute a Python program from within a Flask server? 如何在ubuntu服务器上将matplotlib中的绘图输出到烧瓶中的html模板? - How do I output plots from matplotlib to an html template in flask on a ubuntu server? 如何在 Ubuntu 16.04 上的 apache2 服务器上运行 python cgi 脚本? - How do I run python cgi script on apache2 server on Ubuntu 16.04? 如何使用JavaScript从CGI脚本调用python函数? - How do I call a python function from a cgi script with javascript? 如何从不同的脚本调用方法? (Python) - How do I call a method from a different script? (Python) 如何调用python / flask服务器从服务器端功能重新加载客户端页面? - How to invoke python/flask server to reload client page from server-side function? 按功能重新加载python flask服务器 - Reload python flask server by function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM