2017-06-06 131 views
0

我有以下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。抓我的頭。

謝謝

+0

是否有可能啓動腳本是從一個不同的用戶執行的,你用它來連接'ssh',因此它不在你的主目錄中? –

+1

這裏它告訴你如何重新運行啓動腳本和輸出寫入的地方。你可以在啓動腳本中包含一個'pwd',看看你得到了什麼。 https://cloud.google.com/compute/docs/startupscript#rerunthescript –

回答

0
  1. 你不應該有 「\ n」 或 「EOF」。無論你在哪裏得到它們,只是試圖在shell腳本中執行相當於"""..."""的操作。

  2. 您也不應該在#!之前有任何前導空格,因爲這會導致它被忽略。目前你有一條新線和幾個空格。嘗試:

    """#!/bin/bash 
    ... 
    """ 
    

    我不認爲這是問題的原因,但默認情況下,您的文件中的所有命令都會運行。

  3. 由於您指定的相對路徑爲mkdir它將在啓動腳本運行的任何位置創建目錄。這與您登錄時的位置(您的主目錄)不同。如果你修復(1)和(2),你會發現before_all創造了令人驚訝的地方(可能在/)。如果您想要mkdir在您的主目錄中創建一個目錄,請嘗試使用mkdir /home/YOUR_USERNAME/before_all。但請注意,它將由root用戶擁有,而不是由您自己的用戶擁有。