繁体   English   中英

元数据中的启动脚本未运行(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 实例的元数据中。 我的云功能如下所示:

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 启动,但启动脚本未运行。 出于问题的目的,该脚本已被简化,但这只是我尝试运行的一个基本命令。 我会将脚本直接添加到控制台中的元数据中,但我使用来自云 function 触发器的值在 VM 中运行命令。 我错过了什么?

我尝试以两种方式设置元数据:

"items": [{"key": "startup-script", "value": startup_script}]

也:

"items": [{"startup-script" : startup_script}]

都不工作。 如果我在 shell 中手动键入命令,它们会运行得很好。

查看您的日志以确定它为什么不执行。

https://cloud.google.com/compute/docs/startupscript#viewing_startup_script_logs

可能您的问题是您正在尝试执行 python 脚本而不是 bash 脚本。

你的启动脚本应该是这样的:

#! /bin/bash

# ...
python3 paht/to/python_script.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM