Google Compute Engine 運算子¶
先決條件任務¶
若要使用這些運算子,您必須執行以下幾項操作
使用 Cloud Console 選擇或建立 Cloud Platform 專案。
為您的專案啟用計費功能,如 Google Cloud 文件所述。
啟用 API,如 Cloud Console 文件所述。
透過 pip 安裝 API 程式庫。
pip install 'apache-airflow[google]'詳細資訊請參閱 安裝。
ComputeEngineInsertInstanceOperator¶
使用 ComputeEngineInsertInstanceOperator
建立新的 Google Compute Engine 執行個體。
使用運算子¶
建立運算子的程式碼
gce_instance_insert = ComputeEngineInsertInstanceOperator(
task_id="gcp_compute_create_instance_task",
project_id=PROJECT_ID,
zone=LOCATION,
body=GCE_INSTANCE_BODY,
)
您也可以在不使用專案 ID 的情況下建立運算子 - 專案 ID 將從使用的 Google Cloud 連線 ID 中檢索
gce_instance_insert2 = ComputeEngineInsertInstanceOperator(
task_id="gcp_compute_create_instance_task_2",
zone=LOCATION,
body=GCE_INSTANCE_BODY,
)
範本化¶
template_fields: Sequence[str] = (
"body",
"project_id",
"zone",
"request_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
"resource_id",
)
ComputeEngineInsertInstanceFromTemplateOperator¶
使用 ComputeEngineInsertInstanceFromTemplateOperator
根據指定的執行個體範本建立新的 Google Compute Engine 執行個體。
使用運算子¶
建立運算子的程式碼
gce_instance_insert_from_template = ComputeEngineInsertInstanceFromTemplateOperator(
task_id="gcp_compute_create_instance_from_template_task",
project_id=PROJECT_ID,
zone=LOCATION,
body=GCE_INSTANCE_FROM_TEMPLATE_BODY,
source_instance_template=f"global/instanceTemplates/{TEMPLATE_NAME}",
)
您也可以在不使用專案 ID 的情況下建立運算子 - 專案 ID 將從使用的 Google Cloud 連線 ID 中檢索
gce_instance_insert_from_template2 = ComputeEngineInsertInstanceFromTemplateOperator(
task_id="gcp_compute_create_instance_from_template_task_2",
zone=LOCATION,
body=GCE_INSTANCE_FROM_TEMPLATE_BODY,
source_instance_template=f"global/instanceTemplates/{TEMPLATE_NAME}",
)
範本化¶
template_fields: Sequence[str] = (
"body",
"source_instance_template",
"project_id",
"zone",
"request_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
"resource_id",
)
ComputeEngineDeleteInstanceOperator¶
使用 ComputeEngineDeleteInstanceOperator
刪除現有的 Google Compute Engine 執行個體。
使用運算子¶
您可以建立不使用專案 ID 的運算子 - 專案 ID 將從使用的 Google Cloud 連線 ID 中檢索。建立運算子的程式碼
gce_instance_delete = ComputeEngineDeleteInstanceOperator(
task_id="gcp_compute_delete_instance_task",
zone=LOCATION,
resource_id=GCE_INSTANCE_NAME,
trigger_rule=TriggerRule.ALL_DONE,
)
範本化¶
template_fields: Sequence[str] = (
"zone",
"resource_id",
"request_id",
"project_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
)
ComputeEngineStartInstanceOperator¶
使用 ComputeEngineStartInstanceOperator
啟動現有的 Google Compute Engine 執行個體。
使用運算子¶
建立運算子的程式碼
gce_instance_start = ComputeEngineStartInstanceOperator(
task_id="gcp_compute_start_task",
project_id=PROJECT_ID,
zone=LOCATION,
resource_id=GCE_INSTANCE_NAME,
)
您也可以在不使用專案 ID 的情況下建立運算子 - 專案 ID 將從使用的 Google Cloud 連線 ID 中檢索
gce_instance_start2 = ComputeEngineStartInstanceOperator(
task_id="gcp_compute_start_task_2",
zone=LOCATION,
resource_id=GCE_INSTANCE_NAME,
)
範本化¶
template_fields: Sequence[str] = (
"project_id",
"zone",
"resource_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
)
ComputeEngineStopInstanceOperator¶
使用此運算子停止 Google Compute Engine 執行個體。
如需參數定義,請參閱 ComputeEngineStopInstanceOperator
使用運算子¶
建立運算子的程式碼
gce_instance_stop = ComputeEngineStopInstanceOperator(
task_id="gcp_compute_stop_task",
project_id=PROJECT_ID,
zone=LOCATION,
resource_id=GCE_INSTANCE_NAME,
trigger_rule=TriggerRule.ALL_DONE,
)
您也可以在不使用專案 ID 的情況下建立運算子 - 專案 ID 將從使用的 Google Cloud 連線中檢索
gce_instance_stop2 = ComputeEngineStopInstanceOperator(
task_id="gcp_compute_stop_task_2",
zone=LOCATION,
resource_id=GCE_INSTANCE_NAME,
trigger_rule=TriggerRule.ALL_DONE,
)
範本化¶
template_fields: Sequence[str] = (
"project_id",
"zone",
"resource_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
)
ComputeEngineSetMachineTypeOperator¶
使用此運算子變更 Google Compute Engine 執行個體的機器類型。
如需參數定義,請參閱 ComputeEngineSetMachineTypeOperator
。
引數¶
使用運算子¶
建立運算子的程式碼
gce_set_machine_type = ComputeEngineSetMachineTypeOperator(
task_id="gcp_compute_set_machine_type",
project_id=PROJECT_ID,
zone=LOCATION,
resource_id=GCE_INSTANCE_NAME,
body={"machineType": f"zones/{LOCATION}/machineTypes/{SHORT_MACHINE_TYPE_NAME}"},
)
您也可以在不使用專案 ID 的情況下建立運算子 - 專案 ID 將從使用的 Google Cloud 連線中檢索
gce_set_machine_type2 = ComputeEngineSetMachineTypeOperator(
zone=LOCATION,
resource_id=GCE_INSTANCE_NAME,
body={"machineType": f"zones/{LOCATION}/machineTypes/{SHORT_MACHINE_TYPE_NAME}"},
task_id="gcp_compute_set_machine_type_2",
)
範本化¶
template_fields: Sequence[str] = (
"project_id",
"zone",
"resource_id",
"body",
"gcp_conn_id",
"api_version",
"impersonation_chain",
)
ComputeEngineDeleteInstanceTemplateOperator¶
使用此運算子刪除 Google Compute Engine 執行個體範本。
如需參數定義,請參閱 ComputeEngineDeleteInstanceTemplateOperator
。
使用運算子¶
建立運算子的程式碼
您可以建立不使用專案 ID 的運算子 - 專案 ID 將從使用的 Google Cloud 連線中檢索。建立運算子的程式碼
gce_instance_template_old_delete = ComputeEngineDeleteInstanceTemplateOperator(
task_id="gcp_compute_delete_old_template_task",
resource_id=TEMPLATE_NAME,
trigger_rule=TriggerRule.ALL_DONE,
)
範本化¶
template_fields: Sequence[str] = (
"resource_id",
"request_id",
"project_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
)
ComputeEngineInsertInstanceTemplateOperator¶
使用此運算子建立 Google Compute Engine 執行個體範本。
如需參數定義,請參閱 ComputeEngineInsertInstanceTemplateOperator
。
使用運算子¶
建立運算子的程式碼
gce_instance_template_insert = ComputeEngineInsertInstanceTemplateOperator(
task_id="gcp_compute_create_template_task",
project_id=PROJECT_ID,
body=INSTANCE_TEMPLATE_BODY,
)
您也可以在不使用專案 ID 的情況下建立運算子 - 專案 ID 將從使用的 Google Cloud 連線中檢索
gce_instance_template_insert2 = ComputeEngineInsertInstanceTemplateOperator(
task_id="gcp_compute_create_template_task_2",
body=INSTANCE_TEMPLATE_BODY,
)
範本化¶
template_fields: Sequence[str] = (
"body",
"project_id",
"request_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
"resource_id",
)
ComputeEngineCopyInstanceTemplateOperator¶
使用此運算子複製現有的 Google Compute Engine 執行個體範本,並對其套用修補程式。
如需參數定義,請參閱 ComputeEngineCopyInstanceTemplateOperator
。
使用運算子¶
建立運算子的程式碼
gce_instance_template_copy = ComputeEngineCopyInstanceTemplateOperator(
task_id="gcp_compute_igm_copy_template_task",
project_id=PROJECT_ID,
resource_id=TEMPLATE_NAME,
body_patch=INSTANCE_TEMPLATE_BODY_UPDATE,
)
您也可以在不使用專案 ID 的情況下建立運算子 - 專案 ID 將從使用的 Google Cloud 連線中檢索
gce_instance_template_copy2 = ComputeEngineCopyInstanceTemplateOperator(
task_id="gcp_compute_igm_copy_template_task_2",
resource_id=TEMPLATE_NAME,
body_patch=INSTANCE_TEMPLATE_BODY_UPDATE,
)
範本化¶
template_fields: Sequence[str] = (
"project_id",
"resource_id",
"request_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
)
更多資訊¶
請參閱 Google Compute Engine API 文件以使用現有範本建立新執行個體。
ComputeEngineInsertInstanceGroupManagerOperator¶
使用此運算子建立 Compute Engine 執行個體群組管理員。
如需參數定義,請參閱 ComputeEngineInsertInstanceGroupManagerOperator
。
引數¶
使用運算子¶
建立運算子的程式碼
gce_igm_insert = ComputeEngineInsertInstanceGroupManagerOperator(
task_id="gcp_compute_create_group_task",
zone=LOCATION,
body=INSTANCE_GROUP_MANAGER_BODY,
project_id=PROJECT_ID,
)
您也可以在不使用專案 ID 的情況下建立運算子 - 專案 ID 將從使用的 Google Cloud 連線中檢索
gce_igm_insert2 = ComputeEngineInsertInstanceGroupManagerOperator(
task_id="gcp_compute_create_group_task_2",
zone=LOCATION,
body=INSTANCE_GROUP_MANAGER_BODY,
)
範本化¶
template_fields: Sequence[str] = (
"project_id",
"body",
"zone",
"request_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
"resource_id",
)
ComputeEngineDeleteInstanceGroupManagerOperator¶
使用此運算子刪除 Compute Engine 執行個體群組管理員。
如需參數定義,請參閱 ComputeEngineDeleteInstanceGroupManagerOperator
。
引數¶
使用運算子¶
您可以建立不使用專案 ID 的運算子 - 專案 ID 將從使用的 Google Cloud 連線中檢索。建立運算子的程式碼
gce_igm_delete = ComputeEngineDeleteInstanceGroupManagerOperator(
task_id="gcp_compute_delete_group_task",
resource_id=INSTANCE_GROUP_MANAGER_NAME,
zone=LOCATION,
trigger_rule=TriggerRule.ALL_DONE,
)
範本化¶
template_fields: Sequence[str] = (
"project_id",
"resource_id",
"zone",
"request_id",
"gcp_conn_id",
"api_version",
"impersonation_chain",
)
ComputeEngineInstanceGroupUpdateManagerTemplateOperator¶
使用此運算子更新 Google Compute Engine 執行個體群組管理員中的範本。
如需參數定義,請參閱 ComputeEngineInstanceGroupUpdateManagerTemplateOperator
。
引數¶
使用運算子¶
建立運算子的程式碼
gce_instance_group_manager_update_template = ComputeEngineInstanceGroupUpdateManagerTemplateOperator(
task_id="gcp_compute_igm_group_manager_update_template",
project_id=PROJECT_ID,
resource_id=INSTANCE_GROUP_MANAGER_NAME,
zone=LOCATION,
source_template=SOURCE_TEMPLATE_URL,
destination_template=DESTINATION_TEMPLATE_URL,
update_policy=UPDATE_POLICY,
)
您也可以在不使用專案 ID 的情況下建立運算子 - 專案 ID 將從使用的 Google Cloud 連線中檢索
gce_instance_group_manager_update_template2 = ComputeEngineInstanceGroupUpdateManagerTemplateOperator(
task_id="gcp_compute_igm_group_manager_update_template_2",
resource_id=INSTANCE_GROUP_MANAGER_NAME,
zone=LOCATION,
source_template=SOURCE_TEMPLATE_URL,
destination_template=DESTINATION_TEMPLATE_URL,
)
範本化¶
template_fields: Sequence[str] = (
"project_id",
"resource_id",
"zone",
"request_id",
"source_template",
"destination_template",
"gcp_conn_id",
"api_version",
"impersonation_chain",
)
疑難排解¶
您可能會發現您的 ComputeEngineInstanceGroupUpdateManagerTemplateOperator 因權限遺失而失敗。若要執行此操作,服務帳戶需要「服務帳戶使用者」角色提供的權限(透過 Google Cloud IAM 指派)。