1 | <?php |
||
12 | class Remote extends ValidatableArrayObject implements NodeInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var \Symfony\Component\Process\ProcessBuilder $sshProcessBuilder |
||
16 | */ |
||
17 | protected $processBuilder; |
||
18 | |||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | * |
||
22 | * Built-in properties are: |
||
23 | * [Mandatory] |
||
24 | * [ssh][host] - The hostname or IP address for the ssh connection |
||
25 | * |
||
26 | * [Optional] |
||
27 | * [ssh][user] - The user to use for the ssh connection |
||
28 | * |
||
29 | * [ssh][options] - An associative array of ssh options, see -o option in ssh(1) |
||
30 | * |
||
31 | * [ssh][control][useControlMaster] - Use control master connection? |
||
32 | * [ssh][control][ControlPath] - see ControlPath in ssh_config(5) |
||
33 | * [ssh][control][ControlPersist] - see ControlPersist in ssh_config(5) |
||
34 | * [ssh][control][closeOnDestruct] - Should the control master connection be destroyed when this node is? |
||
35 | * |
||
36 | * @throws \InvalidArgumentException |
||
37 | */ |
||
38 | 10 | public function __construct($input = array(), $flags = 0, $iteratorClass = 'ArrayIterator') |
|
39 | { |
||
40 | // Handle string shortcut setup |
||
41 | 10 | if (is_string($input)) { |
|
42 | 6 | $userHost = explode('@', $input); |
|
43 | 6 | if (count($userHost) === 2) { |
|
44 | $input = array( |
||
45 | 'ssh' => array( |
||
46 | 2 | 'user' => $userHost[0], |
|
47 | 2 | 'host' => $userHost[1], |
|
48 | 2 | ), |
|
49 | 2 | ); |
|
50 | 2 | } else { |
|
51 | $input = array( |
||
52 | 'ssh' => array( |
||
53 | 4 | 'host' => $input, |
|
54 | 4 | ), |
|
55 | 4 | ); |
|
56 | } |
||
57 | 6 | } |
|
58 | |||
59 | // Defaults |
||
60 | 10 | $input = array_replace_recursive(array( |
|
61 | 'ssh' => array( |
||
62 | 10 | 'host' => null, |
|
63 | 'options' => array( |
||
64 | 10 | 'RequestTTY' => 'no', // Disable pseudo-tty allocation |
|
65 | 10 | ), |
|
66 | 'control' => array( |
||
67 | 10 | 'useControlMaster' => true, |
|
68 | 10 | ), |
|
69 | 10 | ), |
|
70 | 10 | ), $input); |
|
71 | |||
72 | 10 | if ($input['ssh']['control']['useControlMaster']) { |
|
73 | 10 | $input['ssh']['control'] = array_merge(array( |
|
74 | 10 | 'ControlPath' => '~/.ssh/tempo_ctl_%r@%h:%p', |
|
75 | 10 | 'ControlPersist' => '5m', |
|
76 | 10 | 'closeOnDestruct' => false, |
|
77 | 10 | ), $input['ssh']['control']); |
|
78 | 10 | } |
|
79 | |||
80 | 10 | parent::__construct($input, $flags, $iteratorClass); |
|
81 | 8 | } |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 10 | protected function validate($index = null) |
|
92 | |||
93 | /** |
||
94 | * @throws \InvalidArgumentException |
||
95 | */ |
||
96 | 10 | protected function validateSsh() |
|
114 | |||
115 | /** |
||
116 | * @return array |
||
117 | */ |
||
118 | 1 | protected function getSshOptionArgs() |
|
129 | |||
130 | /** |
||
131 | * Destroy ControlMaster if nessasary |
||
132 | * @throws \RuntimeException |
||
133 | */ |
||
134 | 10 | public function __destruct() |
|
135 | { |
||
136 | 10 | if (isset($this['ssh']) |
|
137 | 10 | && isset($this['ssh']['control']) |
|
138 | 10 | && $this['ssh']['control']['useControlMaster'] |
|
139 | 10 | && $this['ssh']['control']['closeOnDestruct'] |
|
140 | 10 | && $this->isControlMasterEstablished() |
|
141 | 10 | ) { |
|
142 | 2 | $process = $this->getProcessBuilder() |
|
143 | 2 | ->setArguments(array( |
|
144 | 2 | '-O', // Control an active connection multiplexing master process |
|
145 | 2 | 'exit', |
|
146 | (string)$this |
||
147 | 2 | )) |
|
148 | 2 | ->getProcess() |
|
149 | 2 | ; |
|
150 | |||
151 | $process |
||
152 | 2 | ->disableOutput() |
|
153 | 2 | ->mustRun() |
|
154 | ; |
||
155 | 2 | } |
|
156 | 10 | } |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 5 | public function __toString() |
|
175 | |||
176 | /** |
||
177 | * @param \Symfony\Component\Process\ProcessBuilder $processBuilder |
||
178 | * @return self |
||
179 | */ |
||
180 | 2 | public function setProcessBuilder(ProcessBuilder $processBuilder) |
|
186 | |||
187 | /** |
||
188 | * @return \Symfony\Component\Process\ProcessBuilder |
||
189 | */ |
||
190 | 3 | public function getProcessBuilder() |
|
207 | |||
208 | 3 | protected function isControlMasterEstablished() |
|
209 | { |
||
228 | |||
229 | /** |
||
230 | * @throws \RuntimeException |
||
231 | */ |
||
232 | 1 | protected function establishControlMaster() |
|
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | 1 | public function run($command) |
|
290 | } |
||
291 |