|
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/Shell |
|
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\Shell; |
|
45
|
|
|
|
|
46
|
|
|
use DataSift\Storyplayer\OsLib; |
|
47
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
|
48
|
|
|
|
|
49
|
|
|
use Storyplayer\SPv2\Modules\Exceptions; |
|
50
|
|
|
use Storyplayer\SPv2\Modules\Log; |
|
51
|
|
|
use Storyplayer\SPv2\Modules\Host\HostAwareModule; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* run commands on a (possibly) remote host, using the shell |
|
55
|
|
|
* |
|
56
|
|
|
* @category Libraries |
|
57
|
|
|
* @package Storyplayer/Modules/Shell |
|
58
|
|
|
* @author Stuart Herbert <[email protected]> |
|
59
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
60
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
61
|
|
|
* @link http://datasift.github.io/storyplayer |
|
62
|
|
|
*/ |
|
63
|
|
|
class UsingShell extends HostAwareModule |
|
64
|
|
|
{ |
|
65
|
|
View Code Duplication |
public function runCommand($command) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
// what are we doing? |
|
68
|
|
|
$log = Log::usingLog()->startAction("run command '{$command}' on host '{$this->args[0]}'"); |
|
69
|
|
|
|
|
70
|
|
|
// make sure we have valid host details |
|
71
|
|
|
$hostDetails = $this->getHostDetails(); |
|
72
|
|
|
|
|
73
|
|
|
// get an object to talk to this host |
|
74
|
|
|
$host = OsLib::getHostAdapter($this->st, $hostDetails->osName); |
|
75
|
|
|
|
|
76
|
|
|
// run the command in the guest operating system |
|
77
|
|
|
$result = $host->runCommand($hostDetails, $command); |
|
78
|
|
|
|
|
79
|
|
|
// did the command succeed? |
|
80
|
|
|
if ($result->didCommandFail()) { |
|
81
|
|
|
$msg = "command failed with return code '{$result->returnCode}' and output '{$result->output}'"; |
|
82
|
|
|
$log->endAction($msg); |
|
83
|
|
|
throw Exceptions::newActionFailedException(__METHOD__, $msg); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// all done |
|
87
|
|
|
$log->endAction(); |
|
88
|
|
|
return $result; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function runCommandAsUser($command, $user) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
// what are we doing? |
|
94
|
|
|
$log = Log::usingLog()->startAction("run command '{$command}' as user '{$user}' on host '{$this->args[0]}'"); |
|
95
|
|
|
|
|
96
|
|
|
// make sure we have valid host details |
|
97
|
|
|
$hostDetails = $this->getHostDetails(); |
|
98
|
|
|
|
|
99
|
|
|
// get an object to talk to this host |
|
100
|
|
|
$host = OsLib::getHostAdapter($this->st, $hostDetails->osName); |
|
101
|
|
|
|
|
102
|
|
|
// make a copy of the hostDetails, so that we can override them |
|
103
|
|
|
$myHostDetails = clone $hostDetails; |
|
104
|
|
|
$myHostDetails->sshUsername = $user; |
|
105
|
|
|
|
|
106
|
|
|
// run the command in the guest operating system |
|
107
|
|
|
$result = $host->runCommand($myHostDetails, $command); |
|
108
|
|
|
|
|
109
|
|
|
// did the command succeed? |
|
110
|
|
|
if ($result->didCommandFail()) { |
|
111
|
|
|
$msg = "command failed with return code '{$result->returnCode}' and output '{$result->output}'"; |
|
112
|
|
|
$log->endAction($msg); |
|
113
|
|
|
throw Exceptions::newActionFailedException(__METHOD__, $msg); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// all done |
|
117
|
|
|
$log->endAction(); |
|
118
|
|
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function runCommandAndIgnoreErrors($command) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
// what are we doing? |
|
124
|
|
|
$log = Log::usingLog()->startAction("run command '{$command}' on host '{$this->args[0]}'"); |
|
125
|
|
|
|
|
126
|
|
|
// make sure we have valid host details |
|
127
|
|
|
$hostDetails = $this->getHostDetails(); |
|
128
|
|
|
|
|
129
|
|
|
// get an object to talk to this host |
|
130
|
|
|
$host = OsLib::getHostAdapter($this->st, $hostDetails->osName); |
|
131
|
|
|
|
|
132
|
|
|
// run the command in the guest operating system |
|
133
|
|
|
$result = $host->runCommand($hostDetails, $command); |
|
134
|
|
|
|
|
135
|
|
|
// all done |
|
136
|
|
|
$log->endAction(); |
|
137
|
|
|
return $result; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function runCommandAsUserAndIgnoreErrors($command, $user) |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
|
|
// what are we doing? |
|
143
|
|
|
$log = Log::usingLog()->startAction("run command '{$command}' as user '{$user}' on host '{$this->args[0]}'"); |
|
144
|
|
|
|
|
145
|
|
|
// make sure we have valid host details |
|
146
|
|
|
$hostDetails = $this->getHostDetails(); |
|
147
|
|
|
|
|
148
|
|
|
// get an object to talk to this host |
|
149
|
|
|
$host = OsLib::getHostAdapter($this->st, $hostDetails->osName); |
|
150
|
|
|
|
|
151
|
|
|
// make a copy of the hostDetails, so that we can override them |
|
152
|
|
|
$myHostDetails = clone $hostDetails; |
|
153
|
|
|
$myHostDetails->sshUsername = $user; |
|
154
|
|
|
|
|
155
|
|
|
// run the command in the guest operating system |
|
156
|
|
|
$result = $host->runCommand($myHostDetails, $command); |
|
157
|
|
|
|
|
158
|
|
|
// all done |
|
159
|
|
|
$log->endAction(); |
|
160
|
|
|
return $result; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.