Created
May 12, 2018 14:09
-
-
Save eighteight/772851faee1eeb5be7389bca0bbff6fd to your computer and use it in GitHub Desktop.
Restart gaeapp docker image in Google App Engine flexible environment custom runtime with --privileged flag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
if [ -f '/home/gaebuilder/google-cloud-sdk/path.bash.inc' ]; then source '/home/gaebuilder/google-cloud-sdk/path.bash.inc'; fi | |
if [ "$#" -ne 3 ]; then | |
echo "Usage $0 [PROJECT] [SERVICE] [APPLICATION_ENV]" | |
exit 1 | |
fi | |
project=$1 | |
service=$2 | |
gae_env=$3 | |
date | |
#echo "delete known hosts" | |
#rm -v ~/.ssh/google_compute_known_hosts | |
echo "gcloud query running instances project:${project} service:$service" | |
raw_output="$(gcloud app instances list --project ${project} --service ${service} --sort-by VM_STATUS)" | |
echo "${raw_output}" | |
echo "" | |
let cnt=-1 | |
declare -a version | |
declare -a instance | |
declare -a status | |
declare -a debug_mode | |
IFS=$'\n' | |
while read -r entry; do | |
if [ $cnt -gt -1 ] #skip header | |
then | |
version[$cnt]=$(echo "$entry" | awk '{print $2}') | |
instance[$cnt]=$(echo "$entry" | awk '{print $3}') | |
status[$cnt]=$(echo "$entry" | awk '{print $4}') | |
debug_mode[$cnt]=$(echo "$entry" | awk '{print $5}') | |
fi | |
((cnt++)) | |
done <<< "$raw_output" | |
#executing mount command needs to be in a separate loop, otherwise, it breaks parsing data above | |
for (( j=0; j<$cnt; j++ )) | |
do | |
if [ "${status[$j]}" == "RUNNING" ] | |
then | |
date | |
echo "First: Find if script for this container is running" | |
restart_script_name="/home/gaebuilder/bin/restart_"$project"_"$service".tmp" | |
if [ -f $restart_script_name ]; then | |
echo "Script $restart_script_name is running. " | |
echo "Exiting" | |
exit 1 | |
fi | |
echo "Second: Find if container is already privileged by checking the sentinel" | |
is_privileged="$(gcloud --project ${project} --quiet app instances ssh ${instance[$j]} --service ${service} --version ${version[$j]} -- \ | |
'[ -e GAE_CONTAINER_IS_PRIVILEGED ] && echo YES || echo NO')" | |
if [ $is_privileged == "NO" ] | |
then | |
date | |
echo "will try restarting as privileged PROJECT ${project} VERSION ${version[$j]} INSTANCE ${instance[$j]}" | |
echo "first, find the IMAGE_ID of the running app container" | |
image_r="$(gcloud --project ${project} --quiet app instances ssh ${instance[$j]} --service ${service} --version ${version[$j]} -- \ | |
'docker images')" | |
echo "" | |
let imgcnt=-1 | |
IFS=$'\n' | |
while read -r entr; do | |
if [ $imgcnt -gt -1 ] #skip header | |
then | |
rep=$(echo "$entr" | awk '{print $1}') | |
tag=$(echo "$entr" | awk '{print $2}') | |
image_id=$(echo "$entr" | awk '{print $3}') | |
if [[ $rep == *"${project}"* ]]; then | |
appimage_id=$image_id | |
echo "Found suitable IMAGE_ID $appimage_id" | |
fi | |
fi | |
((imgcnt++)) | |
done <<< "$image_r" | |
date | |
echo "create restart script" | |
cmd_restart="docker kill gaeapp && docker rm gaeapp && docker run --privileged -d -p 8080:8080 --name gaeapp \ | |
-e APPLICATION_ENV=${gae_env} \ | |
-e GAE_SERVICE=${service} \ | |
-e GCLOUD_PROJECT=${project} \ | |
-e GOOGLE_CLOUD_PROJECT=${project} \ | |
-e GAE_INSTANCE=${instance[$j]} \ | |
-v /cloudsql:/cloudsql ${appimage_id} && touch GAE_CONTAINER_IS_PRIVILEGED" | |
echo "${cmd_restart}" >> $restart_script_name | |
chmod a+x $restart_script_name | |
date | |
echo "SCP the script to GAE" | |
result0="$(gcloud --project "${project}" --quiet app instances scp --service ${service} --version ${version[$j]} ${restart_script_name} \ | |
${instance[$j]}:restart.sh)" | |
echo "${result0}" | |
date | |
echo "Execute script remotely" | |
result="$(gcloud --project "${project}" --quiet app instances ssh "${instance[$j]}" --service "${service}" --version "${version[$j]}" -- './restart.sh')" | |
echo "${result}" | |
echo "" | |
rm -v $restart_script_name | |
else | |
echo "Sentinel Exists. Already privileged" | |
fi | |
else | |
echo "Debug mode ${debug_mode} -- assuming container is privileged" | |
fi | |
done | |
echo "DONE" | |
date | |
echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment