Total Complexity | 57 |
Total Lines | 666 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like AnsiblePlaybook often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AnsiblePlaybook, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | final class AnsiblePlaybook extends AbstractAnsibleCommand implements AnsiblePlaybookInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var boolean |
||
25 | */ |
||
26 | private $hasInventory = false; |
||
27 | |||
28 | /** |
||
29 | * Executes a command process. |
||
30 | * Returns either exitcode or string output if no callback is given. |
||
31 | * |
||
32 | * @param callable|null $callback |
||
33 | * @return integer|string |
||
34 | */ |
||
35 | public function execute($callback = null) |
||
36 | { |
||
37 | $this->checkInventory(); |
||
38 | |||
39 | return $this->runProcess($callback); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * The play to be executed. |
||
44 | * |
||
45 | * @param string $playbook |
||
46 | * @return AnsiblePlaybookInterface |
||
47 | */ |
||
48 | public function play(string $playbook): AnsiblePlaybookInterface |
||
49 | { |
||
50 | $this->addBaseoption($playbook); |
||
51 | |||
52 | return $this; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Ask for SSH password. |
||
57 | * |
||
58 | * @return AnsiblePlaybookInterface |
||
59 | */ |
||
60 | public function askPass(): AnsiblePlaybookInterface |
||
61 | { |
||
62 | $this->addParameter('--ask-pass'); |
||
63 | |||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Ask for su password. |
||
69 | * |
||
70 | * @return AnsiblePlaybookInterface |
||
71 | */ |
||
72 | public function askSuPass(): AnsiblePlaybookInterface |
||
73 | { |
||
74 | $this->addParameter('--ask-su-pass'); |
||
75 | |||
76 | return $this; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Ask for sudo password. |
||
81 | * |
||
82 | * @return AnsiblePlaybookInterface |
||
83 | */ |
||
84 | public function askBecomePass(): AnsiblePlaybookInterface |
||
85 | { |
||
86 | $this->addParameter('--ask-become-pass'); |
||
87 | |||
88 | return $this; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Ask for vault password. |
||
93 | * |
||
94 | * @return AnsiblePlaybookInterface |
||
95 | */ |
||
96 | public function askVaultPass(): AnsiblePlaybookInterface |
||
97 | { |
||
98 | $this->addParameter('--ask-vault-pass'); |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Enable privilege escalation |
||
105 | * |
||
106 | * @return AnsiblePlaybookInterface |
||
107 | * @see http://docs.ansible.com/ansible/become.html |
||
108 | */ |
||
109 | public function become(): AnsiblePlaybookInterface |
||
110 | { |
||
111 | $this->addParameter('--become'); |
||
112 | |||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Desired sudo user (default=root). |
||
118 | * |
||
119 | * @param string $user |
||
120 | * @return AnsiblePlaybookInterface |
||
121 | */ |
||
122 | public function becomeUser(string $user = 'root'): AnsiblePlaybookInterface |
||
123 | { |
||
124 | $this->addOption('--become-user', $user); |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Don't make any changes; instead, try to predict some of the changes that may occur. |
||
131 | * |
||
132 | * @return AnsiblePlaybookInterface |
||
133 | */ |
||
134 | public function check(): AnsiblePlaybookInterface |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Connection type to use (default=smart). |
||
143 | * |
||
144 | * @param string $connection |
||
145 | * @return AnsiblePlaybookInterface |
||
146 | */ |
||
147 | public function connection(string $connection = 'smart'): AnsiblePlaybookInterface |
||
148 | { |
||
149 | $this->addOption('--connection', $connection); |
||
150 | |||
151 | return $this; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * When changing (small) files and templates, show the |
||
156 | * differences in those files; works great with --check. |
||
157 | * |
||
158 | * @return AnsiblePlaybookInterface |
||
159 | */ |
||
160 | public function diff(): AnsiblePlaybookInterface |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Example: |
||
169 | * ```php |
||
170 | * $ansible = new Ansible()->extraVars('path=/some/path'); |
||
171 | * ``` |
||
172 | * Sends extra variables to Ansible. The $extraVars parameter can be one of the following. |
||
173 | * |
||
174 | * ## Array |
||
175 | * If an array is passed, it must contain the [ 'key' => 'value' ] pairs of the variables. |
||
176 | * |
||
177 | * Example: |
||
178 | * ```php |
||
179 | * $ansible = new Ansible()->playbook()->extraVars(['path' => 'some/path']); |
||
180 | * ``` |
||
181 | * |
||
182 | * ## File |
||
183 | * As Ansible also supports extra vars loaded from an YML file, you can also pass a file path. |
||
184 | * |
||
185 | * Example: |
||
186 | * ```php |
||
187 | * $ansible = new Ansible()->playbook()->extraVars('/path/to/extra/vars.yml'); |
||
188 | * ``` |
||
189 | * |
||
190 | * ## String |
||
191 | * You can also pass the raw extra vars string directly. |
||
192 | |||
193 | * Example: |
||
194 | * ```php |
||
195 | * $ansible = new Ansible()->playbook()->extraVars('path=/some/path'); |
||
196 | * ``` |
||
197 | * |
||
198 | * @param string|array $extraVars |
||
199 | * @return AnsiblePlaybookInterface |
||
200 | */ |
||
201 | public function extraVars($extraVars = ''): AnsiblePlaybookInterface |
||
202 | { |
||
203 | if (empty($extraVars)) |
||
204 | return $this; |
||
205 | |||
206 | // Building the key=>value parameter |
||
207 | if (is_array($extraVars)){ |
||
208 | $vars = []; |
||
209 | foreach ($extraVars as $key => $value){ |
||
210 | $vars[] = sprintf('%s=%s', $key, $value); |
||
211 | } |
||
212 | $this->addOption('--extra-vars', implode(' ', $vars)); |
||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | // Should we consider $extraVars as a JSON/YML file? |
||
217 | if (@is_file($extraVars)) { |
||
218 | $this->addOption('--extra-vars', sprintf('@"%s"', $extraVars)); |
||
219 | return $this; |
||
220 | } |
||
221 | |||
222 | // At this point, the only allowed type is string. |
||
223 | if (!is_string($extraVars)) |
||
|
|||
224 | throw new InvalidArgumentException(sprintf('Expected string|array, got "%s"', gettype($extraVars))); |
||
225 | |||
226 | |||
227 | if (strpos($extraVars, '=') === false) { |
||
228 | throw new InvalidArgumentException('The extra vars raw string should be in the "key=value" form.'); |
||
229 | } |
||
230 | |||
231 | $this->addOption('--extra-vars', $extraVars); |
||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Run handlers even if a task fails. |
||
237 | * |
||
238 | * @return AnsiblePlaybookInterface |
||
239 | */ |
||
240 | public function forceHandlers(): AnsiblePlaybookInterface |
||
241 | { |
||
242 | $this->addParameter('--force-handlers'); |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Specify number of parallel processes to use (default=5). |
||
249 | * |
||
250 | * @param int $forks |
||
251 | * @return AnsiblePlaybookInterface |
||
252 | */ |
||
253 | public function forks(int $forks = 5): AnsiblePlaybookInterface |
||
254 | { |
||
255 | $this->addOption('--forks', $forks); |
||
256 | |||
257 | return $this; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Show help message and exit. |
||
262 | * |
||
263 | * @return AnsiblePlaybookInterface |
||
264 | */ |
||
265 | public function help(): AnsiblePlaybookInterface |
||
266 | { |
||
267 | $this->addParameter('--help'); |
||
268 | |||
269 | return $this; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Specify inventory host file (default=/etc/ansible/hosts). |
||
274 | * |
||
275 | * @param string $inventory filename for hosts file |
||
276 | * @return AnsiblePlaybookInterface |
||
277 | */ |
||
278 | public function inventoryFile(string $inventory = '/etc/ansible/hosts'): AnsiblePlaybookInterface |
||
279 | { |
||
280 | $this->addOption('--inventory-file', $inventory); |
||
281 | $this->hasInventory = true; |
||
282 | |||
283 | return $this; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Further limit selected hosts to an additional pattern. |
||
288 | * |
||
289 | * @param array|string $subset list of hosts |
||
290 | * @return AnsiblePlaybookInterface |
||
291 | */ |
||
292 | public function limit($subset = ''): AnsiblePlaybookInterface |
||
293 | { |
||
294 | $subset = $this->checkParam($subset, ','); |
||
295 | |||
296 | $this->addOption('--limit', $subset); |
||
297 | |||
298 | return $this; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Outputs a list of matching hosts; does not execute anything else. |
||
303 | * |
||
304 | * @return AnsiblePlaybookInterface |
||
305 | */ |
||
306 | public function listHosts(): AnsiblePlaybookInterface |
||
307 | { |
||
308 | $this->addParameter('--list-hosts'); |
||
309 | |||
310 | return $this; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * List all tasks that would be executed. |
||
315 | * |
||
316 | * @return AnsiblePlaybookInterface |
||
317 | */ |
||
318 | public function listTasks(): AnsiblePlaybookInterface |
||
319 | { |
||
320 | $this->addParameter('--list-tasks'); |
||
321 | |||
322 | return $this; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Specify path(s) to module library (default=/usr/share/ansible/). |
||
327 | * |
||
328 | * @param array $path list of paths for modules |
||
329 | * @return AnsiblePlaybookInterface |
||
330 | */ |
||
331 | public function modulePath(array $path = ['/usr/share/ansible/']): AnsiblePlaybookInterface |
||
332 | { |
||
333 | $this->addOption('--module-path', implode(',', $path)); |
||
334 | |||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Disable cowsay |
||
340 | * |
||
341 | * @codeCoverageIgnore |
||
342 | * @return AnsiblePlaybookInterface |
||
343 | */ |
||
344 | public function noCows(): AnsiblePlaybookInterface |
||
345 | { |
||
346 | $this->processBuilder->setEnv('ANSIBLE_NOCOWS', 1); |
||
347 | |||
348 | return $this; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Enable/Disable Colors |
||
353 | * |
||
354 | * @param bool $colors |
||
355 | * @return AnsiblePlaybookInterface |
||
356 | */ |
||
357 | public function colors(bool $colors = true): AnsiblePlaybookInterface |
||
358 | { |
||
359 | $this->processBuilder->setEnv('ANSIBLE_FORCE_COLOR', intval($colors)); |
||
360 | |||
361 | return $this; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Use this file to authenticate the connection. |
||
366 | * |
||
367 | * @param string $file private key file |
||
368 | * @return AnsiblePlaybookInterface |
||
369 | */ |
||
370 | public function privateKey(string $file): AnsiblePlaybookInterface |
||
371 | { |
||
372 | $this->addOption('--private-key', $file); |
||
373 | |||
374 | return $this; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Only run plays and tasks whose tags do not match these values. |
||
379 | * |
||
380 | * @param array|string $tags list of tags to skip |
||
381 | * @return AnsiblePlaybookInterface |
||
382 | */ |
||
383 | public function skipTags($tags = ''): AnsiblePlaybookInterface |
||
384 | { |
||
385 | $tags = $this->checkParam($tags, ','); |
||
386 | $this->addOption('--skip-tags', $tags); |
||
387 | |||
388 | return $this; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Start the playbook at the task matching this name. |
||
393 | * |
||
394 | * @param string $task name of task |
||
395 | * @return AnsiblePlaybookInterface |
||
396 | */ |
||
397 | public function startAtTask(string $task): AnsiblePlaybookInterface |
||
398 | { |
||
399 | $this->addOption('--start-at-task', $task); |
||
400 | |||
401 | return $this; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * One-step-at-a-time: confirm each task before running. |
||
406 | * |
||
407 | * @return AnsiblePlaybookInterface |
||
408 | */ |
||
409 | public function step(): AnsiblePlaybookInterface |
||
410 | { |
||
411 | $this->addParameter('--step'); |
||
412 | |||
413 | return $this; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Run operations with su. |
||
418 | * |
||
419 | * @return AnsiblePlaybookInterface |
||
420 | */ |
||
421 | public function su(): AnsiblePlaybookInterface |
||
422 | { |
||
423 | $this->addParameter('--su'); |
||
424 | |||
425 | return $this; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Run operations with su as this user (default=root). |
||
430 | * |
||
431 | * @param string $user |
||
432 | * @return AnsiblePlaybookInterface |
||
433 | */ |
||
434 | public function suUser(string $user = 'root'): AnsiblePlaybookInterface |
||
435 | { |
||
436 | $this->addOption('--su-user', $user); |
||
437 | |||
438 | return $this; |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Perform a syntax check on the playbook, but do not execute it. |
||
443 | * |
||
444 | * @return AnsiblePlaybookInterface |
||
445 | */ |
||
446 | public function syntaxCheck(): AnsiblePlaybookInterface |
||
447 | { |
||
448 | $this->addParameter('--syntax-check'); |
||
449 | |||
450 | return $this; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Only run plays and tasks tagged with these values. |
||
455 | * |
||
456 | * @param string|array $tags list of tags |
||
457 | * @return AnsiblePlaybookInterface |
||
458 | */ |
||
459 | public function tags($tags): AnsiblePlaybookInterface |
||
460 | { |
||
461 | $tags = $this->checkParam($tags, ','); |
||
462 | $this->addOption('--tags', $tags); |
||
463 | |||
464 | return $this; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Override the SSH timeout in seconds (default=10). |
||
469 | * |
||
470 | * @param int $timeout |
||
471 | * @return AnsiblePlaybookInterface |
||
472 | */ |
||
473 | public function timeout(int $timeout = 10): AnsiblePlaybookInterface |
||
474 | { |
||
475 | $this->addOption('--timeout', $timeout); |
||
476 | |||
477 | return $this; |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Connect as this user. |
||
482 | * |
||
483 | * @param string $user |
||
484 | * @return AnsiblePlaybookInterface |
||
485 | */ |
||
486 | public function user(string $user): AnsiblePlaybookInterface |
||
487 | { |
||
488 | $this->addOption('--user', $user); |
||
489 | |||
490 | return $this; |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Vault password file. |
||
495 | * |
||
496 | * @param string $file |
||
497 | * @return AnsiblePlaybookInterface |
||
498 | */ |
||
499 | public function vaultPasswordFile(string $file): AnsiblePlaybookInterface |
||
500 | { |
||
501 | $this->addoption('--vault-password-file', $file); |
||
502 | |||
503 | return $this; |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Verbose mode (vvv for more, vvvv to enable connection debugging). |
||
508 | * |
||
509 | * @param string $verbose |
||
510 | * @return AnsiblePlaybookInterface |
||
511 | */ |
||
512 | public function verbose(string $verbose = 'v'): AnsiblePlaybookInterface |
||
513 | { |
||
514 | $this->addParameter('-' . $verbose); |
||
515 | |||
516 | return $this; |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Show program's version number and exit. |
||
521 | * |
||
522 | * @return AnsiblePlaybookInterface |
||
523 | */ |
||
524 | public function version(): AnsiblePlaybookInterface |
||
525 | { |
||
526 | $this->addParameter('--version'); |
||
527 | |||
528 | return $this; |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * clear the fact cache |
||
533 | * |
||
534 | * @return AnsiblePlaybookInterface |
||
535 | */ |
||
536 | public function flushCache(): AnsiblePlaybookInterface |
||
537 | { |
||
538 | $this->addParameter('--flush-cache'); |
||
539 | |||
540 | return $this; |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * the new vault identity to use for rekey |
||
545 | * |
||
546 | * @param string $vaultId |
||
547 | * @return AnsiblePlaybookInterface |
||
548 | */ |
||
549 | public function newVaultId(string $vaultId): AnsiblePlaybookInterface |
||
550 | { |
||
551 | $this->addOption('--new-vault-id', $vaultId); |
||
552 | |||
553 | return $this; |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * new vault password file for rekey |
||
558 | * |
||
559 | * @param string $passwordFile |
||
560 | * @return AnsiblePlaybookInterface |
||
561 | */ |
||
562 | public function newVaultPasswordFile(string $passwordFile): AnsiblePlaybookInterface |
||
563 | { |
||
564 | $this->addOption('--new-vault-password-file', $passwordFile); |
||
565 | |||
566 | return $this; |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * specify extra arguments to pass to scp only (e.g. -l) |
||
571 | * |
||
572 | * @param string|array $scpExtraArgs |
||
573 | * @return AnsiblePlaybookInterface |
||
574 | */ |
||
575 | public function scpExtraArgs($scpExtraArgs): AnsiblePlaybookInterface |
||
576 | { |
||
577 | $scpExtraArgs = $this->checkParam($scpExtraArgs, ','); |
||
578 | $this->addOption('--scp-extra-args', $scpExtraArgs); |
||
579 | |||
580 | return $this; |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * specify extra arguments to pass to sftp only (e.g. -f, -l) |
||
585 | * |
||
586 | * @param string|array $sftpExtraArgs |
||
587 | * @return AnsiblePlaybookInterface |
||
588 | */ |
||
589 | public function sftpExtraArgs($sftpExtraArgs): AnsiblePlaybookInterface |
||
590 | { |
||
591 | $sftpExtraArgs = $this->checkParam($sftpExtraArgs, ','); |
||
592 | $this->addOption('--sftp-extra-args', $sftpExtraArgs); |
||
593 | |||
594 | return $this; |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * specify common arguments to pass to sftp/scp/ssh (e.g. ProxyCommand) |
||
599 | * |
||
600 | * @param string|array $sshArgs |
||
601 | * @return AnsiblePlaybookInterface |
||
602 | */ |
||
603 | public function sshCommonArgs($sshArgs): AnsiblePlaybookInterface |
||
604 | { |
||
605 | $sshArgs = $this->checkParam($sshArgs, ','); |
||
606 | $this->addOption('--ssh-common-args', $sshArgs); |
||
607 | |||
608 | return $this; |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * specify extra arguments to pass to ssh only (e.g. -R) |
||
613 | * |
||
614 | * @param string|array $extraArgs |
||
615 | * @return AnsiblePlaybookInterface |
||
616 | */ |
||
617 | public function sshExtraArgs($extraArgs): AnsiblePlaybookInterface |
||
618 | { |
||
619 | $extraArgs = $this->checkParam($extraArgs, ','); |
||
620 | $this->addOption('--ssh-extra-args', $extraArgs); |
||
621 | |||
622 | return $this; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * the vault identity to use |
||
627 | * |
||
628 | * @param string $vaultId |
||
629 | * @return AnsiblePlaybookInterface |
||
630 | */ |
||
631 | public function vaultId(string $vaultId): AnsiblePlaybookInterface |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * Get parameter string which will be used to call ansible. |
||
640 | * |
||
641 | * @param bool $asArray |
||
642 | * @return string|array |
||
643 | */ |
||
644 | public function getCommandlineArguments(bool $asArray = true) |
||
645 | { |
||
646 | $this->checkInventory(); |
||
647 | |||
649 | } |
||
650 | |||
651 | /** |
||
652 | * @inheritDoc |
||
653 | */ |
||
654 | public function rolesPath(string $path): AnsiblePlaybookInterface |
||
655 | { |
||
656 | if (empty($path)) |
||
657 | return $this; |
||
658 | |||
659 | if (!file_exists($path)) |
||
660 | throw new InvalidArgumentException(sprintf('The path "%s" does not exist.', $path)); |
||
661 | |||
662 | $this->processBuilder->setEnv('ANSIBLE_ROLES_PATH', $path); |
||
663 | return $this; |
||
664 | } |
||
665 | |||
666 | /** |
||
667 | * @inheritDoc |
||
668 | */ |
||
669 | public function hostKeyChecking(bool $enable = true): AnsiblePlaybookInterface |
||
670 | { |
||
671 | $enable ? |
||
672 | $flag = 'True' : |
||
673 | $flag = 'False'; |
||
674 | |||
675 | $this->processBuilder->setEnv('ANSIBLE_HOST_KEY_CHECKING', $flag); |
||
676 | return $this; |
||
677 | } |
||
678 | |||
679 | /** |
||
680 | * If no inventory file is given, assume |
||
681 | */ |
||
682 | private function checkInventory(): void |
||
687 | } |
||
688 | } |
||
689 | } |
||
690 |