GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 0e985d...08b4ed )
by Stuart
05:49
created

UsingHost::stopAllScreens()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4286
cc 2
eloc 7
nc 2
nop 0
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 DataSift\Storyplayer\OsLib;
47
use DataSift\Stone\ObjectLib\BaseObject;
48
49
use Storyplayer\SPv2\Modules\Exceptions;
50
51
/**
52
 * do things with vagrant
53
 *
54
 * @category  Libraries
55
 * @package   Storyplayer/Modules/Host
56
 * @author    Stuart Herbert <[email protected]>
57
 * @copyright 2011-present Mediasift Ltd www.datasift.com
58
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
59
 * @link      http://datasift.github.io/storyplayer
60
 */
61
class UsingHost extends HostBase
62
{
63 View Code Duplication
    public function runCommand($command)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
64
    {
65
        // what are we doing?
66
        $log = usingLog()->startAction("run command '{$command}' on host '{$this->args[0]}'");
67
68
        // make sure we have valid host details
69
        $hostDetails = $this->getHostDetails();
70
71
        // get an object to talk to this host
72
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
73
74
        // run the command in the guest operating system
75
        $result = $host->runCommand($hostDetails, $command);
76
77
        // did the command succeed?
78
        if ($result->didCommandFail()) {
79
            $msg = "command failed with return code '{$result->returnCode}' and output '{$result->output}'";
80
            $log->endAction($msg);
81
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
82
        }
83
84
        // all done
85
        $log->endAction();
86
        return $result;
87
    }
88
89
    public function runCommandAsUser($command, $user)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
90
    {
91
        // what are we doing?
92
        $log = usingLog()->startAction("run command '{$command}' as user '{$user}' on host '{$this->args[0]}'");
93
94
        // make sure we have valid host details
95
        $hostDetails = $this->getHostDetails();
96
97
        // get an object to talk to this host
98
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
99
100
        // make a copy of the hostDetails, so that we can override them
101
        $myHostDetails = clone $hostDetails;
102
        $myHostDetails->sshUsername = $user;
103
104
        // run the command in the guest operating system
105
        $result = $host->runCommand($myHostDetails, $command);
106
107
        // did the command succeed?
108
        if ($result->didCommandFail()) {
109
            $msg = "command failed with return code '{$result->returnCode}' and output '{$result->output}'";
110
            $log->endAction($msg);
111
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
112
        }
113
114
        // all done
115
        $log->endAction();
116
        return $result;
117
    }
118
119 View Code Duplication
    public function runCommandAndIgnoreErrors($command)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        // what are we doing?
122
        $log = usingLog()->startAction("run command '{$command}' on host '{$this->args[0]}'");
123
124
        // make sure we have valid host details
125
        $hostDetails = $this->getHostDetails();
126
127
        // get an object to talk to this host
128
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
129
130
        // run the command in the guest operating system
131
        $result = $host->runCommand($hostDetails, $command);
132
133
        // all done
134
        $log->endAction();
135
        return $result;
136
    }
137
138
    public function runCommandAsUserAndIgnoreErrors($command, $user)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
139
    {
140
        // what are we doing?
141
        $log = usingLog()->startAction("run command '{$command}' as user '{$user}' on host '{$this->args[0]}'");
142
143
        // make sure we have valid host details
144
        $hostDetails = $this->getHostDetails();
145
146
        // get an object to talk to this host
147
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
148
149
        // make a copy of the hostDetails, so that we can override them
150
        $myHostDetails = clone $hostDetails;
151
        $myHostDetails->sshUsername = $user;
152
153
        // run the command in the guest operating system
154
        $result = $host->runCommand($myHostDetails, $command);
155
156
        // all done
157
        $log->endAction();
158
        return $result;
159
    }
160
161
    public function startInScreen($sessionName, $commandLine)
162
    {
163
        // what are we doing?
164
        $log = usingLog()->startAction("start screen session '{$sessionName}' ({$commandLine}) on host '{$this->args[0]}'");
165
166
        // do we already have this session running on the host?
167
        expectsHost($this->args[0])->screenIsNotRunning($sessionName);
168
169
        // build up our command to run
170
        $commandLine = 'screen -L -d -m -S "' . $sessionName . '" bash -c "' . $commandLine . '"';
171
172
        // run our command
173
        //
174
        // this creates a detached screen session called $sessionName
175
        $this->runCommand($commandLine);
176
177
        // find the PID of the screen session, for future use
178
        $sessionDetails = fromHost($this->args[0])->getScreenSessionDetails($sessionName);
179
180
        // did the process start, or has it already terminated?
181
        if (empty($sessionDetails->pid)) {
182
            $log->endAction("session failed to start, or command exited quickly");
183
            throw Exceptions::newActionFailedException(__METHOD__, "failed to start session '{$sessionName}'");
184
        }
185
186
        // all done
187
        $log->endAction("session running as PID {$sessionDetails->pid}");
188
    }
