scripts/github/check.gitea.update

329 lines
9.4 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# Purpose {{{
## Create a temp file (to monitor) if an upgrade is available for Gitea
## project on dl.gitea.io/Github.
## It based on gitea's binary available in $PATH or give as first argument.
## How-to use {{{
### 1. Needs releasetags script, in the same directory
### cf. https://git.ipr.univ-rennes1.fr/cellinfo/scripts/src/master/github/releasetags
# wget https://git.ipr.univ-rennes1.fr/cellinfo/scripts/raw/master/github/releasetags
### 2. Create a cron job, eg.
#00 20 * * * root /opt/repos/ipr.scripts/github/check_gitea_update
### 2-1 Create a cron job with a specific path for gitea bin, eg.
#00 20 * * * root /opt/repos/ipr.scripts/github/check_gitea_update /srv/bin/gitea
### 3. Monitor the temp file: /tmp/.github.gitea.upgrade
# Or enable MAILTO in cronjob and edit the script to print a message.
# Or send a mail.
# …
## }}}
# }}}
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}"
readonly NBARGS="${#}"
## Test if DEBUG is already defined (by parent script,…)
[ -z "${DEBUG}" ] && DEBUG=1
## Default values for some vars
GITEA_BIN_PATH_DEFAULT="$(command -v gitea)"
GITEA_DOWNLOAD_DEFAULT=1
gitea_download_github=1
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat << HELP
usage: $PROGNAME (/path/to/gitea) [-b|-p|-d|-h]
Compare current version of an installed Gitea and the last available.
EXAMPLES:
- Compare the current version of Gitea with bin available in \$PATH
${PROGNAME}
- Compare the current version of Gitea at a specific place
${PROGNAME} /srv/bin/gitea
${PROGNAME} --path /srv/bin/gitea
- Compare version and try to download the new version if require,
next to current bin with '.to.upgrade.vX.YY.ZZ' suffix.
${PROGNAME} --download
${PROGNAME} --path /srv/bin/gitea --download
- Compare version and try to download the new version from github.
${PROGNAME} --download --github
OPTIONS:
-b,--bin,-p,--path
Set the Gitea's path to use.
--download
Try to download the new release if available.
--github
Download new version from Github repository
(dl.gitea.io is used by default).
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
HELP
}
# }}}
debug_message() { # {{{
local_debug_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_debug_message}"
unset local_debug_message
return 0
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## Print message
printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
exit "${local_error_code:=66}"
}
# }}}
is_var_empty() { # {{{
## Return False by default
return_var_empty="1"
## Total number of variables to test
local_total_var_empty="${#}"
loop_count_var_empty="0"
## While it remains a variable to test
while [ "${local_total_var_empty}" -gt "${loop_count_var_empty}" ]; do
debug_message "is_var_empty \
Test var: ${1}."
### Test if this is empty and set return value to True
[ -z "${1}" ] && return_var_empty="0"
### Increase the number of tested variables
loop_count_var_empty=$((loop_count_var_empty+1))
### Shift to the next variable
shift
done
unset local_total_var_empty
unset loop_count_var_empty
return "${return_var_empty}"
}
# }}}
is_file_empty() { # {{{
local_file="${1}"
## File is empty by default
return_is_file_empty="0"
### Check if the file is empty
if [ ! -s "${local_file}" ]; then
return_is_file_empty="0"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} is empty or doesn't exists."
else
return_is_file_empty="1"
debug_message "is_file_empty \
The file ${RED}${local_file}${COLOR_DEBUG} exists and has a size greater than zero."
fi
return "${return_is_file_empty}"
}
# }}}
define_vars() { # {{{
## If gitea_bin_path wasn't defined (argument) {{{
## Use default value
is_var_empty "${gitea_bin_path}" \
&& gitea_bin_path="${GITEA_BIN_PATH_DEFAULT}"
## }}}
## If gitea_download wasn't defined (argument) {{{
## Use default value
is_var_empty "${gitea_download}" \
&& gitea_download="${GITEA_DOWNLOAD_DEFAULT}"
## }}}
## Directory where is current Gitea bin
gitea_bin_dir=$(readlink -m $(dirname "${gitea_bin_path}"))
## Main Gitea repo's URL
gitea_repo_url="https://github.com/go-gitea/gitea"
## Gitea's versions
gitea_current_version=$("${gitea_bin_path}" --version | cut --delimiter=' ' --fields=3)
gitea_new_version=$("${PROGDIR}"/releasetags "${gitea_repo_url}" | grep --invert-match --extended-regexp -- '(dev|rc|^$)' | head --lines=1 | sed 's/v//')
## Gitea new version's URL
if [ "${gitea_download_github}" -eq "0" ]; then
gitea_new_version_url="https://github.com/go-gitea/gitea/releases/download/v${gitea_new_version}/gitea-${gitea_new_version}-linux-amd64"
else
gitea_new_version_url="https://dl.gitea.io/gitea/${gitea_new_version}/gitea-${gitea_new_version}-linux-amd64"
fi
## Gitea new version's path
gitea_new_bin_path="${gitea_bin_path}.to.upgrade.v${gitea_new_version}"
## Temp file to monitor
gitea_new_version_file="/tmp/.github.gitea.upgrade"
}
# }}}
main() { # {{{
define_vars
# If gitea_bin_path is empty {{{
# Exit with error
is_var_empty "${gitea_current_version}" \
&& error_message "Can't get current version from Gitea's binary (${gitea_bin_path})." 1
# }}}
# If gitea_new_version is empty {{{
# Exit with error
is_var_empty "${gitea_new_version}" \
&& error_message "Can't get new version (${gitea_new_version}) from Gitea's repository (${gitea_repo_url})." 1
# }}}
# If an update is available {{{
if [ "${gitea_current_version}" != "${gitea_new_version}" ]; then
debug_message "Test version \
Current version (${RED}${gitea_current_version}${COLOR_DEBUG}) and new one (${RED}${gitea_new_version}${COLOR_DEBUG}) seems different."
## Create a temp file to monitor
touch -- "${gitea_new_version_file}"
printf '\e[1;35m%-6s\e[m\n' "An upgrade is available for Gitea (current: ${gitea_current_version}): ${gitea_new_version}." >> "${gitea_new_version_file}"
## If new release should be downloaded
if [ "${gitea_download}" -eq "0" ]; then
### Download and prepare the new bin {{{
debug_message "Test version \
Download Gitea binary (from ${gitea_new_version_url} ) to ${RED}${gitea_new_bin_path}${COLOR_DEBUG} ."
wget --quiet "${gitea_new_version_url}" --output-document="${gitea_new_bin_path}"
### }}}
### If the new binary file is empty {{{
### Ensure to remove (don't want an empty upgrade file)
### Exit with error
is_file_empty "${gitea_new_bin_path}" \
&& rm --force -- "${gitea_new_bin_path}" \
&& error_message "New binary (${gitea_new_bin_path}) is empty or doesn't exists." 2
### }}}
chmod +x -- "${gitea_new_bin_path}"
fi
# }}}
# Gitea is up-to-date {{{
else
debug_message "Test version \
The current version (${RED}${gitea_current_version}${COLOR_DEBUG}) is up-to-date."
debug_message "Test version \
Ensure to remove temp file and any previous and unused releases."
rm --force -- "${gitea_new_version_file}"
find "${gitea_bin_dir}" -maxdepth 1 -type f -iname "gitea.to.upgrade*" -delete
fi
# }}}
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to arguments
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
## If the first argument is not an option
if ! printf -- '%s' "${1}" | grep --quiet --extended-regexp -- "^-+";
then
## Consider it as the path to Gitea
gitea_bin_path="${1}"
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep --quiet --extended-regexp -- "^-+"; do
case "${1}" in
-b|--bin|-p|--path ) ## Define gitea_bin_path
## Move to the next argument
shift
## Define var
readonly gitea_bin_path="${1}"
;;
--download ) ## Enable download_mode
## Try to download new release if available
gitea_download=0
;;
--github ) ## Download new release from Github
## Try to download new release from Github
gitea_download_github=0
;;
-d|--debug ) ## debug
DEBUG=0
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
-- ) ## End of options list
## End the while loop
break
;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main
exit 0