元数据中的启动脚本未运行(Python,Google Compute Engine,云存储触发器) [英] Startup Script in Metadata Not Running (Python, Google Compute Engine, Cloud Storage Trigger)
问题描述
我有一个在Google App Engine上运行的应用程序和一个在Google Compute Engine上运行的AI.我触发VM实例从Google Cloud Storage存储桶中的更改开始,并且有一个启动脚本,我尝试将其存储在GCE实例的元数据中.我的云功能看起来像这样:
I have an app running on Google App Engine, and an AI running on Google Compute Engine. I'm triggering the VM instance to start on a change in a Google Cloud Storage bucket, and have a start-up script that I attempt to store in the metadata of the GCE instance. My cloud functions looks like this:
import os
from googleapiclient.discovery import build
def start(event, context):
file = event
print(file["id"])
string = file["id"]
new_string = string.split('/')
user_id = new_string[1]
payment_id = new_string[2]
name = new_string[3]
print(name)
if name == "uploadcomplete.txt":
startup_script = """ #! /bin/bash
sudo su username
cd directory/directory
python analysis.py -- gs://location/{userId}/{paymentId}
""".format(userId=user_id, paymentId=payment_id)
# initialize compute api
service = build('compute', 'v1', cache_discovery=False)
print('VM Instance starting')
project = 'zephyrd'
zone = 'us-east1-c'
instance = 'zephyr-a'
# get metadata fingerprint in order to set new metadata
metadata = service.instances().get(project=project, zone=zone, instance=instance)
metares = metadata.execute()
fingerprint = metares["metadata"]["fingerprint"]
# set new metadata
bodydata = {"fingerprint": fingerprint,
"items": [{"key": "startup-script", "value": startup_script}]}
meta = service.instances().setMetadata(project=project, zone=zone, instance=instance,
body=bodydata).execute()
print(meta)
# confirm new metdata
instanceget = service.instances().get(project=project, zone=zone, instance=instance).execute()
print("'New Metadata:", instanceget['metadata'])
print(instanceget)
# start VM
request = service.instances().start(project=project, zone=zone, instance=instance)
response = request.execute()
print('VM Instance started')
print(response)
VM已启动,但启动脚本未运行.出于问题的考虑,脚本已简化,但这只是我尝试运行的基本命令.我将脚本直接添加到控制台中的元数据,但是我使用云函数触发器中的值在VM中运行命令.我想念什么?
The VM starts, but the startup script does not run. The script has been simplified for the purposes of the question, but this is just a basic command I'm trying to run. I would add the script directly to the metadata in the console, but I use values from the cloud function trigger to run commands in the VM. What am I missing?
我试图通过两种方式设置元数据:
I've attempted to set the metadata in two ways:
"items": [{"key": "startup-script", "value": startup_script}]
以及:
"items": [{"startup-script" : startup_script}]
均无效.如果我在外壳中手动键入命令,这些命令可以很好地运行.
Neither work. The commands run beautifully if I manually type them in the shell.
推荐答案
查看您的日志以确定其为什么不执行.
Look into your logs to determine why its not executing.
https://cloud.google.com/compute/docs/startupscript#viewing_startup_script_logs
可能是您的问题是您试图执行python脚本而不是bash脚本.
Probably you the issue is that you are trying to execute a python script instead of a bash script.
您的启动脚本应类似于:
Your startup script should be something like:
#! /bin/bash
# ...
python3 paht/to/python_script.py
这篇关于元数据中的启动脚本未运行(Python,Google Compute Engine,云存储触发器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!