Skip to content

Commit 2e75785

Browse files
Change the method of AWS S3 enablement (#79)
1 parent cd0e0fb commit 2e75785

File tree

3 files changed

+108
-73
lines changed

3 files changed

+108
-73
lines changed

src/App/Error.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class Error
138138
public const WARN_REMOTE_STORAGE_CANNOT_BE_ENABLED = 2028;
139139
public const WARN_DEPRECATED_SPLIT_DB = 2029;
140140
public const WARN_OS_ES_SERVICES_BOTH_INSTALLED = 2030;
141+
public const WARN_REMOTE_STORAGE_CANNOT_BE_DISABLED = 2031;
141142

142143
/**
143144
* Post-deploy

src/Step/Deploy/RemoteStorage.php

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
namespace Magento\MagentoCloud\Step\Deploy;
99

1010
use Magento\MagentoCloud\App\Error;
11+
use Magento\MagentoCloud\Filesystem\FileSystemException;
12+
use Magento\MagentoCloud\Config\Magento\Env\WriterInterface;
1113
use Magento\MagentoCloud\Package\MagentoVersion;
1214
use Magento\MagentoCloud\Package\UndefinedPackageException;
13-
use Magento\MagentoCloud\Shell\MagentoShell;
14-
use Magento\MagentoCloud\Shell\ShellException;
1515
use Magento\MagentoCloud\Step\StepException;
1616
use Magento\MagentoCloud\Step\StepInterface;
1717
use Magento\MagentoCloud\Config\RemoteStorage as RemoteStorageConfig;
@@ -27,11 +27,6 @@ class RemoteStorage implements StepInterface
2727
*/
2828
private $config;
2929

30-
/**
31-
* @var MagentoShell
32-
*/
33-
private $magentoShell;
34-
3530
/**
3631
* @var MagentoVersion
3732
*/
@@ -42,22 +37,27 @@ class RemoteStorage implements StepInterface
4237
*/
4338
private $logger;
4439

40+
/**
41+
* @var WriterInterface
42+
*/
43+
private $writer;
44+
4545
/**
4646
* @param RemoteStorageConfig $config
47-
* @param MagentoShell $magentoShell
4847
* @param MagentoVersion $magentoVersion
4948
* @param LoggerInterface $logger
49+
* @param WriterInterface $writer
5050
*/
5151
public function __construct(
5252
RemoteStorageConfig $config,
53-
MagentoShell $magentoShell,
5453
MagentoVersion $magentoVersion,
55-
LoggerInterface $logger
54+
LoggerInterface $logger,
55+
WriterInterface $writer
5656
) {
5757
$this->config = $config;
58-
$this->magentoShell = $magentoShell;
5958
$this->magentoVersion = $magentoVersion;
6059
$this->logger = $logger;
60+
$this->writer = $writer;
6161
}
6262

6363
/**
@@ -78,9 +78,26 @@ public function execute(): void
7878
}
7979

8080
if ($driver = $this->config->getDriver()) {
81-
$this->updateConfig($driver);
81+
$this->enableRemoteStorage($driver);
82+
83+
$this->logger->info(sprintf('Remote storage driver set to: "%s"', $driver));
8284
} else {
83-
$this->magentoShell->execute('setup:config:set --remote-storage-driver=file -n');
85+
try {
86+
$this->writer->update(['remote_storage' => ['driver' => 'file']]);
87+
88+
$this->logger->debug(sprintf('Remote storage driver was reset'));
89+
} catch (FileSystemException $exception) {
90+
$this->logger->critical(
91+
$exception->getMessage(),
92+
['errorCode' => Error::WARN_REMOTE_STORAGE_CANNOT_BE_DISABLED]
93+
);
94+
95+
throw new StepException(
96+
$exception->getMessage(),
97+
Error::WARN_REMOTE_STORAGE_CANNOT_BE_DISABLED,
98+
$exception
99+
);
100+
}
84101
}
85102
}
86103

@@ -90,35 +107,38 @@ public function execute(): void
90107
* @param string $driver
91108
* @throws StepException
92109
*/
93-
private function updateConfig(string $driver): void
110+
private function enableRemoteStorage(string $driver): void
94111
{
95112
$config = $this->config->getConfig();
96113

97114
if (empty($config['bucket']) || empty($config['region'])) {
98115
throw new StepException('Bucket and region are required configurations');
99116
}
100117

101-
$options = [
102-
'--remote-storage-driver=' . $driver,
103-
'--remote-storage-bucket=' . $config['bucket'],
104-
'--remote-storage-region=' . $config['region']
118+
$data = [
119+
'remote_storage' => [
120+
'driver' => $driver,
121+
'config' => [
122+
'bucket' => $config['bucket'],
123+
'region' => $config['region']
124+
]
125+
],
105126
];
106127

107128
if ($prefix = $this->config->getPrefix()) {
108-
$options[] = '--remote-storage-prefix=' . $prefix;
129+
$data['remote_storage']['config']['prefix'] = $prefix;
109130
}
110131

111132
if (isset($config['key'], $config['secret'])) {
112-
$options[] = '--remote-storage-key=' . $config['key'];
113-
$options[] = '--remote-storage-secret=' . $config['secret'];
133+
$data['remote_storage']['config']['credentials'] = [
134+
'key' => $config['key'],
135+
'secret' => $config['secret']
136+
];
114137
}
115138

116139
try {
117-
$this->magentoShell->execute(sprintf(
118-
'setup:config:set %s -n',
119-
implode(' ', $options)
120-
));
121-
} catch (ShellException $exception) {
140+
$this->writer->update($data);
141+
} catch (FileSystemException $exception) {
122142
$this->logger->critical(
123143
$exception->getMessage(),
124144
['errorCode' => Error::WARN_REMOTE_STORAGE_CANNOT_BE_ENABLED]
@@ -130,10 +150,5 @@ private function updateConfig(string $driver): void
130150
$exception
131151
);
132152
}
133-
134-
$this->logger->info(sprintf(
135-
'Remote storage with driver "%s" was enabled',
136-
$driver
137-
));
138153
}
139154
}

src/Test/Unit/Step/Deploy/RemoteStorageTest.php

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
namespace Magento\MagentoCloud\Test\Unit\Step\Deploy;
99

10+
use Magento\MagentoCloud\Config\Magento\Env\WriterInterface;
11+
use Magento\MagentoCloud\Filesystem\FileSystemException;
1012
use Magento\MagentoCloud\Package\MagentoVersion;
11-
use Magento\MagentoCloud\Shell\MagentoShell;
12-
use Magento\MagentoCloud\Shell\ShellException;
1313
use Magento\MagentoCloud\Step\Deploy\RemoteStorage;
1414
use Magento\MagentoCloud\Step\StepException;
1515
use PHPUnit\Framework\MockObject\MockObject;
@@ -32,11 +32,6 @@ class RemoteStorageTest extends TestCase
3232
*/
3333
private $configMock;
3434

35-
/**
36-
* @var MagentoShell|MockObject
37-
*/
38-
private $magentoShellMock;
39-
4035
/**
4136
* @var MagentoVersion|MockObject
4237
*/
@@ -47,21 +42,26 @@ class RemoteStorageTest extends TestCase
4742
*/
4843
private $loggerMock;
4944

45+
/**
46+
* @var WriterInterface|MockObject
47+
*/
48+
private $writerMock;
49+
5050
/**
5151
* @inheritDoc
5252
*/
5353
protected function setUp(): void
5454
{
5555
$this->configMock = $this->createMock(Config::class);
5656
$this->magentoVersionMock = $this->createMock(MagentoVersion::class);
57-
$this->magentoShellMock = $this->createMock(MagentoShell::class);
5857
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
58+
$this->writerMock = $this->getMockForAbstractClass(WriterInterface::class);
5959

6060
$this->step = new RemoteStorage(
6161
$this->configMock,
62-
$this->magentoShellMock,
6362
$this->magentoVersionMock,
64-
$this->loggerMock
63+
$this->loggerMock,
64+
$this->writerMock
6565
);
6666
}
6767

@@ -80,15 +80,20 @@ public function testExecute(): void
8080
'bucket' => 'test_bucket',
8181
'region' => 'test_region',
8282
]);
83-
$this->magentoShellMock->expects(self::once())
84-
->method('execute')
85-
->with(
86-
'setup:config:set --remote-storage-driver=adapter'
87-
. ' --remote-storage-bucket=test_bucket --remote-storage-region=test_region -n'
88-
);
83+
$this->writerMock->expects(self::once())
84+
->method('update')
85+
->with([
86+
'remote_storage' => [
87+
'driver' => 'adapter',
88+
'config' => [
89+
'bucket' => 'test_bucket',
90+
'region' => 'test_region'
91+
]
92+
]
93+
]);
8994
$this->loggerMock->expects(self::once())
9095
->method('info')
91-
->with('Remote storage with driver "adapter" was enabled');
96+
->with('Remote storage driver set to: "adapter"');
9297

