[英]GCP / gcloud : Your app may not have more than N versions - how to remove old versions automatically?
While doing new deploy:在进行新部署时:
gcloud beta app deploy app_stage.yaml
We've got this error:我们遇到了这个错误:
ERROR: (gcloud.beta.app.deploy) INVALID_ARGUMENT: Your app may not have more than 210 versions. Please delete one of the existing versions before trying to create a new version.
After deleting some versions manually, it worked again.手动删除一些版本后,它又起作用了。
Does anybody know how to limit each service to work with N least versions automatically?有人知道如何限制每项服务自动使用 N 最少的版本吗? That is, automatically delete version N+1 after new one is created.即创建新版本后自动删除版本N+1。
There is not but it should be relatively (?.) straightforward to code a solution.没有,但它应该相对(?。)直接编写解决方案。
Importantly, test the solution exhaustively before use so that you don't accidently delete an important version in case of unexpected errors;重要的是,在使用前彻底测试解决方案,以免在出现意外错误时意外删除重要版本; you may want to consider some behavior that, for example, only deletes a version if it's not serving traffic, if there are currently 210 versions and if the version to be deleted is some significant number of days old.您可能需要考虑一些行为,例如,如果当前有 210 个版本并且要删除的版本已经有相当长的天数,则仅删除不提供流量的版本。
For better error handling, I'd prefer to use a Google SDK for this (and something like Go or Python) but for ease here's some bash pseudocode:为了更好地处理错误,我更愿意为此使用 Google SDK(以及 Go 或 Python 之类的东西),但为了方便起见,这里有一些 bash 伪代码:
NOTE I don't have an App Engine app deployed and so what follows is from memory.注意我没有部署 App Engine 应用程序,因此以下内容来自 memory。
DO NOT RUN THIS CODE IN PRODUCTION不要在生产环境中运行此代码
deploy.sh
: deploy.sh
:
# Env
PROJECT=...
# Do nothing unless you have to
gcloud app deploy \
--project=${PROJECT}
# Failed?
if [ $? -ne 0 ]
then
# Did it fail because there are 210 versions?
# Perhaps you could simply parse the previous command's error?
# Enumerate the list of versions with name and status into a bash array
VERSIONTIMESTAMPS=($(\
gcloud app versions list \
--sort-by="LAST_DEPLOYED" \
--format="csv[no-heading](id,version.createTime,version.servingStatus)" \
--project=${PROJECT}))
# If there are 210
if [ "${#VERSIONTIMESTAMPS[@]}" -eq 210 ]
then
# We're interested in the oldest (either 0, 209) depending on sort
VERSIONTIMESTAMP=${VERSIONTIMESTAMPS[0]}
# Extract VERSION, TIMESTAMP and STATUS
IFS=, read VERSION TIMESTAMP STATUS <<< ${VERSIONTIMESTAMP}
# Ensure it's STOPPED
if [ "${STATUS}" == "STOPPED" ]
then
CREATED=$(date +%s --date=${TIMESTAMP})
NOW=$(date +%s)
DIFF=$((${NOW}-${CREATED}))
# Seconds to Days
DAYS=$((${DIFF}/3600/24))
if [ "${DAYS}" -gt 30 ]
then
# There are 210 versions
# This version is STOPPED
# The version is older than 30 days
echo "Deleting: ${VERSION}"
gcloud app versions delete ${VERSION} \
--project=${PROJECT}
# Try the deploy again
gcloud app deploy \
--project=${PROJECT}
fi
fi
fi
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.