"""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-09-03 (世界標準時間)。"],[[["\u003cp\u003eHelper scripts, or template modules, enhance template efficiency by performing specific functions like interpreting metadata, creating files, and launching services.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Python helper script, \u003ccode\u003ecommon.py\u003c/code\u003e, demonstrates how to generate virtual machine names using a specified prefix and suffix.\u003c/p\u003e\n"],["\u003cp\u003eTo use the helper script within a template, the \u003ccode\u003evm-template.py\u003c/code\u003e file must import \u003ccode\u003ecommon.py\u003c/code\u003e and call the \u003ccode\u003eGenerateMachineName()\u003c/code\u003e function to dynamically generate resource names.\u003c/p\u003e\n"],["\u003cp\u003eThe updated configuration file, \u003ccode\u003etwo-vms.yaml\u003c/code\u003e, must also import \u003ccode\u003ecommon.py\u003c/code\u003e to utilize the helper script's functionality during deployment, which can be done using the gcloud CLI.\u003c/p\u003e\n"],["\u003cp\u003eThe deployment, including resources with dynamically generated names, can be viewed using the \u003ccode\u003egcloud deployment-manager deployments describe\u003c/code\u003e command.\u003c/p\u003e\n"]]],[],null,["# Exploring helper scripts\n\n*Helper scripts* , or *template modules*, are helper files that can make your\ntemplates more efficient by performing specific functions. For example, you can\nuse helper scripts to interpret resource metadata, create files, and launch\nservices.\n\nYou will now explore a Python helper script that names a virtual machine, given\na prefix and a suffix.\n\nBefore you begin\n----------------\n\n- If you want to use the command-line examples in this guide, install the [\\`gcloud\\` command-line tool](/sdk).\n- If you want to use the API examples in this guide, set up [API access](/deployment-manager/docs/reference/latest).\n\nOpening the helper script\n-------------------------\n\nThe basic helper script in this example generates the name for a virtual machine\n(VM). To view the script, run these commands: \n\n cd deploymentmanager-samples/examples/v2/step_by_step_guide/create_a_helper_script\n\n nano common.py # use your preferred text editor\n\nThe `GenerateMachineName()` function takes a prefix and suffix, and generates a\nname in the format `prefix-suffix`: \n\n \"\"\"Generates name of a VM.\"\"\"\n\n\n def GenerateMachineName(prefix, suffix):\n return prefix + \"-\" + suffix\n\nUsing the helper script in the template\n---------------------------------------\n\nTo use `common.py` in `vm-template.py`, several changes must be made to the\ntemplate.\n\nTo view the changes, open `vm-template.py`: \n\n nano vm-template.py\n\nThe template contains code comments that highlight the changes.\n\nNote that the template imports `common.py` at the top of the file.\nIn the `resources` section, the `name` fields for the VMs now call\n`GenerateMachineName()`. \n\n \"\"\"Creates the virtual machine.\"\"\"\n\n # `common.py` is imported below.\n import common\n\n COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/'\n\n\n def GenerateConfig(context):\n \"\"\"Generates configuration of a VM.\"\"\"\n resources = [{\n 'name': common.GenerateMachineName('myfrontend', 'prod'),\n 'type': 'compute.v1.instance',\n 'properties': {\n 'zone': 'us-central1-f',\n 'machineType': COMPUTE_URL_BASE + 'projects/' + context.env['project']\n + '/zones/us-central1-f/machineTypes/f1-micro',\n 'disks': [{\n 'deviceName': 'boot',\n 'type': 'PERSISTENT',\n 'boot': True,\n 'autoDelete': True,\n 'initializeParams': {\n 'sourceImage': COMPUTE_URL_BASE + 'projects/'\n 'debian-cloud/global/images/family/debian-11'}\n }],\n 'networkInterfaces': [{\n 'network': COMPUTE_URL_BASE + 'projects/' + context.env['project']\n + '/global/networks/default',\n 'accessConfigs': [{\n 'name': 'External NAT',\n 'type': 'ONE_TO_ONE_NAT'\n }]\n }]\n }\n }]\n return {'resources': resources}\n\nViewing the changes to the configuration\n----------------------------------------\n\nTo view the updated configuration, open `two-vms.yaml`: \n\n nano two-vms.yaml\n\nNote that the helper script `common.py` must be imported in the configuration\nas well.\n\nDeploy your configuration: \n\n gcloud deployment-manager deployments create deployment-with-helper-script --config two-vms.yaml\n\nTo view the deployment, including the resources with the generated names, run: \n\n gcloud deployment-manager deployments describe deployment-with-helper-script\n\nDeleting your deployment\n------------------------\n\nWe recommend that you delete the deployment to avoid charges. You don't need\nthis deployment for the next step. Run the following command to delete the\ndeployment: \n\n gcloud deployment-manager deployments delete deployment-with-helper-script\n\nLooking ahead: updating deployments\n-----------------------------------\n\nNext, learn to add, delete, and change the properties of resources in a\ndeployment as your app evolves."]]