Completed
Pull Request — master (#35)
by
unknown
03:08
created

AnsiblePlaybook::noCows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
/*
3
 * This file is part of the php-ansible package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Asm\Ansible\Command;
12
13
/**
14
 * Class AnsiblePlaybook
15
 *
16
 * @package Asm\Ansible\Command
17
 * @author Marc Aschmann <[email protected]>
18
 */
19
final class AnsiblePlaybook extends AbstractAnsibleCommand implements AnsiblePlaybookInterface
20
{
21
    /**
22
     * @var boolean
23
     */
24
    private $hasInventory = false;
25
26
    /**
27
     * Executes a command process.
28
     * Returns either exitcode or string output if no callback is given.
29
     *
30
     * @param null $callback
31
     * @return integer|string
32
     */
33 1
    public function execute($callback = null)
34
    {
35 1
        $this->checkInventory();
36
37 1
        return $this->runProcess($callback);
38
    }
39
40
    /**
41
     * The play to be executed.
42
     *
43
     * @param string $playbook
44
     * @return $this
45
     */
46 30
    public function play($playbook)
47
    {
48 30
        $this->addBaseoption($playbook);
49
50 30
        return $this;
51
    }
52
53
    /**
54
     * Ask for SSH password.
55
     *
56
     * @return $this
57
     */
58 1
    public function askPass()
59
    {
60 1
        $this->addParameter('--ask-pass');
61
62 1
        return $this;
63
    }
64
65
    /**
66
     * Ask for su password.
67
     *
68
     * @return $this
69
     */
70 1
    public function askSuPass()
71
    {
72 1
        $this->addParameter('--ask-su-pass');
73
74 1
        return $this;
75
    }
76
77
    /**
78
     * Ask for sudo password.
79
     *
80
     * @return $this
81
     */
82 1
    public function askSudoPass()
83
    {
84 1
        $this->addParameter('--ask-sudo-pass');
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * Ask for vault password.
91
     *
92
     * @return $this
93
     */
94 1
    public function askVaultPass()
95
    {
96 1
        $this->addParameter('--ask-vault-pass');
97
98 1
        return $this;
99
    }
100
101
    /**
102
     * Don't make any changes; instead, try to predict some of the changes that may occur.
103
     *
104
     * @return $this
105
     */
106 1
    public function check()
107
    {
108 1
        $this->addParameter('--check');
109
110 1
        return $this;
111
    }
112
113
    /**
114
     * Connection type to use (default=smart).
115
     *
116
     * @param string $connection
117
     * @return $this
118
     */
119 1
    public function connection($connection = 'smart')
120
    {
121 1
        $this->addOption('--connection', $connection);
122
123 1
        return $this;
124
    }
125
126
    /**
127
     * When changing (small) files and templates, show the
128
     * differences in those files; works great with --check.
129
     *
130
     * @return $this
131
     */
132 1
    public function diff()
133
    {
134 1
        $this->addParameter('--diff');
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * Set additional variables as array [ 'key' => 'value' ] or string.
141
     *
142
     * @param string|array $extraVars
143
     * @return $this
144
     */
145 1
    public function extraVars($extraVars = '')
146
    {
147 1
        $extraVars = $this->checkParam($extraVars, ' ');
148 1
        $this->addOption('--extra-vars', $extraVars);
149
150 1
        return $this;
151
    }
152
153
    /**
154
     * Run handlers even if a task fails.
155
     *
156
     * @return $this
157
     */
158 1
    public function forceHandlers()
159
    {
160 1
        $this->addParameter('--force-handlers');
161
162 1
        return $this;
163
    }
164
165
    /**
166
     * Specify number of parallel processes to use (default=5).
167
     *
168
     * @param int $forks
169
     * @return $this
170
     */
171 1
    public function forks($forks = 5)
172
    {
173 1
        $this->addOption('--forks', $forks);
174
175 1
        return $this;
176
    }
177
178
    /**
179
     * Show help message and exit.
180
     *
181
     * @return $this
182
     */
183 1
    public function help()
184
    {
185 1
        $this->addParameter('--help');
186
187 1
        return $this;
188
    }
189
190
    /**
191
     * Specify inventory host file (default=/etc/ansible/hosts).
192
     *
193
     * @param string $inventory filename for hosts file
194
     * @return $this
195
     */
196 1
    public function inventoryFile($inventory = '/etc/ansible/hosts')
197
    {
198 1
        $this->addOption('--inventory-file', $inventory);
199 1
        $this->hasInventory = true;
200
201 1
        return $this;
202
    }
203
204
    /**
205
     * Further limit selected hosts to an additional pattern.
206
     *
207
     * @param array|string $subset list of hosts
208
     * @return $this
209
     */
210 2
    public function limit($subset = '')
211
    {
212 2
        $subset = $this->checkParam($subset, ',');
213
214 2
        $this->addOption('--limit', $subset);
215
216 2
        return $this;
217
    }
218
219
    /**
220
     * Outputs a list of matching hosts; does not execute anything else.
221
     *
222
     * @return $this
223
     */
224 1
    public function listHosts()
225
    {
226 1
        $this->addParameter('--list-hosts');
227
228 1
        return $this;
229
    }
230
231
    /**
232
     * List all tasks that would be executed.
233
     *
234
     * @return $this
235
     */
236 1
    public function listTasks()
237
    {
238 1
        $this->addParameter('--list-tasks');
239
240 1
        return $this;
241
    }
242
243
    /**
244
     * Specify path(s) to module library (default=/usr/share/ansible/).
245
     *
246
     * @param array $path list of paths for modules
247
     * @return $this
248
     */
249 1
    public function modulePath($path = ['/usr/share/ansible/'])
250
    {
251 1
        $this->addOption('--module-path', implode(',', $path));
252
253 1
        return $this;
254
    }
255
256
    /**
257
     * Disable cowsay
258
     *
259
     * @codeCoverageIgnore
260
     * @return $this
261
     */
262
    public function noCows()
263
    {
264
        $this->processBuilder->setEnv('ANSIBLE_NOCOWS', 1);
265
266
        return $this;
267
    }
268
269
    /**
270
     * Disable host key checking
271
     *
272
     * @codeCoverageIgnore
273
     * @return $this
274
     */
275
    public function noHostKeyChecking()
276
    {
277
        $this->processBuilder->setEnv('ANSIBLE_HOST_KEY_CHECKING', 'False');
278
279
        return $this;
280
    }
281
282
    /**
283
     * Use this file to authenticate the connection.
284
     *
285
     * @param string $file private key file
286
     * @return $this
287
     */
288 1
    public function privateKey($file)
289
    {
290 1
        $this->addOption('--private-key', $file);
291
292 1
        return $this;
293
    }
294
295
    /**
296
     * Set an environment var in the Ansible process
297
     *
298
     * @codeCoverageIgnore
299
     * @return $this
300
     */
301
    public function setEnv($var, $value)
302
    {
303
        $this->processBuilder->setEnv($var, $value);
304
305
        return $this;
306
    }
307
308
    /**
309
     * Only run plays and tasks whose tags do not match these values.
310
     *
311
     * @param array|string $tags list of tags to skip
312
     * @return $this
313
     */
314 1
    public function skipTags($tags = '')
315
    {
316 1
        $tags = $this->checkParam($tags, ',');
317
318 1
        $this->addOption('--skip-tags', $tags);
319
320 1
        return $this;
321
    }
322
323
    /**
324
     * Start the playbook at the task matching this name.
325
     *
326
     * @param string $task name of task
327
     * @return $this
328
     */
329 1
    public function startAtTask($task)
330
    {
331 1
        $this->addOption('--start-at-task', $task);
332
333 1
        return $this;
334
    }
335
336
    /**
337
     * One-step-at-a-time: confirm each task before running.
338
     *
339
     * @return $this
340
     */
341 1
    public function step()
342
    {
343 1
        $this->addParameter('--step');
344
345 1
        return $this;
346
    }
347
348
    /**
349
     * Run operations with su.
350
     *
351
     * @return $this
352
     */
353 1
    public function su()
354
    {
355 1
        $this->addParameter('--su');
356
357 1
        return $this;
358
    }
359
360
    /**
361
     * Run operations with su as this user (default=root).
362
     *
363
     * @param string $user
364
     * @return $this
365
     */
366 1
    public function suUser($user = 'root')
367
    {
368 1
        $this->addOption('--su-user', $user);
369
370 1
        return $this;
371
    }
372
373
    /**
374
     * Run operations with sudo (nopasswd).
375
     *
376
     * @return $this
377
     */
378 1
    public function sudo()
379
    {
380 1
        $this->addParameter('--sudo');
381
382 1
        return $this;
383
    }
384
385
    /**
386
     * Desired sudo user (default=root).
387
     *
388
     * @param string $user
389
     * @return $this
390
     */
391 1
    public function sudoUser($user = 'root')
392
    {
393 1
        $this->addOption('--sudo-user', $user);
394
395 1
        return $this;
396
    }
397
398
    /**
399
     * Perform a syntax check on the playbook, but do not execute it.
400
     *
401
     * @return $this
402
     */
403 1
    public function syntaxCheck()
404
    {
405 1
        $this->addParameter('--syntax-check');
406
407 1
        return $this;
408
    }
409
410
    /**
411
     * Only run plays and tasks tagged with these values.
412
     *
413
     * @param string|array $tags list of tags
414
     * @return $this
415
     */
416 1
    public function tags($tags)
417
    {
418 1
        $tags = $this->checkParam($tags, ',');
419
420 1
        $this->addOption('--tags', $tags);
421
422 1
        return $this;
423
    }
424
425
    /**
426
     * Override the SSH timeout in seconds (default=10).
427
     *
428
     * @param int $timeout
429
     * @return $this
430
     */
431 1
    public function timeout($timeout = 10)
432
    {
433 1
        $this->addOption('--timeout', $timeout);
434
435 1
        return $this;
436
    }
437
438
    /**
439
     * Connect as this user.
440
     *
441
     * @param string $user
442
     * @return $this
443
     */
444 2
    public function user($user)
445
    {
446 2
        $this->addOption('--user', $user);
447
448 2
        return $this;
449
    }
450
451
    /**
452
     * Vault password file.
453
     *
454
     * @param string $file
455
     * @return $this
456
     */
457 1
    public function vaultPasswordFile($file)
458
    {
459 1
        $this->addoption('--vault-password-file', $file);
460
461 1
        return $this;
462
    }
463
464
    /**
465
     * Verbose mode (vvv for more, vvvv to enable connection debugging).
466
     *
467
     * @param string $verbose
468
     * @return $this
469
     */
470 1
    public function verbose($verbose = 'v')
471
    {
472 1
        $this->addParameter('-' . $verbose);
473
474 1
        return $this;
475
    }
476
477
    /**
478
     * Get parameter string which will be used to call ansible.
479
     *
480
     * @param bool $asArray
481
     * @return string|array
482
     */
483 30
    public function getCommandlineArguments($asArray = true)
484
    {
485 30
        $this->checkInventory();
486
487 30
        return $this->prepareArguments($asArray);
488
    }
489
490
    /**
491
     * Show program's version number and exit.
492
     *
493
     * @return $this
494
     */
495 1
    public function version()
496
    {
497 1
        $this->addParameter('--version');
498
499 1
        return $this;
500
    }
501
502
    /**
503
     * If no inventory file is given, assume
504
     */
505 31
    private function checkInventory()
506
    {
507 31
        if (!$this->hasInventory) {
508 1
            $inventory = str_replace('.yml', '', $this->getBaseOptions());
509 1
            $this->inventoryFile($inventory);
510 1
        }
511 31
    }
512
}
513