Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-35238 Add simple logging of mdeploy.php execution

  • Loading branch information...
commit ec8e1cbce67d5976efae1cdf2de8f99848be6811 1 parent 5bd9b0a
@mudrd8mz mudrd8mz authored
Showing with 80 additions and 4 deletions.
  1. +80 −4 mdeploy.php
View
84 mdeploy.php
@@ -639,11 +639,16 @@ class worker extends singleton_pattern {
/** @var array|false the most recent cURL request info, if it was successful */
private $curlinfo = null;
+ /** @var string the full path to the log file */
+ private $logfile = null;
+
/**
* Main - the one that actually does something
*/
public function execute() {
+ $this->log('=== MDEPLOY EXECUTION START ===');
+
// Authorize access. None in CLI. Passphrase in HTTP.
$this->authorize();
@@ -654,16 +659,19 @@ public function execute() {
}
if ($this->input->get_option('upgrade')) {
+ $this->log('Plugin upgrade requested');
+
// Fetch the ZIP file into a temporary location.
$source = $this->input->get_option('package');
$target = $this->target_location($source);
+ $this->log('Downloading package '.$source);
if ($this->download_file($source, $target)) {
- $this->log('ZIP fetched into '.$target);
+ $this->log('Package downloaded into '.$target);
} else {
$this->log('cURL error ' . $this->curlerrno . ' ' . $this->curlerror);
$this->log('Unable to download the file');
- throw new download_file_exception('Unable to download the ZIP package');
+ throw new download_file_exception('Unable to download the package');
}
// Compare MD5 checksum of the ZIP file
@@ -674,6 +682,7 @@ public function execute() {
$this->log('MD5 checksum failed. Expected: '.$md5remote.' Got: '.$md5local);
throw new checksum_exception('MD5 checksum failed');
}
+ $this->log('MD5 checksum ok');
// Backup the current version of the plugin
$plugintyperoot = $this->input->get_option('typeroot');
@@ -681,6 +690,9 @@ public function execute() {
$sourcelocation = $plugintyperoot.'/'.$pluginname;
$backuplocation = $this->backup_location($sourcelocation);
+ $this->log('Current plugin code location: '.$sourcelocation);
+ $this->log('Moving the current code into archive: '.$backuplocation);
+
// We don't want to touch files unless we are pretty sure it would be all ok.
if (!$this->move_directory_source_precheck($sourcelocation)) {
throw new backup_folder_exception('Unable to backup the current version of the plugin (source precheck failed)');
@@ -696,6 +708,7 @@ public function execute() {
// Unzip the plugin package file into the target location.
$this->unzip_plugin($target, $plugintyperoot, $sourcelocation, $backuplocation);
+ $this->log('Package successfully extracted');
// Redirect to the given URL (in HTTP) or exit (in CLI).
$this->done();
@@ -748,6 +761,7 @@ protected function done($exitcode = self::EXIT_OK) {
protected function authorize() {
if (PHP_SAPI === 'cli') {
+ $this->log('Successfully authorized using the CLI SAPI');
return;
}
@@ -785,6 +799,36 @@ protected function authorize() {
if ($password !== $stored[0]) {
throw new unauthorized_access_exception('Session passphrase does not match the stored one.');
}
+
+ $this->log('Successfully authorized using the passphrase file');
+ }
+
+ /**
+ * Returns the full path to the log file.
+ *
+ * @return string
+ */
+ protected function log_location() {
+
+ if (!is_null($this->logfile)) {
+ return $this->logfile;
+ }
+
+ $dataroot = $this->input->get_option('dataroot', false);
+
+ if ($dataroot === false) {
+ $this->logfile = false;
+ return $this->logfile;
+ }
+
+ $myroot = $dataroot.'/mdeploy';
+
+ if (!is_dir($myroot)) {
+ mkdir($myroot, 02777, true);
+ }
+
+ $this->logfile = $myroot.'/mdeploy.log';
+ return $this->logfile;
}
/**
@@ -856,7 +900,7 @@ protected function download_file($source, $target) {
throw new download_file_exception('Unsupported transport protocol.');
}
if (!$ch = curl_init($source)) {
- // $this->log('Unable to init cURL.');
+ $this->log('Unable to init cURL.');
return false;
}
@@ -906,7 +950,39 @@ protected function download_file($source, $target) {
* @param string $message
*/
protected function log($message) {
- // TODO
+
+ $logpath = $this->log_location();
+
+ if (empty($logpath)) {
+ // no logging available
+ return;
+ }
+
+ $f = fopen($logpath, 'ab');
+
+ if ($f === false) {
+ throw new filesystem_exception('Unable to open the log file for appending');
+ }
+
+ $message = $this->format_log_message($message);
+
+ fwrite($f, $message);
+
+ fclose($f);
+ }
+
+ /**
+ * Prepares the log message for writing into the file
+ *
+ * @param string $msg
+ * @return string
+ */
+ protected function format_log_message($msg) {
+
+ $msg = trim($msg);
+ $timestamp = date("Y-m-d H:i:s");
+
+ return $timestamp . ' '. $msg . PHP_EOL;
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.