2022-08-26 13:27:52 +02:00
#!/bin/sh
#
# Purpose {{{
# This script will :
# 1. Get Etherpad version from current installation (from directory)
# 2. Get latest Etherpad version from Github.
# 3. Compare installed and latest version.
# 4. Create a temp file (to monitor) if versions mismatch.
#
## 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 with Etherpad directory as argument, eg.
#00 20 * * * root /opt/repos/ipr.scripts/github/check_cachet_update --directory /srv/www/etherpad-lite
### 3. Monitor the temp file : /tmp/.github.etherpad.upgrade
### 3.b Or enable MAILTO in cronjob and edit the script to print a message.
## }}}
#
# 2022-08-25
# }}}
# Vars {{{
PROGNAME = $( basename " ${ 0 } " ) ; readonly PROGNAME
PROGDIR = $( readlink -m $( dirname " ${ 0 } " ) ) ; readonly PROGDIR
ARGS = " ${ * } " ; readonly ARGS
readonly NBARGS = " ${# } "
[ -z " ${ DEBUG } " ] && DEBUG = 1
## Export DEBUG for sub-script
export DEBUG
## Default values for some vars
readonly ETHERPAD_INSTALL_DIR_DEFAULT = "/srv/www/etherpad-lite"
readonly ETHERPAD_REPO_URL = "https://github.com/ether/etherpad-lite"
readonly ETHERPAD_NEW_VERSION_FILE = "/tmp/.github.etherpad.upgrade"
## Colors
readonly PURPLE = '\033[1;35m'
readonly RED = '\033[0;31m'
readonly RESET = '\033[0m'
readonly COLOR_DEBUG = " ${ PURPLE } "
# }}}
usage( ) { # {{{
cat <<- HELP
usage: $PROGNAME [ -d| -h]
Compare Etherpad installed version and latest one.
EXAMPLES :
- Check Etherpad version from default directory ( ${ ETHERPAD_INSTALL_DIR_DEFAULT } )
${ PROGNAME }
- Check Etherpad version from a specific location
2023-08-02 14:38:04 +02:00
${ PROGNAME } --directory /srv/www/my_etherpad
2022-08-26 13:27:52 +02:00
OPTIONS :
--debug
Enable debug messages.
-d,--dir,--directory
Etherppad installation directory.
( default: ${ ETHERPAD_INSTALL_DIR_DEFAULT } ) .
-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 } "
}
# }}}
define_vars( ) { # {{{
## If etherpad_install_dir wasn't defined (argument) {{{
if [ -z " ${ etherpad_install_dir } " ] ; then
## Use default value
readonly etherpad_install_dir = " ${ ETHERPAD_INSTALL_DIR_DEFAULT } "
fi
## }}}
}
# }}}
is_var_empty( ) { # {{{
## Return False by default
return_var_empty = "1"
## Total number of variables to test
local_total_var = " ${# } "
loop_count_var_empty = "0"
## While it remains a variable to test
while [ " ${ local_total_var } " -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
return " ${ return_var_empty } "
}
# }}}
main( ) { # {{{
## Define all vars
define_vars
## If Etherpad install dir doesn't exists {{{
### AND Exit with error message
! test -d ${ etherpad_install_dir } \
&& error_message " Etherpad installation directory ( ${ etherpad_install_dir } ) doesn't seem to exist. " 01
## }}}
## Get current version from src/package.json file {{{
### OR Exit with error message
etherpad_current_version = $( sed --silent 's/.*"version": "\(.*\)".*/\1/p' " ${ etherpad_install_dir } " /src/package.json \
|| error_message " Can't get current Etherpad version from file ( ${ etherpad_install_dir } /src/package.json). " 02 )
debug_message " main − Etherpad installed version : ${ RED } ${ etherpad_current_version } ${ COLOR_DEBUG } (from ${ etherpad_install_dir } /src/package.json file). "
## }}}
## Get latest version from project repository {{{
### OR Exit with error message
2023-08-02 14:38:04 +02:00
etherpad_latest_version = $( " ${ PROGDIR } /releasetags " " ${ ETHERPAD_REPO_URL } " | head --lines= 1 | sed 's/v//' \
2022-08-26 13:27:52 +02:00
|| error_message " Can't get latest Etherpad version from repository ( ${ ETHERPAD_REPO_URL } ). " 02 )
2023-08-02 14:38:04 +02:00
debug_message " main − Etherpad available version : ${ RED } ${ etherpad_latest_version } ${ COLOR_DEBUG } (from ${ ETHERPAD_REPO_URL } project repository). "
2022-08-26 13:27:52 +02:00
## }}}
## If any of the two variables is empty {{{
### OR Exit with error message
### Double check :)
is_var_empty " ${ etherpad_current_version } " " ${ etherpad_latest_version } " \
&& error_message " At least one variable is empty (current version : ${ etherpad_current_version } , latest version : ${ etherpad_latest_version } ) ! " 04
## }}}
# Check if the current version is the last one {{{
## If the two versions are the same {{{
if [ " ${ etherpad_current_version } " = " ${ etherpad_latest_version } " ] ; then
debug_message " Test version − \
The current Etherpad version ( ${ etherpad_current_version } ) is up-to-date."
### Ensure to remove any temp file
rm --force -- " ${ ETHERPAD_NEW_VERSION_FILE } "
exit 0
## }}}
## If the versions are different {{{
else
debug_message " Test version − \
Current version ( ${ etherpad_current_version } ) and latest one ( ${ etherpad_latest_version } ) seems to be different."
## Create a temp file to monitor
touch -- " ${ ETHERPAD_NEW_VERSION_FILE } "
debug_message " An upgrade is available for Cachet (current : ${ etherpad_current_version } ) : ${ etherpad_latest_version } . " >> " ${ ETHERPAD_NEW_VERSION_FILE } "
exit 0
fi
## }}}
# }}}
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to argument management
if [ ! " ${ NBARGS } " -eq "0" ] ; then
manage_arg = "0"
## If the first argument is not an option
if ! printf -- '%s' " ${ 1 } " | grep -q -E -- "^-+" ;
then
## Print help message and exit
printf '%b\n' " ${ RED } Invalid option: ${ 1 } ${ RESET } "
printf '%b\n' "---"
usage
exit 1
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' " ${ 1 } " | grep -q -E -- "^-+" ; do
case " ${ 1 } " in
--debug ) ## debug
DEBUG = 0
; ;
-h| --help ) ## help
usage
## Exit after help informations
exit 0
; ;
-d| --dir| --directory ) ## Define etherpad_install_dir with given arg
### Move to the next argument
shift
### Define var
readonly etherpad_install_dir = " ${ 1 } "
; ;
* ) ## 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 255