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

Migrate available temp space check to new SetupCheck API #42839

Merged
merged 3 commits into from
Jan 25, 2024
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
1 change: 1 addition & 0 deletions apps/settings/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
'OCA\\Settings\\SetupChecks\\ReadOnlyConfig' => $baseDir . '/../lib/SetupChecks/ReadOnlyConfig.php',
'OCA\\Settings\\SetupChecks\\SupportedDatabase' => $baseDir . '/../lib/SetupChecks/SupportedDatabase.php',
'OCA\\Settings\\SetupChecks\\SystemIs64bit' => $baseDir . '/../lib/SetupChecks/SystemIs64bit.php',
'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => $baseDir . '/../lib/SetupChecks/TempSpaceAvailable.php',
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => $baseDir . '/../lib/SetupChecks/TransactionIsolation.php',
'OCA\\Settings\\UserMigration\\AccountMigrator' => $baseDir . '/../lib/UserMigration/AccountMigrator.php',
'OCA\\Settings\\UserMigration\\AccountMigratorException' => $baseDir . '/../lib/UserMigration/AccountMigratorException.php',
Expand Down
1 change: 1 addition & 0 deletions apps/settings/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class ComposerStaticInitSettings
'OCA\\Settings\\SetupChecks\\ReadOnlyConfig' => __DIR__ . '/..' . '/../lib/SetupChecks/ReadOnlyConfig.php',
'OCA\\Settings\\SetupChecks\\SupportedDatabase' => __DIR__ . '/..' . '/../lib/SetupChecks/SupportedDatabase.php',
'OCA\\Settings\\SetupChecks\\SystemIs64bit' => __DIR__ . '/..' . '/../lib/SetupChecks/SystemIs64bit.php',
'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => __DIR__ . '/..' . '/../lib/SetupChecks/TempSpaceAvailable.php',
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => __DIR__ . '/..' . '/../lib/SetupChecks/TransactionIsolation.php',
'OCA\\Settings\\UserMigration\\AccountMigrator' => __DIR__ . '/..' . '/../lib/UserMigration/AccountMigrator.php',
'OCA\\Settings\\UserMigration\\AccountMigratorException' => __DIR__ . '/..' . '/../lib/UserMigration/AccountMigratorException.php',
Expand Down
2 changes: 2 additions & 0 deletions apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
use OCA\Settings\SetupChecks\ReadOnlyConfig;
use OCA\Settings\SetupChecks\SupportedDatabase;
use OCA\Settings\SetupChecks\SystemIs64bit;
use OCA\Settings\SetupChecks\TempSpaceAvailable;
use OCA\Settings\SetupChecks\TransactionIsolation;
use OCA\Settings\UserMigration\AccountMigrator;
use OCA\Settings\WellKnown\ChangePasswordHandler;
Expand Down Expand Up @@ -206,6 +207,7 @@ public function register(IRegistrationContext $context): void {
$context->registerSetupCheck(ReadOnlyConfig::class);
$context->registerSetupCheck(SupportedDatabase::class);
$context->registerSetupCheck(SystemIs64bit::class);
$context->registerSetupCheck(TempSpaceAvailable::class);
$context->registerSetupCheck(TransactionIsolation::class);

$context->registerUserMigrator(AccountMigrator::class);
Expand Down
53 changes: 0 additions & 53 deletions apps/settings/lib/Controller/CheckSetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ITempManager;
use OCP\IURLGenerator;
use OCP\Notification\IManager;
use OCP\SetupCheck\ISetupCheckManager;
Expand All @@ -73,8 +72,6 @@ class CheckSetupController extends Controller {
private $checker;
/** @var LoggerInterface */
private $logger;
/** @var ITempManager */
private $tempManager;
/** @var IManager */
private $manager;
private ISetupCheckManager $setupCheckManager;
Expand All @@ -86,7 +83,6 @@ public function __construct($AppName,
IL10N $l10n,
Checker $checker,
LoggerInterface $logger,
ITempManager $tempManager,
IManager $manager,
ISetupCheckManager $setupCheckManager,
) {
Expand All @@ -96,7 +92,6 @@ public function __construct($AppName,
$this->l10n = $l10n;
$this->checker = $checker;
$this->logger = $logger;
$this->tempManager = $tempManager;
$this->manager = $manager;
$this->setupCheckManager = $setupCheckManager;
}
Expand Down Expand Up @@ -192,52 +187,6 @@ public function getFailedIntegrityCheckFiles(): DataDisplayResponse {
);
}

private function isTemporaryDirectoryWritable(): bool {
try {
if (!empty($this->tempManager->getTempBaseDir())) {
return true;
}
} catch (\Exception $e) {
}
return false;
}

protected function isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed(): bool {
$objectStore = $this->config->getSystemValue('objectstore', null);
$objectStoreMultibucket = $this->config->getSystemValue('objectstore_multibucket', null);

if (!isset($objectStoreMultibucket) && !isset($objectStore)) {
return true;
}

if (isset($objectStoreMultibucket['class']) && $objectStoreMultibucket['class'] !== 'OC\\Files\\ObjectStore\\S3') {
return true;
}

if (isset($objectStore['class']) && $objectStore['class'] !== 'OC\\Files\\ObjectStore\\S3') {
return true;
}

$tempPath = sys_get_temp_dir();
if (!is_dir($tempPath)) {
$this->logger->error('Error while checking the temporary PHP path - it was not properly set to a directory. Returned value: ' . $tempPath);
return false;
}
$freeSpaceInTemp = function_exists('disk_free_space') ? disk_free_space($tempPath) : false;
if ($freeSpaceInTemp === false) {
$this->logger->error('Error while checking the available disk space of temporary PHP path or no free disk space returned. Temporary path: ' . $tempPath);
return false;
}

$freeSpaceInTempInGB = $freeSpaceInTemp / 1024 / 1024 / 1024;
if ($freeSpaceInTempInGB > 50) {
return true;
}

$this->logger->warning('Checking the available space in the temporary path resulted in ' . round($freeSpaceInTempInGB, 1) . ' GB instead of the recommended 50GB. Path: ' . $tempPath);
return false;
}

/**
* @return DataResponse
* @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Overview)
Expand All @@ -247,9 +196,7 @@ public function check() {
[
'isFairUseOfFreePushService' => $this->isFairUseOfFreePushService(),
'reverseProxyDocs' => $this->urlGenerator->linkToDocs('admin-reverse-proxy'),
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed' => $this->isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed(),
'reverseProxyGeneratedURL' => $this->urlGenerator->getAbsoluteURL('index.php'),
'temporaryDirectoryWritable' => $this->isTemporaryDirectoryWritable(),
'generic' => $this->setupCheckManager->runAll(),
]
);
Expand Down
126 changes: 126 additions & 0 deletions apps/settings/lib/SetupChecks/TempSpaceAvailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Settings\SetupChecks;

use OCP\IConfig;
use OCP\IL10N;
use OCP\ITempManager;
use OCP\IURLGenerator;
use OCP\SetupCheck\ISetupCheck;
use OCP\SetupCheck\SetupResult;

class TempSpaceAvailable implements ISetupCheck {
public function __construct(
private IL10N $l10n,
private IConfig $config,
private IURLGenerator $urlGenerator,
private ITempManager $tempManager,
) {
}

public function getName(): string {
return $this->l10n->t('Temporary space available');
}

public function getCategory(): string {
return 'system';
}

private function isPrimaryStorageS3(): bool {
$objectStore = $this->config->getSystemValue('objectstore', null);
$objectStoreMultibucket = $this->config->getSystemValue('objectstore_multibucket', null);

if (!isset($objectStoreMultibucket) && !isset($objectStore)) {
return false;
}

if (isset($objectStoreMultibucket['class']) && $objectStoreMultibucket['class'] !== 'OC\\Files\\ObjectStore\\S3') {
return false;
}

if (isset($objectStore['class']) && $objectStore['class'] !== 'OC\\Files\\ObjectStore\\S3') {
return false;
}

return true;
}

public function run(): SetupResult {
$phpTempPath = sys_get_temp_dir();
$nextcloudTempPath = '';
try {
$nextcloudTempPath = $this->tempManager->getTempBaseDir();
} catch (\Exception $e) {
}

if (empty($nextcloudTempPath)) {
return SetupResult::error('The temporary directory of this instance points to an either non-existing or non-writable directory.');
}

if (!is_dir($phpTempPath)) {
return SetupResult::error($this->l10n->t('Error while checking the temporary PHP path - it was not properly set to a directory. Returned value: %s', [$phpTempPath]));
}

$freeSpaceInTemp = function_exists('disk_free_space') ? disk_free_space($phpTempPath) : false;
if ($freeSpaceInTemp === false) {
return SetupResult::error($this->l10n->t('Error while checking the available disk space of temporary PHP path or no free disk space returned. Temporary path: %s', [$phpTempPath]));
}

/** Build details data about temporary directory, either one or two of them */
$freeSpaceInTempInGB = $freeSpaceInTemp / 1024 / 1024 / 1024;
$spaceDetail = $this->l10n->t('- %.1f GiB available in %s (PHP temporary directory)', [round($freeSpaceInTempInGB, 1),$phpTempPath]);
if ($nextcloudTempPath !== $phpTempPath) {
$freeSpaceInNextcloudTemp = function_exists('disk_free_space') ? disk_free_space($nextcloudTempPath) : false;
if ($freeSpaceInNextcloudTemp === false) {
return SetupResult::error($this->l10n->t('Error while checking the available disk space of temporary PHP path or no free disk space returned. Temporary path: %s', [$nextcloudTempPath]));
}
$freeSpaceInNextcloudTempInGB = $freeSpaceInNextcloudTemp / 1024 / 1024 / 1024;
$spaceDetail .= "\n".$this->l10n->t('- %.1f GiB available in %s (Nextcloud temporary directory)', [round($freeSpaceInNextcloudTempInGB, 1),$nextcloudTempPath]);
}

if (!$this->isPrimaryStorageS3()) {
return SetupResult::success(
$this->l10n->t("Temporary directory is correctly configured:\n%s", [$spaceDetail])
);
}

if ($freeSpaceInTempInGB > 50) {
return SetupResult::success(
$this->l10n->t(
"This instance uses an S3 based object store as primary storage, and has enough space in the temporary directory.\n%s",
[$spaceDetail]
)
);
}

return SetupResult::warning(
$this->l10n->t(
"This instance uses an S3 based object store as primary storage. The uploaded files are stored temporarily on the server and thus it is recommended to have 50 GiB of free space available in the temp directory of PHP. To improve this please change the temporary directory in the php.ini or make more space available in that path. \nChecking the available space in the temporary path resulted in %.1f GiB instead of the recommended 50 GiB. Path: %s",
[round($freeSpaceInTempInGB, 1),$phpTempPath]
)
);
}
}
57 changes: 0 additions & 57 deletions apps/settings/tests/Controller/CheckSetupControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ITempManager;
use OCP\IURLGenerator;
use OCP\Notification\IManager;
use OCP\SetupCheck\ISetupCheckManager;
Expand Down Expand Up @@ -72,8 +71,6 @@ class CheckSetupControllerTest extends TestCase {
private $logger;
/** @var Checker|\PHPUnit\Framework\MockObject\MockObject */
private $checker;
/** @var ITempManager|\PHPUnit\Framework\MockObject\MockObject */
private $tempManager;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
private $notificationManager;
/** @var ISetupCheckManager|MockObject */
Expand All @@ -98,7 +95,6 @@ protected function setUp(): void {
$this->checker = $this->getMockBuilder('\OC\IntegrityCheck\Checker')
->disableOriginalConstructor()->getMock();
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$this->tempManager = $this->getMockBuilder(ITempManager::class)->getMock();
$this->notificationManager = $this->getMockBuilder(IManager::class)->getMock();
$this->setupCheckManager = $this->createMock(ISetupCheckManager::class);
$this->checkSetupController = $this->getMockBuilder(CheckSetupController::class)
Expand All @@ -110,15 +106,13 @@ protected function setUp(): void {
$this->l10n,
$this->checker,
$this->logger,
$this->tempManager,
$this->notificationManager,
$this->setupCheckManager,
])
->setMethods([
'getCurlVersion',
'isPhpOutdated',
'isPHPMailerUsed',
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed',
])->getMock();
}

