"""Generates name of a VM."""defGenerateMachineName(prefix,suffix):returnprefix+"-"+suffix
使用範本中的輔助指令碼
如要使用 vm-template.py 中的 common.py,必須對範本做出多項變更。
如要查看變更,請開啟 vm-template.py:
nanovm-template.py
範本含有重點介紹變更的程式碼註解。
請注意,範本會在檔案的頂端匯入 common.py。在 resources 區段中,VM 的 name 欄位現在會呼叫 GenerateMachineName()。
"""Creates the virtual machine."""# `common.py` is imported below.importcommonCOMPUTE_URL_BASE='https://www.googleapis.com/compute/v1/'defGenerateConfig(context):"""Generates configuration of a VM."""resources=[{'name':common.GenerateMachineName('myfrontend','prod'),'type':'compute.v1.instance','properties':{'zone':'us-central1-f','machineType':COMPUTE_URL_BASE+'projects/'+context.env['project']+'/zones/us-central1-f/machineTypes/f1-micro','disks':[{'deviceName':'boot','type':'PERSISTENT','boot':True,'autoDelete':True,'initializeParams':{'sourceImage':COMPUTE_URL_BASE+'projects/''debian-cloud/global/images/family/debian-11'}}],'networkInterfaces':[{'network':COMPUTE_URL_BASE+'projects/'+context.env['project']+'/global/networks/default','accessConfigs':[{'name':'External NAT','type':'ONE_TO_ONE_NAT'}]}]}}]return{'resources':resources}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-14 (世界標準時間)。"],[[["Helper scripts, or template modules, enhance template efficiency by performing specific functions like interpreting metadata, creating files, and launching services."],["The provided Python helper script, `common.py`, demonstrates how to generate virtual machine names using a specified prefix and suffix."],["To use the helper script within a template, the `vm-template.py` file must import `common.py` and call the `GenerateMachineName()` function to dynamically generate resource names."],["The updated configuration file, `two-vms.yaml`, must also import `common.py` to utilize the helper script's functionality during deployment, which can be done using the gcloud CLI."],["The deployment, including resources with dynamically generated names, can be viewed using the `gcloud deployment-manager deployments describe` command."]]],[]]