Skip to content
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

allow to configure deployment commands from env variables #19

Merged
merged 1 commit into from
Dec 21, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Changelog
* Added client `varnish-controller:deploy` for the new Varnish Controller
* The Varnish Controller additionally needs the organization parameter
* The filename must have the `.vcl` extension, it is no longer magically added
* In addition to flags to the command, this command also takes configuration from environment variables starting with `VARNISH_CONTROLLER_`
* Renamed the VAC deployment command from `vcl:deploy` to `vac:deploy`.
* In addition to flags to the command, this command also takes configuration from environment variables starting with `VAC_`

1.x
===
Expand Down
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ $ chmod u+x varnish-plus-cli.phar

## Usage

There is no configuration file for this tool. The expected usage is that you write a makefile or bash script
that calls the tool with the right arguments.
There is no configuration file for this tool. The expected usage is that you provide the necessary arguments as
environment variables or write a makefile or bash script that calls the tool with the right arguments.

### Compile the VCL with twig

Expand All @@ -45,8 +45,18 @@ Run `./varnish-plus-cli.phar vcl:twig:compile --help` for a full explanation of

`varnish-controller:deploy` takes a VCL file and deploys it to a Varnish Controller into the specified group.

You can provide most settings with environment variables:
* VARNISH_CONTROLLER_URI: API domain of the varnish controller
* VARNISH_CONTROLLER_ORGANIZATION: The organization name to use for the login
* VARNISH_CONTROLLER_USERNAME: Username for the login
* VARNISH_CONTROLLER_PASSWORD: Password for the login
* VARNISH_CONTROLLER_VCL_NAME: Filename on the server
* VARNISH_CONTROLLER_VCL_GROUP: Group to use on the server

If the command option is set and the environment variable exists, the option wins.

```bash
$ ./varnish-plus-cli.phar vac:deploy -u https://$HOST --organization $ORGANIZATION --username $USERNAME --password $PASSWORD --vcl-name $VCL_NAME --vcl-group $VCL_GROUP $FILENAME
$ ./varnish-plus-cli.phar varnish-controller:deploy --uri https://localhost --organization my-organization --username my-username --password my-password --vcl-name name-on-server.vcl --vcl-group my-group output.vcl
```

Run `./dist/varnish-plus-cli.phar varnish-controller:deploy --help` for a full explanation of all arguments.
Expand All @@ -55,8 +65,17 @@ Run `./dist/varnish-plus-cli.phar varnish-controller:deploy --help` for a full e

`vac:deploy` takes a VCL file and deploys it to a VAC at the location specified by the vcl name and group.

You can provide most settings with environment variables:
* VAC_URI: API domain of the VAC
* VAC_USERNAME: Username for the login
* VAC_PASSWORD: Password for the login
* VAC_VCL_NAME: Filename on the server
* VAC_VCL_GROUP: Group to use on the server

If the command option is set and the environment variable exists, the option wins.

```bash
$ ./varnish-plus-cli.phar vac:deploy -u https://$HOST --username $USERNAME --password $PASSWORD --vcl-name $VCL_NAME --vcl-group $VCL_GROUP $FILENAME
$ ./varnish-plus-cli.phar vac:deploy --uri https://localhost --username my-username --password my-password --vcl-name name-on-server.vcl --vcl-group my-group output.vcl
```

Run `./dist/varnish-plus-cli.phar vac:deploy --help` for a full explanation of all arguments.
Expand Down
18 changes: 10 additions & 8 deletions src/Command/BaseDeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@

abstract class BaseDeployCommand extends Command
{
protected string $envPrefix;

protected function configure(): void
{
$this
->addArgument('vcl', InputArgument::REQUIRED, 'VCL file to deploy')
->addOption('uri', 'u', InputOption::VALUE_REQUIRED, 'URI to deploy to [required]')
->addOption('username', '', InputOption::VALUE_REQUIRED, 'Username [required]')
->addOption('password', '', InputOption::VALUE_REQUIRED, 'Password [required]')
->addOption('vcl-name', '', InputOption::VALUE_REQUIRED, 'VCL name [required]')
->addOption('vcl-group', '', InputOption::VALUE_REQUIRED, 'VCL group [required]')
->addOption('uri', 'u', InputOption::VALUE_REQUIRED, 'URI to deploy to, default from env variable '.$this->envPrefix.'_URI [required]')
->addOption('username', '', InputOption::VALUE_REQUIRED, 'Username, default from env variable '.$this->envPrefix.'_USERNAME [required]')
->addOption('password', '', InputOption::VALUE_REQUIRED, 'Password, default from env variable '.$this->envPrefix.'_PASSWORD [required]')
->addOption('vcl-name', '', InputOption::VALUE_REQUIRED, 'VCL name, default from env variable '.$this->envPrefix.'_VCL_NAME [required]')
->addOption('vcl-group', '', InputOption::VALUE_REQUIRED, 'VCL group, default from env variable '.$this->envPrefix.'_VCL_GROUP [required]')
->addOption('verify-tls', '', InputOption::VALUE_REQUIRED, 'Specifies TLS verification, true|false|/path/to/certificate. See http://docs.guzzlephp.org/en/stable/request-options.html#verify for possible options', 'true')
;
}
Expand All @@ -39,11 +41,11 @@ protected function getArgumentString(InputInterface $input, string $name): strin
protected function requireStringOption(InputInterface $input, string $name): string
{
$option = $input->getOption($name);
if (!$option) {
throw new InvalidOptionException($name.' is required');
if (!$option && !$option = getenv($this->envPrefix.'_'.mb_strtoupper(str_replace('-', '_', $name)))) {
throw new InvalidOptionException("You need to specify the option {$name} or set the environment variable for it");
}
if (!\is_string($option)) {
throw new InvalidOptionException($name.' must be of type string but is '.\gettype($option));
throw new InvalidOptionException("{$name} must be of type string but is ".\gettype($option));
}

return $option;
Expand Down
1 change: 1 addition & 0 deletions src/Command/VacDeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class VacDeployCommand extends BaseDeployCommand
{
protected function configure(): void
{
$this->envPrefix = 'VAC';
$this
->setName('vac:deploy')
->setDescription('Deploy compiled VCL to legacy Varnish Admin Console (VAC)')
Expand Down
3 changes: 2 additions & 1 deletion src/Command/VarnishControllerDeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ final class VarnishControllerDeployCommand extends BaseDeployCommand
{
protected function configure(): void
{
$this->envPrefix = 'VARNISH_CONTROLLER';
$this
->setName('varnish-controller:deploy')
->setDescription('Deploy compiled VCL to the Varnish Controller')
->addOption('organization', '', InputOption::VALUE_REQUIRED, 'Organization [required]')
->addOption('organization', '', InputOption::VALUE_REQUIRED, 'Organization , default from env variable '.$this->envPrefix.'_ORGANIZATION [required]')
;
parent::configure();
}
Expand Down