Skip to content

Instantly share code, notes, and snippets.

@pacharanero
Last active January 11, 2024 12:15
Show Gist options
  • Save pacharanero/4b9730d20ed750a7dea8ffc37aafb770 to your computer and use it in GitHub Desktop.
Save pacharanero/4b9730d20ed750a7dea8ffc37aafb770 to your computer and use it in GitHub Desktop.
#!/bin/bash -x
set -e
# COMMAND
# ssh into the required server and run the following command
# curl -Lks https://gist.github.com/pacharanero/4b9730d20ed750a7dea8ffc37aafb770/raw | sudo /bin/bash
#function definitions
update-ubuntu() {
echo "Starting update..."
local aptDate="$(stat -c %Y '/var/cache/apt')"
local nowDate="$(date +'%s')"
local updateInterval=$((60 * 60 * 24))
echo "update performed last:" $((nowDate - aptDate)) "seconds ago"
# check when the last update was performed
if [ $((nowDate - aptDate)) -gt $updateInterval ]
then
echo "Starting OS update..."
if """
export DEBIAN_FRONTEND=noninteractive &&
sudo apt-get update &&
sudo apt-get -y dist-upgrade &&
sudo apt-get -y autoremove &&
sudo apt-get -y autoclean"""
then
echo "OS update completed successfully."
else
echo "Error occurred while updating OS. Stopping the script."
exit 1
fi
else
echo "Skipping OS update. Last OS update was performed less than 24 hours ago."
fi
# discourse updates (if applicable)
if [ -d /var/discourse ]
then
echo "Checking Discourse repository status..."
# Check if the local repository is up-to-date with the remote repository
local_commit=$(cd /var/discourse && git rev-parse HEAD)
remote_commit=$(cd /var/discourse && git fetch origin && git rev-parse origin/master)
if [ "$local_commit" != "$remote_commit" ]
then
echo "Starting Discourse update..."
if
cd /var/discourse;
git pull;
./launcher rebuild app;
echo Y Y Y | ./launcher cleanup
then
echo -e "\e[31m\nDiscourse update completed successfully.\e[0m"
else
echo -e "\e[31m\nError occurred while updating Discourse. Stopping the script.\e[0m"
exit 1
fi
else
echo "\e[31m\nDiscourse is up-to-date.\e[0m"
fi
else
echo -e "\e[31m\nNo Discourse installation found.\e[0m"
fi
# perform reboot (if required)
if cat /var/run/reboot-required
then
echo -e "\e[31m OS reboot is required\n\n\n\e[0m"
sudo reboot now
else
echo -e "\e[32mOS reboot is not required\n\n\n\e[0m"
fi
}
update-ubuntu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment