Skip to content

Commit af21795

Browse files
authored
Merge pull request #19 from liip/env
allow to configure deployment commands from env variables
2 parents 74b6599 + c440554 commit af21795

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Changelog
1313
* Added client `varnish-controller:deploy` for the new Varnish Controller
1414
* The Varnish Controller additionally needs the organization parameter
1515
* The filename must have the `.vcl` extension, it is no longer magically added
16+
* In addition to flags to the command, this command also takes configuration from environment variables starting with `VARNISH_CONTROLLER_`
1617
* Renamed the VAC deployment command from `vcl:deploy` to `vac:deploy`.
18+
* In addition to flags to the command, this command also takes configuration from environment variables starting with `VAC_`
1719

1820
1.x
1921
===

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ $ chmod u+x varnish-plus-cli.phar
1919

2020
## Usage
2121

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

2525
### Compile the VCL with twig
2626

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

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

48+
You can provide most settings with environment variables:
49+
* VARNISH_CONTROLLER_URI: API domain of the varnish controller
50+
* VARNISH_CONTROLLER_ORGANIZATION: The organization name to use for the login
51+
* VARNISH_CONTROLLER_USERNAME: Username for the login
52+
* VARNISH_CONTROLLER_PASSWORD: Password for the login
53+
* VARNISH_CONTROLLER_VCL_NAME: Filename on the server
54+
* VARNISH_CONTROLLER_VCL_GROUP: Group to use on the server
55+
56+
If the command option is set and the environment variable exists, the option wins.
57+
4858
```bash
49-
$ ./varnish-plus-cli.phar vac:deploy -u https://$HOST --organization $ORGANIZATION --username $USERNAME --password $PASSWORD --vcl-name $VCL_NAME --vcl-group $VCL_GROUP $FILENAME
59+
$ ./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
5060
```
5161

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

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

68+
You can provide most settings with environment variables:
69+
* VAC_URI: API domain of the VAC
70+
* VAC_USERNAME: Username for the login
71+
* VAC_PASSWORD: Password for the login
72+
* VAC_VCL_NAME: Filename on the server
73+
* VAC_VCL_GROUP: Group to use on the server
74+
75+
If the command option is set and the environment variable exists, the option wins.
76+
5877
```bash
59-
$ ./varnish-plus-cli.phar vac:deploy -u https://$HOST --username $USERNAME --password $PASSWORD --vcl-name $VCL_NAME --vcl-group $VCL_GROUP $FILENAME
78+
$ ./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
6079
```
6180

6281
Run `./dist/varnish-plus-cli.phar vac:deploy --help` for a full explanation of all arguments.

src/Command/BaseDeployCommand.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313

1414
abstract class BaseDeployCommand extends Command
1515
{
16+
protected string $envPrefix;
17+
1618
protected function configure(): void
1719
{
1820
$this
1921
->addArgument('vcl', InputArgument::REQUIRED, 'VCL file to deploy')
20-
->addOption('uri', 'u', InputOption::VALUE_REQUIRED, 'URI to deploy to [required]')
21-
->addOption('username', '', InputOption::VALUE_REQUIRED, 'Username [required]')
22-
->addOption('password', '', InputOption::VALUE_REQUIRED, 'Password [required]')
23-
->addOption('vcl-name', '', InputOption::VALUE_REQUIRED, 'VCL name [required]')
24-
->addOption('vcl-group', '', InputOption::VALUE_REQUIRED, 'VCL group [required]')
22+
->addOption('uri', 'u', InputOption::VALUE_REQUIRED, 'URI to deploy to, default from env variable '.$this->envPrefix.'_URI [required]')
23+
->addOption('username', '', InputOption::VALUE_REQUIRED, 'Username, default from env variable '.$this->envPrefix.'_USERNAME [required]')
24+
->addOption('password', '', InputOption::VALUE_REQUIRED, 'Password, default from env variable '.$this->envPrefix.'_PASSWORD [required]')
25+
->addOption('vcl-name', '', InputOption::VALUE_REQUIRED, 'VCL name, default from env variable '.$this->envPrefix.'_VCL_NAME [required]')
26+
->addOption('vcl-group', '', InputOption::VALUE_REQUIRED, 'VCL group, default from env variable '.$this->envPrefix.'_VCL_GROUP [required]')
2527
->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')
2628
;
2729
}
@@ -39,11 +41,11 @@ protected function getArgumentString(InputInterface $input, string $name): strin
3941
protected function requireStringOption(InputInterface $input, string $name): string
4042
{
4143
$option = $input->getOption($name);
42-
if (!$option) {
43-
throw new InvalidOptionException($name.' is required');
44+
if (!$option && !$option = getenv($this->envPrefix.'_'.mb_strtoupper(str_replace('-', '_', $name)))) {
45+
throw new InvalidOptionException("You need to specify the option {$name} or set the environment variable for it");
4446
}
4547
if (!\is_string($option)) {
46-
throw new InvalidOptionException($name.' must be of type string but is '.\gettype($option));
48+
throw new InvalidOptionException("{$name} must be of type string but is ".\gettype($option));
4749
}
4850

4951
return $option;

src/Command/VacDeployCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ final class VacDeployCommand extends BaseDeployCommand
1515
{
1616
protected function configure(): void
1717
{
18+
$this->envPrefix = 'VAC';
1819
$this
1920
->setName('vac:deploy')
2021
->setDescription('Deploy compiled VCL to legacy Varnish Admin Console (VAC)')

src/Command/VarnishControllerDeployCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ final class VarnishControllerDeployCommand extends BaseDeployCommand
1616
{
1717
protected function configure(): void
1818
{
19+
$this->envPrefix = 'VARNISH_CONTROLLER';
1920
$this
2021
->setName('varnish-controller:deploy')
2122
->setDescription('Deploy compiled VCL to the Varnish Controller')
22-
->addOption('organization', '', InputOption::VALUE_REQUIRED, 'Organization [required]')
23+
->addOption('organization', '', InputOption::VALUE_REQUIRED, 'Organization , default from env variable '.$this->envPrefix.'_ORGANIZATION [required]')
2324
;
2425
parent::configure();
2526
}

0 commit comments

Comments
 (0)