Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logs to upgrade flow to make it easier to triage errors #6589

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/ChurchCRM/FileSystemUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,22 @@ public static function deleteFiles($path, $extArray)
}
}

public static function moveDir($src, $dest)
public static function moveDir(string $src, string $dest)
{
$logger = LoggerUtils::getAppLogger();

if (!is_dir($src)) {
$msg = 'provided src path is not a directory: '. $src;
$logger->error($msg);
throw new \Exception($msg);
}
if (!is_dir($dest)) {
$msg = 'provided dest path is not a directory: '. $dest;
$logger->error($msg);
throw new \Exception($msg);
}

$logger->info('Moving files: '. $src . ' to ' . $dest);
$files = array_diff(scandir($src), ['.', '..']);
foreach ($files as $file) {
if (is_dir("$src/$file")) {
Expand Down
55 changes: 28 additions & 27 deletions src/ChurchCRM/dto/ChurchCRMReleaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public static function getReleaseFromString(string $releaseString): ChurchCRMRel
}
else {
LoggerUtils::getAppLogger()->debug("Attempting to service query for release string " . $releaseString . " from GitHub release cache");
$requestedRelease = array_values(array_filter($_SESSION['ChurchCRMReleases'],fn($r) => $r->__toString() == $releaseString));
if (count($requestedRelease) == 1 && $requestedRelease[0] instanceof ChurchCRMRelease){
$requestedRelease = array_values(array_filter($_SESSION['ChurchCRMReleases'],fn($r) => $r->__toString() === $releaseString));
if (count($requestedRelease) === 1 && $requestedRelease[0] instanceof ChurchCRMRelease){
// this should be the case 99% of the time - the current version of the software has exactly one release on the GitHub account
LoggerUtils::getAppLogger()->debug("Query for release string " . $releaseString . " serviced from GitHub release cache");
return $requestedRelease[0];
}
elseif (count($requestedRelease) == 0) {
elseif (count($requestedRelease) === 0) {
// this will generally happen on dev or demo site instances
// where the currently running software has not yet been released / tagged on GitHun
LoggerUtils::getAppLogger()->debug("Query for release string " . $releaseString . " did not match any GitHub releases. Providing skeleton release object");
Expand Down Expand Up @@ -115,7 +115,7 @@ private static function getHighestReleaseInArray (array $eligibleUpgradeTargetRe

private static function getReleaseNextPatch(array $rs, ChurchCRMRelease $currentRelease) {
$eligibleUpgradeTargetReleases = array_values(array_filter($rs , function(ChurchCRMRelease $r) use ($currentRelease) {
$isSameMajorAndMinorWithGreaterPatch = ($r->MAJOR == $currentRelease->MAJOR) && ($r->MINOR == $currentRelease->MINOR) && ($r->PATCH > $currentRelease->PATCH);
$isSameMajorAndMinorWithGreaterPatch = ($r->MAJOR === $currentRelease->MAJOR) && ($r->MINOR === $currentRelease->MINOR) && ($r->PATCH > $currentRelease->PATCH);
LoggerUtils::getAppLogger()->debug("Release " . $r . " is" . ($isSameMajorAndMinorWithGreaterPatch ? " ":" not ") . "a possible patch upgrade target");
return $isSameMajorAndMinorWithGreaterPatch;
}));
Expand All @@ -124,7 +124,7 @@ private static function getReleaseNextPatch(array $rs, ChurchCRMRelease $current

private static function getReleaseNextMinor(array $rs, ChurchCRMRelease $currentRelease) {
$eligibleUpgradeTargetReleases = array_values(array_filter($rs , function(ChurchCRMRelease $r) use ($currentRelease) {
$isSameMajorAndMinorWithGreaterPatch = ($r->MAJOR == $currentRelease->MAJOR) && ($r->MINOR > $currentRelease->MINOR);
$isSameMajorAndMinorWithGreaterPatch = ($r->MAJOR === $currentRelease->MAJOR) && ($r->MINOR > $currentRelease->MINOR);
LoggerUtils::getAppLogger()->debug("Release " . $r . " is" . ($isSameMajorAndMinorWithGreaterPatch ? " ":" not ") . "a possible minor upgrade target");
return $isSameMajorAndMinorWithGreaterPatch;
}));
Expand Down Expand Up @@ -152,21 +152,16 @@ public static function getNextReleaseStep(ChurchCRMRelease $currentRelease) : Ch
// We should use the newest patch.
LoggerUtils::getAppLogger()->debug("Evaluating next-step release eligibility based on " . count($_SESSION['ChurchCRMReleases']) . " available releases ");

$nextStepRelease = self::getReleaseNextPatch($rs,$currentRelease);
$nextStepRelease = self::getReleaseNextPatch($rs, $currentRelease) ??
self::getReleaseNextMinor($rs, $currentRelease) ??
self::getReleaseNextMajor($rs, $currentRelease);

if (null == $nextStepRelease) {
$nextStepRelease = self::getReleaseNextMinor($rs,$currentRelease);
}

if (null == $nextStepRelease) {
$nextStepRelease = self::getReleaseNextMajor($rs,$currentRelease);
}

if (null == $nextStepRelease) {
if (null === $nextStepRelease) {
throw new \Exception("Could not identify a suitable upgrade target release. Current software version: " . $currentRelease . ". Highest available release: " . $rs[0] ) ;
}

LoggerUtils::getAppLogger()->info("Next upgrade step for " . $currentRelease. " is : " . $nextStepRelease);

return $nextStepRelease;
}

Expand Down Expand Up @@ -231,35 +226,41 @@ public static function doUpgrade($zipFilename, $sha1)
register_shutdown_function(fn() => ChurchCRMReleaseManager::preShutdown());

$logger = LoggerUtils::getAppLogger();
$logger->info("Beginnging upgrade process");
$logger->info("Beginning upgrade process");
$logger->info("PHP max_execution_time is now: " . ini_get("max_execution_time"));
$logger->info("Beginning hash validation on " . $zipFilename);
if ($sha1 == sha1_file($zipFilename)) {
if ($sha1 === sha1_file($zipFilename)) {
$logger->info("Hash validation succeeded on " . $zipFilename . " Got: " . sha1_file($zipFilename));

$zip = new \ZipArchive();
if ($zip->open($zipFilename) == true) {
$logger->info("Extracting " . $zipFilename." to: " . SystemURLs::getDocumentRoot() . '/Upgrade');
$executionTime = new ExecutionTime();
$zip->extractTo(SystemURLs::getDocumentRoot() . '/Upgrade');
$zip->close();
$logger->info("Extraction completed. Took:" . $executionTime->getMilliseconds());
$logger->info("Moving extracted zip into place");
$executionTime = new ExecutionTime();
FileSystemUtils::moveDir(SystemURLs::getDocumentRoot() . '/Upgrade/churchcrm', SystemURLs::getDocumentRoot());
$logger->info("Move completed. Took:" . $executionTime->getMilliseconds());
if ($zip->open($zipFilename) === true) {
$logger->info("Extracting " . $zipFilename." to: " . SystemURLs::getDocumentRoot() . '/Upgrade');
$executionTime = new ExecutionTime();
$isSuccessful = $zip->extractTo(SystemURLs::getDocumentRoot() . '/Upgrade');
MiscUtils::throwIfFailed($isSuccessful);
$zip->close();
$logger->info("Extraction completed. Took:" . $executionTime->getMilliseconds());
$logger->info("Moving extracted zip into place");
$executionTime = new ExecutionTime();

FileSystemUtils::moveDir(SystemURLs::getDocumentRoot() . '/Upgrade/churchcrm', SystemURLs::getDocumentRoot());
$logger->info("Move completed. Took:" . $executionTime->getMilliseconds());
}
$logger->info("Deleting zip archive: ".$zipFilename);
unlink($zipFilename);

SystemConfig::setValue('sLastIntegrityCheckTimeStamp', null);
$logger->debug("Set sLastIntegrityCheckTimeStamp to null");
$logger->info("Upgrade process complete");
ini_set('display_errors',$displayErrors);
self::$isUpgradeInProgress = false;

return 'success';
} else {
self::$isUpgradeInProgress = false;
ini_set('display_errors',$displayErrors);
$logger->error("Hash validation failed on " . $zipFilename.". Expected: ".$sha1. ". Got: ".sha1_file($zipFilename));

return 'hash validation failure';
}
}
Expand Down