From 749c161aaa4f00e4287bce01345b0e815bbe783a Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Wed, 23 Sep 2020 19:31:52 +0300 Subject: [PATCH 1/3] Upgrade fixes --- .../Espo/Core/Console/Commands/Upgrade.php | 259 +++++++----------- 1 file changed, 105 insertions(+), 154 deletions(-) diff --git a/application/Espo/Core/Console/Commands/Upgrade.php b/application/Espo/Core/Console/Commands/Upgrade.php index c95300e5a4..861a74ac53 100644 --- a/application/Espo/Core/Console/Commands/Upgrade.php +++ b/application/Espo/Core/Console/Commands/Upgrade.php @@ -72,17 +72,77 @@ class Upgrade implements Command { $params = $this->normalizeParams($options, $flagList, $argumentList); - switch ($params['mode']) { - case 'local': - $this->runLocalUpgrade($params); + $versionInfo = $this->getVersionInfo(); + $fromVersion = $this->config->get('version'); + $nextVersion = $versionInfo->nextVersion ?? null; + $lastVersion = $infoData->lastVersion ?? null; - break; + $packageFile = $this->getPackageFile($params, $versionInfo); + if (!$packageFile) return; - default: - case 'remote': - $this->runRemoteUpgrade($params); + if ($params->localMode) { + $upgradeId = $this->upload($packageFile); + $manifest = $this->getUpgradeManager()->getManifestById($upgradeId); + $nextVersion = $manifest['version']; + } - break; + fwrite(\STDOUT, "Current version is {$fromVersion}.\n"); + + if (!$params->skipConfirmation) { + fwrite(\STDOUT, "EspoCRM will be upgraded to version {$nextVersion} now. Enter [Y] to continue.\n"); + + if (!$this->confirm()) { + echo "Upgrade canceled.\n"; + return; + } + } + + if (filter_var($packageFile, \FILTER_VALIDATE_URL)) { + fwrite(\STDOUT, "Downloading..."); + + $packageFile = $this->downloadFile($packageFile); + + fwrite(\STDOUT, "\n"); + + if (!$packageFile) { + fwrite(\STDOUT, "Error: Unable to download upgrade package.\n"); + return; + } + } + + $upgradeId = $upgradeId ?? $this->upload($packageFile); + + fwrite(\STDOUT, "Upgrading... This may take a while..."); + + try { + $this->runUpgradeProcess($upgradeId, $params); + } catch (\Exception $e) { + $errorMessage = $e->getMessage(); + } + + fwrite(\STDOUT, "\n"); + + if (!$params->keepPackageFile) { + $this->fileManager->unlink($packageFile); + } + + if (!empty($errorMessage)) { + fwrite(\STDOUT, $errorMessage . "\n"); + return; + } + + $currentVerison = $this->getCurrentVersion(); + + fwrite(\STDOUT, "Upgrade is complete. Current version is {$currentVerison}.\n"); + + if ($lastVersion && $lastVersion !== $currentVerison && $fromVersion !== $currentVerison) { + fwrite(\STDOUT, "Newer version is available. Run command again to upgrade.\n"); + return; + } + + if ($lastVersion && $lastVersion === $currentVerison) { + fwrite(\STDOUT, "You have the latest version.\n"); + return; } } @@ -95,180 +155,70 @@ class Upgrade implements Command * @param array $options * @param array $flagList * @param array $argumentList - * @return array + * @return object */ - protected function normalizeParams($options, $flagList, $argumentList) + protected function normalizeParams(array $options, array $flagList, array $argumentList) { - $params = [ - 'mode' => 'remote', + $params = (object) [ + 'localMode' => false, 'skipConfirmation' => false, 'singleProcess' => false, + 'keepPackageFile' => false, ]; if (!empty($options['file'])) { - $params['mode'] = 'local'; - $params['file'] = $options['file']; + $params->localMode = true; + $params->file = $options['file']; + $params->keepPackageFile = true; } if (in_array('y', $flagList)) { - $params['skipConfirmation'] = true; + $params->skipConfirmation = true; } if (in_array('s', $flagList)) { - $params['singleProcess'] = true; + $params->singleProcess = true; } if (!empty($options['step'])) { - $params['step'] = $options['step']; + $params->step = $options['step']; } return $params; } - protected function runLocalUpgrade(array $params) + protected function getPackageFile(object $params, object $versionInfo) { - if (empty($params['file']) || !file_exists($params['file'])) { - echo "Upgrade package is not found.\n"; - - return; - } - - $packageFile = $params['file']; - $fromVersion = $this->config->get('version'); - - fwrite(\STDOUT, "Current version is {$fromVersion}.\n"); - - $upgradeId = $this->upload($packageFile); - - $manifest = $this->getUpgradeManager()->getManifestById($upgradeId); - - $nextVersion = $manifest['version']; - - if (!$params['skipConfirmation']) { - fwrite(\STDOUT, "EspoCRM will be upgraded to version {$nextVersion} now. Enter [Y] to continue.\n"); - - if (!$this->confirm()) { - echo "Upgrade canceled.\n"; + $packageFile = $params->file ?? null; + if (!$params->localMode) { + if (empty($versionInfo)) { + fwrite(\STDOUT, "Error: Upgrade server is currently unavailable. Please try again later.\n"); return; } - } - - fwrite(\STDOUT, "Upgrading... This may take a while..."); - - try { - $this->runUpgradeProcess($upgradeId, $params); - } - catch (Exception $e) { - fwrite(\STDOUT, "\n"); - fwrite(\STDOUT, $e->getMessage() . "\n"); - - return; - } - - fwrite(\STDOUT, "\n"); - - $currentVerison = $this->getCurrentVersion(); - - fwrite(\STDOUT, "Upgrade is complete. Current version is {$currentVerison}.\n"); - - $infoData = $this->getVersionInfo(); - - $lastVersion = $infoData->lastVersion ?? null; - - if ($lastVersion && $lastVersion !== $currentVerison && $fromVersion !== $currentVerison) { - fwrite(\STDOUT, "Newer version is available.\n"); - - return; - } - - if ($lastVersion && $lastVersion === $currentVerison) { - fwrite(\STDOUT, "You have the latest version.\n"); - - return; - } - } - - protected function runRemoteUpgrade(array $params) - { - $infoData = $this->getVersionInfo(); - - if (!$infoData) { - return; - } - - $nextVersion = $infoData->nextVersion ?? null; - $lastVersion = $infoData->lastVersion ?? null; - - $fromVersion = $this->config->get('version'); - - fwrite(\STDOUT, "Current version is {$fromVersion}.\n"); - - if (!$nextVersion) { - echo "There are no available upgrades.\n"; - - return; - } - - if (!$params['skipConfirmation']) { - fwrite(\STDOUT, "EspoCRM will be upgraded to version {$nextVersion} now. Enter [Y] to continue.\n"); - - if (!$this->confirm()) { - echo "Upgrade canceled.\n"; + if (!isset($versionInfo->nextVersion)) { + fwrite(\STDOUT, "There are no available upgrades.\n"); return; } + + if (!isset($versionInfo->nextPackage)) { + fwrite(\STDOUT, "Error: Upgrade package is not found.\n"); + return; + } + + return $versionInfo->nextPackage; } - fwrite(\STDOUT, "Downloading..."); - - $upgradePackageFilePath = $this->downloadFile($infoData->nextPackage); - - if (!$upgradePackageFilePath) { + if (!$packageFile || !file_exists($packageFile)) { + fwrite(\STDOUT, "Error: Upgrade package is not found.\n"); return; } - fwrite(\STDOUT, "\n"); - - fwrite(\STDOUT, "Upgrading... This may take a while..."); - - $upgradeId = $this->upload($upgradePackageFilePath); - - try { - $this->runUpgradeProcess($upgradeId, $params); - } - catch (Exception $e) { - $error = $e->getMessage(); - } - - $this->fileManager->unlink($upgradePackageFilePath); - - fwrite(\STDOUT, "\n"); - - if (!empty($error)) { - echo $error; - - return; - } - - $currentVerison = $this->getCurrentVersion(); - - fwrite(\STDOUT, "Upgrade is complete. Current version is {$currentVerison}.\n"); - - if ($lastVersion && $lastVersion !== $currentVerison && $fromVersion !== $currentVerison) { - fwrite(\STDOUT, "Newer version is available. Run command again to upgrade.\n"); - - return; - } - - if ($lastVersion && $lastVersion === $currentVerison) { - fwrite(\STDOUT, "You have the latest version.\n"); - - return; - } + return $packageFile; } - protected function upload($filePath) + protected function upload(string $filePath) { try { $fileData = file_get_contents($filePath); @@ -283,11 +233,12 @@ class Upgrade implements Command return $upgradeId; } - protected function runUpgradeProcess($upgradeId, array $params = []) + protected function runUpgradeProcess(string $upgradeId, object $params = null) { - $useSingleProcess = array_key_exists('singleProcess', $params) ? $params['singleProcess'] : false; + $params = $params ?? (object) []; + $useSingleProcess = property_exists($params, 'singleProcess') ? $params->singleProcess : false; - $stepList = !empty($params['step']) ? [$params['step']] : $this->upgradeStepList; + $stepList = !empty($params->step) ? [$params->step] : $this->upgradeStepList; array_unshift($stepList, 'init'); array_push($stepList, 'finalize'); @@ -299,7 +250,7 @@ class Upgrade implements Command return $this->runStepsInSingleProcess($upgradeId, $stepList); } - protected function runStepsInSingleProcess($upgradeId, array $stepList) + protected function runStepsInSingleProcess(string $upgradeId, array $stepList) { $GLOBALS['log']->debug('Installation process ['.$upgradeId.']: Single process mode.'); @@ -319,7 +270,7 @@ class Upgrade implements Command return true; } - protected function runSteps($upgradeId, array $stepList) + protected function runSteps(string $upgradeId, array $stepList) { $phpExecutablePath = $this->getPhpExecutablePath(); @@ -355,7 +306,7 @@ class Upgrade implements Command return true; } - protected function getUpgradeManager($reload = false) + protected function getUpgradeManager(bool $reload = false) { if (!$this->upgradeManager || $reload) { $app = new Application(); @@ -413,7 +364,7 @@ class Upgrade implements Command return $data; } - protected function downloadFile($url) + protected function downloadFile(string $url) { $localFilePath = 'data/upload/upgrades/' . Util::generateId() . '.zip'; From 548e829f2f5e87e8882849428e605a2550113b29 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 25 Sep 2020 10:16:05 +0300 Subject: [PATCH 2/3] cs fix --- .../Espo/Core/Console/Commands/Upgrade.php | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/application/Espo/Core/Console/Commands/Upgrade.php b/application/Espo/Core/Console/Commands/Upgrade.php index 861a74ac53..3f91967989 100644 --- a/application/Espo/Core/Console/Commands/Upgrade.php +++ b/application/Espo/Core/Console/Commands/Upgrade.php @@ -73,16 +73,23 @@ class Upgrade implements Command $params = $this->normalizeParams($options, $flagList, $argumentList); $versionInfo = $this->getVersionInfo(); + $fromVersion = $this->config->get('version'); + $nextVersion = $versionInfo->nextVersion ?? null; $lastVersion = $infoData->lastVersion ?? null; $packageFile = $this->getPackageFile($params, $versionInfo); - if (!$packageFile) return; + + if (!$packageFile) { + return; + } if ($params->localMode) { $upgradeId = $this->upload($packageFile); + $manifest = $this->getUpgradeManager()->getManifestById($upgradeId); + $nextVersion = $manifest['version']; } @@ -93,6 +100,7 @@ class Upgrade implements Command if (!$this->confirm()) { echo "Upgrade canceled.\n"; + return; } } @@ -106,6 +114,7 @@ class Upgrade implements Command if (!$packageFile) { fwrite(\STDOUT, "Error: Unable to download upgrade package.\n"); + return; } } @@ -116,7 +125,8 @@ class Upgrade implements Command try { $this->runUpgradeProcess($upgradeId, $params); - } catch (\Exception $e) { + } + catch (Exception $e) { $errorMessage = $e->getMessage(); } @@ -128,6 +138,7 @@ class Upgrade implements Command if (!empty($errorMessage)) { fwrite(\STDOUT, $errorMessage . "\n"); + return; } @@ -137,11 +148,13 @@ class Upgrade implements Command if ($lastVersion && $lastVersion !== $currentVerison && $fromVersion !== $currentVerison) { fwrite(\STDOUT, "Newer version is available. Run command again to upgrade.\n"); + return; } if ($lastVersion && $lastVersion === $currentVerison) { fwrite(\STDOUT, "You have the latest version.\n"); + return; } } @@ -194,16 +207,19 @@ class Upgrade implements Command if (!$params->localMode) { if (empty($versionInfo)) { fwrite(\STDOUT, "Error: Upgrade server is currently unavailable. Please try again later.\n"); + return; } if (!isset($versionInfo->nextVersion)) { fwrite(\STDOUT, "There are no available upgrades.\n"); + return; } if (!isset($versionInfo->nextPackage)) { fwrite(\STDOUT, "Error: Upgrade package is not found.\n"); + return; } @@ -212,6 +228,7 @@ class Upgrade implements Command if (!$packageFile || !file_exists($packageFile)) { fwrite(\STDOUT, "Error: Upgrade package is not found.\n"); + return; } From 3ee78c29cb1cda61dc6559353c39fed3f3c05dea Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 25 Sep 2020 10:24:14 +0300 Subject: [PATCH 3/3] cs fixes --- application/Espo/Core/Console/Commands/Upgrade.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/application/Espo/Core/Console/Commands/Upgrade.php b/application/Espo/Core/Console/Commands/Upgrade.php index 3f91967989..f4fea64d10 100644 --- a/application/Espo/Core/Console/Commands/Upgrade.php +++ b/application/Espo/Core/Console/Commands/Upgrade.php @@ -165,12 +165,8 @@ class Upgrade implements Command * -s - single process * --file="EspoCRM-upgrade.zip" * --step="beforeUpgradeScript" - * @param array $options - * @param array $flagList - * @param array $argumentList - * @return object */ - protected function normalizeParams(array $options, array $flagList, array $argumentList) + protected function normalizeParams(array $options, array $flagList, array $argumentList) : object { $params = (object) [ 'localMode' => false, @@ -391,9 +387,9 @@ class Upgrade implements Command copy($url, $localFilePath); } else { $options = [ - CURLOPT_FILE => fopen($localFilePath, 'w'), - CURLOPT_TIMEOUT => 3600, - CURLOPT_URL => $url, + \CURLOPT_FILE => fopen($localFilePath, 'w'), + \CURLOPT_TIMEOUT => 3600, + \CURLOPT_URL => $url, ]; $ch = curl_init();