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 | ), |
||
49 | ); |
||
50 | } else { |
||
51 | $input = array( |
||
52 | 'ssh' => array( |
||
53 | 4 | 'host' => $input, |
|
54 | ), |
||
55 | ); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | // Defaults |
||
60 | 10 | $input = array_replace_recursive(array( |
|
61 | 'ssh' => array( |
||
62 | 'host' => null, |
||
63 | 'options' => array( |
||
64 | 'RequestTTY' => 'no', // Disable pseudo-tty allocation |
||
65 | ), |
||
66 | 'control' => array( |
||
67 | 'useControlMaster' => true, |
||
68 | ), |
||
69 | 10 | ), |
|
70 | ), $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 | 'ControlPersist' => '5m', |
||
76 | 'closeOnDestruct' => false, |
||
77 | 10 | ), $input['ssh']['control']); |
|
78 | } |
||
79 | |||
80 | 10 | parent::__construct($input, $flags, $iteratorClass); |
|
81 | 8 | } |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 10 | protected function validate($index = null) |
|
87 | { |
||
88 | 10 | if ($index === null || $index === 'ssh') { |
|
89 | 10 | $this->validateSsh(); |
|
90 | } |
||
91 | 8 | } |
|
92 | |||
93 | /** |
||
94 | * @throws \InvalidArgumentException |
||
95 | */ |
||
96 | 10 | protected function validateSsh() |
|
97 | { |
||
98 | 10 | if (!isset($this['ssh']['host']) || empty($this['ssh']['host'])) { |
|
99 | 1 | throw new InvalidArgumentException('property: [ssh][host] is mandatory'); |
|
100 | } |
||
101 | |||
102 | foreach (array( |
||
103 | 9 | 'ControlPath', |
|
104 | 'ControlPersist', |
||
105 | ) as $controlOption) { |
||
106 | 9 | if (isset($this['ssh']['options'][$controlOption])) { |
|
107 | 1 | throw new InvalidArgumentException(sprintf( |
|
108 | 9 | 'The ssh option %s can only be specified in the [ssh][control] section', |
|
109 | $controlOption |
||
110 | )); |
||
111 | } |
||
112 | } |
||
113 | 8 | } |
|
114 | |||
115 | /** |
||
116 | * @return array |
||
117 | */ |
||
118 | 1 | protected function getSshOptionArgs() |
|
119 | { |
||
120 | 1 | $args = array(); |
|
121 | |||
122 | 1 | foreach ($this['ssh']['options'] as $option => $value) { |
|
123 | 1 | $args[] = '-o'; |
|
124 | 1 | $args[] = $option.'='.$value; |
|
125 | } |
||
126 | |||
127 | 1 | return $args; |
|
128 | } |
||
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 | ) { |
||
142 | 2 | $process = $this->getProcessBuilder() |
|
143 | 2 | ->setArguments(array( |
|
144 | 2 | '-O', // Control an active connection multiplexing master process |
|
145 | 2 | 'exit', |
|
146 | 2 | (string)$this |
|
147 | )) |
||
148 | 2 | ->getProcess() |
|
149 | ; |
||
150 | |||
151 | $process |
||
152 | 2 | ->disableOutput() |
|
153 | 2 | ->mustRun() |
|
154 | ; |
||
155 | } |
||
156 | 10 | } |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 5 | public function __toString() |
|
162 | { |
||
163 | 5 | $string = $this['ssh']['host']; |
|
164 | |||
165 | 5 | if (isset($this['ssh']['user'])) { |
|
166 | 1 | $string = sprintf( |
|
167 | 1 | '%s@%s', |
|
168 | 1 | $this['ssh']['user'], |
|
169 | $string |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | 5 | return $string; |
|
174 | } |
||
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() |
|
191 | { |
||
192 | 3 | if ($this->processBuilder === null) { |
|
207 | |||
208 | 3 | protected function isControlMasterEstablished() |
|
228 | |||
229 | /** |
||
230 | * @throws \RuntimeException |
||
231 | */ |
||
232 | 1 | protected function establishControlMaster() |
|
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | 1 | public function run($command) |
|
290 | } |
||
291 |