Skip to content

Commit

Permalink
MDL-37596 optionally enable CLI maintenance later
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jan 28, 2013
1 parent b3778a0 commit 48e114a
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 13 deletions.
56 changes: 46 additions & 10 deletions admin/cli/maintenance.php
@@ -1,5 +1,4 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand All @@ -16,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Enable or disable maintenance mode
* Enable or disable maintenance mode.
*
* @package core
* @subpackage cli
Expand All @@ -26,12 +25,13 @@

define('CLI_SCRIPT', true);

require(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once($CFG->libdir.'/clilib.php'); // cli only functions
require(__DIR__.'/../../config.php');
require_once("$CFG->libdir/clilib.php");
require_once("$CFG->libdir/adminlib.php");


// now get cli options
list($options, $unrecognized) = cli_get_params(array('enable'=>false, 'disable'=>false, 'help'=>false),
// Now get cli options.
list($options, $unrecognized) = cli_get_params(array('enable'=>false, 'enablelater'=>0, 'enableold'=>false, 'disable'=>false, 'help'=>false),
array('h'=>'help'));

if ($unrecognized) {
Expand All @@ -45,12 +45,14 @@
Current status displayed if not option specified.
Options:
--enable Enable maintenance mode
--enable Enable CLI maintenance mode
--enablelater=MINUTES Number of minutes before entering CLI maintenance mode
--enableold Enable legacy half-maintenance mode
--disable Disable maintenance mode
-h, --help Print out this help
Example:
\$sudo -u www-data /usr/bin/php admin/cli/maintenance.php
\$ sudo -u www-data /usr/bin/php admin/cli/maintenance.php
"; //TODO: localize - to be translated later when everything is finished

echo $help;
Expand All @@ -59,18 +61,52 @@

cli_heading(get_string('sitemaintenancemode', 'admin')." ($CFG->wwwroot)");

if ($options['enable']) {
if ($options['enablelater']) {
if (file_exists("$CFG->dataroot/climaintenance.html")) {
// Already enabled, sorry.
echo get_string('clistatusenabled', 'admin')."\n";
return 1;
}

$time = time() + ($options['enablelater']*60);
set_config('maintenance_later', $time);

echo get_string('clistatusenabledlater', 'admin', userdate($time))."\n";
return 0;

} else if ($options['enable']) {
if (file_exists("$CFG->dataroot/climaintenance.html")) {
// The maintenance is already enabled, nothing to do.
} else {
enable_cli_maintenance_mode();
}
set_config('maintenance_enabled', 0);
unset_config('maintenance_later');
echo get_string('sitemaintenanceoncli', 'admin')."\n";
exit(0);

} else if ($options['enableold']) {
set_config('maintenance_enabled', 1);
unset_config('maintenance_later');
echo get_string('sitemaintenanceon', 'admin')."\n";
exit(0);

} else if ($options['disable']) {
set_config('maintenance_enabled', 0);
unset_config('maintenance_later');
if (file_exists("$CFG->dataroot/climaintenance.html")) {
unlink("$CFG->dataroot/climaintenance.html");
}
echo get_string('sitemaintenanceoff', 'admin')."\n";
exit(0);
}

if (!empty($CFG->maintenance_enabled)) {
if (!empty($CFG->maintenance_enabled) or file_exists("$CFG->dataroot/climaintenance.html")) {
echo get_string('clistatusenabled', 'admin')."\n";

} else if (isset($CFG->maintenance_later)) {
echo get_string('clistatusenabledlater', 'admin', userdate($CFG->maintenance_later))."\n";

} else {
echo get_string('clistatusdisabled', 'admin')."\n";
}
3 changes: 3 additions & 0 deletions lang/en/admin.php
Expand Up @@ -105,6 +105,7 @@
$string['cliincorrectvalueretry'] = 'Incorrect value, please retry';
$string['clistatusdisabled'] = 'Status: disabled';
$string['clistatusenabled'] = 'Status: enabled';
$string['clistatusenabledlater'] = 'status: CLI maintenance mode will be enabled on {$a}';
$string['clitypevalue'] = 'type value';
$string['clitypevaluedefault'] = 'type value, press Enter to use default value ({$a})';
$string['cliunknowoption'] = 'Unrecognised options:
Expand Down Expand Up @@ -664,6 +665,7 @@
$string['loglifetime'] = 'Keep logs for';
$string['longtimewarning'] = '<b>Please note that this process can take a long time.</b>';
$string['maintenancemode'] = 'In maintenance mode';
$string['maintenancemodeisscheduled'] = 'Site is switching to maintenance mode in {$a} minutes';
$string['maintfileopenerror'] = 'Error opening maintenance files!';
$string['maintinprogress'] = 'Maintenance is in progress...';
$string['manageformats'] = 'Manage course formats';
Expand Down Expand Up @@ -955,6 +957,7 @@
$string['sitemaintenancemode'] = 'Maintenance mode';
$string['sitemaintenanceoff'] = 'Maintenance mode has been disabled and the site is running normally again';
$string['sitemaintenanceon'] = 'Your site is currently in maintenance mode (only admins can log in or use the site).';
$string['sitemaintenanceoncli'] = 'Your site is currently in CLI maintenance mode, no web access is allowed.';
$string['sitemaintenancewarning'] = 'Your site is currently in maintenance mode (only admins can log in). To return this site to normal operation, <a href="maintenance.php">disable maintenance mode</a>.';
$string['sitemaintenancewarning2'] = 'Your site is currently in maintenance mode (only admins can log in). To return this site to normal operation, <a href="{$a}">disable maintenance mode</a>.';
$string['sitepolicies'] = 'Site policies';
Expand Down
28 changes: 28 additions & 0 deletions lib/adminlib.php
Expand Up @@ -679,6 +679,34 @@ function is_dataroot_insecure($fetchtest=false) {
return INSECURE_DATAROOT_WARNING;
}

/**
* Enables CLI maintenance mode by creating new dataroot/climaintenance.html file.
*/
function enable_cli_maintenance_mode() {
global $CFG;

if (file_exists("$CFG->dataroot/climaintenance.html")) {
unlink("$CFG->dataroot/climaintenance.html");
}

if (isset($CFG->maintenance_message) and !html_is_blank($CFG->maintenance_message)) {
$data = $CFG->maintenance_message;
$data = bootstrap_renderer::early_error_content($data, null, null, null);
$data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data);

} else if (file_exists("$CFG->dataroot/climaintenance.template.html")) {
$data = file_get_contents("$CFG->dataroot/climaintenance.template.html");

} else {
$data = get_string('sitemaintenance', 'admin');
$data = bootstrap_renderer::early_error_content($data, null, null, null);
$data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data);
}

file_put_contents("$CFG->dataroot/climaintenance.html", $data);
chmod("$CFG->dataroot/climaintenance.html", $CFG->filepermissions);
}

/// CLASS DEFINITIONS /////////////////////////////////////////////////////////


Expand Down
21 changes: 21 additions & 0 deletions lib/outputrenderers.php
Expand Up @@ -428,6 +428,27 @@ public function standard_top_of_body_html() {
if (!empty($CFG->additionalhtmltopofbody)) {
$output .= "\n".$CFG->additionalhtmltopofbody;
}
$output .= $this->maintenance_warning();
return $output;
}

/**
* Scheduled maintenance warning message.
*
* Note: This is a nasty hack to display maintenance notice, this should be moved
* to some general notification area once we have it.
*
* @return string
*/
public function maintenance_warning() {
global $CFG;

$output = '';
if (isset($CFG->maintenance_later) and $CFG->maintenance_later > time()) {
$output .= $this->box_start('errorbox maintenancewarning');
$output .= get_string('maintenancemodeisscheduled', 'admin', (int)(($CFG->maintenance_later-time())/60));
$output .= $this->box_end();
}
return $output;
}

Expand Down
13 changes: 13 additions & 0 deletions lib/setup.php
Expand Up @@ -888,6 +888,19 @@ function stripslashes_deep($value) {
}
}

// Switch to CLI maintenance mode if required, we need to do it here after all the settings are initialised.
if (isset($CFG->maintenance_later) and $CFG->maintenance_later <= time()) {
if (!file_exists("$CFG->dataroot/climaintenance.html")) {
require_once("$CFG->libdir/adminlib.php");
enable_cli_maintenance_mode();
}
unset_config('maintenance_later');
if (AJAX_SCRIPT) {
die;
} else if (!CLI_SCRIPT) {
redirect(new moodle_url('/'));
}
}

// note: we can not block non utf-8 installations here, because empty mysql database
// might be converted to utf-8 in admin/index.php during installation
Expand Down
6 changes: 3 additions & 3 deletions lib/setuplib.php
Expand Up @@ -1596,15 +1596,15 @@ public static function early_redirect_message($encodedurl, $message, $delay) {
* @param string $meta meta tag
* @return string html page
*/
protected static function plain_page($title, $content, $meta = '') {
public static function plain_page($title, $content, $meta = '') {
if (function_exists('get_string') && function_exists('get_html_lang')) {
$htmllang = get_html_lang();
} else {
$htmllang = '';
}

return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" ' . $htmllang . '>
return '<!DOCTYPE html>
<html ' . $htmllang . '>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
'.$meta.'
Expand Down

0 comments on commit 48e114a

Please sign in to comment.