Skip to content

Commit 04f534d

Browse files
authored
Merge pull request #23 from boite/develop
Stablilising
2 parents a4facc5 + 28618be commit 04f534d

10 files changed

Lines changed: 127 additions & 110 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"require": {
1515
"php": ">=5.3.0",
16-
"droid/droid-model": "~1.0",
16+
"droid/droid-model": "~1.3",
1717
"psr/log": "^1.0",
1818
"symfony/property-access": "^3.1",
1919
"symfony/console": "~2.7",

src/Application.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Droid\Model\Inventory\Remote\Enabler;
1111
use Droid\Model\Inventory\Remote\SynchroniserComposer;
1212
use Droid\Model\Inventory\Remote\SynchroniserPhar;
13+
use Droid\Model\Project\Environment;
1314
use Droid\Model\Project\Project;
1415
use Symfony\Component\PropertyAccess\PropertyAccess;
1516
use Symfony\Component\Console\Application as ConsoleApplication;
@@ -51,6 +52,8 @@ public function __construct($autoLoader, $basePath = '')
5152
$this->setVersion('1.0.0');
5253

5354
$this->inventory = new Inventory();
55+
$environment = new Environment;
56+
$this->inventory->setEnvironment($environment);
5457
$this->transformer = $this->buildTransformer();
5558

5659
$loader = new YamlLoader($this->basePath, $this->transformer);
@@ -76,7 +79,7 @@ public function __construct($autoLoader, $basePath = '')
7679
$this->project = new Project($filename);
7780

7881
// Load droid.yml
79-
$loader->load($this->project, $this->inventory);
82+
$loader->load($this->project, $this->inventory, $environment);
8083

8184
$this->loaderErrors = $loader->errors;
8285
if ($this->loaderErrors) {
@@ -167,18 +170,6 @@ protected function registerCustomCommands()
167170
// which is used when using the --help option
168171
$defaultCommands = parent::getDefaultCommands();
169172

170-
if ($this->hasProject()) {
171-
// Register commands defined in project's droid.yml
172-
foreach ($this->getProject()->getRegisteredCommands() as $registeredCommand) {
173-
$className = $registeredCommand->getClassName();
174-
$command = new $className();
175-
if ($registeredCommand->hasProperty('name')) {
176-
$command->setName($registeredCommand->getProperty('name'));
177-
}
178-
$this->add($command);
179-
}
180-
}
181-
182173
// Automatically register commands by scanning namespaces for a 'DroidPlugin' class.
183174
//print_r($this->autoLoader);
184175
$prefixes = $this->autoLoader->getPrefixesPsr4();

src/Command/ConfigCommand.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,35 @@ protected function configure()
2121

2222
public function execute(InputInterface $input, OutputInterface $output)
2323
{
24-
25-
$output->writeln("Droid configuration:");
2624
$project = $this->getApplication()->getProject();
25+
if (! $project) {
26+
return;
27+
}
28+
29+
$header = 'Droid configuration:';
30+
if ($project->name) {
31+
$header .= sprintf(' "%s"', $project->name);
32+
}
33+
$output->writeln($header);
34+
if ($project->description) {
35+
$output->writeln($project->description);
36+
}
2737

28-
$output->writeln("Modules: ");
38+
if ($project->getModules()) {
39+
$output->writeln("Modules: ");
40+
}
2941
foreach ($project->getModules() as $module) {
3042
$output->writeln("<info>Module: " . $module->getName() . "</info> " . $module->getSource());
3143
$output->writeLn(" Description: " . $module->getDescription());
3244
$output->writeln(" Variables: " . $module->getVariablesAsString());
3345
}
34-
46+
3547
foreach ($project->getTargets() as $target) {
3648
$output->writeln("<info>Target: " . $target->getName() . "</info>");
3749
if ($target->getHosts()) {
3850
$output->writeln(" Hosts: " . $target->getHosts());
3951
}
40-
52+
4153
$output->writeln(" Variables: " . $target->getVariablesAsString());
4254
foreach ($target->getModules() as $module) {
4355
$output->writeln(" <comment> Module: <info>" . $module->getName() . "</info></comment>");
@@ -56,6 +68,6 @@ public function execute(InputInterface $input, OutputInterface $output)
5668
}
5769
}
5870
}
59-
$output->writeln("Done: ");
71+
$output->writeln("Done.");
6072
}
6173
}

src/Command/InventoryCommand.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,41 @@ public function execute(InputInterface $input, OutputInterface $output)
2525
$output->writeln("Droid inventory:");
2626
$inventory = $this->getApplication()->getInventory();
2727

