@@ -72,12 +72,12 @@ public function __construct(
7272 * @throws Throwable
7373 * @throws Exception
7474 */
75- public function run (OutputInterface $ output , string $ stage , string $ task , bool $ configureBuildStage , bool $ configureServers ): int
75+ public function run (OutputInterface $ output , string $ stage , string $ task , bool $ configureBuildStage , bool $ configureServers, bool $ reuseBrancher ): int
7676 {
7777 $ deployer = $ this ->deployerLoader ->getOrCreateInstance ($ output );
7878
7979 try {
80- $ this ->prepare ($ configureBuildStage , $ configureServers , $ stage );
80+ $ this ->prepare ($ configureBuildStage , $ configureServers , $ stage, $ reuseBrancher );
8181 } catch (InvalidConfigurationException | ValidationException $ e ) {
8282 $ output ->write ($ e ->getMessage ());
8383 return 1 ;
@@ -94,7 +94,7 @@ public function run(OutputInterface $output, string $stage, string $task, bool $
9494 * @throws InvalidConfigurationException
9595 * @throws Throwable
9696 */
97- private function prepare (bool $ configureBuildStage , bool $ configureServers , string $ stage ): void
97+ private function prepare (bool $ configureBuildStage , bool $ configureServers , string $ stage, bool $ reuseBrancher ): void
9898 {
9999 $ this ->recipeLoader ->load ('common.php ' );
100100 $ tasks = $ this ->taskFactory ->loadAll ();
@@ -108,7 +108,7 @@ private function prepare(bool $configureBuildStage, bool $configureServers, stri
108108 }
109109
110110 if ($ configureServers ) {
111- $ this ->configureServers ($ config , $ stage );
111+ $ this ->configureServers ($ config , $ stage, $ reuseBrancher );
112112 }
113113
114114 foreach ($ tasks as $ task ) {
@@ -150,22 +150,22 @@ private function initializeConfigurableTask(ConfigurableTaskInterface $task, Con
150150 }
151151 }
152152
153- private function configureServers (Configuration $ config , string $ stage ): void
153+ private function configureServers (Configuration $ config , string $ stage, bool $ reuseBrancher ): void
154154 {
155155 foreach ($ config ->getStages () as $ configStage ) {
156156 if ($ configStage ->getName () !== $ stage ) {
157157 continue ;
158158 }
159159
160160 foreach ($ configStage ->getServers () as $ server ) {
161- $ this ->configureStageServer ($ configStage , $ server , $ config );
161+ $ this ->configureStageServer ($ configStage , $ server , $ config, $ reuseBrancher );
162162 }
163163 }
164164 }
165165
166- private function configureStageServer (Stage $ stage , Server $ server , Configuration $ config ): void
166+ private function configureStageServer (Stage $ stage , Server $ server , Configuration $ config, bool $ reuseBrancher ): void
167167 {
168- $ this ->maybeConfigureBrancherServer ($ server );
168+ $ this ->maybeConfigureBrancherServer ($ server, $ reuseBrancher );
169169
170170 $ host = host ($ stage ->getName () . ': ' . $ server ->getHostname ());
171171 $ host ->setHostname ($ server ->getHostname ());
@@ -218,7 +218,7 @@ private function configureStageServer(Stage $stage, Server $server, Configuratio
218218 }
219219 }
220220
221- private function maybeConfigureBrancherServer (Server $ server ): void
221+ private function maybeConfigureBrancherServer (Server $ server, bool $ reuseBrancher ): void
222222 {
223223 $ serverOptions = $ server ->getOptions ();
224224 $ isBrancher = $ serverOptions [Server::OPTION_HN_BRANCHER ] ?? false ;
@@ -241,19 +241,23 @@ private function maybeConfigureBrancherServer(Server $server): void
241241
242242 $ data = $ settings ;
243243 $ data ['labels ' ] = $ labels ;
244- $ brancherApp = $ this ->brancherHypernodeManager ->createForHypernode ($ parentApp , $ data );
245-
246- $ this ->log ->info (sprintf ('Successfully requested brancher Hypernode, name is %s. ' , $ brancherApp ));
247- $ server ->setHostname (sprintf ("%s.hypernode.io " , $ brancherApp ));
248- $ this ->brancherHypernodesRegistered [] = $ brancherApp ;
249-
250- try {
251- $ this ->log ->info ('Waiting for brancher Hypernode to become available... ' );
252- $ this ->brancherHypernodeManager ->waitForAvailability ($ brancherApp );
253- $ this ->log ->info ('Brancher Hypernode has become available! ' );
254- } catch (CreateBrancherHypernodeFailedException | TimeoutException $ e ) {
255- $ this ->brancherHypernodeManager ->cancel ($ brancherApp );
256- throw $ e ;
244+ if ($ reuseBrancher && $ brancherApp = $ this ->brancherHypernodeManager ->reuseExistingBrancherHypernode ($ parentApp , $ labels )) {
245+ $ this ->log ->info (sprintf ('Found existing brancher Hypernode, name is %s. ' , $ brancherApp ));
246+ $ server ->setHostname (sprintf ("%s.hypernode.io " , $ brancherApp ));
247+ } else {
248+ $ brancherApp = $ this ->brancherHypernodeManager ->createForHypernode ($ parentApp , $ data );
249+ $ this ->log ->info (sprintf ('Successfully requested brancher Hypernode, name is %s. ' , $ brancherApp ));
250+ $ server ->setHostname (sprintf ("%s.hypernode.io " , $ brancherApp ));
251+ $ this ->brancherHypernodesRegistered [] = $ brancherApp ;
252+
253+ try {
254+ $ this ->log ->info ('Waiting for brancher Hypernode to become available... ' );
255+ $ this ->brancherHypernodeManager ->waitForAvailability ($ brancherApp );
256+ $ this ->log ->info ('Brancher Hypernode has become available! ' );
257+ } catch (CreateBrancherHypernodeFailedException | TimeoutException $ e ) {
258+ $ this ->brancherHypernodeManager ->cancel ($ brancherApp );
259+ throw $ e ;
260+ }
257261 }
258262 }
259263 }
0 commit comments