使用 gcloud 停止当前版本的应用引擎的推荐方法是什么?

Mik*_*ike 6 google-app-engine gcloud

我想通过运行 bash 脚本来自动启动/停止我们的应用引擎服务。

我知道它很容易运行gcloud app versions start/stop,但我不想手动检查版本号。我想将提供 100% 流量的版本动态传递给 gcloud 并告诉它停止。

另一方面,我还想告诉 gcloud 启动最近部署的版本。

推荐的方法是什么?

谢谢!

A.Q*_*eue 5

一种方法是使用gcloud的键和标志:projections, --format, --filters。要直接从终端读取更多信息,请使用gcloud topic,例如:

gcloud topic projections
Run Code Online (Sandbox Code Playgroud)

为了查看哪些字段/属性可用--format=flattened,请使用,例如:

gcloud app services list --format=flattened
Run Code Online (Sandbox Code Playgroud)

为简单起见,除了gcloud.

for SERVICE in $(gcloud app services list --format='table[no-heading](id)'); do
    echo "for service $SERVICE :"

    RECENT=$(gcloud app versions list --format='table[no-heading](id)' --filter="service=$SERVICE" | tail -n1)

    echo 'y' | gcloud app versions start $RECENT

    VERSIONS=$(gcloud app versions list --format='table[no-heading](id)' --filter="service=$SERVICE AND version.servingStatus=SERVING AND NOT id=$RECENT" | tr '\n' ' ')

    echo 'y' | gcloud app versions stop $VERSIONS
done
Run Code Online (Sandbox Code Playgroud)

'table[no-heading](service)' 输出一个没有标题的表,它被设置在括号中,以及一个带有服务 ID 的列,它被设置在括号中。

--filter="service=$SERVICE AND version.servingStatus=SERVING AND NOT id=$RECENT"将只显示来自指定服务的正在服务的版本,除了由 指示的版本RECENT

此外,如果您想使用日期进行过滤:

gcloud app versions list --format='table(id, version.servingStatus, version.createTime.date(format="%s"))' --filter="service=default" --sort-by="~version.createTime"
Run Code Online (Sandbox Code Playgroud)

version.createTime.date(format="%s")是一个函数 date转换version.createTime.date为自 Epoch 以来的秒数。

%s来自strftime(3)并以更易于理解和比较的 Epoch 格式返回日期。

--sort-by="~version.createTime"按创建日期和因为~降序排序。