이 예시의 기본 도우미 스크립트는 가상 머신(VM)의 이름을 생성합니다. 스크립트를 보려면 다음 명령어를 실행합니다.
cddeploymentmanager-samples/examples/v2/step_by_step_guide/create_a_helper_script
nanocommon.py# use your preferred text editor
GenerateMachineName() 함수는 프리픽스와 서픽스를 사용하고 prefix-suffix 형식으로 된 이름을 생성합니다.
"""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-08(UTC)"],[[["\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."]]