Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move install to php53 branch if running an unsupported version of PHP #8042

Merged
merged 6 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LibreNMS/Validations/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function checkVersion(Validator$validator)

// if update is not set to false and version is min or newer
if (Config::get('update') && version_compare(PHP_VERSION, $min_version, '<')) {
$validator->warn('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.');
$validator->warn('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.');
}
}

Expand Down
15 changes: 11 additions & 4 deletions LibreNMS/Validations/Updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,17 @@ public function validate(Validator $validator)
}

if ($versions['local_branch'] != 'master') {
$validator->warn(
"Your local git branch is not master, this will prevent automatic updates.",
"You can switch back to master with git checkout master"
);
if ($versions['local_branch'] == 'php53') {
$validator->warn(
"You are on the PHP 5.3 support branch, this will prevent automatic updates.",
"Update to PHP 5.6.4 or newer (PHP 7.1 recommended) to continue to receive updates."
);
} else {
$validator->warn(
"Your local git branch is not master, this will prevent automatic updates.",
"You can switch back to master with git checkout master"
);
}
}
}

Expand Down
37 changes: 18 additions & 19 deletions daily.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,6 @@
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')) {
Expand Down Expand Up @@ -150,6 +131,24 @@
'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);
}
}

Expand Down
67 changes: 52 additions & 15 deletions daily.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash
################################################################################
# Copyright (C) 2015 Daniel Preussker, QuxLabs UG <preussker@quxlabs.com>
# Layne "Gorian" Breitkreutz <Layne.Breitkreutz@thelenon.com>
# Copyright (C) 2016 Layne "Gorian" Breitkreutz <Layne.Breitkreutz@thelenon.com>
# Copyright (C) 2017 Tony Murray <murraytony@gmail.com>
# 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
Expand All @@ -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 <LibreNMS Install Directory>/logs
Expand All @@ -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:
Expand All @@ -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}
Expand Down Expand Up @@ -118,6 +119,28 @@ 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
status_run "Supported PHP version, switched back to master branch." 'git checkout master'
branch="master"
elif [[ "$branch" != "php53" ]] && [[ "$ver_res" == "1" ]]; then
status_run "Unsupported PHP version, switched to php53 branch." 'git checkout php53'
branch="php53"
fi

set_notifiable_result phpver ${ver_res}

return ${ver_res};
}


#######################################
# Entry into program
# Globals:
Expand All @@ -129,6 +152,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
Expand All @@ -145,26 +172,32 @@ main () {
fi
fi

if [[ -z "$arg" ]]; then
status_run 'Checking PHP version' "php ${LIBRENMS_DIR}/daily.php -f check_php_ver" 'check_php_ver'
# 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
up=$(php daily.php -f update >&2; echo $?)
if [[ "$up" == "0" ]]; then
${DAILY_SCRIPT} no-code-update
set_notifiable_result update 1 # make sure there are no update notifications if update is disabled
exit
fi

status_run 'Checking PHP version' 'check_php_ver'
php_ver_ret=$?

# make sure the vendor directory is clean
git checkout vendor/ --quiet > /dev/null 2>&1

update_res=0
if [[ "$up" == "1" ]]; then
# Update to Master-Branch
old_ver=$(git show --pretty="%H" -s HEAD)
if [[ "$up" == "1" ]] || [[ "$php_ver_ret" == "0" ]]; then
# 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'))
Expand All @@ -177,13 +210,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)
Expand All @@ -193,6 +221,15 @@ 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

# new_version may be incorrect if we just switch branches... ignoring that detail
status_run "Updated from $old_version to $new_version" ''
set_notifiable_result update 1 # only clear the error if update was a success
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"
Expand Down
71 changes: 71 additions & 0 deletions scripts/composer_wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env php
<?php
/**
* composer_wrapper.php
*
* Wrapper for composer to use system provided composer or download and use composer.phar
*
* 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
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

$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";
}