Pulumi Automation API 没有 运行 Pulumi CLI?
Pulumi Automation API doesn't run the Pulumi CLI?
我正在编写一个使用 Pulumi Automation API 的 Flask 应用程序。我正在关注 Automation API project examples。但是当我发送一个 POST 请求时,我得到一个 程序 运行,但没有可用的 Pulumi 引擎;重新运行 使用 pulumi CLI 报错。自动化 API 不应该 运行 CLI 本身吗?
Pulumi CLI 可用:
pulumi版
v3.24.1
编辑:我按照 pulumi over HTTP 示例进行操作,这是我的 app.py
import pulumi
from flask import Flask, request, make_response, jsonify
from pulumi import automation as auto
import os
from pulumi_aws import s3
app = Flask(__name__)
# This function defines our pulumi s3 static website in terms of the content that the caller passes in.
# This allows us to dynamically deploy websites based on user defined values from the POST body.
def create_pulumi_program(content: str):
# Create a bucket and expose a website index document
site_bucket = s3.Bucket("s3-website-bucket", website=s3.BucketWebsiteArgs(index_document="index.html"))
index_content = content
# Write our index.html into the site bucket
s3.BucketObject("index",
bucket=site_bucket.id,
content=index_content,
key="index.html",
content_type="text/html; charset=utf-8")
# Set the access policy for the bucket so all objects are readable
s3.BucketPolicy("bucket-policy",
bucket=site_bucket.id,
policy={
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
# Policy refers to bucket explicitly
"Resource": [pulumi.Output.concat("arn:aws:s3:::", site_bucket.id, "/*")]
},
})
# Export the website URL
pulumi.export("website_url", site_bucket.website_endpoint)
@app.route('/', methods=['GET'])
def home():
return "<h1>Hello</p>"
@app.route('/v1/code', methods=['POST'])
def create_handler():
content = request.get_json()
project_name = content.get('project_name')
stack_name = content.get('stack_name')
pulumi_access_token = request.headers['pulumi_access_token']
os.environ['PULUMI_ACCESS_TOKEN'] = pulumi_access_token
try:
def pulumi_program():
return create_pulumi_program(content)
stack = auto.create_stack(stack_name=stack_name,
project_name=project_name,
program=create_pulumi_program(content))
stack.workspace.install_plugin("aws", "v4.0.0")
stack.set_config("aws:region", auto.ConfigValue(value="us-west-2"))
stack.set_config("aws:region", auto.ConfigValue("us-west-2"))
# deploy the stack, tailing the logs to stdout
up_res = stack.up(on_output=print)
return jsonify(id=stack_name, url=up_res.outputs['website_url'].value)
except auto.StackAlreadyExistsError:
return make_response(f"stack '{stack_name}' already exists", 409)
except Exception as exn:
return make_response(str(exn), 500)
if __name__ == '__main__':
app.run(debug=True)
我发现了问题,这是因为我在 create_stack
中向程序函数传递了一个参数
stack = automation.create_stack(
stack_name=stack_name,
project_name=project_name,
program=create_pulumi_program(content)
)
应该是这样的:
stack = automation.create_stack(
stack_name=stack_name,
project_name=project_name,
program=create_pulumi_program
)
我正在编写一个使用 Pulumi Automation API 的 Flask 应用程序。我正在关注 Automation API project examples。但是当我发送一个 POST 请求时,我得到一个 程序 运行,但没有可用的 Pulumi 引擎;重新运行 使用 pulumi CLI 报错。自动化 API 不应该 运行 CLI 本身吗?
Pulumi CLI 可用:
pulumi版 v3.24.1
编辑:我按照 pulumi over HTTP 示例进行操作,这是我的 app.py
import pulumi
from flask import Flask, request, make_response, jsonify
from pulumi import automation as auto
import os
from pulumi_aws import s3
app = Flask(__name__)
# This function defines our pulumi s3 static website in terms of the content that the caller passes in.
# This allows us to dynamically deploy websites based on user defined values from the POST body.
def create_pulumi_program(content: str):
# Create a bucket and expose a website index document
site_bucket = s3.Bucket("s3-website-bucket", website=s3.BucketWebsiteArgs(index_document="index.html"))
index_content = content
# Write our index.html into the site bucket
s3.BucketObject("index",
bucket=site_bucket.id,
content=index_content,
key="index.html",
content_type="text/html; charset=utf-8")
# Set the access policy for the bucket so all objects are readable
s3.BucketPolicy("bucket-policy",
bucket=site_bucket.id,
policy={
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
# Policy refers to bucket explicitly
"Resource": [pulumi.Output.concat("arn:aws:s3:::", site_bucket.id, "/*")]
},
})
# Export the website URL
pulumi.export("website_url", site_bucket.website_endpoint)
@app.route('/', methods=['GET'])
def home():
return "<h1>Hello</p>"
@app.route('/v1/code', methods=['POST'])
def create_handler():
content = request.get_json()
project_name = content.get('project_name')
stack_name = content.get('stack_name')
pulumi_access_token = request.headers['pulumi_access_token']
os.environ['PULUMI_ACCESS_TOKEN'] = pulumi_access_token
try:
def pulumi_program():
return create_pulumi_program(content)
stack = auto.create_stack(stack_name=stack_name,
project_name=project_name,
program=create_pulumi_program(content))
stack.workspace.install_plugin("aws", "v4.0.0")
stack.set_config("aws:region", auto.ConfigValue(value="us-west-2"))
stack.set_config("aws:region", auto.ConfigValue("us-west-2"))
# deploy the stack, tailing the logs to stdout
up_res = stack.up(on_output=print)
return jsonify(id=stack_name, url=up_res.outputs['website_url'].value)
except auto.StackAlreadyExistsError:
return make_response(f"stack '{stack_name}' already exists", 409)
except Exception as exn:
return make_response(str(exn), 500)
if __name__ == '__main__':
app.run(debug=True)
我发现了问题,这是因为我在 create_stack
中向程序函数传递了一个参数stack = automation.create_stack(
stack_name=stack_name,
project_name=project_name,
program=create_pulumi_program(content)
)
应该是这样的:
stack = automation.create_stack(
stack_name=stack_name,
project_name=project_name,
program=create_pulumi_program
)