使用 pythonSDK 停止 OpenStack 服务器实例

Stop OpenStack Server Instance with pythonSDK

我正在尝试创建一个端点以使用 pythonSDK 停止我的 openstasck 服务器实例,但是当我尝试访问 / 时出现错误 stop_server,你能帮我吗?

from flask import Flask, request
from openstack import connection
from pprint import pprint
import json
import os

app = Flask(__name__)
# Source the openstack project rc file before running this app, to create the
# environment variables needed
conn = connection.Connection(auth_url=os.environ['OS_AUTH_URL'],
                             project_name=os.environ['OS_PROJECT_NAME'],
                             username=os.environ['OS_USERNAME'],
                             password=os.environ['OS_PASSWORD'],
                             user_domain_id="default",
                             project_domain_id=os.environ['OS_PROJECT_DOMAIN_ID'])


@app.route("/")
def hello():
    return "Hello from Flask!"


@app.route("/list")
def list_openstack_resources():
    # Check if connection is established
    print("conn: ", conn)

    # Print list of servers, images, flavors, endpoints, projects, users
    server_list = list(conn.compute.servers())
    image_list = list(conn.compute.images())
    flavor_list = list(conn.compute.flavors())
    project_list = list(conn.identity.projects())
    user_list = list(conn.identity.users())
    pprint(server_list)
    pprint(image_list)
    pprint(flavor_list)
    pprint(project_list)
    pprint(user_list)
    return "List printed to stdout"


@app.route("/create_server")
def create_server():
    # Check if connection is established
    print("conn: ", conn)

    # Create the volume first
    volume_size = request.args.get('volume_size')
    print("Starting to create volume with size (in GiB): ", volume_size)
    volume = conn.create_volume(size=volume_size,
                                image="cirros-0.5.1-x86_64-disk",
                                wait=True,
                                bootable=True,
                                )
    print("Create volume: ", json.dumps(volume))

    # Create the server using the server_name parameter in the GET request
    server_name = request.args.get('server_name')
    print("Starting to create the server with name: ", server_name)
    server = conn.create_server(name=server_name,
                                flavor="m1.micro",
                                terminate_volume=True,
                                timeout=180,
                                boot_volume=volume.id,
                                key_name="test-app",
                                network='shared'
                                )
    print("Created server: ", json.dumps(server))
    return "Server create request sent!"

@app.route("/stop_server")
def stop_server():
    for server in conn.compute.servers():
        print(json.dumps(server))



@app.route("/delete_server")
def delete_server():
    print("delete: ", conn)
    server_id = request.args.get('server_id')
    delete_server = conn.delete_server(name=server_id, ignore_missing=False, force=False)
    print('removed', json.dumps(delete_server))


if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=8080)

我收到此错误:

类型错误

TypeError: 视图函数没有 return 有效响应。该函数要么 returned None 要么在没有 return 语句的情况下结束。

在技术client-server中,当client发送request时,server应该发送回response

但在函数 stop_server()delete_server() 中,您不使用 return textreturn render_template(...)return jsonify()return response(...) 等。 - 所以 Python 使用标准 return None (默认)并且 flask 不知道要发送给客户端什么 - 这会产生你的错误。

您应该至少发送空字符串 return "" 或最好发送有关成功的信息。

@app.route("/stop_server")
def stop_server():
    # ... your code ...
    return "success"

@app.route("/delete_server")
def delete_server():
    # ... your code ...
    return "success"