55namespace Hypernode \Deploy \Command ;
66
77use Hypernode \Deploy \Brancher \BrancherHypernodeManager ;
8+ use Hypernode \Deploy \ConfigurationLoader ;
9+ use Hypernode \Deploy \DeployerLoader ;
810use Hypernode \Deploy \Report \ReportLoader ;
11+ use Hypernode \DeployConfiguration \BrancherServer ;
12+ use Hypernode \DeployConfiguration \Configuration ;
13+ use Hypernode \DeployConfiguration \Server ;
914use Symfony \Component \Console \Command \Command ;
15+ use Symfony \Component \Console \Input \InputArgument ;
1016use Symfony \Component \Console \Input \InputInterface ;
1117use Symfony \Component \Console \Output \OutputInterface ;
1218use Throwable ;
1319
1420class Cleanup extends Command
1521{
1622 private ReportLoader $ reportLoader ;
23+ private DeployerLoader $ deployerLoader ;
24+ private ConfigurationLoader $ configurationLoader ;
1725 private BrancherHypernodeManager $ brancherHypernodeManager ;
1826
19- public function __construct (ReportLoader $ reportLoader , BrancherHypernodeManager $ brancherHypernodeManager )
20- {
27+ public function __construct (
28+ ReportLoader $ reportLoader ,
29+ DeployerLoader $ deployerLoader ,
30+ ConfigurationLoader $ configurationLoader ,
31+ BrancherHypernodeManager $ brancherHypernodeManager
32+ ) {
2133 parent ::__construct ();
2234
2335 $ this ->reportLoader = $ reportLoader ;
36+ $ this ->deployerLoader = $ deployerLoader ;
37+ $ this ->configurationLoader = $ configurationLoader ;
2438 $ this ->brancherHypernodeManager = $ brancherHypernodeManager ;
2539 }
2640
@@ -31,6 +45,7 @@ protected function configure()
3145 $ this ->setDescription (
3246 'Clean up any acquired resources during the deployment, like brancher Hypernodes. '
3347 );
48+ $ this ->addArgument ('stage ' , InputArgument::OPTIONAL , 'Stage to cleanup ' );
3449 }
3550
3651 /**
@@ -40,13 +55,46 @@ protected function execute(InputInterface $input, OutputInterface $output)
4055 {
4156 $ report = $ this ->reportLoader ->loadReport ();
4257
43- if ($ report === null ) {
44- $ output ->writeln ('No report found, skipping cleanup. ' );
45- return 0 ;
58+ if ($ report ) {
59+ $ this ->brancherHypernodeManager ->cancel (...$ report ->getBrancherHypernodes ());
4660 }
4761
48- $ this ->brancherHypernodeManager ->cancel (...$ report ->getBrancherHypernodes ());
62+ /** @var string $stageName */
63+ $ stageName = $ input ->getArgument ('stage ' );
64+ if ($ stageName ) {
65+ $ this ->deployerLoader ->getOrCreateInstance ($ output );
66+ $ config = $ this ->configurationLoader ->load ($ input ->getOption ('file ' ) ?: 'deploy.php ' );
67+ $ this ->cancelByStage ($ stageName , $ config );
68+ }
4969
5070 return 0 ;
5171 }
72+
73+ /**
74+ * Cancel brancher nodes by stage and their configured labels.
75+ *
76+ * @param string $stageName Stage to clean up
77+ * @param Configuration $config Deployment configuration to read stages/servers from
78+ * @return void
79+ */
80+ private function cancelByStage (string $ stageName , Configuration $ config ): void
81+ {
82+ foreach ($ config ->getStages () as $ stage ) {
83+ if ($ stage ->getName () !== $ stageName ) {
84+ continue ;
85+ }
86+ foreach ($ stage ->getServers () as $ server ) {
87+ if (!($ server instanceof BrancherServer)) {
88+ continue ;
89+ }
90+ $ labels = $ server ->getLabels ();
91+ $ hypernode = $ server ->getOptions ()[Server::OPTION_HN_PARENT_APP ];
92+ $ brancherHypernodes = $ this ->brancherHypernodeManager ->queryBrancherHypernodes (
93+ $ hypernode ,
94+ $ labels
95+ );
96+ $ this ->brancherHypernodeManager ->cancel (...$ brancherHypernodes );
97+ }
98+ }
99+ }
52100}
0 commit comments