Skip to content

Commit

Permalink
Fixes issue deployphp#1671, Range expansion in FileLoader when host.y…
Browse files Browse the repository at this point in the history
…ml is loaded.
  • Loading branch information
loqx committed Sep 3, 2018
1 parent 17ce9a8 commit f15d790
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Changelog



## master
[v6.3.0...master](https://github.com/deployphp/deployer/compare/v6.3.0...master)
- Added a function to Deployer\Host\FileLoader, expandOnLoad, which rebuilds the parsed yaml array with expanded hostnames.
- Updated deployer/test/fixtures/inventory.xml by combining db1.deployer.org and db2.deployer.org into db[1:2].deployer.org in order to test file loading with range.
- Added tests for db1.deployer.org and db2.deployer.org in FileLoaderTest testLoad().
- Added tests in FunctionTest testInventory() function to test for db1.deployer.org and db2.deployer.org because the bug report referenced inventory().

### Changed
- Laravel recipe should not run `artisan:cache:clear` in `deploy` task
Expand Down
21 changes: 20 additions & 1 deletion src/Host/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@ class FileLoader
* @var Host[]
*/
private $hosts = [];
/**
* @param array $datain
* @return array $dataexp
*/
public function expandOnLoad($datain) {
$dataout = array();
foreach ($datain as $hostname => $config) {
if (preg_match('/\[(.+?)\]/', $hostname)) {
foreach(Range::expand([$hostname]) as $splithost) {
$dataout["$splithost"] = $config;
}
} else {
$dataout["$hostname"] = $config;
}

}

return $dataout;
}
/**
* @param string $file
* @return $this
Expand All @@ -29,7 +47,8 @@ public function load($file)
}

$data = Yaml::parse(file_get_contents($file));

$data = $this->expandOnLoad($data);

if (!is_array($data)) {
throw new Exception("Hosts file `$file` should contains array of hosts.");
}
Expand Down
6 changes: 1 addition & 5 deletions test/fixture/inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ app.deployer.org:
roles: app
deploy_path: ~/app

db1.deployer.org:
stage: production
roles: db

db2.deployer.org:
db[1:2].deployer.org:
stage: production
roles: db

Expand Down
2 changes: 1 addition & 1 deletion test/src/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function testInventory()
{
inventory(__DIR__ . '/../fixture/inventory.yml');

foreach (['app.deployer.org', 'beta.deployer.org'] as $hostname) {
foreach (['app.deployer.org', 'beta.deployer.org', 'db1.deployer.org', 'db2.deployer.org'] as $hostname) {
self::assertInstanceOf(Host::class, $this->deployer->hosts->get($hostname));
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/src/Host/FileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function testLoad()
'-f -A -someFlag value -p 22 -F configFile -i identityFile -o Option=Value',
$bar->getSshArguments()->getCliArguments()
);

$db1 = $this->getHost('db1.deployer.org');
self::assertEquals('db1.deployer.org', $db1->getHostname());
$db2 = $this->getHost('db2.deployer.org');
self::assertEquals('db2.deployer.org', $db2->getHostname());
}

/**
Expand Down

0 comments on commit f15d790

Please sign in to comment.