diff --git a/app.yaml b/app.yaml index 88b18a4..09bf37b 100644 --- a/app.yaml +++ b/app.yaml @@ -1,7 +1,7 @@ runtime: go1.15 env: flex resources: - cpu: 2 + cpu: 1 memory_gb: 1.4 disk_size_gb: 10 network: diff --git a/deploy/delete-old-verions.sh b/deploy/delete-old-verions.sh new file mode 100755 index 0000000..72f42cc --- /dev/null +++ b/deploy/delete-old-verions.sh @@ -0,0 +1,19 @@ +#!/bin/bash +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +source $SCRIPT_DIR/gcp_version.sh + +#wait GAE pending operations +for ((i=1; i<120; i++)); do + echo "check pending GAE operations ... " + op=`gcloud app operations list --filter=status=PENDING --format=json` + if [ "$op" == "[]" ]; then + echo "no pending"; + del_old_instances + break; + else + echo "$op" + echo "check $i of 120 (normally it takes 70 checks = 3-5 minutes" + sleep 3; + fi +done +echo "finished"; diff --git a/deploy/deploy.sh b/deploy/deploy.sh index 5d90fd2..cbce8ff 100755 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -3,13 +3,19 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" PROJ_DIR="$SCRIPT_DIR/../" rm -rf /tmp/goweb +echo "create /tmp/goweb" mkdir -p /tmp/goweb +echo "prepare golang project" rsync -a $PROJ_DIR /tmp/goweb/ rm -rf /tmp/goweb/html/* -rsync -avh /mnt/hgfs/workspace/2021-07-31-BiukopWeb/ /tmp/goweb/html/ +echo "sync Web html" +rsync -a /mnt/hgfs/workspace/2021-07-31-BiukopWeb/ /tmp/goweb/html/ cd /tmp/goweb -#gcloud app deploy +gcloud app deploy + +#list all versions +gcloud app versions list ; + + -source $SCRIPT_DIR/gcp_version.sh -del_old_instances \ No newline at end of file diff --git a/deploy/gcp_version.sh b/deploy/gcp_version.sh index 133e17a..bac8978 100644 --- a/deploy/gcp_version.sh +++ b/deploy/gcp_version.sh @@ -17,39 +17,21 @@ get_instance_id() { } del_old_instances(){ - get_instance_id - echo "RUNNING VERSION: " $LAST_VERSION_ID - - GCP_VERSIONS=(` gcloud app versions list | awk -F' ' '{print $2}' `) - LENGTH=${#GCP_VERSIONS[@]} - for i in $(seq 1 1 $(expr $LENGTH - 1) ) - do - if [ "${GCP_VERSIONS[$i]}" != "$LAST_VERSION_ID" ] ; then - echo "delete old version ${GCP_VERSIONS[$i]} " - del_instance_by_version ${GCP_VERSIONS[$i]} - else - echo "KEEP RUNNING VERSION ${GCP_VERSIONS[$i]}" + VERSION_OUTPUT=$(gcloud app versions list) + # VERSION_OUTPUT=$` cat /home/sp/go/src/goweb/deploy/sample-version-list.txt` + echo "$VERSION_OUTPUT" + dropList=() + + while IFS= read -r line; do + status=(` echo $line | awk -F' ' '{print $5}' `) + version=(` echo $line | awk -F' ' '{print $2}' `) + if [ "$status" == "STOPPED" ] ; then + dropList+=("$version") fi - done -} - -del_instance_by_version() { - versionId=$1 - if [ "$versionId" == "" ]; then - return - fi - gcloud app versions delete --service=default $versionId ; - -# while true; do -# read -p "Do you wish to delete version $versionId ?" yn -# case $yn in -# [Yy]* ) -# echo gcloud app instances delete $INSTANCE_ID --service=default --version=$versionId ; -# break;; -# [Nn]* ) exit;; -# * ) echo "Please answer yes or no.";; -# esac -# done + done <<< "$VERSION_OUTPUT" + #cannot do this within while, as the user input will by bypassed by <<< + for version in ${dropList[@]} ; do + gcloud app versions delete --service=default $version ; + done } - diff --git a/deploy/sample-version-list.txt b/deploy/sample-version-list.txt new file mode 100644 index 0000000..625d21b --- /dev/null +++ b/deploy/sample-version-list.txt @@ -0,0 +1,4 @@ +SERVICE VERSION.ID TRAFFIC_SPLIT LAST_DEPLOYED SERVING_STATUS +default 20210809t011326 0.00 2021-08-09T01:14:37+10:00 STOPPED +default 20210809t013157 0.00 2021-08-09T01:33:04+10:00 STOPPED +default 20210809t014534 1.00 2021-08-09T01:46:41+10:00 SERVING