9398
$this->step->execute();
9499
}
@@ -110,16 +115,24 @@ public function testExecuteWithKeys(): void
110115
'key' => 'test_key',
111116
'secret' => 'test_secret'
112117
]);
113-
$this->magentoShellMock->expects(self::once())
114-
->method('execute')
115-
->with(
116-
'setup:config:set --remote-storage-driver=adapter'
117-
. ' --remote-storage-bucket=test_bucket --remote-storage-region=test_region'
118-
. ' --remote-storage-key=test_key --remote-storage-secret=test_secret -n'
119-
);
118+
$this->writerMock->expects(self::once())
119+
->method('update')
120+
->with([
121+
'remote_storage' => [
122+
'driver' => 'adapter',
123+
'config' => [
124+
'bucket' => 'test_bucket',
125+
'region' => 'test_region',
126+
'credentials' => [
127+
'key' => 'test_key',
128+
'secret' => 'test_secret'
129+
]
130+
]
131+
]
132+
]);
120133
$this->loggerMock->expects(self::once())
121134
->method('info')
122-
->with('Remote storage with driver "adapter" was enabled');
135+
->with('Remote storage driver set to: "adapter"');
123136

124137
$this->step->execute();
125138
}
@@ -143,17 +156,25 @@ public function testExecuteWithKeysAndPrefix(): void
143156
'key' => 'test_key',
144157
'secret' => 'test_secret',
145158
]);
146-
$this->magentoShellMock->expects(self::once())
147-
->method('execute')
148-
->with(
149-
'setup:config:set --remote-storage-driver=adapter'
150-
. ' --remote-storage-bucket=test_bucket --remote-storage-region=test_region'
151-
. ' --remote-storage-prefix=test_prefix'
152-
. ' --remote-storage-key=test_key --remote-storage-secret=test_secret -n'
153-
);
159+
$this->writerMock->expects(self::once())
160+
->method('update')
161+
->with([
162+
'remote_storage' => [
163+
'driver' => 'adapter',
164+
'config' => [
165+
'bucket' => 'test_bucket',
166+
'region' => 'test_region',
167+
'credentials' => [
168+
'key' => 'test_key',
169+
'secret' => 'test_secret'
170+
],
171+
'prefix' => 'test_prefix'
172+
]
173+
]
174+
]);
154175
$this->loggerMock->expects(self::once())
155176
->method('info')
156-
->with('Remote storage with driver "adapter" was enabled');
177+
->with('Remote storage driver set to: "adapter"');
157178

158179
$this->step->execute();
159180
}
@@ -168,11 +189,9 @@ public function testExecuteDisable(): void
168189
->willReturn(true);
169190
$this->configMock->method('getDriver')
170191
->willReturn('');
171-
$this->magentoShellMock->expects(self::once())
172-
->method('execute')
173-
->with(
174-
'setup:config:set --remote-storage-driver=file -n'
175-
);
192+
$this->writerMock->expects(self::once())
193+
->method('update')
194+
->with(['remote_storage' => ['driver' => 'file']]);
176195

177196
$this->step->execute();
178197
}
@@ -197,9 +216,9 @@ public function testExecuteWithException(): void
197216
'key' => 'test_key',
198217
'secret' => 'test_secret'
199218
]);
200-
$this->magentoShellMock->expects(self::once())
201-
->method('execute')
202-
->willThrowException(new ShellException('Some error'));
219+
$this->writerMock->expects(self::once())
220+
->method('update')
221+
->willThrowException(new FileSystemException('Some error'));
203222
$this->loggerMock->expects(self::once())
204223
->method('critical')
205224
->with('Some error');

0 commit comments

Comments
 (0)