Passed
Branch master (ac7040)
by Marc
01:56
created

AnsiblePlaybook   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 469
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 37
lcom 3
cbo 2
dl 0
loc 469
ccs 113
cts 113
cp 1
rs 9.44
c 0
b 0
f 0

36 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 6 1
A play() 0 6 1
A askPass() 0 6 1
A askSuPass() 0 6 1
A askBecomePass() 0 6 1
A askVaultPass() 0 6 1
A check() 0 6 1
A connection() 0 6 1
A diff() 0 6 1
A extraVars() 0 7 1
A forceHandlers() 0 6 1
A forks() 0 6 1
A help() 0 6 1
A inventoryFile() 0 7 1
A limit() 0 8 1
A listHosts() 0 6 1
A listTasks() 0 6 1
A modulePath() 0 6 1
A noCows() 0 6 1
A privateKey() 0 6 1
A skipTags() 0 8 1
A startAtTask() 0 6 1
A step() 0 6 1
A su() 0 6 1
A suUser() 0 6 1
A become() 0 6 1
A becomeUser() 0 6 1
A syntaxCheck() 0 6 1
A tags() 0 8 1
A timeout() 0 6 1
A user() 0 6 1
A vaultPasswordFile() 0 6 1
A verbose() 0 6 1
A getCommandlineArguments() 0 6 1
A version() 0 6 1
A checkInventory() 0 7 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 askBecomePass()
83
    {
84 1
        $this->addParameter('--ask-become-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
     * Use this file to authenticate the connection.
271
     *
272
     * @param string $file private key file
273
     * @return $this
274
     */
275 1
    public function privateKey($file)
276
    {
277 1
        $this->addOption('--private-key', $file);
278
279 1
        return $this;
280
    }
281
282
    /**
283
     * Only run plays and tasks whose tags do not match these values.
284
     *
285
     * @param array|string $tags list of tags to skip
286
     * @return $this
287
     */
288 1
    public function skipTags($tags = '')
289
    {
290 1
        $tags = $this->checkParam($tags, ',');
291
292 1
        $this->addOption('--skip-tags', $tags);
293
294 1
        return $this;
295
    }
296
297
    /**
298
     * Start the playbook at the task matching this name.
299
     *
300
     * @param string $task name of task
301
     * @return $this
302
     */
303 1
    public function startAtTask($task)
304
    {
305 1
        $this->addOption('--start-at-task', $task);
306
307 1
        return $this;
308
    }
309
310
    /**
311
     * One-step-at-a-time: confirm each task before running.
312
     *
313
     * @return $this
314
     */
315 1
    public function step()
316
    {
317 1
        $this->addParameter('--step');
318
319 1
        return $this;
320
    }
321
322
    /**
323
     * Run operations with su.
324
     *
325
     * @return $this
326
     */
327 1
    public function su()
328
    {
329 1
        $this->addParameter('--su');
330
331 1
        return $this;
332
    }
333
334
    /**
335
     * Run operations with su as this user (default=root).
336
     *
337
     * @param string $user
338
     * @return $this
339
     */
340 1
    public function suUser($user = 'root')
341
    {
342 1
        $this->addOption('--su-user', $user);
343
344 1
        return $this;
345
    }
346
347
    /**
348
     * Enable privilege escalation
349
     *
350
     * @return $this
351
     * @see http://docs.ansible.com/ansible/become.html
352
     */
353 1
    public function become()
354
    {
355 1
        $this->addParameter('--become');
356
357 1
        return $this;
358
    }
359
360
    /**
361
     * Desired sudo user (default=root).
362
     *
363
     * @param string $user
364
     * @return $this
365
     */
366 1
    public function becomeUser($user = 'root')
367
    {
368 1
        $this->addOption('--become-user', $user);
369
370 1
        return $this;
371
    }
372
373
    /**
374
     * Perform a syntax check on the playbook, but do not execute it.
375
     *
376
     * @return $this
377
     */
378 1
    public function syntaxCheck()
379
    {
380 1
        $this->addParameter('--syntax-check');
381
382 1
        return $this;
383
    }
384
385
    /**
386
     * Only run plays and tasks tagged with these values.
387
     *
388
     * @param string|array $tags list of tags
389
     * @return $this
390
     */
391 1
    public function tags($tags)
392
    {
393 1
        $tags = $this->checkParam($tags, ',');
394
395 1
        $this->addOption('--tags', $tags);
396
397 1
        return $this;
398
    }
399
400
    /**
401
     * Override the SSH timeout in seconds (default=10).
402
     *
403
     * @param int $timeout
404
     * @return $this
405
     */
406 1
    public function timeout($timeout = 10)
407
    {
408 1
        $this->addOption('--timeout', $timeout);
409
410 1
        return $this;
411
    }
412
413
    /**
414
     * Connect as this user.
415
     *
416
     * @param string $user
417
     * @return $this
418
     */
419 2
    public function user($user)
420
    {
421 2
        $this->addOption('--user', $user);
422
423 2
        return $this;
424
    }
425
426
    /**
427
     * Vault password file.
428
     *
429
     * @param string $file
430
     * @return $this
431
     */
432 1
    public function vaultPasswordFile($file)
433
    {
434 1
        $this->addoption('--vault-password-file', $file);
435
436 1
        return $this;
437
    }
438
439
    /**
440
     * Verbose mode (vvv for more, vvvv to enable connection debugging).
441
     *
442
     * @param string $verbose
443
     * @return $this
444
     */
445 1
    public function verbose($verbose = 'v')
446
    {
447 1
        $this->addParameter('-' . $verbose);
448
449 1
        return $this;
450
    }
451
452
    /**
453
     * Get parameter string which will be used to call ansible.
454
     *
455
     * @param bool $asArray
456
     * @return string|array
457
     */
458 30
    public function getCommandlineArguments($asArray = true)
459
    {
460 30
        $this->checkInventory();
461
462 30
        return $this->prepareArguments($asArray);
463
    }
464
465
    /**
466
     * Show program's version number and exit.
467
     *
468
     * @return $this
469
     */
470 1
    public function version()
471
    {
472 1
        $this->addParameter('--version');
473
474 1
        return $this;
475
    }
476
477
    /**
478
     * If no inventory file is given, assume
479
     */
480 31
    private function checkInventory()
481
    {
482 31
        if (!$this->hasInventory) {
483 1
            $inventory = str_replace('.yml', '', $this->getBaseOptions());
484 1
            $this->inventoryFile($inventory);
485 1
        }
486 31
    }
487
}
488