|
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/Screen |
|
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\Screen; |
|
45
|
|
|
|
|
46
|
|
|
use DataSift\Storyplayer\OsLib; |
|
47
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
|
48
|
|
|
|
|
49
|
|
|
use Storyplayer\SPv2\Modules\Exceptions; |
|
50
|
|
|
use Storyplayer\SPv2\Modules\Host; |
|
51
|
|
|
use Storyplayer\SPv2\Modules\Host\HostAwareModule; |
|
52
|
|
|
use Storyplayer\SPv2\Modules\Log; |
|
53
|
|
|
use Storyplayer\SPv2\Modules\Screen; |
|
54
|
|
|
use Storyplayer\SPv2\Modules\Shell; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* do things with (possibly remote) screen sessions |
|
58
|
|
|
* |
|
59
|
|
|
* @category Libraries |
|
60
|
|
|
* @package Storyplayer/Modules/Screen |
|
61
|
|
|
* @author Stuart Herbert <[email protected]> |
|
62
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
63
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
64
|
|
|
* @link http://datasift.github.io/storyplayer |
|
65
|
|
|
*/ |
|
66
|
|
|
class UsingScreen extends HostAwareModule |
|
67
|
|
|
{ |
|
68
|
|
|
public function startScreen($sessionName, $commandLine) |
|
69
|
|
|
{ |
|
70
|
|
|
// what are we doing? |
|
71
|
|
|
$log = Log::usingLog()->startAction("start screen session '{$sessionName}' ({$commandLine}) on host '{$this->args[0]}'"); |
|
72
|
|
|
|
|
73
|
|
|
// do we already have this session running on the host? |
|
74
|
|
|
Screen::expectsHost($this->args[0])->screenIsNotRunning($sessionName); |
|
75
|
|
|
|
|
76
|
|
|
// build up our command to run |
|
77
|
|
|
$commandLine = 'screen -L -d -m -S "' . $sessionName . '" bash -c "' . $commandLine . '"'; |
|
78
|
|
|
|
|
79
|
|
|
// run our command |
|
80
|
|
|
// |
|
81
|
|
|
// this creates a detached screen session called $sessionName |
|
82
|
|
|
Shell::onHost($this->args[0])->runCommand($commandLine); |
|
83
|
|
|
|
|
84
|
|
|
// find the PID of the screen session, for future use |
|
85
|
|
|
$sessionDetails = Screen::fromHost($this->args[0])->getScreenSessionDetails($sessionName); |
|
86
|
|
|
|
|
87
|
|
|
// did the process start, or has it already terminated? |
|
88
|
|
|
if (empty($sessionDetails->pid)) { |
|
89
|
|
|
$log->endAction("session failed to start, or command exited quickly"); |
|
90
|
|
|
throw Exceptions::newActionFailedException(__METHOD__, "failed to start session '{$sessionName}'"); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// all done |
|
94
|
|
|
$log->endAction("session running as PID {$sessionDetails->pid}"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function stopScreen($sessionName) |
|
98
|
|
|
{ |
|
99
|
|
|
// what are we doing? |
|
100
|
|
|
$log = Log::usingLog()->startAction("stop screen session '{$sessionName}' on host '{$this->args[0]}'"); |
|
101
|
|
|
|
|
102
|
|
|
// get the process details |
|
103
|
|
|
$processDetails = Screen::fromHost($this->args[0])->getScreenSessionDetails($sessionName); |
|
104
|
|
|
|
|
105
|
|
|
// stop the process |
|
106
|
|
|
Host::onHost($this->args[0])->stopProcess($processDetails->pid); |
|
107
|
|
|
|
|
108
|
|
|
// all done |
|
109
|
|
|
$log->endAction(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function stopAllScreens() |
|
113
|
|
|
{ |
|
114
|
|
|
// what are we doing? |
|
115
|
|
|
$log = Log::usingLog()->startAction("stop all running screen sessions on host '{$this->args[0]}'"); |
|
116
|
|
|
|
|
117
|
|
|
// get the app details |
|
118
|
|
|
$processes = Screen::fromHost($this->args[0])->getAllScreenSessions(); |
|
119
|
|
|
|
|
120
|
|
|
// stop the process |
|
121
|
|
|
foreach ($processes as $processDetails) { |
|
122
|
|
|
Host::onHost($this->args[0])->stopProcess($processDetails->pid); |
|
123
|
|
|
usingProcessesTable()->removeProcess($this->args[0], $processDetails); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// all done |
|
127
|
|
|
$log->endAction("stopped " . count($processes) . " session(s)"); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|