Completed
Pull Request — master (#27)
by
unknown
03:26
created

AnsiblePlaybook::privateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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
     * 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
     * Run operations with sudo (nopasswd).
349
     *
350
     * @return $this
351
     */
352 1
    public function sudo()
353
    {
354 1
        $this->addParameter('--sudo');
355
356 1
        return $this;
357
    }
358
359
    /**
360
     * Desired sudo user (default=root).
361
     *
362
     * @param string $user
363
     * @return $this
364
     */
365 1
    public function sudoUser($user = 'root')
366
    {
367 1
        $this->addOption('--sudo-user', $user);
368
369 1
        return $this;
370
    }
371
372
    /**
373
     * Perform a syntax check on the playbook, but do not execute it.
374
     *
375
     * @return $this
376
     */
377 1
    public function syntaxCheck()
378
    {
379 1
        $this->addParameter('--syntax-check');
380
381 1
        return $this;
382
    }
383
384
    /**
385
     * Only run plays and tasks tagged with these values.
386
     *
387
     * @param string|array $tags list of tags
388
     * @return $this
389
     */
390 1
    public function tags($tags)
391
    {
392 1
        $tags = $this->checkParam($tags, ',');
393
394 1
        $this->addOption('--tags', $tags);
395
396 1
        return $this;
397
    }
398
399
    /**
400
     * Override the SSH timeout in seconds (default=10).
401
     *
402
     * @param int $timeout
403
     * @return $this
404
     */
405 1
    public function timeout($timeout = 10)
406
    {
407 1
        $this->addOption('--timeout', $timeout);
408
409 1
        return $this;
410
    }
411
412
    /**
413
     * Connect as this user.
414
     *
415
     * @param string $user
416
     * @return $this
417
     */
418 2
    public function user($user)
419
    {
420 2
        $this->addOption('--user', $user);
421
422 2
        return $this;
423
    }
424
425
    /**
426
     * Vault password file.
427
     *
428
     * @param string $file
429
     * @return $this
430
     */
431 1
    public function vaultPasswordFile($file)
432
    {
433 1
        $this->addoption('--vault-password-file', $file);
434
435 1
        return $this;
436
    }
437
438
    /**
439
     * Verbose mode (vvv for more, vvvv to enable connection debugging).
440
     *
441
     * @param string $verbose
442
     * @return $this
443
     */
444 1
    public function verbose($verbose = 'v')
445
    {
446 1
        $this->addParameter('-' . $verbose);
447
448 1
        return $this;
449
    }
450
451
    /**
452
     * Get parameter string which will be used to call ansible.
453
     *
454
     * @param bool $asArray
455
     * @return string|array
456
     */
457 30
    public function getCommandlineArguments($asArray = true)
458
    {
459 30
        $this->checkInventory();
460
461 30
        return $this->prepareArguments($asArray);
462
    }
463
464
    /**
465
     * Show program's version number and exit.
466
     *
467
     * @return $this
468
     */
469 1
    public function version()
470
    {
471 1
        $this->addParameter('--version');
472
473 1
        return $this;
474
    }
475
476
    /**
477
     * If no inventory file is given, assume
478
     */
479 31
    private function checkInventory()
480
    {
481 31
        if (!$this->hasInventory) {
482 1
            $inventory = str_replace('.yml', '', $this->getBaseOptions());
483 1
            $this->inventoryFile($inventory);
484 1
        }
485 31
    }
486
}
487