运行 来自启动脚本的 python 脚本

Run a python script from the startup script

当一个新文件上传到存储时创建一个实例。启动运行 python 脚本,为新文件生成 pdf 文件,将 pdf 上传回存储并删除实例。由于 python 脚本相当冗长,我将启动脚本和 python 脚本存储在同一位置(云存储)。我在创建实例时传递了路径是元数据。 python 脚本的输入是新文件的文件名。我检查了实例的日志,它在那里抛出了一些错误。谁能指出我做错了什么。

已编辑

错误信息:

{
  "cpuPlatform": "Intel Haswell",
  "creationTimestamp": "2021-08-02T06:40:36.346-07:00",
  "deletionProtection": false,
  "disks": [
    {
      "autoDelete": true,
      "boot": true,
      "deviceName": "xyz",
      "diskSizeGb": "10",
      "guestOsFeatures": [
        {
          "type": "UEFI_COMPATIBLE"
        },
        {
          "type": "VIRTIO_SCSI_MULTIQUEUE"
        }
      ],
      "index": 0,
      "interface": "SCSI",
      "kind": "compute#attachedDisk",
      "licenses": [
        "projects/debian-cloud/global/licenses/debian-10-buster"
      ],
      "mode": "READ_WRITE",
      "source": "projects/patch-us/zones/us-central1-a/disks/instance-name",
      "type": "PERSISTENT"
    }
  ],
  "fingerprint": "XlZ7biyVpAI=",
  "id": "3984870299667155772",
  "kind": "compute#instance",
  "labelFingerprint": "42WmSpB8rSM=",
  "lastStartTimestamp": "2021-08-02T06:40:46.210-07:00",
  "machineType": "projects/project-name/zones/us-central1-a/machineTypes/e2-medium",
  "metadata": {
    "fingerprint": "f5o3Pxed5VY=",
    "items": [
      {
        "key": "startup-script-url",
        "value": "https://storage.cloud.google.com/project-name.appspot.com/start-up-script/start-script.sh"
      },
      {
        "key": "file_name",
        "value": "123456"
      },
      {
        "key": "python_script_name",
        "value": "https://storage.cloud.google.com/project-name.appspot.com/start-up-script/generate_fd_report.py"
      }
    ],
    "kind": "compute#metadata"
  },
  "name": "instance-name",
  "networkInterfaces": [
    {
      "accessConfigs": [
        {
          "kind": "compute#accessConfig",
          "name": "External NAT",
          "natIP": "35.202.255.222",
          "networkTier": "PREMIUM",
          "type": "ONE_TO_ONE_NAT"
        }
      ],
      "fingerprint": "565TD6a2Y2c=",
      "kind": "compute#networkInterface",
      "name": "nic0",
      "network": "projects/project-name/global/networks/default",
      "networkIP": "10.128.0.29",
      "stackType": "IPV4_ONLY",
      "subnetwork": "projects/project-name/regions/us-central1/subnetworks/default"
    }
  ],
  "scheduling": {
    "automaticRestart": true,
    "onHostMaintenance": "MIGRATE",
    "preemptible": false
  },
  "selfLink": "projects/project-name/zones/us-central1-a/instances/instance-name",
  "serviceAccounts": [
    {
      "email": "project-id-compute@developer.gserviceaccount.com",
      "scopes": [
        "https://www.googleapis.com/auth/cloud-platform"
      ]
    }
  ],
  "shieldedInstanceConfig": {
    "enableIntegrityMonitoring": true,
    "enableSecureBoot": false,
    "enableVtpm": true
  },
  "shieldedInstanceIntegrityPolicy": {
    "updateAutoLearnPolicy": true
  },
  "startRestricted": false,
  "status": "RUNNING",
  "tags": {
    "fingerprint": "42WmSpB8rSM="
  },
  "zone": "projects/project-name/zones/us-central1-a"
}   

启动脚本-sh

#! /bin/bash
ECG_FILE_PATH = $(curl http://metadata/computeMetadata/v1/instance/attributes/file_path -H "Metadata-Flavor: Google")
PYTHON_FILE_PATH = $(curl http://metadata/computeMetadata/v1/instance/attributes/python_script_name -H "Metadata-Flavor: Google")
ECG_FILE_NAME = $(curl http://metadata/computeMetadata/v1/instance/attributes/file_name -H "Metadata-Flavor: Google")

curl -s -o generate_fd.py PYTHON_FILE_PATH

chmod +x generate_fd.py
python3 generate_fd.py ECG_FILE_PATH &

generate_fd_report.py

#!/usr/bin/env python3
def main(file_name):
    print("Hello")


main(file_name)

日志

要下载脚本,URL您保存为元数据值的路径如下:

curl -s -o filename.txt $(curl -s http://metadata/computeMetadata/v1/instance/attributes/filename -H "Metadata-Flavor: Google")