Expand All @@ -141,11 +135,6 @@ public function testCheck() {
$this->request->expects($this->never())
->method('getHeader');

$this->checkSetupController
->expects($this->once())
->method('isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed')
->willReturn(true);

$this->urlGenerator->method('linkToDocs')
->willReturnCallback(function (string $key): string {
if ($key === 'admin-performance') {
Expand Down Expand Up @@ -180,10 +169,8 @@ public function testCheck() {
$expected = new DataResponse(
[
'reverseProxyDocs' => 'reverse-proxy-doc-link',
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed' => true,
'reverseProxyGeneratedURL' => 'https://server/index.php',
'isFairUseOfFreePushService' => false,
'temporaryDirectoryWritable' => false,
'generic' => [],
]
);
Expand Down Expand Up @@ -645,48 +632,4 @@ public function testGetFailedIntegrityCheckFilesWithSomeErrorsFound() {
);
$this->assertEquals($expected, $this->checkSetupController->getFailedIntegrityCheckFiles());
}

public function dataForIsEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed() {
return [
['singlebucket', 'OC\\Files\\ObjectStore\\Swift', true],
['multibucket', 'OC\\Files\\ObjectStore\\Swift', true],
['singlebucket', 'OC\\Files\\ObjectStore\\Custom', true],
['multibucket', 'OC\Files\\ObjectStore\\Custom', true],
['singlebucket', 'OC\Files\ObjectStore\Swift', true],
['multibucket', 'OC\Files\ObjectStore\Swift', true],
['singlebucket', 'OC\Files\ObjectStore\Custom', true],
['multibucket', 'OC\Files\ObjectStore\Custom', true],
];
}

/**
* @dataProvider dataForIsEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed
*/
public function testIsEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed(string $mode, string $className, bool $expected) {
$this->config->method('getSystemValue')
->willReturnCallback(function ($key, $default) use ($mode, $className) {
if ($key === 'objectstore' && $mode === 'singlebucket') {
return ['class' => $className];
}
if ($key === 'objectstore_multibucket' && $mode === 'multibucket') {
return ['class' => $className];
}
return $default;
});

$checkSetupController = new CheckSetupController(
'settings',
$this->request,
$this->config,
$this->urlGenerator,
$this->l10n,
$this->checker,
$this->logger,
$this->tempManager,
$this->notificationManager,
$this->setupCheckManager,
);

$this->assertSame($expected, $this->invokePrivate($checkSetupController, 'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed'));
}
}
12 changes: 0 additions & 12 deletions core/js/setupchecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,6 @@
});
}

if (!data.isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed) {
messages.push({
msg: t('core', 'This instance uses an S3 based object store as primary storage. The uploaded files are stored temporarily on the server and thus it is recommended to have 50 GB of free space available in the temp directory of PHP. Check the logs for full details about the path and the available space. To improve this please change the temporary directory in the php.ini or make more space available in that path.'),
type: OC.SetupChecks.MESSAGE_TYPE_WARNING
})
}
if (!data.temporaryDirectoryWritable) {
messages.push({
msg: t('core', 'The temporary directory of this instance points to an either non-existing or non-writable directory.'),
type: OC.SetupChecks.MESSAGE_TYPE_WARNING
})
}
if (window.location.protocol === 'https:' && data.reverseProxyGeneratedURL.split('/')[0] !== 'https:') {
messages.push({
msg: t('core', 'You are accessing your instance over a secure connection, however your instance is generating insecure URLs. This most likely means that you are behind a reverse proxy and the overwrite config variables are not set correctly. Please read {linkstart}the documentation page about this ↗{linkend}.')
Expand Down
Loading
Loading