From f64664703620ac918c99cf158b7958c1e5125b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sun, 15 Dec 2013 23:44:28 -0200 Subject: [PATCH] Send mail notifications. --- Mage/Command/BuiltIn/DeployCommand.php | 13 +++- Mage/Console.php | 27 +++++++- Mage/Mailer.php | 92 ++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 Mage/Mailer.php diff --git a/Mage/Command/BuiltIn/DeployCommand.php b/Mage/Command/BuiltIn/DeployCommand.php index 9c94f36..8f1c10b 100644 --- a/Mage/Command/BuiltIn/DeployCommand.php +++ b/Mage/Command/BuiltIn/DeployCommand.php @@ -19,6 +19,7 @@ use Mage\Task\ErrorWithMessageException; use Mage\Task\SkipException; use Mage\Console; use Mage\Config; +use Mage\Mailer; use Exception; @@ -177,7 +178,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment Console::output('Total time: ' . $timeText . '.', 1, 2); // Send Notifications - $this->sendNotification(); + $this->sendNotification(self::$failedTasks > 0 ? false : true); // Unlock if (file_exists('.mage/~working.lock')) { @@ -514,8 +515,9 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment /** * Send Email Notification if enabled + * @param boolean $result */ - protected function sendNotification() + protected function sendNotification($result) { $projectName = $this->getConfig()->general('name', false); $projectEmail = $this->getConfig()->general('email', false); @@ -525,6 +527,13 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment if (!$projectName || !$projectEmail || !$notificationsEnabled) { return false; } + + $mailer = new Mailer; + $mailer->setAddress($projectEmail) + ->setProject($projectName) + ->setLogFile(Console::getLogFile()) + ->setEnvironment($this->getConfig()->getEnvironment()) + ->send($result); } } diff --git a/Mage/Console.php b/Mage/Console.php index 809c15c..ed6c56e 100644 --- a/Mage/Console.php +++ b/Mage/Console.php @@ -26,6 +26,12 @@ class Console */ private static $log = null; + /** + * The current logfile + * @var string + */ + private static $logFile = null; + /** * Enables or Disables Logging * @var boolean @@ -181,7 +187,8 @@ class Console { if (self::$logEnabled) { if (self::$log == null) { - self::$log = fopen('.mage/logs/log-' . date('Ymd-His') . '.log', 'w'); + self::$logFile = realpath('.mage/logs') . '/log-' . date('Ymd-His') . '.log'; + self::$log = fopen(self::$logFile, 'w'); } $message = date('Y-m-d H:i:s -- ') . $message; @@ -189,6 +196,24 @@ class Console } } + /** + * Return the screen buffer + * @return string + */ + public static function getOutput() + { + return self::$screenBuffer; + } + + /** + * Returns the Log File + * @return string + */ + public static function getLogFile() + { + return self::$logFile; + } + /** * Check Logs * @param \Mage\Config $config diff --git a/Mage/Mailer.php b/Mage/Mailer.php new file mode 100644 index 0000000..6fdcfe7 --- /dev/null +++ b/Mage/Mailer.php @@ -0,0 +1,92 @@ + +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ + +namespace Mage; + +use Mage\Console; + +/** + * Mailer Helper. + * + * @author Andrés Montañez + */ +class Mailer +{ + const EOL = "\r\n"; + const SUBJECT = '[Magallanes] Deployment of {project} to {environment}: {result}'; + + protected $address; + protected $project; + protected $environment; + protected $logFile; + + public function setAddress($address) + { + $this->address = $address; + return $this; + } + + public function setProject($project) + { + $this->project = $project; + return $this; + } + + public function setEnvironment($environment) + { + $this->environment = $environment; + return $this; + } + + public function setLogFile($logFile) + { + $this->logFile = $logFile; + return $this; + } + + public function send($result) + { + $boundary = md5(date('r', time())); + + $headers = 'From: ' . $this->address + . self::EOL + . 'Reply-To: ' . $this->address + . self::EOL + . 'MIME-Version: 1.0' + . self::EOL + . 'Content-Type: multipart/mixed; boundary=Mage-mixed-' . $boundary; + + $subject = str_replace( + array('{project}', '{environment}', '{result}'), + array($this->project, $this->environment, $result ? 'SUCCESS' : 'FAILURE'), + self::SUBJECT + ) + ; + $attachment = chunk_split(base64_encode(file_get_contents($this->logFile))); + + $message = 'This is a multi-part message in MIME format.' . self::EOL + . '--Mage-mixed-' . $boundary . self::EOL + . 'Content-Type: text/plain; charset=iso-8859-1' . self::EOL + . 'Content-Transfer-Encoding: quoted-printable' . self::EOL + . self::EOL + . strip_tags(Console::getOutput()) . self::EOL + . self::EOL + . '--Mage-mixed-' . $boundary . self::EOL + . 'Content-Type: text/plain; name="log.txt"' . self::EOL + . 'Content-Transfer-Encoding: base64' . self::EOL + . 'Content-Disposition: attachment' . self::EOL + . self::EOL + . $attachment . self::EOL + . '--Mage-mixed-' . $boundary . '--' . self::EOL + ; + + $mail_sent = @mail($this->address, $subject, $message, $headers); + } +} \ No newline at end of file