/** * @param \Cogeco\Build\Entity\Database $db * @param \Cogeco\Build\Entity\File $dumpFile * @param $dbName * @param array $tables * @throws \Cogeco\Build\Exception */ public static function mysqlDump(Database $db, $dbName, array $tables = array(), File $dumpFile) { if (!is_array($tables) || count($tables) === 0) { $tablesParam = ''; } else { $tablesParam = explode(' ', $tables) . ' '; } // Presentation self::log("- Getting dump of {$dbName}"); if ($db->isRemote()) { Task::log(" from {$db->host->hostname}"); } self::log("\n"); // For use when converting a dump to SQLite // --compatible=ansi --skip-extended-insert --compact // Build the command $cmd = Config::get('mysqldump.bin') . " -vf -P {$db->port}"; if ($db->isRemote()) { $cmd .= " -h {$db->host->hostname}"; } // Create the folder if it doesn't exist already FileSystemTask::mkdir($dumpFile->dir->getPath()); $cmd .= " -u {$db->account->username} -p{$db->account->password} {$dbName} {$tablesParam}| sed '/^\\/\\*\\!50013 DEFINER/d' > {$dumpFile->getPath()}"; //$cmd .= " --result-file={$dumpFile->getPath()} -u {$db->account->username} -p{$db->account->password} {$dbName} {$tablesParam} 2>&1"; self::runCmd($cmd); self::log("\n"); }
/** * Performs operations meant to run at the end of a script */ public static function endOfScriptMaintenance() { if (Config::get('output.script.duration')) { Task::outputElapsedTime(); } if (!empty(self::$logFileHandle)) { fclose(self::$logFileHandle); } }
*/ use Cogeco\Build\Task; use Cogeco\Build\Task\CliTask; use Cogeco\Build\MyAccount\MyAccountTask; // Include the core of the build scripts and properties include_once __DIR__ . '/../../source/php/bootstrap.php'; // ********** // Parse script args - Use the build ID to identify many builds in the same script $buildId = 'uat'; if (isset($argv[1])) { if ($argv[1] === 'prod') { $buildId = 'prod'; } else { if ($argv[1] === 'uat') { $buildId = 'uat'; } else { if (strtolower($argv[1]) === '-help') { exit("Usage: {$argv[1]} [prod|uat]\n\n"); } } } } // ******************************* // Initial output Task::log("\n---------------------------------------\n"); Task::log("-------- Getting revision for My Account {$buildId}\n\n"); // *** // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); Task::log('Revision: ' . MyAccountTask::getRemoteRevision($buildId === 'prod' ? $maoProdDir : $maoUatDir) . "\n\n");
// Start the build script // ******************************* Config::enableLogging(); // ******************** // *** Initial output // Task::log("\n---------------------------------------\n"); Task::log("-------- Sync repository\n\n"); // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); // ******************** // Prompt user input // // Prompt user to input a local folder to sync $workingCopy = CliTask::promptRepo(); Task::log("You chose " . $workingCopy->getRepoUrl() . "\n\n"); // Prompt user to chose a remote folder to sync to $selectedDir = CliTask::promptDir(array($devDir, $dev2Dir, $uatDir, $uat2Dir, $uat3Dir, $preprodDir)); Task::log("You chose " . $selectedDir->getPath() . "\n\n"); CliTask::promptQuit('Continue? [y/n]: '); // ******************** // *** Checkout // SvnTask::checkoutClean($workingCopy); SvnTask::createManifestFile($workingCopy, FALSE, TRUE); // ******************** // Sync files and folders // $rsyncOptions = new RsyncOptions($workingCopy->dir, $selectedDir); $rsyncOptions->chmod('Du=rwx,Dg=rwx,Do=rwx,Fu=rw,Fg=rw,Fo=r')->excludesAppend(array('/dev/', '/db/')); FileSyncTask::sync($rsyncOptions);
// ******************************* Config::enableLogging(); // ******************** // *** Initial output // Task::log("\n---------------------------------------\n"); Task::log("-------- Sync database\n\n"); // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); // ******************** // Prompt user input // // Prepare a list of possible databases to sync from $dbList = array($localPublicDb, $localPublicUBCDb, $localPublicPromo, $localPublicGBIncrease, $localDevPhoenixDb, $devPublicDb, $dev2PublicDb, $dev3PublicDb, $dev4PublicDb, $uatPublicDb, $preprodPublicDb); // Prompt user to input a source database $sourceDb = CliTask::promptDatabase($dbList, 'Choose a source database:'); Task::log("You chose " . $sourceDb->getDbName() . " on " . $sourceDb->getHost()->getHostname() . "\n\n"); if (($key = array_search($sourceDb, $dbList)) !== false) { unset($dbList[$key]); } // Prepare a list of possible databases to sync to $destinationDb = CliTask::promptDatabase($dbList, 'Choose a destination database:'); Task::log("You chose " . $destinationDb->getDbName() . " on " . $destinationDb->getHost()->getHostname() . "\n\n"); CliTask::promptQuit('Continue? [y/n]: '); // Dump DB to an sql script $tmpDumpFile = new File(SCRIPT_DB_DIR, $sourceDb->getHost()->getHostname() . '-' . $sourceDb->getDbName() . '-' . Config::get('datetime.slug') . '.sql'); MysqlTask::mysqlDump($sourceDb, $sourceDb->getDbName(), array(), $tmpDumpFile); // Import DB from the sql script MysqlTask::importDump($destinationDb, $tmpDumpFile->getPath(), $destinationDb->getDbName()); // Delete the temporary script //unlink($tmpDumpFile->getPath());
$changesText = ViewTask::getLogEntriesText($logEntries); $changesHtml = ViewTask::getLogEntriesHtml($logEntries); // Load the email templates with the changes $emailText = ViewTask::load('views/template-release-text.php', array('changes' => $changesText), TRUE); $emailHtml = ViewTask::load('views/template-release-html.php', array('changes' => $changesHtml), TRUE); // Create the emails as files $emailFileBaseName = "release-prod-r{$workingCopy->info->commitRevision}"; EmailTask::createEmailFiles($emailFileBaseName, $emailHtml, $emailText); // ******************** // *** Deploy intermediate // // Sync files from the working copy to the temporary prod directory FileSyncTask::sync($rsyncOptions1); // Task::log("- Syncing intermediate directory {$syncDestinationDir1->path} with {$syncDestinationDir2->path}\n\n"); // $rsyncCommand = FileSyncTask::getRsyncCommand($rsyncOptions2); SshTask::exec($syncDestinationDir2->getHost(), $rsyncCommand); Task::log("\n"); // ******************** // *** Tag release // if ($build === 'prod') { SvnTask::tagRelease($maoTag, $workingCopy->dir->path, "Release to production - Revision: {$workingCopy->info->commitRevision}"); } // ******************** // *** Notify // $maoReleaseEmail->bodyHtml = $emailHtml; $maoReleaseEmail->bodyText = $emailText; EmailTask::sendEmail($emailConnector, $maoReleaseEmail);
// ******************************* if (!isset($argv[1]) || $argv[1] !== 'dev' && $argv[1] !== 'uat' && $argv[1] !== 'prod') { echo "Usage: php " . $argv[0] . " dev|uat|prod\n"; exit(1); } // ******************************* // Script configuration // ******************************* $host = $maoDevHost; if ($argv[1] == 'uat') { $host = $maoUatHost; } else { if ($argv[1] == 'prod') { $host = $maoProdHost; } } // ******************************* // Start the build script // ******************************* // *** // Initial output Task::log("\n---------------------------------------\n"); Task::log("-------- Clear memcached on " . $host->getHostname() . " ({$argv[1]})\n\n"); // *** // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); // *** $command = "php /var/www/scripts/memcached-clear.php"; SshTask::exec($host, $command); Task::log("- Clearing memcached completed\n");
// Task::log("- Syncing revision {$workingCopy->info->commitRevision} with {$syncDestinationDir1->getPath()} on {$syncDestinationDir1->getHost()->hostname}\n\n"); // FileSyncTask::sync($rsyncOptions1); // ******************** // *** Deploy // TimerTask::start(); // Import DB MysqlTask::importDump($destinationDb, $tmpDumpFile->getPath(), $destinationDbSchema); // Task::log("- Syncing intermediate directory {$syncDestinationDir1->path} with {$syncDestinationDir2->path}\n\n"); // $rsyncCommand = FileSyncTask::getRsyncCommand($rsyncOptions2); SshTask::exec($syncDestinationDir1->getHost(), $rsyncCommand); // Stop the deploy timer and log it TimerTask::stop(); Task::log("Deploy time: " . TimerTask::getLastElapsedTime() . "\n\n"); exit; // ******************** // *** Tag // if ($build === 'prod') { SvnTask::commit($workingCopy, "Release to Production"); } // ******************** // *** Notify // $cogecoCaReleaseEmail->bodyHtml = ViewTask::load('views/template-release-html.php', array('changes' => ''), TRUE); $cogecoCaReleaseEmail->bodyText = ViewTask::load('views/template-release-text.php', array('changes' => ''), TRUE); EmailTask::sendEmail($emailConnector, $cogecoCaReleaseEmail);
use Cogeco\Build\Task\FileSyncTask; use Cogeco\Build\Task\SshTask; use Cogeco\Build\Task\SvnTask; use Cogeco\Build\Task\ViewTask; // Include the build script core include_once __DIR__ . '/../../bootstrap.php'; // ******************************* // Script configuration // ******************************* // ******************************* // Start the build script // ******************************* // ******************** // *** Prompt for user input // $workingCopy = CliTask::PromptWorkingCopy(); Task::log("Checking out " . $workingCopy->id . "\n\n"); // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); // ******************** // *** SVN checkout // SvnTask::checkoutClean($workingCopy, 0); // Create the supplemental files for non tag working copies if (stripos($workingCopy->id, 'tag') === FALSE) { SvnTask::createManifestFile($workingCopy); // Duplicate the .htaccess file as a backup for when switching to maintenance mode Task::log("- Creating a duplicate of the root .htaccess\n"); copy($workingCopy->dir->path . '.htaccess', $workingCopy->dir->path . '.htaccess-default'); Task::log("Created file {$workingCopy->dir->path}.htaccess-default\n\n"); }
$logEntries = SvnTask::getLogEntries($workingCopy, $lastTagRevision + 1, $workingCopy->info->commitRevision); // Convert log entries into text and html $changesText = ViewTask::getLogEntriesText($logEntries); $changesHtml = ViewTask::getLogEntriesHtml($logEntries); // Load the email templates with the changes $emailText = ViewTask::load('views/template-release-text.php', array('changes' => $changesText), TRUE); $emailHtml = ViewTask::load('views/template-release-html.php', array('changes' => $changesHtml), TRUE); // Create the emails as files $emailFileBaseName = "release-prod-r{$workingCopy->info->commitRevision}"; EmailTask::createEmailFiles($emailFileBaseName, $emailHtml, $emailText); // ******************** // *** Tag release // if ($build === 'prod') { SvnTask::tagRelease($maoTag, $workingCopy->dir->path, "Release to production - Revision: {$workingCopy->info->commitRevision}\n"); } // ******************** // *** Notify // $maoReleaseEmail->bodyHtml = $emailHtml; $maoReleaseEmail->bodyText = $emailText; try { EmailTask::sendEmail($emailConnector, $maoReleaseEmail); } catch (\Exception $e) { Task::log($e->getMessage()); } // ******************** // *** Deactivate maintenance mode // $command = "php /var/www/scripts/maintenance-mode.php off prod"; SshTask::exec($remoteHost, $command);
exit(1); } // ******************************* // Script configuration // ******************************* $enableMaintenanceMode = false; $envName = $argv[2]; $remoteDir = $maoDev1Dir; $action = 'Deactivate'; if ($argv[1] == 'on') { $enableMaintenanceMode = true; $action = 'Activate'; } if ($argv[2] == 'prod') { $remoteDir = $maoProdDir; } $remoteHost = $remoteDir->getHost(); // ******************************* // Start the build script // ******************************* // *** // Initial output Task::log("\n---------------------------------------\n"); Task::log("-------- {$action} maintenance mode\n\n"); // *** // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); // *** //$command = "php /var/www/scripts/maintenance-mode.php " . $argv[1]; $command = "php /var/www/scripts/maintenance-mode.php " . $argv[1] . ' ' . $envName; SshTask::exec($remoteHost, $command);
Config::enableLogging(TRUE, TRUE); // ******************** // *** Initial output // Task::log("\n---------------------------------------\n"); Task::log("-------- Deploy Preprod\n\n"); // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); // ******************** // *** Checkout // SvnTask::checkoutClean($workingCopy, $revisionToCheckOut, 0); SvnTask::createManifestFile($workingCopy, FALSE, TRUE); // ******************** // *** Dump DB // if ($deployDatabase) { Task::log("\n- Deploy schema {$sourceDbSchema} from {$sourceDb->host->hostname} "); Task::log("to {$destinationDbSchema} on {$destinationDb->getHost()->hostname}\n\n"); // Export DB $mysqlDumpFile = new File($workingCopy->getDir()->getPath() . '/db/dump', 'cogeco.sql'); MysqlTask::mysqlDump($sourceDb, $sourceDbSchema, array(), $mysqlDumpFile); // Import DB MysqlTask::importDump($destinationDb, $mysqlDumpFile->getPath(), $destinationDbSchema); } // ******************** // *** Prep sync to intermediate remote location // Task::log("- Syncing revision {$workingCopy->info->commitRevision} with {$syncDestinationDir1->getPath()} on {$syncDestinationDir1->getHost()->hostname}\n\n"); // FileSyncTask::sync($rsyncOptions1);
/** * Send an email using PHPMailer * @param \Cogeco\Build\Entity\Email\Connector $connector * @param \Cogeco\Build\Entity\Email $emailData * @throws \Cogeco\Build\Exception */ public static function sendEmail(Connector $connector, Email $emailData) { $multipleRecipients = count($emailData->to) > 1; Task::log("- Sending email {$emailData->subject}\n"); $mail = new \PHPMailer(); if ($connector instanceof SmtpConnector) { //Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 0; //Ask for HTML-friendly debug output $mail->Debugoutput = 'html'; //Set the hostname of the mail server $mail->Host = $connector->host; //Set the SMTP port number - likely to be 25, 465 or 587 $mail->Port = $connector->port; //Whether to use SMTP authentication $mail->SMTPAuth = false; // SMTP auth is currently unsupported // if ($connector->auth) { } } else { throw new Exception("EmailTask error: Unsupported email connector"); } // Set the FRom address and name $mail->setFrom($emailData->fromAddress, $emailData->fromName); //Set a reply-to address, if there is one if (!empty($emailData->replyAddress)) { $mail->addReplyTo($emailData->replyAddress, $emailData->replyName); } foreach ($emailData->to as $i => $toAddress) { $mail->addAddress($toAddress); } // Set the subject, HTML body and text $mail->Subject = $emailData->subject; $mail->msgHTML($emailData->bodyHtml); $mail->AltBody = $emailData->bodyText; // Set attachments if (!empty($emailData->attachments)) { foreach ($emailData->attachments as $i => $attachment) { $mail->addAttachment($attachment); } } $mail->CharSet = $emailData->encoding; // Send the message, check for errors if ($mail->send()) { Task::log("Email notification" . ($multipleRecipients ? 's' : '') . " sent\n\n"); } else { throw new Exception("Mailer Error: " . $mail->ErrorInfo); } }
use Cogeco\Build\Task\SvnTask; // Include the build script core include_once __DIR__ . '/../../bootstrap.php'; // ******************************* // Script configuration // ******************************* // ******************************* // Start the build script // ******************************* Config::enableLogging(); // ******************** // *** Initial output // Task::log("\n---------------------------------------\n"); Task::log("-------- Dump database\n\n"); // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); // ******************** // Prompt user input // // Prepare a list of possible databases to dump $dbList = array($localPublicDb, $localPublicUBCDb, $localPublicPromo, $localPublicGBIncrease, $devPublicDb, $dev2PublicDb, $dev3PublicDb, $dev4PublicDb, $uatPublicDb, $preprodPublicDb, $prodPublicDb); // Prompt user to input a source database $sourceDb = CliTask::promptDatabase($dbList, 'Choose a database:'); Task::log("You chose " . $sourceDb->getDbName() . " on " . $sourceDb->getHost()->getHostname() . "\n\n"); if (($key = array_search($sourceDb, $dbList)) !== false) { unset($dbList[$key]); } // Dump DB to an sql script $tmpDumpFile = new File(SCRIPT_DB_DIR, $sourceDb->getHost()->getHostname() . '-' . $sourceDb->getDbName() . '-' . Config::get('datetime.slug') . '.sql'); MysqlTask::mysqlDump($sourceDb, $sourceDb->getDbName(), array(), $tmpDumpFile);
$destDirPathPrefixCygwin = 'S:'; $sharedDestinationDir = $destDirPathPrefix . $destinationDirPath; $localDestinationDir = $destDirPathPrefixCygwin . $destinationDirPath; $remoteFileNew = new File($remoteFileOld->dir, 'orders_' . Config::get('datetime.slug') . '.log'); $destinationFile = new File($localDestinationDir, $remoteFileNew->name); $rsyncOptions = new RsyncOptions($remoteFileNew, $destinationFile); $rsyncOptions->chmod('u=rw,g=rw,o='); // ******************************* // Start the build script // ******************************* Config::enableLogging(); // ******************** // *** Initial output // Task::log("\n---------------------------------------\n"); Task::log("- Move cogeco.ca orders.log to secured shared drive\n\n"); // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); // ******************** // *** Move the file to the shared drive // // Firstly, move it on the remote server SshTask::moveFile($remoteFileOld, $remoteFileNew->getPath()); //SshTask::copyFile($remoteFileOld, $remoteFileNew->getPath()); // Sync from the remote server to the shared drive FileSyncTask::sync($rsyncOptions); // Verify that the file was created on the shared drive and then delete it on the remote server if it exists if (!is_file($destinationFile->getPath())) { throw new \Exception('File was not successfully copied to the shared drive!'); } //If the file was successfully transferred, delete it on the remote host
// ******************************* // Script arguments // ******************************* if (!isset($argv[1]) || $argv[1] !== 'dev' && $argv[1] !== 'uat' && $argv[1] !== 'prod') { echo "Usage: php " . $argv[0] . " dev|uat|prod\n"; exit(1); } // ******************************* // Script configuration // ******************************* $host = $maoDevHost; if ($argv[1] == 'uat') { $host = $maoUatHost; } else { if ($argv[1] == 'prod') { $host = $maoProdHost; } } // ******************************* // Start the build script // ******************************* // *** // Initial output Task::log("\n---------------------------------------\n"); Task::log("-------- Getting memcached stats from " . $host->getHostname() . " ({$argv[1]})\n\n"); // *** // Prompt for user password, if it's not hardcoded CliTask::promptAccountPassword($cogecoAccount); // *** $command = "php /var/www/scripts/memcached-stats.php"; SshTask::exec($host, $command);