gcloud启动脚本不运行
问题描述:
我有以下Python代码:gcloud启动脚本不运行
def create_instance(compute_arg, project_arg, zone_arg, config="default", startup_script="", name="", instances=[],
strategy_args=[]):
startup_script = """
#! /bin/bash\n
mkdir before_all;\n
EOF
"""
print name
if config == "default":
print 'config is default'
config = {
"name": name,
"zone": "projects/my-project-12345/zones/us-east1-b",
"machineType": "projects/my-project-12345/zones/us-east1-b/machineTypes/n1-standard-1",
"metadata": {
"items": [{'key': 'startup-script', 'value': startup_script}]
},
"tags": {
"items": [
"http-server",
"https-server"
]
},
"disks": [
{
"type": "PERSISTENT",
"boot": True,
"mode": "READ_WRITE",
"autoDelete": True,
"deviceName": "instance-6",
"initializeParams": {
"sourceImage": "projects/my-project-12345/global/images/image-root-git-algo",
"diskType": "projects/my-project-12345/zones/us-east1-b/diskTypes/pd-standard",
"diskSizeGb": "10"
}
}
],
"canIpForward": False,
"networkInterfaces": [
{
"network": "projects/my-project-12345/global/networks/default",
"subnetwork": "projects/my-project-12345/regions/us-east1/subnetworks/default",
"accessConfigs": [
{
"name": "External NAT",
"type": "ONE_TO_ONE_NAT"
}
]
}
],
"description": "",
"labels": {},
"scheduling": {
"preemptible": False,
"onHostMaintenance": "MIGRATE",
"automaticRestart": True
},
"serviceAccounts": [
{
"email": "[email protected]",
"scopes": [
"https://www.googleapis.com/auth/cloud-platform"
]
}
]
}
return compute_arg.instances().insert(
project=project_arg,
zone=zone_arg,
body=config).execute()
compute = googleapiclient.discovery.build('compute', 'v1')
project = 'my-project-12345'
zone = 'us-east1-b'
instance_name = 'instance-algo-' + str(uuid.uuid4())
operation = create_instance(compute, project, zone, name=instance_name, instances=[])
这没有问题运行。我的实例是基于正确的图像创建的,我能够发现它们预期的所有文件和目录。
这很好,但我的启动脚本没有运行。当我登录到我的实例时,我没有看到dir before_all
。如果我进入实例我可以运行mkdir before_all;
,它很高兴地运行。
我以前有一个复杂得多的启动脚本,它没有运行和简化,看看我上面有什么。
我不确定为什么格式看起来与this example from google中的内容匹配。
有没有人看到我可能在这里做错了?
请注意,我尝试过使用和不使用EOF,使用和不使用分号,以及使用和不使用\n
。抓我的头。
谢谢
答
你不应该有 “\ n” 或 “EOF”。无论你在哪里得到它们,只是试图在shell脚本中执行相当于
"""..."""
的操作。-
您也不应该在
#!
之前有任何前导空格,因为这会导致它被忽略。目前你有一条新线和几个空格。尝试:"""#!/bin/bash ... """
我不认为这是问题的原因,但默认情况下,您的文件中的所有命令都会运行。
由于您指定的相对路径为
mkdir
它将在启动脚本运行的任何位置创建目录。这与您登录时的位置(您的主目录)不同。如果你修复(1)和(2),你会发现before_all创造了令人惊讶的地方(可能在/
)。如果您想要mkdir
在您的主目录中创建一个目录,请尝试使用mkdir /home/YOUR_USERNAME/before_all
。但请注意,它将由root用户拥有,而不是由您自己的用户拥有。
是否有可能启动脚本是从一个不同的用户执行的,你用它来连接'ssh',因此它不在你的主目录中? –
这里它告诉你如何重新运行启动脚本和输出写入的地方。你可以在启动脚本中包含一个'pwd',看看你得到了什么。 https://cloud.google.com/compute/docs/startupscript#rerunthescript –