|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
|
5
|
|
|
* All rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
8
|
|
|
* modification, are permitted provided that the following conditions |
|
9
|
|
|
* are met: |
|
10
|
|
|
* |
|
11
|
|
|
* * Redistributions of source code must retain the above copyright |
|
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
16
|
|
|
* the documentation and/or other materials provided with the |
|
17
|
|
|
* distribution. |
|
18
|
|
|
* |
|
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
|
20
|
|
|
* contributors may be used to endorse or promote products derived |
|
21
|
|
|
* from this software without specific prior written permission. |
|
22
|
|
|
* |
|
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
35
|
|
|
* |
|
36
|
|
|
* @category Libraries |
|
37
|
|
|
* @package Storyplayer/Modules/Host |
|
38
|
|
|
* @author Stuart Herbert <[email protected]> |
|
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
41
|
|
|
* @link http://datasift.github.io/storyplayer |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
namespace Storyplayer\SPv2\Modules\Host; |
|
45
|
|
|
|
|
46
|
|
|
use Storyplayer\SPv2\Modules\Exceptions; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* |
|
50
|
|
|
* test the state of a (possibly remote) computer |
|
51
|
|
|
* |
|
52
|
|
|
* @category Libraries |
|
53
|
|
|
* @package Storyplayer/Modules/Host |
|
54
|
|
|
* @author Stuart Herbert <[email protected]> |
|
55
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
56
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
57
|
|
|
* @link http://datasift.github.io/storyplayer |
|
58
|
|
|
*/ |
|
59
|
|
|
class ExpectsHost extends HostBase |
|
60
|
|
|
{ |
|
61
|
|
|
public function hostIsRunning() |
|
62
|
|
|
{ |
|
63
|
|
|
// what are we doing? |
|
64
|
|
|
$log = usingLog()->startAction("make sure host '{$this->args[0]}' is running"); |
|
65
|
|
|
|
|
66
|
|
|
// make sure we have valid host details |
|
67
|
|
|
$hostDetails = $this->getHostDetails(); |
|
68
|
|
|
|
|
69
|
|
|
// is it running? |
|
70
|
|
|
$running = fromHost($hostDetails->hostId)->getHostIsRunning(); |
|
71
|
|
|
if (!$running) { |
|
72
|
|
|
$log->endAction(); |
|
73
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, 'host is running', 'host is not running'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// all done |
|
77
|
|
|
$log->endAction(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function hostIsNotRunning() |
|
81
|
|
|
{ |
|
82
|
|
|
// what are we doing? |
|
83
|
|
|
$log = usingLog()->startAction("make sure host '{$this->args[0]}' is not running"); |
|
84
|
|
|
|
|
85
|
|
|
// make sure we have valid host details |
|
86
|
|
|
$hostDetails = $this->getHostDetails(); |
|
87
|
|
|
|
|
88
|
|
|
// is it running? |
|
89
|
|
|
$running = fromHost($hostDetails->hostId)->getHostIsRunning(); |
|
90
|
|
|
if ($running) { |
|
91
|
|
|
$log->endAction(); |
|
92
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, 'host is not running', 'host is running'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// all done |
|
96
|
|
|
$log->endAction(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
public function packageIsInstalled($packageName) |
|
100
|
|
|
{ |
|
101
|
|
|
// what are we doing? |
|
102
|
|
|
$log = usingLog()->startAction("make sure package '{$packageName}' is installed on host '{$this->args[0]}'"); |
|
103
|
|
|
|
|
104
|
|
|
// make sure we have valid host details |
|
105
|
|
|
$hostDetails = $this->getHostDetails(); |
|
106
|
|
|
|
|
107
|
|
|
// is it installed? |
|
108
|
|
|
$details = fromHost($hostDetails->hostId)->getInstalledPackageDetails($packageName); |
|
109
|
|
|
if (!isset($details->version)) { |
|
110
|
|
|
$log->endAction(); |
|
111
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "package installed", "package is not installed"); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// all done |
|
115
|
|
|
$log->endAction(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
View Code Duplication |
public function packageIsNotInstalled($packageName) |
|
119
|
|
|
{ |
|
120
|
|
|
// what are we doing? |
|
121
|
|
|
$log = usingLog()->startAction("make sure package '{$packageName}' is not installed on host '{$this->args[0]}'"); |
|
122
|
|
|
|
|
123
|
|
|
// make sure we have valid host details |
|
124
|
|
|
$hostDetails = $this->getHostDetails(); |
|
125
|
|
|
|
|
126
|
|
|
// is it installed? |
|
127
|
|
|
$details = fromHost($hostDetails->hostId)->getInstalledPackageDetails($packageName); |
|
128
|
|
|
|
|
129
|
|
|
if (isset($details->version)) { |
|
130
|
|
|
$log->endAction(); |
|
131
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "package not installed", "package is installed"); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// all done |
|
135
|
|
|
$log->endAction(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function processIsRunning($processName) |
|
139
|
|
|
{ |
|
140
|
|
|
// what are we doing? |
|
141
|
|
|
$log = usingLog()->startAction("make sure process '{$processName}' is running on host '{$this->args[0]}'"); |
|
142
|
|
|
|
|
143
|
|
|
// make sure we have valid host details |
|
144
|
|
|
$hostDetails = $this->getHostDetails(); |
|
145
|
|
|
|
|
146
|
|
|
// is the process running? |
|
147
|
|
|
$isRunning = fromHost($hostDetails->hostId)->getProcessIsRunning($processName); |
|
148
|
|
|
|
|
149
|
|
|
if (!$isRunning) { |
|
150
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "process '{$processName}' running", "process '{$processName}' is not running"); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// all done |
|
154
|
|
|
$log->endAction(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function processIsNotRunning($processName) |
|
158
|
|
|
{ |
|
159
|
|
|
// what are we doing? |
|
160
|
|
|
$log = usingLog()->startAction("make sure process '{$processName}' is not running on host '{$this->args[0]}'"); |
|
161
|
|
|
|
|
162
|
|
|
// make sure we have valid host details |
|
163
|
|
|
$hostDetails = $this->getHostDetails(); |
|
164
|
|
|
|
|
165
|
|
|
// is the process running? |
|
166
|
|
|
$isRunning = fromHost($hostDetails->hostId)->getProcessIsRunning($processName); |
|
167
|
|
|
|
|
168
|
|
|
if ($isRunning) { |
|
169
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "process '{$processName}' not running", "process '{$processName}' is running"); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// all done |
|
173
|
|
|
$log->endAction(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
View Code Duplication |
public function screenIsRunning($sessionName) |
|
177
|
|
|
{ |
|
178
|
|
|
// what are we doing? |
|
179
|
|
|
$log = usingLog()->startAction("make sure screen session '{$sessionName}' is running on host '{$this->args[0]}'"); |
|
180
|
|
|
|
|
181
|
|
|
// is this session still running? |
|
182
|
|
|
if (fromHost($this->args[0])->getScreenIsRunning($sessionName)) { |
|
183
|
|
|
$log->endAction(); |
|
184
|
|
|
return; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$log->endAction("session is not running"); |
|
188
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "session '{$sessionName}' running", "session '{$sessionName}' not running"); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
View Code Duplication |
public function screenIsNotRunning($sessionName) |
|
192
|
|
|
{ |
|
193
|
|
|
// what are we doing? |
|
194
|
|
|
$log = usingLog()->startAction("make sure screen session '{$sessionName}' is not running on host '{$this->args[0]}'"); |
|
195
|
|
|
|
|
196
|
|
|
// get the details |
|
197
|
|
|
if (!fromHost($this->args[0])->getScreenIsRunning($sessionName)) { |
|
198
|
|
|
$log->endAction("session is not running"); |
|
199
|
|
|
return; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$log->endAction("session is running"); |
|
203
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "session not running", "session running as PID {$processDetails->pid}"); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* the old SPv1 call, when this was part of expectsShell() |
|
208
|
|
|
* |
|
209
|
|
|
* @deprecated use $this->screenIsRunning() instead |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $sessionName |
|
212
|
|
|
* name of the screen session to check on |
|
213
|
|
|
* @return void |
|
214
|
|
|
*/ |
|
215
|
|
|
public function isRunningInScreen($sessionName) |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->screenIsRunning($sessionName); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* the old SPv1 call, when this was part of expectsShell() |
|
222
|
|
|
* |
|
223
|
|
|
* @deprecated use $this->screenIsNotRunning() instead |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $sessionName |
|
226
|
|
|
* name of the screen session to check on |
|
227
|
|
|
* @return void |
|
228
|
|
|
*/ |
|
229
|
|
|
public function isNotRunningInScreen($sessionName) |
|
230
|
|
|
{ |
|
231
|
|
|
return $this->screenIsNotRunning($sessionName); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
View Code Duplication |
public function hasFileWithPermissions($filename, $owner, $group, $mode) |
|
235
|
|
|
{ |
|
236
|
|
|
// shorthand |
|
237
|
|
|
$octMode = decoct($mode); |
|
238
|
|
|
|
|
239
|
|
|
// what are we doing? |
|
240
|
|
|
$log = usingLog()->startAction("make sure file '{$filename}' exists on host '{$this->args[0]}' with permissions '{$octMode}' owned by '{$owner}:{$group}'"); |
|
241
|
|
|
|
|
242
|
|
|
// make sure we have valid host details |
|
243
|
|
|
$hostDetails = $this->getHostDetails(); |
|
244
|
|
|
|
|
245
|
|
|
// get the file details |
|
246
|
|
|
$details = fromHost($hostDetails->hostId)->getFileDetails($filename); |
|
247
|
|
|
|
|
248
|
|
|
// validate the details |
|
249
|
|
|
if ($details === null) { |
|
250
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$filename}' exists", "'{$filename}' does not exist"); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
if ($details->type != 'file') { |
|
254
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$filename}' is a file", "'{$filename}' is type '{$details->type}'"); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
if ($details->mode != $mode) { |
|
258
|
|
|
$theirOctMode = decoct($details->mode); |
|
259
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$filename}' has permissions '{$octMode}'", "'{$filename}' has permissions '{$theirOctMode}'"); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
if ($details->user != $owner || $details->group != $group) { |
|
263
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$filename}' has ownership '{$owner}:{$group}'", "'{$filename}' has ownership '{$details->user}:{$details->group}'"); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
// if we get here, then all is good |
|
267
|
|
|
$log->endAction(); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
View Code Duplication |
public function hasFolderWithPermissions($folder, $owner, $group, $mode) |
|
271
|
|
|
{ |
|
272
|
|
|
// shorthand |
|
273
|
|
|
$octMode = decoct($mode); |
|
274
|
|
|
|
|
275
|
|
|
// what are we doing? |
|
276
|
|
|
$log = usingLog()->startAction("make sure folder '{$folder}' exists on host '{$this->args[0]}' with permissions '{$octMode}' owned by '{$owner}:{$group}'"); |
|
277
|
|
|
|
|
278
|
|
|
// make sure we have valid host details |
|
279
|
|
|
$hostDetails = $this->getHostDetails(); |
|
280
|
|
|
|
|
281
|
|
|
// get the file details |
|
282
|
|
|
$details = fromHost($hostDetails->hostId)->getFileDetails($folder); |
|
283
|
|
|
|
|
284
|
|
|
// validate the details |
|
285
|
|
|
if ($details === null) { |
|
286
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$folder}' exists", "'{$folder}' does not exist"); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
if ($details->type != 'dir') { |
|
290
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$folder}' is a file", "'{$folder}' is type '{$details->type}'"); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
if ($details->mode != $mode) { |
|
294
|
|
|
$theirOctMode = decoct($details->mode); |
|
295
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$folder}' has permissions '{$octMode}'", "'{$folder}' has permissions '{$theirOctMode}'"); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
if ($details->user != $owner || $details->group != $group) { |
|
299
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$folder}' has ownership '{$owner}:{$group}'", "'{$folder}' has ownership '{$details->user}:{$details->group}'"); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
// if we get here, then all is good |
|
303
|
|
|
$log->endAction(); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|