From 6f15488d83a265d1e6b962d14e837695182be69c Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sun, 15 Nov 2026 17:04:46 +0100 Subject: [PATCH 1/2] Show why removing an environment takes long, and stop when it fails --- src/BrefCloudClient.php | 3 ++- src/Commands/Remove.php | 20 ++++++++++++++++++-- tests/Commands/RemoveTest.php | 33 +++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/BrefCloudClient.php b/src/BrefCloudClient.php index d89dbf6..37d21be 100644 --- a/src/BrefCloudClient.php +++ b/src/BrefCloudClient.php @@ -260,6 +260,7 @@ public function markDeploymentFinished( * outputs: array, * app: array{id: int, name: string}, * aws_account_id: int|null, + * removal?: array{status: string, message: string|null}|null, * } * * @throws HttpExceptionInterface @@ -267,7 +268,7 @@ public function markDeploymentFinished( */ public function getEnvironment(int $id): array { - /** @var array{id: int, name: string, region: string|null, url: string|null, outputs: array, 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, 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; diff --git a/src/Commands/Remove.php b/src/Commands/Remove.php index 5a819a8..66f60de 100644 --- a/src/Commands/Remove.php +++ b/src/Commands/Remove.php @@ -50,16 +50,32 @@ 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); + 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'); diff --git a/tests/Commands/RemoveTest.php b/tests/Commands/RemoveTest.php index 5ac4377..765cc63 100644 --- a/tests/Commands/RemoveTest.php +++ b/tests/Commands/RemoveTest.php @@ -34,18 +34,43 @@ 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_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 $deletion What Bref Cloud returns when asked to remove the environment + * @param list|null> $polls The `removal` of the environment on each check, before it is gone * @param array $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"); From 6cc3df62f0650cdf180ed9ef4510d3b921514ca5 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sun, 15 Nov 2026 18:40:07 +0100 Subject: [PATCH 2/2] Stop waiting once only AWS cleaning up is left --- src/Commands/Remove.php | 7 +++++++ tests/Commands/RemoveTest.php | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Commands/Remove.php b/src/Commands/Remove.php index 66f60de..9767e70 100644 --- a/src/Commands/Remove.php +++ b/src/Commands/Remove.php @@ -62,6 +62,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $removal = null; } + // 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'))); diff --git a/tests/Commands/RemoveTest.php b/tests/Commands/RemoveTest.php index 765cc63..98d44de 100644 --- a/tests/Commands/RemoveTest.php +++ b/tests/Commands/RemoveTest.php @@ -44,6 +44,18 @@ public function test_shows_why_the_removal_takes_long(): void $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']]);