MDL-49087 mnet: Use real dataroot instead of user-provided

This commit is contained in:
Andrew Nicols 2015-02-11 10:03:15 +08:00 committed by Eloy Lafuente (stronk7)
parent e24066ff85
commit 9b92620ff1
2 changed files with 104 additions and 13 deletions

View File

@ -31,14 +31,12 @@
* --typeroot=/var/www/moodle/htdocs/blocks
* --name=loancalc
* --md5=...
* --dataroot=/var/www/moodle/data
*
* $ sudo -u apache php mdeploy.php --upgrade \
* --package=https://moodle.org/plugins/download.php/...zip \
* --typeroot=/var/www/moodle/htdocs/blocks
* --name=loancalc
* --md5=...
* --dataroot=/var/www/moodle/data
*
* When called via HTTP, additional parameters returnurl, passfile and password must be
* provided. Optional proxy configuration can be passed using parameters proxy, proxytype
@ -60,6 +58,13 @@ if (defined('MOODLE_INTERNAL')) {
die('This is a standalone utility that should not be included by any other Moodle code.');
}
// This stops immediately at the beginning of lib/setup.php.
define('ABORT_AFTER_CONFIG', true);
if (PHP_SAPI === 'cli') {
// Called from the CLI - we need to set CLI_SCRIPT to ensure that appropriate CLI checks are made in setup.php.
define('CLI_SCRIPT', true);
}
require(__DIR__ . '/config.php');
// Exceptions //////////////////////////////////////////////////////////////////
@ -72,6 +77,7 @@ class backup_folder_exception extends Exception {}
class zip_exception extends Exception {}
class filesystem_exception extends Exception {}
class checksum_exception extends Exception {}
class invalid_setting_exception extends Exception {}
// Various support classes /////////////////////////////////////////////////////
@ -201,7 +207,6 @@ class input_manager extends singleton_pattern {
array('', 'proxytype', input_manager::TYPE_RAW, 'Proxy type (HTTP or SOCKS5)'),
array('', 'proxyuserpwd', input_manager::TYPE_RAW, 'Proxy username and password (e.g. \'username:password\')'),
array('', 'returnurl', input_manager::TYPE_URL, 'Return URL (HTTP access only)'),
array('d', 'dataroot', input_manager::TYPE_PATH, 'Full path to the dataroot (moodledata) directory'),
array('h', 'help', input_manager::TYPE_FLAG, 'Prints usage information'),
array('i', 'install', input_manager::TYPE_FLAG, 'Installation mode'),
array('m', 'md5', input_manager::TYPE_MD5, 'Expected MD5 hash of the ZIP package to deploy'),
@ -729,6 +734,11 @@ class worker extends singleton_pattern {
/** @var string the full path to the log file */
private $logfile = null;
/** @var array the whitelisted config options which can be queried. */
private $validconfigoptions = array(
'dataroot' => true,
);
/**
* Main - the one that actually does something
*/
@ -909,17 +919,15 @@ class worker extends singleton_pattern {
* @throws unauthorized_access_exception
*/
protected function authorize() {
if (PHP_SAPI === 'cli') {
$this->log('Successfully authorized using the CLI SAPI');
return;
}
$dataroot = $this->input->get_option('dataroot');
$passfile = $this->input->get_option('passfile');
$password = $this->input->get_option('password');
$passpath = $dataroot.'/mdeploy/auth/'.$passfile;
$passpath = $this->get_env('dataroot') . '/mdeploy/auth/' . $passfile;
if (!is_readable($passpath)) {
throw new unauthorized_access_exception('Unable to read the passphrase file.');
@ -959,12 +967,11 @@ class worker extends singleton_pattern {
* @return string
*/
protected function log_location() {
if (!is_null($this->logfile)) {
return $this->logfile;
}
$dataroot = $this->input->get_option('dataroot', '');
$dataroot = $this->get_env('dataroot');
if (empty($dataroot)) {
$this->logfile = false;
@ -988,8 +995,7 @@ class worker extends singleton_pattern {
* @return string
*/
protected function target_location($source) {
$dataroot = $this->input->get_option('dataroot');
$dataroot = $this->get_env('dataroot');
$pool = $dataroot.'/mdeploy/var';
if (!is_dir($pool)) {
@ -1013,8 +1019,7 @@ class worker extends singleton_pattern {
* @return string
*/
protected function backup_location($path) {
$dataroot = $this->input->get_option('dataroot');
$dataroot = $this->get_env('dataroot');
$pool = $dataroot.'/mdeploy/archive';
if (!is_dir($pool)) {
@ -1129,12 +1134,32 @@ class worker extends singleton_pattern {
return true;
}
/**
* Fetch environment settings.
*
* @param string $key The key to fetch
* @return mixed The value of the key if found.
* @throws invalid_setting_exception if the option is not set, or is invalid.
*/
protected function get_env($key) {
global $CFG;
if (array_key_exists($key, $this->validconfigoptions)) {
if (isset($CFG->$key)) {
return $CFG->$key;
}
throw new invalid_setting_exception("Requested environment setting '{$key}' is not currently set.");
} else {
throw new invalid_setting_exception("Requested environment setting '{$key}' is invalid.");
}
}
/**
* Get the location of ca certificates.
* @return string absolute file path or empty if default used
*/
protected function get_cacert() {
$dataroot = $this->input->get_option('dataroot');
$dataroot = $this->get_env('dataroot');
// Bundle in dataroot always wins.
if (is_readable($dataroot.'/moodleorgca.crt')) {

View File

@ -113,6 +113,10 @@ class testable_worker extends worker {
public function create_directory_precheck($path) {
return parent::create_directory_precheck($path);
}
public function get_env($key) {
return parent::get_env($key);
}
}
@ -300,4 +304,66 @@ class mdeploytest extends PHPUnit_Framework_TestCase {
$this->assertTrue($worker->create_directory_precheck($root));
$this->assertFalse(file_exists($root)); // The precheck is supposed to remove it again.
}
/**
* Test that an invalid setting throws an exception.
*
* @dataProvider get_env_unlisted_provider
* @expectedException invalid_setting_exception
* @expectedExceptionMessageRegExp /^Requested environment setting '[^']*' is invalid.$/
*/
public function test_get_env_unlisted($key) {
$worker = testable_worker::instance();
$worker->get_env($key);
}
public function get_env_unlisted_provider() {
return array(
// Completely invalid environment variables.
array('example'),
array('invalid'),
// Valid ones which have not been whitelisted.
array('noemailever'),
array('dbname'),
);
}
/**
* Test that a valid, but unset setting throws an exception.
*
* @dataProvider get_env_valid_provider
* @expectedException invalid_setting_exception
* @expectedExceptionMessageRegExp /^Requested environment setting '[^']*' is not current set.$/
*/
public function test_get_env_unset($key) {
// Ensure that the setting is currently unset.
global $CFG;
$CFG->$key = null;
$worker = testable_worker::instance();
$worker->get_env($key);
}
/**
* Test that a valid setting with data returns that data.
*
* @dataProvider get_env_valid_provider
*/
public function test_get_env_valid($key) {
// Ensure that the setting is currently unset.
global $CFG;
$CFG->$key = rand(0, 1000);
$worker = testable_worker::instance();
$value = $worker->get_env($key);
$this->assertEquals($CFG->$key, $value);
}
public function get_env_valid_provider() {
return array(
array('dataroot'),
);
}
}