Skip to content

Instantly share code, notes, and snippets.

@damonvjanis
Last active July 4, 2023 09:43
Show Gist options
  • Save damonvjanis/66ef8e24d6a3c50506926d66f96283c5 to your computer and use it in GitHub Desktop.
Save damonvjanis/66ef8e24d6a3c50506926d66f96283c5 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# Navigate to app directory
cd /home/<YOUR_USERNAME>/my_app
# Find the current release and the second newest release
current_release=$(ls ../releases | sort -nr | head -n 1)
previous_release=$(ls ../releases | sort -nr | tail -n +2 | head -n 1)
# Get the HTTP_PORT variable from the currently running release
source ../releases/${current_release}/releases/0.1.0/env.sh
if [[ $HTTP_PORT == '4000' ]]
then
http=4001
https=4041
old_port=4000
else
http=4000
https=4040
old_port=4001
fi
# Put env vars with the ports to forward to, and set non-conflicting node name
echo "export HTTP_PORT=${http}" >> ../releases/${previous_release}/releases/0.1.0/env.sh
echo "export HTTPS_PORT=${https}" >> ../releases/${previous_release}/releases/0.1.0/env.sh
echo "export RELEASE_NAME=${http}" >> ../releases/${previous_release}/releases/0.1.0/env.sh
# Set the release to the the previous version
rm ../env_vars || true
touch ../env_vars
echo "RELEASE=${previous_release}" >> ../env_vars
# Boot the new version of the app
sudo systemctl start my_app@${http}
# Wait for the new version to boot
until $(curl --output /dev/null --silent --head --fail localhost:${http}); do
echo 'Waiting for app to boot...'
sleep 1
done
# Switch forwarding of ports 443 and 80 to the ones the new app is listening on
sudo iptables -t nat -R PREROUTING 1 -p tcp --dport 80 -j REDIRECT --to-port ${http}
sudo iptables -t nat -R PREROUTING 2 -p tcp --dport 443 -j REDIRECT --to-port ${https}
# Stop the old version
sudo systemctl stop my_app@${old_port}
# Remove the problematic release
rm -rf ../releases/${current_release}
echo 'Rolled back!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment