Skip to content

Commit f74f7ff

Browse files
Merge pull request #81 from ByteInternet/reuse-brancher
2 parents 5d57faa + 7531ce9 commit f74f7ff

File tree

7 files changed

+64
-26
lines changed

7 files changed

+64
-26
lines changed

ci/test/run-brancher.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ $DP test -f deployment-report.json
5252
$DP jq . deployment-report.json
5353
$DP jq .version deployment-report.json -r -e
5454
$DP jq .stage deployment-report.json -r -e
55-
$DP jq .hostnames[0] deployment-report.json -r -e
55+
BRANCHER_INSTANCE=$($DP jq .hostnames[0] deployment-report.json -r -e)
5656
$DP jq .brancher_hypernodes[0] deployment-report.json -r -e
5757

58+
# Run another test by reusing the last instance
59+
$DP hypernode-deploy deploy test -f /web/deploy.php -vvv --reuse-brancher
60+
61+
# Hostname of the reused Brancher instance should be the same as the previous one
62+
$DP jq .hostnames[0] deployment-report.json -r -e | grep -F "${BRANCHER_INSTANCE}"
63+
5864
# cleanup data
5965
$DP hypernode-deploy cleanup -vvv
6066

src/Brancher/BrancherHypernodeManager.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ public function queryBrancherHypernodes(string $hypernode, array $labels = []):
5656
return $result;
5757
}
5858

59+
/**
60+
* Query brancher instances for the given Hypernode and label and return the
61+
* most recent Brancher instance name.
62+
*
63+
* @param string $hypernode The parent hypernode to query the Brancher instances from
64+
* @param string[] $labels Labels to match against, may be empty
65+
* @return string|null The found Brancher instance name, or null if none was found
66+
*/
67+
public function reuseExistingBrancherHypernode(string $hypernode, array $labels = []): ?string
68+
{
69+
$brancherHypernodes = $this->queryBrancherHypernodes($hypernode, $labels);
70+
if (count($brancherHypernodes) > 0) {
71+
// Return the last brancher Hypernode, which is the most recently created one
72+
return $brancherHypernodes[count($brancherHypernodes) - 1];
73+
}
74+
75+
return null;
76+
}
77+
5978
/**
6079
* Create brancher Hypernode instance for given Hypernode.
6180
*

src/Command/Build.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ protected function configure()
3333
*/
3434
protected function execute(InputInterface $input, OutputInterface $output)
3535
{
36-
return $this->deployRunner->run($output, 'build', DeployRunner::TASK_BUILD, true, false);
36+
return $this->deployRunner->run($output, 'build', DeployRunner::TASK_BUILD, true, false, false);
3737
}
3838
}

src/Command/ComposerAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ protected function configure()
3434
*/
3535
protected function execute(InputInterface $input, OutputInterface $output)
3636
{
37-
return $this->deployRunner->run($output, 'build', 'deploy:vendors:auth', false, false);
37+
return $this->deployRunner->run($output, 'build', 'deploy:vendors:auth', false, false, false);
3838
}
3939
}

src/Command/Deploy.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\Console\Command\Command;
88
use Symfony\Component\Console\Input\InputArgument;
99
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
1011
use Symfony\Component\Console\Output\OutputInterface;
1112
use Throwable;
1213

@@ -28,6 +29,12 @@ protected function configure()
2829
$this->setName('deploy');
2930
$this->setDescription('Deploy application.');
3031
$this->addArgument('stage', InputArgument::REQUIRED, 'Stage deploy to');
32+
$this->addOption(
33+
'reuse-brancher',
34+
null,
35+
InputOption::VALUE_NONE,
36+
'Reuse the brancher Hypernode from the previous deploy. Only works when using addBrancherServer in your deploy configuration.'
37+
);
3138
}
3239

3340
/**
@@ -40,7 +47,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
4047
$input->getArgument('stage'),
4148
DeployRunner::TASK_DEPLOY,
4249
false,
43-
true
50+
true,
51+
$input->getOption('reuse-brancher')
4452
);
4553

4654
if ($result === 0) {

src/Command/RunTask.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5050
$input->getArgument(self::ARGUMENT_TASK),
5151
$input->getOption(self::OPTION_CONFIGURE_BUILD_STAGE),
5252
$input->getOption(self::OPTION_CONFIGURE_SERVERS),
53+
false
5354
);
5455
}
5556
}

src/DeployRunner.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)