1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
<?php
namespace Icinga\Module\Director\Cli;
use Icinga\Cli\Params;
use Icinga\Exception\MissingParameterException;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Data\Exporter;
use Icinga\Module\Director\Data\PropertyMangler;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject;
use InvalidArgumentException;
class ObjectCommand extends Command
{
protected $type;
private $name;
private $object;
private $experimentalFlags = array();
public function init()
{
$this->shiftExperimentalFlags();
}
/**
* Show a specific object
*
* Use this command to show single objects rendered as Icinga 2
* config or in JSON format.
*
* USAGE
*
* icingacli director <type> show <name> [options]
*
* OPTIONS
*
* --resolved Resolve all inherited properties and show a flat
* object
* --json Use JSON format
* --no-pretty JSON is pretty-printed per default. Use this flag
* to enforce un-formatted JSON
* --no-defaults Per default JSON output ships null or default
* values. This flag skips those properties
* --with-services For hosts only, also shows attached services
* --all-services For hosts only, show applied and inherited services
* too
*/
public function showAction()
{
$db = $this->db();
$object = $this->getObject();
$exporter = new Exporter($db);
$resolve = (bool) $this->params->shift('resolved');
$withServices = (bool) $this->params->get('with-services');
$allServices = (bool) $this->params->get('all-services');
if ($withServices) {
if (!$object instanceof IcingaHost) {
$this->fail('--with-services is available for Hosts only');
}
$exporter->enableHostServices();
}
if ($allServices) {
if (!$object instanceof IcingaHost) {
$this->fail('--all-services is available for Hosts only');
}
$exporter->serviceLoader()->resolveHostServices();
}
$exporter->resolveObjects($resolve);
$exporter->showDefaults($this->params->shift('no-defaults', false));
if ($this->params->shift('json')) {
echo $this->renderJson($exporter->export($object), !$this->params->shift('no-pretty'));
} else {
$config = new IcingaConfig($db);
if ($resolve) {
$object = $object::fromPlainObject($object->toPlainObject(true, false, null, false), $db);
}
$object->renderToConfig($config);
if ($withServices) {
foreach ($exporter->serviceLoader()->fetchServicesForHost($object) as $service) {
$service->renderToConfig($config);
}
}
foreach ($config->getFiles() as $filename => $content) {
printf("/** %s **/\n\n", $filename);
echo $content;
}
}
}
/**
* Create a new object
*
* Use this command to create a new Icinga object
*
* USAGE
*
* icingacli director <type> create [<name>] [options]
*
* OPTIONS
*
* --<key> <value> Provide all properties as single command line
* options
* --json Otherwise provide all options as a JSON string
*
* EXAMPLES
*
* icingacli director host create localhost \
* --imports generic-host \
* --address 127.0.0.1 \
* --vars.location 'My datacenter'
*
* icingacli director host create localhost \
* --json '{ "address": "127.0.0.1" }'
*/
public function createAction()
{
$type = $this->getType();
$props = $this->getObjectProperties();
$name = $props['object_name'];
$object = IcingaObject::createByType($type, $props, $this->db());
if ($object->store()) {
printf("%s '%s' has been created\n", $type, $name);
if ($this->hasExperimental('live-creation')) {
if ($this->api()->createObjectAtRuntime($object)) {
echo "Live creation for '$name' succeeded\n";
} else {
echo "Live creation for '$name' succeeded\n";
exit(1);
}
if ($type === 'Host' && $this->hasExperimental('immediate-check')) {
echo "Waiting for check result...";
flush();
if ($res = $this->api()->checkHostAndWaitForResult($name)) {
echo " done\n" . $res->output . "\n";
} else {
echo "TIMEOUT\n";
}
}
}
exit(0);
} else {
printf("%s '%s' has not been created\n", $type, $name);
exit(1);
}
}
/**
* Modify an existing objects properties
*
* Use this command to modify specific properties of an existing
* Icinga object
*
* USAGE
*
* icingacli director <type> set <name> [options]
*
* OPTIONS
*
* --<key> <value> Provide all properties as single command line
* options
* --append-<key> <value> Appends to array values, like `imports`,
* `groups` or `vars.system_owners`
* --remove-<key> [<value>] Remove a specific property, eventually only
* when matching `value`. In case the property is an
* array it will remove just `value` when given
* --json Otherwise provide all options as a JSON string
* --replace Replace all object properties with the given ones
* --auto-create Create the object in case it does not exist
*
* EXAMPLES
*
* icingacli director host set localhost \
* --address 127.0.0.2 \
* --vars.location 'Somewhere else'
*
* icingacli director host set localhost \
* --json '{ "address": "127.0.0.2" }'
*/
public function setAction()
{
$name = $this->getName();
$type = $this->getType();
if ($this->params->shift('auto-create') && ! $this->exists($name)) {
$action = 'created';
$object = $this->create($type, $name);
} else {
$action = 'modified';
$object = $this->getObject();
}
$appends = self::stripPrefixedProperties($this->params, 'append-');
$remove = self::stripPrefixedProperties($this->params, 'remove-');
if ($this->params->shift('replace')) {
$object->replaceWith($this->create($type, $name, $this->remainingParams()));
} else {
$object->setProperties($this->remainingParams());
}
PropertyMangler::appendToArrayProperties($object, $appends);
PropertyMangler::removeProperties($object, $remove);
$this->persistChanges($object, $type, $name, $action);
}
protected function persistChanges(DbObject $object, $type, $name, $action)
{
if ($object->hasBeenModified() && $object->store()) {
printf("%s '%s' has been %s\n", $type, $name, $action);
exit(0);
}
printf("%s '%s' has not been modified\n", $type, $name);
exit(0);
}
/**
* Delete a specific object
*
* Use this command to delete a single Icinga object
*
* USAGE
*
* icingacli director <type> delete <name>
*
* EXAMPLES
*
* icingacli director host delete localhost2
*
* icingacli director host delete localhost{3..8}
*/
public function deleteAction()
{
$type = $this->getType();
foreach ($this->shiftOneOrMoreNames() as $name) {
if ($this->load($name)->delete()) {
printf("%s '%s' has been deleted\n", $type, $name);
} else {
printf("Something went wrong while deleting %s '%s'\n", $type, $name);
exit(1);
}
$this->object = null;
}
exit(0);
}
/**
* Whether a specific object exists
*
* Use this command to find out whether a single Icinga object exists
*
* USAGE
*
* icingacli director <type> exists <name>
*/
public function existsAction()
{
$name = $this->getName();
$type = $this->getType();
if ($this->exists($name)) {
printf("%s '%s' exists\n", $type, $name);
exit(0);
} else {
printf("%s '%s' does not exist\n", $type, $name);
exit(1);
}
}
/**
* Clone an existing object
*
* Use this command to clone a specific object
*
* USAGE
*
* icingacli director <type> clone <name> --from <original> [options]
*
* OPTIONS
* --from <original> The name of the object you want to clone
* --<key> <value> Override specific properties while cloning
* --replace In case an object <name> already exists replace
* it with the clone
* --flat Do no keep inherited properties but create a flat
* object with all resolved/inherited properties
*
* EXAMPLES
*
* icingacli director host clone localhost2 --from localhost
*
* icingacli director host clone localhost{3..8} --from localhost2
*
* icingacli director host clone localhost3 --from localhost \
* --address 127.0.0.3
*/
public function cloneAction()
{
$fromName = $this->params->shiftRequired('from');
$from = $this->load($fromName);
// $name = $this->getName();
$type = $this->getType();
$resolve = $this->params->shift('flat');
$replace = $this->params->shift('replace');
$from->setProperties($this->remainingParams());
foreach ($this->shiftOneOrMoreNames() as $name) {
$object = $from::fromPlainObject(
$from->toPlainObject($resolve),
$from->getConnection()
);
$object->set('object_name', $name);
if ($replace && $this->exists($name)) {
$object = $this->load($name)->replaceWith($object);
}
if ($object->hasBeenModified() && $object->store()) {
printf("%s '%s' has been cloned from %s\n", $type, $name, $fromName);
} else {
printf("%s '%s' has not been modified\n", $this->getType(), $name);
}
}
exit(0);
}
protected static function stripPrefixedProperties(Params $params, $prefix = 'append-')
{
$appends = [];
$len = strlen($prefix);
foreach ($params->getParams() as $key => $value) {
if (substr($key, 0, $len) === $prefix) {
$appends[substr($key, $len)] = $value;
}
}
foreach ($appends as $key => $value) {
$params->shift("$prefix$key");
}
return $appends;
}
protected function getObjectProperties()
{
$name = $this->params->shift();
$props = $this->remainingParams();
if (! array_key_exists('object_type', $props)) {
$props['object_type'] = 'object';
}
// Normalize object_name, compare to given name
if ($name) {
if (array_key_exists('object_name', $props)) {
if ($name !== $props['object_name']) {
$this->fail(sprintf(
"Name '%s' conflicts with object_name '%s'\n",
$name,
$props['object_name']
));
}
} else {
$props['object_name'] = $name;
}
} else {
if (! array_key_exists('object_name', $props)) {
$this->fail('Cannot create an object with at least an object name');
}
}
return $props;
}
protected function shiftOneOrMoreNames()
{
$names = array();
while ($name = $this->params->shift()) {
$names[] = $name;
}
if (empty($names)) {
throw new MissingParameterException('Required object name is missing');
}
return $names;
}
protected function remainingParams()
{
if ($json = $this->params->shift('json')) {
if ($json === true) {
$json = $this->readFromStdin();
if ($json === null) {
$this->fail('Please pass JSON either via STDIN or via --json');
}
}
return (array) $this->parseJson($json);
} else {
return $this->params->getParams();
}
}
protected function readFromStdin()
{
if (!defined('STDIN')) {
define('STDIN', fopen('php://stdin', 'r'));
}
$inputIsTty = function_exists('posix_isatty') && posix_isatty(STDIN);
if ($inputIsTty) {
return null;
}
$stdin = file_get_contents('php://stdin');
if (strlen($stdin) === 0) {
return null;
}
return $stdin;
}
protected function exists($name)
{
return IcingaObject::existsByType(
$this->getType(),
$name,
$this->db()
);
}
protected function load($name)
{
return IcingaObject::loadByType(
$this->getType(),
$name,
$this->db()
);
}
protected function create($type, $name, $properties = [])
{
return IcingaObject::createByType($type, $properties + [
'object_type' => 'object',
'object_name' => $name
], $this->db());
}
/**
* @return IcingaObject
*/
protected function getObject()
{
if ($this->object === null) {
$this->object = $this->load($this->getName());
}
return $this->object;
}
protected function getType()
{
if ($this->type === null) {
// Extract the command class name...
$className = substr(strrchr(get_class($this), '\\'), 1);
// ...and strip the Command extension
$this->type = substr($className, 0, -7);
}
return $this->type;
}
protected function getName()
{
if ($this->name === null) {
$name = $this->params->shift();
if (! $name) {
throw new InvalidArgumentException('Object name parameter is required');
}
$this->name = $name;
}
return $this->name;
}
protected function hasExperimental($flag)
{
return array_key_exists($flag, $this->experimentalFlags);
}
protected function shiftExperimentalFlags()
{
if ($flags = $this->params->shift('experimental')) {
foreach (preg_split('/,/', $flags) as $flag) {
$this->experimentalFlags[$flag] = true;
}
}
return $this;
}
}
|