Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the recommended way to stop the current version of app engine using gcloud?

I want to automatically start/stop our app engine services by running a bash script.

I know it's easy to run gcloud app versions start/stop, but I don't want to manually check the version number. I want to dynamically pass the version that is serving 100% traffic to gcloud and tell it to stop.

On the flip side, I also want to tell gcloud to start the most recently deployed version.

What's the recommended way to do this?

Thanks!

like image 561
Mike Avatar asked Feb 24 '18 16:02

Mike


1 Answers

One way to do that is to use gcloud's keys and flags: projections, --format, --filters. To read more directly from the terminal use gcloud topic, for example:

gcloud topic projections

In order to see what fields/properties are available use --format=flattened, like:

gcloud app services list --format=flattened

For the sake of simplicity I will leave outside everything but 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

'table[no-heading](service)' outputs a table without heading, which is set in brackets, and a single column with service IDs, which is set in parentheses.

--filter="service=$SERVICE AND version.servingStatus=SERVING AND NOT id=$RECENT" will only show versions from indicated service that are serving, except the one indicated by RECENT.

Additionally, if you would want to use dates for filtering:

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

version.createTime.date(format="%s") is a function date converting version.createTime.date into the number of seconds since the Epoch.

%s comes from strftime(3) and returns dates in Epoch format which is easier to understand and compare.

--sort-by="~version.createTime"sorts by creation date and because of ~ in descending order.

like image 129
A.Queue Avatar answered Oct 14 '22 05:10

A.Queue