更新时间:2023-11-04 19:31:10
我终于找到了一种方法,可以使用Python API发现客户端和服务帐户以编程方式重新启动所有实例.它首先获取活动实例的列表,并删除所有实例.然后,执行一个简单的请求以启动其中一个.
I finally found a way to restart all instances programatically, by using the Python API discovery Client and a service account. It first gets the list of active instances and delets all of them. Then, performs a simple request to initiate one of them.
import requests
from apiclient.discovery import build
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('credentials.json')
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/appengine.admin',"https://www.googleapis.com/auth/cloud-platform"])
appengine = build(serviceName="appengine",version="v1",credentials=scoped_credentials)
VERSION_ID = "version_id"
PROJECT_ID = "project_id"
SERVICE_ID = "appengine_service_name"
APP_URL = "http://some_url.com"
active_instances_dict = appengine.apps().services().versions().instances().list(servicesId=SERVICE_ID,appsId=PROJECT_ID,versionsId=VERSION_ID).execute()
list_of_instances = active_instances_dict["instances"]
for instance in list_of_instances:
appengine.apps().services().versions().instances().delete(servicesId=SERVICE_ID,appsId=PROJECT_ID,
versionsId=VERSION_ID,instancesId=instance["id"]).execute()
requests.get(url=APP_URL)