From d6391c856138041205754a604f1b1cad33b12845 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Wed, 27 Dec 2017 13:09:39 -0600 Subject: [PATCH] Move install to php53 branch if running an unsupport version of PHP --- daily.php | 37 +++++++++--------- daily.sh | 74 ++++++++++++++++++++++++++++-------- scripts/composer_wrapper.php | 71 ++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 34 deletions(-) create mode 100755 scripts/composer_wrapper.php diff --git a/daily.php b/daily.php index 669889693b..ef1262cd0b 100644 --- a/daily.php +++ b/daily.php @@ -34,25 +34,6 @@ if ($options['f'] === 'update') { exit(0); } -if ($options['f'] === 'check_php_ver') { - $min_version = '5.6.4'; - $warn_title = 'Warning: PHP version too low'; - - // if update is not set to false and version is min or newer - if (Config::get('update') && version_compare(PHP_VERSION, $min_version, '<')) { - new_notification( - $warn_title, - 'PHP version 5.6.4 will be the minimum supported version on January 10, 2018. We recommend you update to PHP a supported version of PHP (7.1 suggested) to continue to receive updates. If you do not update PHP, LibreNMS will continue to function but stop receiving bug fixes and updates.', - 1, - 'daily.sh' - ); - exit(1); - } - - remove_notification($warn_title); - exit(0); -} - if ($options['f'] === 'rrd_purge') { try { if (Config::get('distributed_poller')) { @@ -150,6 +131,24 @@ if ($options['f'] === 'handle_notifiable') { 'daily.sh' ); } + } elseif ($options['t'] === 'phpver') { + $error_title = 'Error: PHP version too low'; + $warn_title = 'Warning: PHP version too low'; + remove_notification($warn_title); // remove warning + + // if update is not set to false and version is min or newer + if (Config::get('update') && $options['r']) { + new_notification( + $error_title, + 'PHP version 5.6.4 is the minimum supported version as of January 10, 2018. We recommend you update to PHP a supported version of PHP (7.1 suggested) to continue to receive updates. If you do not update PHP, LibreNMS will continue to function but stop receiving bug fixes and updates.', + 2, + 'daily.sh' + ); + exit(1); + } + + remove_notification($error_title); + exit(0); } } diff --git a/daily.sh b/daily.sh index 5467ae2da5..95546973d4 100755 --- a/daily.sh +++ b/daily.sh @@ -1,7 +1,8 @@ #!/usr/bin/env bash ################################################################################ # Copyright (C) 2015 Daniel Preussker, QuxLabs UG -# Layne "Gorian" Breitkreutz +# Copyright (C) 2016 Layne "Gorian" Breitkreutz +# Copyright (C) 2017 Tony Murray # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or @@ -22,6 +23,7 @@ # define DAILY_SCRIPT as the full path to this script and LIBRENMS_DIR as the directory this script is in DAILY_SCRIPT=$(readlink -f "$0") LIBRENMS_DIR=$(dirname "$DAILY_SCRIPT") +COMPOSER="php ${LIBRENMS_DIR}/scripts/composer_wrapper.php" # set log_file, using librenms $config['log_dir'], if set # otherwise we default to /logs @@ -31,7 +33,6 @@ LOG_DIR=$(php -r "@include '${LIBRENMS_DIR}/config.php'; echo isset(\$config['lo LIBRENMS_USER=$(php -r "@include '${LIBRENMS_DIR}/config.php'; echo isset(\$config['user']) ? \$config['user'] : 'root';") LIBRENMS_USER_ID=$(id -u "$LIBRENMS_USER") - ####################################### # Fancy-Print and run commands # Globals: @@ -58,7 +59,7 @@ status_run() { printf "%-50s" "${arg_text}"; echo "${arg_text}" >> ${log_file} - tmp=$(bash -c "${arg_command}" 2>&1); + tmp=$(${arg_command} 2>&1); exit_code=$? echo "${tmp}" >> ${log_file} echo "Returned: ${exit_code}" >> ${log_file} @@ -118,6 +119,26 @@ set_notifiable_result() { php "${LIBRENMS_DIR}/daily.php" -f handle_notifiable -t ${arg_type} -r ${arg_result}; } +####################################### +# Check the PHP version and branch and switch to the appropriate branch +# Returns: +# Exit-Code: 0 >= min ver, 1 < min ver +####################################### +check_php_ver() { + local branch=$(git rev-parse --abbrev-ref HEAD) + local ver_res=$(php -r "echo (int)version_compare(PHP_VERSION, '5.6.4', '<');") + if [[ "$branch" == "php53" && "$ver_res" == "0" ]]; then + git checkout master + branch="master" + elif [[ "$branch" != "php53" && "$ver_res" == "1" ]]; then + git checkout php53 + branch="php53" + fi + + return ${ver_res}; +} + + ####################################### # Entry into program # Globals: @@ -129,6 +150,10 @@ set_notifiable_result() { ####################################### main () { local arg="$1"; + local old_version="$2"; + local new_version="$3"; + local old_version="${old_version:=unset}" # if $1 is unset, make it mismatch for pre-update daily.sh + cd ${LIBRENMS_DIR}; # if not running as $LIBRENMS_USER (unless $LIBRENMS_USER = root), relaunch @@ -145,8 +170,14 @@ main () { fi fi + # make sure autoload.php exists before trying to run any php that may require it + if [ ! -f "${LIBRENMS_DIR}/vendor/autoload.php" ]; then + ${COMPOSER} install --no-dev + fi + if [[ -z "$arg" ]]; then - status_run 'Checking PHP version' "php ${LIBRENMS_DIR}/daily.php -f check_php_ver" 'check_php_ver' + status_run 'Checking PHP version' 'check_php_ver' + php_ver_ret=$? up=$(php daily.php -f update >&2; echo $?) if [[ "$up" == "0" ]]; then @@ -160,16 +191,22 @@ main () { update_res=0 if [[ "$up" == "1" ]]; then - # Update to Master-Branch - old_ver=$(git show --pretty="%H" -s HEAD) + # Update current branch to latest + old_ver=$(git rev-parse --short HEAD) status_run 'Updating to latest codebase' 'git pull --quiet' 'update' update_res=$? - new_ver=$(git show --pretty="%H" -s HEAD) + new_ver=$(git rev-parse --short HEAD) elif [[ "$up" == "3" ]]; then # Update to last Tag old_ver=$(git describe --exact-match --tags $(git log -n1 --pretty='%h')) - status_run 'Updating to latest release' 'git fetch --tags && git checkout $(git describe --tags $(git rev-list --tags --max-count=1))' 'update' - update_res=$? + if [[ "$php_ver_ret" == "0" ]]; then + status_run 'Updating to latest release' 'git fetch --tags && git checkout $(git describe --tags $(git rev-list --tags --max-count=1))' 'update' + update_res=$? + else + # incompatible php version, check out last supported release + status_run 'Updating to latest release' 'git fetch --tags && git checkout 1.35' 'update' + update_res=$? + fi new_ver=$(git describe --exact-match --tags $(git log -n1 --pretty='%h')) fi @@ -177,13 +214,8 @@ main () { set_notifiable_result update 0 fi - if [[ "$old_ver" != "$new_ver" ]]; then - status_run "Updated from $old_ver to $new_ver" '' - set_notifiable_result update 1 # only clear the error if update was a success - fi - # Call ourself again in case above pull changed or added something to daily.sh - ${DAILY_SCRIPT} post-pull + ${DAILY_SCRIPT} post-pull ${old_ver} ${new_ver} else case $arg in no-code-update) @@ -193,6 +225,18 @@ main () { status_run 'Cleaning up DB' "$DAILY_SCRIPT cleanup" ;; post-pull) + # Check if we need to revert (Must be in post pull so we can update it) + if [[ "$old_version" != "$new_version" ]]; then + check_php_ver # check php version and switch branches + php_res=$? + if [[ "$php_res" == "1" ]]; then + status_run "Reverting update, PHP version older than 5.6.4" '' + else + status_run "Updated from $old_version to $new_version" '' + set_notifiable_result update 1 # only clear the error if update was a success + fi + fi + # List all tasks to do after pull in the order of execution status_run 'Updating SQL-Schema' 'php includes/sql-schema/update.php' status_run 'Updating submodules' "$DAILY_SCRIPT submodules" diff --git a/scripts/composer_wrapper.php b/scripts/composer_wrapper.php new file mode 100755 index 0000000000..e70e0752c0 --- /dev/null +++ b/scripts/composer_wrapper.php @@ -0,0 +1,71 @@ +#!/usr/bin/env php +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2017 Tony Murray + * @author Tony Murray + */ + +$install_dir = realpath(__DIR__ . '/..'); +chdir($install_dir); + +// Set up proxy if needed +$proxy = getenv("HTTP_PROXY") ?: getenv("http_proxy"); +if (!$proxy && $proxy = rtrim(shell_exec('git config --global --get http.proxy'))) { + // Use git http.proxy if available + putenv("HTTP_PROXY=$proxy"); +} + +$exec = false; + +$path_exec = shell_exec("which composer 2> /dev/null"); +if (!empty($path_exec)) { + $exec = trim($path_exec); +} elseif (is_file($install_dir . '/composer.phar')) { + $exec = 'php ' . $install_dir . '/composer.phar'; +} else { + if ($proxy) { + $stream_default_opts = array( + 'http' => array( + 'proxy' => str_replace('http://', 'tcp://', $proxy), + 'request_fulluri' => true, + ) + ); + + stream_context_set_default($stream_default_opts); + } + + // Download composer.phar (code from the composer web site) + $sha = trim(file_get_contents('http://composer.github.io/installer.sig')); + @copy('http://getcomposer.org/installer', 'composer-setup.php'); + if (@hash_file('SHA384', 'composer-setup.php') === $sha) { + // Installer verified + shell_exec('php composer-setup.php'); + $exec = 'php ' . $install_dir . '/composer.phar'; + } + @unlink('composer-setup.php'); +} + +if ($exec) { + passthru("$exec " . implode(' ', array_splice($argv, 1)) . ' 2>&1'); +} else { + echo "Composer not available, please manually install composer.\n"; +}