Skip to content
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
3 changes: 2 additions & 1 deletion src/BrefCloudClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,15 @@ public function markDeploymentFinished(
* outputs: array<string, string>,
* app: array{id: int, name: string},
* aws_account_id: int|null,
* removal?: array{status: string, message: string|null}|null,
* }
*
* @throws HttpExceptionInterface
* @throws ExceptionInterface
*/
public function getEnvironment(int $id): array
{
/** @var array{id: int, name: string, region: string|null, url: string|null, outputs: array<string, string>, app: array{id: int, name: string}, aws_account_id: int|null} $result */
/** @var array{id: int, name: string, region: string|null, url: string|null, outputs: array<string, string>, app: array{id: int, name: string}, aws_account_id: int|null, removal?: array{status: string, message: string|null}|null} $result */
$result = $this->client->request('GET', '/api/v1/environments/' . $id)->toArray();

return $result;
Expand Down
27 changes: 25 additions & 2 deletions src/Commands/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,39 @@ protected function execute(InputInterface $input, OutputInterface $output): int
IO::verbose('Triggering asynchronous removal of environment ' . $environmentId);
$message = $brefCloud->removeEnvironment($environmentId, (bool) $input->getOption('delete-secrets'));

// Bref Cloud says why the removal takes long, or why it failed
$shownMessage = null;
while (true) {
try {
$brefCloud->getEnvironment($environmentId);
$removal = $brefCloud->getEnvironment($environmentId)['removal'] ?? null;
} catch (HttpExceptionInterface $e) {
if ($e->getResponse()->getStatusCode() === 404) {
break;
}
$removal = null;
}

delay(1);
// Only AWS cleaning up is left (e.g. network interfaces of functions in a VPC): Bref
// Cloud finishes the removal, there is nothing for the user to wait for
if (($removal['status'] ?? null) === 'finishing') {
IO::spinSuccess('removed');
IO::writeln(['', Styles::gray($removal['message'] ?? 'Bref Cloud finishes the removal in the background.')]);
break;
}
if (($removal['status'] ?? null) === 'failed') {
IO::spinError('failed');
IO::writeln(Styles::bold(Styles::red($removal['message'] ?? 'Unknown error')));
return 1;
}
$progress = $removal['message'] ?? null;
if ($progress !== null && $progress !== $shownMessage) {
IO::spinClear();
IO::writeln([Styles::gray($progress), '']);
IO::spin('removing');
$shownMessage = $progress;
}

delay(3);
}

IO::spinSuccess('removed');
Expand Down
45 changes: 41 additions & 4 deletions tests/Commands/RemoveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,55 @@ public function test_bref_cloud_may_return_no_message(): void
$this->assertStringContainsString('removed', $display);
}

public function test_shows_why_the_removal_takes_long(): void
{
$waiting = ['status' => 'in_progress', 'message' => 'Waiting for AWS to release the network interfaces of shop-prod-web.'];
[$status, $display] = $this->remove([], polls: [$waiting, $waiting]);

$this->assertSame(0, $status, $display);
$this->assertSame(1, substr_count($display, 'Waiting for AWS to release the network interfaces of shop-prod-web.'), $display);
$this->assertStringContainsString('removed', $display);
}

public function test_stops_waiting_when_only_aws_cleanup_is_left(): void
{
$finishing = ['status' => 'finishing', 'message' => 'AWS releases the network interfaces of shop-prod-web in the background.'];
// The environment is still there afterwards: the command does not wait for it
[$status, $display] = $this->remove(['message' => 'Secrets are kept.'], polls: [$finishing, $finishing, $finishing]);

$this->assertSame(0, $status, $display);
$this->assertStringContainsString('removed', $display);
$this->assertStringContainsString('AWS releases the network interfaces of shop-prod-web in the background.', $display);
$this->assertStringContainsString('Secrets are kept.', $display);
}

public function test_stops_when_the_removal_fails(): void
{
[$status, $display] = $this->remove([], polls: [['status' => 'failed', 'message' => 'AWS could not delete everything: bucket not empty']]);

$this->assertSame(1, $status, $display);
$this->assertStringContainsString('AWS could not delete everything: bucket not empty', $display);
}

/**
* @param array<string, mixed> $deletion What Bref Cloud returns when asked to remove the environment
* @param list<array<string, mixed>|null> $polls The `removal` of the environment on each check, before it is gone
* @param array<string, mixed> $options Options of the command
* @return array{int, string} The exit code and the output
*/
private function remove(array $deletion, array $options = []): array
private function remove(array $deletion, array $options = [], array $polls = []): array
{
$command = new Remove($this->brefCloud([
'/api/v1/environments/find' => $this->environment(),
'/api/v1/environments/12' => fn(string $method) => $method === 'DELETE'
? $this->json($deletion, 202)
: $this->json(['message' => 'Not found'], 404),
'/api/v1/environments/12' => function (string $method) use ($deletion, &$polls) {
if ($method === 'DELETE') {
return $this->json($deletion, 202);
}
if ($polls === []) {
return $this->json(['message' => 'Not found'], 404);
}
return $this->json([...$this->environment(), 'removal' => array_shift($polls)]);
},
]));
$configFile = sys_get_temp_dir() . '/bref-cli-test-' . bin2hex(random_bytes(4)) . '.yml';
file_put_contents($configFile, "service: shop\nbref:\n team: acme\n");
Expand Down
Loading