Skip to content

Add interface for specifying brancher labels and settings #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2022
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
38 changes: 38 additions & 0 deletions src/BrancherServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Hypernode\DeployConfiguration;

class BrancherServer extends Server
{
public function getLabels(): array
{
return $this->getOptions()[self::OPTION_HN_BRANCHER_LABELS] ?? [];
}

/**
* @param string[] $labels Labels to be applied to the brancher node
* @return $this
*/
public function setLabels(array $labels): self
{
$this->setOption(self::OPTION_HN_BRANCHER_LABELS, $labels);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's wise to add input validation in the client here or should we let the API validate the data?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think so, since this is a very light component, let's add the validation in hypernode-deploy

return $this;
}

public function getSettings(): array
{
return $this->getOptions()[self::OPTION_HN_BRANCHER_SETTINGS] ?? [];
}

/**
* @param array $settings Settings to be applied to the brancher node
* @return $this
*/
public function setSettings(array $settings): self
{
$this->setOption(self::OPTION_HN_BRANCHER_SETTINGS, $settings);
return $this;
}
}
21 changes: 13 additions & 8 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
class Server
{
public const OPTION_HN_BRANCHER = 'hn_brancher';
public const OPTION_HN_BRANCHER_LABELS = 'hn_brancher_labels';
public const OPTION_HN_BRANCHER_SETTINGS = 'hn_brancher_settings';
public const OPTION_HN_PARENT_APP = 'hn_parent_app';

/**
Expand All @@ -20,10 +22,7 @@ class Server
*/
private $roles;

/**
* @var string[]
*/
private $options = [];
private array $options;

/**
* @var string[]
Expand All @@ -32,7 +31,6 @@ class Server

/**
* @param string[] $roles
* @param string[] $options
*/
public function __construct(string $hostname, array $roles = null, array $options = [])
{
Expand Down Expand Up @@ -62,9 +60,6 @@ public function getRoles(): array
return $this->roles;
}

/**
* @return string[]
*/
public function getOptions(): array
{
return $this->options;
Expand All @@ -78,6 +73,16 @@ public function getSshOptions(): array
return $this->sshOptions;
}

/**
* @param string $option
* @param mixed $value
* @return void
*/
protected function setOption(string $option, $value)
{
$this->options[$option] = $value;
}

/**
* @param $options
*/
Expand Down
8 changes: 5 additions & 3 deletions src/Stage.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ public function addServer(
* @param array|null $roles Roles for the server to be applied
* @param array $options Extra host options for Deployer
* @see ServerRole
* @return Server
* @return BrancherServer
*/
public function addBrancherServer(string $appName, array $roles = null, array $options = []): Server
public function addBrancherServer(string $appName, array $roles = null, array $options = []): BrancherServer
{
$brancherOptions = [
Server::OPTION_HN_BRANCHER => true,
Server::OPTION_HN_PARENT_APP => $appName,
Server::OPTION_HN_BRANCHER_LABELS => [],
Server::OPTION_HN_BRANCHER_SETTINGS => [],
];
$options = array_merge($brancherOptions, $options);
$server = new Server('', $roles, $options);
$server = new BrancherServer('', $roles, $options);
$this->servers[] = $server;
return $server;
}
Expand Down
4 changes: 3 additions & 1 deletion templates/deploy.magento2.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
$productionStage->addServer('appname.hypernode.io');

$testStage = $configuration->addStage('test', 'example.com');
$testStage->addBrancherServer('appname');
$testStage->addBrancherServer('appname')
->setLabels(['stage=test', 'ci_ref=' . \getenv('GITHUB_RUN_ID') ?: 'none'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These labels are all formatted as X=Y, but there isn't some validation based on this format right? Any format of label is allowed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any format is allowed indeed

->setSettings(['cron_enabled' => false, 'supervisor_enabled' => false]);

return $configuration;