28-
foreach ($inventory->getVariables() as $name => $value) {
29-
$output->writeln("- $name=`$value`");
30-
}
31-
3228
$output->writeln("HOSTS");
3329
foreach ($inventory->getHosts() as $host) {
3430
$output->writeln(' <comment>'. $host->getName() . "</comment>");
35-
$output->writeln(" Public: " . $host->getPublicIp() . ':' . $host->getPublicPort());
36-
if ($host->getPrivateIp()) {
37-
$output->writeln(" Private: " . $host->getPrivateIp() . ':' . $host->getPrivatePort());
31+
if ($host->droid_ip) {
32+
$output->writeln(
33+
sprintf(
34+
' Droid socket: %s:%d',
35+
$host->droid_ip,
36+
$host->getConnectionPort()
37+
)
38+
);
39+
}
40+
if ($host->public_ip) {
41+
$output->writeln(
42+
sprintf(
43+
' Public: %s:%d',
44+
$host->public_ip,
45+
$host->getConnectionPort()
46+
)
47+
);
3848
}
39-
40-
$output->writeln(" Auth: " . $host->getAuth());
41-
foreach ($host->getVariables() as $name => $value) {
49+
if ($host->private_ip) {
50+
$output->writeln(
51+
sprintf(
52+
' Private: %s:%d',
53+
$host->private_ip,
54+
$host->getConnectionPort()
55+
)
56+
);
57+
}
58+
59+
foreach ($host->variables as $name => $value) {
60+
if (is_array($value)) {
61+
$value = '{...}';
62+
}
4263
$output->writeln(" - $name=`$value`");
4364
}
4465
}
@@ -53,6 +74,9 @@ public function execute(InputInterface $input, OutputInterface $output)
5374
$output->writeln(trim($hostnames, ' ,'));
5475

5576
foreach ($group->getVariables() as $name => $value) {
77+
if (is_array($value)) {
78+
$value = '{...}';
79+
}
5680
$output->writeln(" - $name=`$value`");
5781
}
5882
}

src/Command/PingCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ protected function initialize(InputInterface $input, OutputInterface $output)
5858

5959
public function execute(InputInterface $input, OutputInterface $output)
6060
{
61-
$hosts = array();
61+
$hosts = null;
6262
if ($input->getArgument('hostname')) {
63-
$hosts[] = $this->getInventory()->getHostsByName($input->getArgument('hostname'));
63+
$hosts = $this->getInventory()->getHostsByName($input->getArgument('hostname'));
6464
} else {
6565
$hosts = $this->getInventory()->getHosts();
6666
}

src/Loader/YamlLoader.php

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use Droid\Model\Inventory\Host;
1010
use Droid\Model\Inventory\HostGroup;
1111
use Droid\Model\Inventory\Inventory;
12+
use Droid\Model\Project\Environment;
1213
use Droid\Model\Project\Module;
1314
use Droid\Model\Project\Project;
14-
use Droid\Model\Project\RegisteredCommand;
1515
use Droid\Model\Project\Target;
1616
use Droid\Model\Project\Task;
1717
use Symfony\Component\Yaml\Exception\ParseException;
@@ -39,9 +39,13 @@ public function __construct(
3939
$this->transformer = $transformer;
4040
}
4141

42-
public function load(Project $project, Inventory $inventory)
43-
{
42+
public function load(
43+
Project $project,
44+
Inventory $inventory,
45+
Environment $environment
46+
) {
4447
$data = $this->loadYaml($project->getConfigFilePath());
48+
$this->loadEnvironment($environment, $data);
4549
$this->loadInventory($inventory, $data);
4650
$this->loadProject($project, $data);
4751
}
@@ -106,18 +110,31 @@ public function loadYaml($filename)
106110

107111
}
108112

109-
private function loadProject(Project $project, $data)
113+
private function loadEnvironment(Environment $environment, $data)
110114
{
111-
$this->loadVariables($data, $project);
115+
if (! array_key_exists('environment', $data)
116+
|| ! is_array($data['environment'])
117+
) {
118+
return;
119+
}
112120

113-
if (isset($data['register'])) {
114-
foreach ($data['register'] as $registerNode) {
115-
foreach ($registerNode as $className => $parameters) {
116-
$command = new RegisteredCommand($className, $parameters);
117-
$project->addRegisteredCommand($command);
118-
}
121+
foreach ($data['environment'] as $k => $v) {
122+
if (property_exists(Environment::class, $k)) {
123+
$environment->$k = $v;
119124
}
120125
}
126+
}
127+
128+
private function loadProject(Project $project, $data)
129+
{
130+
if (array_key_exists('name', $data) && is_string($data['name'])) {
131+
$project->name = $data['name'];
132+
}
133+
if (array_key_exists('description', $data) && is_string($data['description'])) {
134+
$project->description = $data['description'];
135+
}
136+
137+
$this->loadVariables($data, $project);
121138

122139
if (isset($data['modules'])) {
123140
foreach ($data['modules'] as $name => $source) {
@@ -185,7 +202,9 @@ public function loadModule(Module $module)
185202
$filename = $path . '/droid.yml';
186203
$data = $this->loadYaml($filename);
187204
$module->setBasePath($path);
188-
$module->setDescription($data['project']['description']);
205+
if (array_key_exists('description', $data) && is_string($data['description'])) {
206+
$module->setDescription($data['description']);
207+
}
189208
$this->loadVariables($data, $module);
190209
$this->loadTasks($data, $module, 'tasks');
191210
$this->loadTasks($data, $module, 'triggers');
@@ -242,42 +261,35 @@ private function loadHosts(Inventory $inventory, $hosts)
242261
foreach ($hosts as $hostName => $hostData) {
243262
$host = new Host($hostName);
244263
$this->loadRules($host, $hostData);
245-
$inventory->addHost($host);
246-
if (!$hostData) {
247-
continue;
248-
}
249264
foreach ($hostData as $key => $value) {
250265
switch ($key) {
251266
case 'variables':
252267
$this->loadVariables($hostData, $host);
253268
break;
269+
case 'droid_ip':
270+
$host->droid_ip = $value;
271+
break;
272+
case 'droid_port':
273+
if (! is_numeric($value)) {
274+
throw new RuntimeException('Expected numeric droid_port.');
275+
}
276+
$host->droid_port = (int) $value;
277+
break;
254278
case 'public_ip':
255279
$host->public_ip = $value;
256280
break;
257281
case 'private_ip':
258282
$host->private_ip = $value;
259283
break;
260-
case 'public_port':
261-
$host->public_port = $value;
262-
break;
263-
case 'private_port':
264-
$host->private_port = $value;
265-
break;
266284
case 'username':
267285
$host->setUsername($value);
268286
break;
269-
case 'password':
270-
$host->setPassword($value);
271-
break;
272-
case 'auth':
273-
$host->setAuth($value);
287+
case 'firewall_policy':
288+
$host->setFirewallPolicy($value);
274289
break;
275290
case 'keyfile':
276291
$host->setKeyFile(Utils::absoluteFilename($value));
277292
break;
278-
case 'keypass':
279-
$host->setKeyPass($value);
280-
break;
281293
case 'ssh_options':
282294
$host->setSshOptions($value);
283295
break;
@@ -294,6 +306,7 @@ private function loadHosts(Inventory $inventory, $hosts)
294306
throw new RuntimeException("Unknown host property: " . $key);
295307
}
296308
}
309+
$inventory->addHost($host);
297310
}
298311
foreach ($want_gateway as $want => $gateway) {
299312
if (! $inventory->hasHost($gateway)) {
@@ -323,7 +336,7 @@ private function loadHosts(Inventory $inventory, $hosts)
323336
}
324337
if ($incompleteHosts) {
325338
$this->errors[] = sprintf(
326-
'The following hosts fail to meet the minimum requirement that they exhibit an IP address (public_ip): %s.',
339+
'The following hosts fail to meet the minimum requirement that they exhibit an IP address (droid_ip or public_ip): %s.',
327340
implode(', ', $incompleteHosts)
328341
);
329342
}
@@ -335,6 +348,10 @@ private function loadHostGroups(Inventory $inventory, $groups)
335348
$group = new HostGroup($groupName);
336349
$this->loadRules($group, $groupNode);
337350

351+
if (isset($groupNode['firewall_policy'])) {
352+
$group->setFirewallPolicy($groupNode['firewall_policy']);
353+
}
354+
338355
if (isset($groupNode['hosts'])) {
339356
foreach ($groupNode['hosts'] as $hostName) {
340357
if (!$inventory->hasHost($hostName)) {

src/TaskRunner.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,14 @@ public function runTaskRemotely(Task $task, $variables, $hostsExpression)
8585
);
8686
$commandInput = array();
8787
foreach ($taskHosts as $host) {
88-
$perHostVars = array_merge($variables, array('host' => $host));
89-
// Allow host-level variables to override project, module and target variables
90-
$perHostVars = array_merge($perHostVars, $host->getVariables());
88+
$perHostVars = $variables;
89+
// Allow group variables to override target, project and module variables
90+
foreach ($this->app->getInventory()->getHostGroupsByHost($host) as $group) {
91+
$perHostVars = array_replace_recursive($perHostVars, $group->variables);
92+
}
93+
// Allow host variables to override group, target, project and module variables
94+
$perHostVars = array_replace_recursive($perHostVars, $host->variables);
95+
$perHostVars = array_merge($perHostVars, array('host' => $host));
9196
$commandInput[$host->name] = $this
9297
->prepareCommandInput($command, $task->getArguments(), $perHostVars)
9398
;
@@ -183,12 +188,12 @@ public function runTarget($project, $targetName)
183188
}
184189

185190
foreach ($target->getModules() as $module) {
186-
$variables = array_merge($module->variables, $project->variables, $target->variables);
191+
$variables = array_replace_recursive($module->variables, $project->variables, $target->variables);
187192
$variables['mod_path'] = $module->getBasePath();
188193
$this->runTaskList($module, $variables, $target->getHosts());
189194
}
190195

191-
$variables = array_merge($project->variables, $target->variables);
196+
$variables = array_replace_recursive($project->variables, $target->variables);
192197
$this->runTaskList($target, $variables, $target->getHosts());
193198
return 0;
194199
}

src/Transform/InventoryTransformer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ protected function resolve($expression)
9191
if ($this->inventory->hasHostGroup($expression)) {
9292
return $this->inventory->getHostGroup($expression);
9393
}
94-
if ($this->inventory->hasVariable($expression)) {
95-
return $this->inventory->getVariable($expression);
96-
}
9794
return false;
9895
}
9996
}

0 commit comments

Comments
 (0)