Add hosts lists, inventory, proxy

This commit is contained in:
Anton Medvedev 2017-03-14 20:18:30 +07:00
parent 34c985ec28
commit ba4416cee9
4 changed files with 83 additions and 5 deletions

View File

@ -1,3 +1,40 @@
# Upgrade from 4.x to 5.x
1. Servers to Hosts
* Refactor `server($name, $hostname)` to `host($hostname)`
* Refactor `localServer($name)` to `localhost()`
* Rename `serverList($file)` to `inventory($file)`
2. Configuration options
* Rename `{{server.name}}` to `{{hostname}}`
3. DotArray syntax
In v5 access to nested arrays in config via dot notation was removed.
If you was using it, consider to move to plain config options.
Refactor this:
```php
set('a', ['b' => 1]);
// ...
get('a.b');
```
To:
```php
set('a_b', 1);
// ...
get('a_b');
```
# Upgrade from 3.x to 4.x # Upgrade from 3.x to 4.x
1. Namespace for functions 1. Namespace for functions

View File

@ -22,6 +22,10 @@ class FileLoader
*/ */
public function load($file) public function load($file)
{ {
if (!file_exists($file) || !is_readable($file)) {
throw new ConfigurationException("File `$file` doesn't exists or doesn't readable.");
}
$data = Yaml::parse(file_get_contents($file)); $data = Yaml::parse(file_get_contents($file));
if (!is_array($data)) { if (!is_array($data)) {

26
src/Utility/Proxy.php Normal file
View File

@ -0,0 +1,26 @@
<?php
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Utility;
class Proxy
{
private $objects;
public function __construct(array $objects)
{
$this->objects = $objects;
}
public function __call($name, $arguments)
{
foreach ($this->objects as $object) {
$object->$name(...$arguments);
}
return $this;
}
}

View File

@ -13,6 +13,7 @@ use Deployer\Task\Context;
use Deployer\Task\GroupTask; use Deployer\Task\GroupTask;
use Deployer\Task\Task as T; use Deployer\Task\Task as T;
use Deployer\Type\Result; use Deployer\Type\Result;
use Deployer\Utility\Proxy;
use Monolog\Logger; use Monolog\Logger;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -30,12 +31,22 @@ use Symfony\Component\Finder\Finder;
// execution stage. They are acts like two different function, but have same name. Example of such function // execution stage. They are acts like two different function, but have same name. Example of such function
// is set() func. This function determine in which stage it was called by Context::get() method. // is set() func. This function determine in which stage it was called by Context::get() method.
function host($hostname, $alias = null) function host(...$hostnames)
{ {
$deployer = Deployer::get(); $deployer = Deployer::get();
$host = new Host($hostname);
$deployer->hosts->set($alias ?: $hostname, $host); if (count($hostnames) === 1) {
return $host; $host = new Host($hostnames[0]);
$deployer->hosts->set($hostnames[0], $host);
return $host;
} else {
$hosts = array_map(function ($hostname) use ($deployer) {
$host = new Host($hostname);
$deployer->hosts->set($hostname, $host);
return $host;
}, $hostnames);
return new Proxy($hosts);
}
} }
function localhost($alias = 'localhost') function localhost($alias = 'localhost')
@ -51,7 +62,7 @@ function localhost($alias = 'localhost')
* *
* @param string $file * @param string $file
*/ */
function hostsList($file) function inventory($file)
{ {
$deployer = Deployer::get(); $deployer = Deployer::get();
$fileLoader = new FileLoader(); $fileLoader = new FileLoader();