Skip to content

Commit

Permalink
[crons-python] Attempt to fix deploy breaking in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
realkosty committed Nov 16, 2023
1 parent db64f06 commit de8faf9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
1 change: 0 additions & 1 deletion auto-deploy.exclude
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ ruby
laravel
tda
postgres
crons-python
26 changes: 17 additions & 9 deletions crons-python/deploy_project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@ source .env
HOST="$CRONSPYTHON_DEPLOY_HOST"
DIR="$CRONSPYTHON_DEPLOY_DIR"
CRONTAB_USER=$CRONSPYTHON_CRONTAB_USER
function ssh_cmd() {
local host_full=$1
local host_name=${host_full%%.*}
local rest=${host_full#*.}
local region=${rest%%.*}
shift
gcloud compute ssh $host_name --zone $region -- "$@" 2>&1 | grep -v '^Connection to .* closed\.' || true
}

function cleanup {
echo "NOTE: if ssh check hangs, re-run 'gcloud compute config-ssh; ssh $HOST exit' to fix"
}
trap cleanup EXIT

echo "Checking ssh connection can be established..."
ssh $HOST exit
ssh_cmd $HOST exit
if [ $? != "0" ]; then
echo "Running 'glcoud compute config-ssh' ..."
gcloud compute config-ssh
fi
ssh $HOST exit
ssh_cmd $HOST exit
if [ $? != "0" ]; then
echo "[ERROR] Can't ssh into destination host. Please make sure your gcloud is set up correctly and you \
have the right IAM permissions in sales-engingeering-sf GCP project."
Expand All @@ -34,11 +42,11 @@ trap - EXIT
reqs_changed=0
env_nonempty=0
echo "Checking state of current deployment..."
diff -q requirements.txt <(ssh $HOST 'cat '$DIR'/requirements.txt')
diff -q requirements.txt <(ssh_cmd $HOST 'cat '$DIR'/requirements.txt')
if [ $? == 1 ]; then # files differ or failed to compare
reqs_changed=1
fi
if ssh $HOST '[[ -d '"$DIR/env"' ]] && [[ ! -z `ls -A '"$DIR/env"'` ]]'; then
if ssh_cmd $HOST '[[ -d '"$DIR/env"' ]] && [[ ! -z `ls -A '"$DIR/env"'` ]]'; then
env_nonempty=1
fi

Expand All @@ -53,7 +61,7 @@ echo "Code copied."
if [[ $reqs_changed == "1" || $env_nonempty == "0" ]]; then
echo "re-installing requirements (because requirements.txt changed OR 'env' directory does not exist or is empty)..."
# Host must have python3.8 and virtualenv installed
ssh $HOST 'cd '$DIR' && ./build.sh'
ssh_cmd $HOST 'cd '$DIR' && ./build.sh'
if [ $? != 0 ]; then
echo "[ERROR] failed to install requirements on destination host"
exit 1
Expand All @@ -64,18 +72,18 @@ fi

# handle case when crontab is empty
echo "EXISTING crontab:"
ssh $HOST 'sudo crontab -u '$CRONTAB_USER' -l'
ssh_cmd $HOST 'sudo crontab -u '$CRONTAB_USER' -l'
if [ $? != 0 ]; then
ssh $HOST 'echo "" | crontab -'
ssh_cmd $HOST 'echo "" | crontab -'
fi
echo "---"

# set up cron job if not set up already
# NOTE: this will overwrite any existing cron jobs from same project directory
ssh $HOST '(sudo crontab -u '$CRONTAB_USER' -l | grep -v '"$DIR"'; cat '$DIR'/crontab) | sort - | uniq - | sudo crontab -u '$CRONTAB_USER' -'
ssh_cmd $HOST '(sudo crontab -u '$CRONTAB_USER' -l | grep -v '"$DIR"'; cat '$DIR'/crontab) | sort - | uniq - | sudo crontab -u '$CRONTAB_USER' -'

echo "UPDATED crontab:"
ssh $HOST 'sudo crontab -u '$CRONTAB_USER' -l'
ssh_cmd $HOST 'sudo crontab -u '$CRONTAB_USER' -l'
echo "---"

echo "Done. New code should be picked up when cron job runs next time."
Expand Down

0 comments on commit de8faf9

Please sign in to comment.