error checking fixed, comments added

This commit is contained in:
Filip Rojek 2023-08-31 18:31:37 +02:00
parent 4ab11cb237
commit d89b59e13f

View File

@ -1,8 +1,15 @@
#!/bin/bash #!/bin/bash
# version 1.0.1
# made by Filip Rojek <filip@filiprojek.cz>
# project website: https://filiprojek.cz/projects/gitlab-ce-auto-minor-upgrade/
# git repo: https://git.filiprojek.cz/fr/gitlab-ce-auto-minor-upgrade/
#
# TODO:
# - check and notify for new major version
PKG_NAME="gitlab-ce" PKG_NAME="gitlab-ce"
CURRENT_VERSION=$(dpkg -s $PKG_NAME | grep Version | sed 's/Version: //') CURRENT_VERSION=$(dpkg -s $PKG_NAME | grep Version | sed 's/Version: //') # TODO: make this as an function and call it in success log line
MINOR_PREFIX=$(dpkg -s $PKG_NAME | grep Version | sed 's/Version: //' | awk -F "." '{print $1 "." $2}') MINOR_PREFIX=$(dpkg -s $PKG_NAME | grep Version | sed 's/Version: //' | awk -F "." '{print $1 "." $2}')
UPDATE_TO=$(apt-cache madison $PKG_NAME | grep $CURRENT_VERSION -B5 | awk -F "|" '{print $2}' | sed 's/ //' | grep $MINOR_PREFIX | head -n1) UPDATE_TO=$(apt-cache madison $PKG_NAME | grep $CURRENT_VERSION -B5 | awk -F "|" '{print $2}' | sed 's/ //' | grep $MINOR_PREFIX | head -n1)
@ -17,17 +24,23 @@ UPDATE_TO=$(echo "$UPDATE_TO" | tr -d '[:space:]')
if [[ "$CURRENT_VERSION" == "$UPDATE_TO" ]]; then if [[ "$CURRENT_VERSION" == "$UPDATE_TO" ]]; then
echo "$DATE | $CURRENT_VERSION | no new minor version is available" >> /var/log/gitlab-auto-update.log echo "$DATE | $CURRENT_VERSION | no new minor version is available" >> /var/log/gitlab-auto-update.log
exit 0
else else
echo "$DATE | $CURRENT_VERSION | updating to $UPDATE_TO" >> /var/log/gitlab-auto-update.log echo "$DATE | $CURRENT_VERSION | updating to $UPDATE_TO" >> /var/log/gitlab-auto-update.log
sudo apt install -y "$PKG_NAME=$UPDATE_TO" 2>"$ERROR_LOG" INSTALL_LOG=$(sudo apt install -y "$PKG_NAME=$UPDATE_TO")
INSTALLATION_STATUS_CODE=$(echo $?)
# Check if there were any errors # Check if there were any errors
if [ -s "$ERROR_LOG" ]; then if [ INSTALLATION_STATUS_CODE != 0 ]; then
echo "$DATE | $CURRENT_VERSION | error occurred while updating the package" >> $UPDATE_LOG echo "$DATE | $CURRENT_VERSION | error occurred while updating the package" >> $UPDATE_LOG
echo "$DATE | $CURRENT_VERSION | error occurred while updating the package" >> $ERROR_LOG
echo $INSTALL_LOG >> $ERROR_LOG
echo "\n\n\n" >> $ERROR_LOG
cat "$ERROR_LOG" cat "$ERROR_LOG"
exit 1 exit 1
else else
echo "$DATE | $CURRENT_VERSION | package updated successfully" >> $UPDATE_LOG echo "$DATE | $CURRENT_VERSION | package has been successfully updated to $UPDATE_TO" >> $UPDATE_LOG
exit 0
fi fi
fi fi