189
190 View Code Duplication
    public function stopInScreen($sessionName)
191
    {
192
        // what are we doing?
193
        $log = usingLog()->startAction("stop screen session '{$sessionName}' on host '{$this->args[0]}'");
194
195
        // get the process details
196
        $processDetails = fromHost($this->args[0])->getScreenSessionDetails($sessionName);
197
198
        // stop the process
199
        usingHost($this->args[0])->stopProcess($processDetails->pid);
200
201
        // all done
202
        $log->endAction();
203
    }
204
205
    public function stopAllScreens()
206
    {
207
        // what are we doing?
208
        $log = usingLog()->startAction("stop all running screen sessions on host '{$this->args[0]}'");
209
210
        // get the app details
211
        $processes = fromHost($this->args[0])->getAllScreenSessions();
212
213
        // stop the process
214
        foreach ($processes as $processDetails) {
215
            usingHost($this->args[0])->stopProcess($processDetails->pid);
216
            usingProcessesTable()->removeProcess($this->args[0], $processDetails);
217
        }
218
219
        // all done
220
        $log->endAction("stopped " . count($processes) . " session(s)");
221
    }
222
223
    public function stopProcess($pid, $grace = 5)
224
    {
225
        // what are we doing?
226
        $log = usingLog()->startAction("stop process '{$pid}' on host '{$this->args[0]}'");
227
228
        // is the process running at all?
229
        if (!fromHost($this->args[0])->getPidIsRunning($pid)) {
230
            $log->endAction("process is not running");
231
            return;
232
        }
233
234
        // yes it is, so stop it
235
        // send a TERM signal to the screen session
236
        $log->addStep("send SIGTERM to process '{$pid}'", function() use ($pid) {
237 View Code Duplication
            if ($this->getIsLocalhost()) {
238
                posix_kill($pid, SIGTERM);
239
            }
240
            else {
241
                usingHost($this->args[0])->runCommand("kill {$pid}");
242
            }
243
        });
244
245
        // has this worked?
246
        $isStopped = $log->addStep("wait for process to terminate", function() use($pid, $grace, $log) {
247
            for($i = 0; $i < $grace; $i++) {
248
                if (!fromHost($this->args[0])->getPidIsRunning($pid)) {
249
                    return true;
250
                }
251
252
                // process still exists
253
                sleep(1);
254
            }
255
256
            return false;
257
        });
258
259
        // did the process stop?
260
        if ($isStopped) {
261
            $log->endAction();
262
            return;
263
        }
264
265
        $log->addStep("send SIGKILL to process '{$pid}'", function() use($pid) {
266 View Code Duplication
            if ($this->getIsLocalhost()) {
267
                posix_kill($pid, SIGKILL);
268
            }
269
            else {
270
                usingHost($this->args[0])->runCommand("kill -9 {$pid}");
271
            }
272
            sleep(1);
273
        });
274
275
        // success?
276
        if (fromHost($this->args[0])->getProcessIsRunning($pid)) {
277
            $log->endAction("process is still running :(");
278
            throw Exceptions::newActionFailedException(__METHOD__);
279
        }
280
281
        // all done
282
        $log->endAction("process has finished");
283
    }
284
285
    public function uploadFile($sourceFilename, $destFilename)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
286
    {
287
        // what are we doing?
288
        $log = usingLog()->startAction("upload file '{$sourceFilename}' to '{$this->args[0]}':'{$destFilename}'");
289
290
        // does the source file exist?
291
        if (!is_file($sourceFilename)) {
292
            $log->endAction("file '{$sourceFilename}' not found :(");
293
            throw Exceptions::newActionFailedException(__METHOD__);
294
        }
295
296
        // make sure we have valid host details
297
        $hostDetails = $this->getHostDetails();
298
299
        // get an object to talk to this host
300
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
301
302
        // upload the file
303
        $result = $host->uploadFile($hostDetails, $sourceFilename, $destFilename);
304
305
        // did the command used to upload succeed?
306
        if ($result->didCommandFail()) {
307
            $msg = "upload failed with return code '{$result->returnCode}' and output '{$result->output}'";
308
            $log->endAction($msg);
309
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
310
        }
311
312
        // all done
313
        $log->endAction();
314
        return $result;
315
    }
316
}
317