1 | <?php |
||
27 | final class Ansible |
||
28 | { |
||
29 | |||
30 | const DEFAULT_TIMEOUT = 300; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $playbookCommand; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $galaxyCommand; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $ansibleBaseDir; |
||
46 | |||
47 | /** |
||
48 | * @var integer |
||
49 | */ |
||
50 | private $timeout; |
||
51 | |||
52 | /** |
||
53 | * @param string $ansibleBaseDir base directory of ansible project structure |
||
54 | * @param string $playbookCommand path to playbook executable, default ansible-playbook |
||
55 | * @param string $galaxyCommand path to galaxy executable, default ansible-galaxy |
||
56 | * @throws CommandException |
||
57 | */ |
||
58 | 7 | public function __construct( |
|
59 | $ansibleBaseDir, |
||
60 | $playbookCommand = '', |
||
61 | $galaxyCommand = '' |
||
62 | ) { |
||
63 | 7 | $this->ansibleBaseDir = $this->checkDir($ansibleBaseDir); |
|
64 | 5 | $this->playbookCommand = $this->checkCommand($playbookCommand, 'ansible-playbook'); |
|
65 | 3 | $this->galaxyCommand = $this->checkCommand($galaxyCommand, 'ansible-galaxy'); |
|
66 | |||
67 | 3 | $this->timeout = Ansible::DEFAULT_TIMEOUT; |
|
68 | 3 | } |
|
69 | |||
70 | /** |
||
71 | * AnsiblePlaybook instance creator |
||
72 | * |
||
73 | * @return AnsiblePlaybookInterface |
||
74 | */ |
||
75 | 1 | public function playbook() |
|
76 | { |
||
77 | 1 | return new AnsiblePlaybook( |
|
78 | 1 | $this->createProcess($this->playbookCommand) |
|
79 | 1 | ); |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * AnsibleGalaxy instance creator |
||
84 | * |
||
85 | * @return AnsibleGalaxyInterface |
||
86 | */ |
||
87 | 1 | public function galaxy() |
|
88 | { |
||
89 | 1 | return new AnsibleGalaxy( |
|
90 | 1 | $this->createProcess($this->galaxyCommand) |
|
91 | 1 | ); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Set process timeout in seconds. |
||
96 | * |
||
97 | * @param integer $timeout |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function setTimeout($timeout) |
||
101 | { |
||
102 | $this->timeout = $timeout; |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string $prefix command to execute |
||
109 | * @return ProcessBuilder |
||
110 | */ |
||
111 | 2 | private function createProcess($prefix) |
|
112 | { |
||
113 | 2 | $process = new ProcessBuilder(); |
|
114 | |||
115 | return $process |
||
116 | 2 | ->setPrefix($prefix) |
|
117 | 2 | ->setWorkingDirectory($this->ansibleBaseDir) |
|
118 | 2 | ->setTimeout($this->timeout); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param string $command |
||
123 | * @param string $default |
||
124 | * @return string |
||
125 | * @throws CommandException |
||
126 | */ |
||
127 | 5 | private function checkCommand($command, $default) |
|
147 | |||
148 | /** |
||
149 | * @param string $dir directory to check |
||
150 | * @return string |
||
151 | * @throws CommandException |
||
152 | */ |
||
153 | 7 | private function checkDir($dir) |
|
161 | } |
||
162 |