我有以下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
。抓我的頭。
謝謝
是否有可能啓動腳本是從一個不同的用戶執行的,你用它來連接'ssh',因此它不在你的主目錄中? –
這裏它告訴你如何重新運行啓動腳本和輸出寫入的地方。你可以在啓動腳本中包含一個'pwd',看看你得到了什麼。 https://cloud.google.com/compute/docs/startupscript#rerunthescript –