forked from rodrigopedra/laravel-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractsWithSsh.php
219 lines (191 loc) · 5.18 KB
/
InteractsWithSsh.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
namespace App;
use Facades\App\ShellProcessRunner;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
trait InteractsWithSsh
{
/**
* Run the given script on a remote server.
*
* @return $this
*/
public function run()
{
$this->markAsRunning();
$this->ensureWorkingDirectoryExists();
try {
$this->upload();
} catch (ProcessTimedOutException $e) {
return $this->markAsTimedOut();
}
return $this->updateForResponse($this->runInline(sprintf(
'bash %s 2>&1 | tee %s',
$this->scriptFile(),
$this->outputFile()
), $this->options['timeout'] ?? 60));
}
/**
* Update the model for the given SSH response.
*
* @param object $response
* @return $this
*/
protected function updateForResponse($response)
{
return tap($this)->update([
'status' => $response->timedOut ? 'timeout' : 'finished',
'exit_code' => $response->exitCode,
'output' => $response->output,
]);
}
/**
* Run the given script in the background on a remote server.
*
* @return $this
*/
public function runInBackground()
{
$this->markAsRunning();
$this->addCallbackToScript();
$this->ensureWorkingDirectoryExists();
try {
$this->upload();
} catch (ProcessTimedOutException $e) {
return $this->markAsTimedOut();
}
ShellProcessRunner::run($this->toProcess(sprintf(
'\'nohup bash %s >> %s 2>&1 &\'',
$this->scriptFile(),
$this->outputFile()
), 10));
return $this;
}
/**
* Add a callback to the script.
*
* @return void
*/
protected function addCallbackToScript()
{
$this->update([
'script' => view('scripts.tools.callback', [
'task' => $this,
'path' => str_replace('.sh', '-script.sh', $this->scriptFile()),
'token' => str_random(20),
])->render(),
]);
}
/**
* Create the remote working directory for the task.
*
* @return void
*/
protected function ensureWorkingDirectoryExists()
{
$this->runInline('mkdir -p '.$this->path(), 10);
}
/**
* Upload the given script to the server.
*
* @return bool
*/
protected function upload()
{
$process = (new Process(SecureShellCommand::forUpload(
$this->provisionable->ipAddress(),
$this->provisionable->port(),
$this->provisionable->ownerKeyPath(),
$this->user,
$localScript = $this->writeScript(),
$this->scriptFile()
), base_path()))->setTimeout(15);
$response = ShellProcessRunner::run($process);
@unlink($localScript);
return $response->exitCode === 0;
}
/**
* Write the script to storage in preparation for upload.
*
* @return string
*/
protected function writeScript()
{
$hash = md5(str_random(20).$this->script);
return tap(storage_path('app/scripts').'/'.$hash, function ($path) {
file_put_contents($path, $this->script);
});
}
/**
* Download the output of the task from the remote server.
*
* @param string|null $path
* @return string
*/
public function retrieveOutput($path = null)
{
return $this->runInline('tail --bytes=2000000 '.($path ?? $this->outputFile()), 10)->output;
}
/**
* Run a given script inline on the remote server.
*
* @param string $script
* @param int $timeout
* @return ShellResponse
*/
protected function runInline($script, $timeout = 60)
{
$token = str_random(20);
return ShellProcessRunner::run($this->toProcess('\'bash -s \' << \''.$token.'\'
'.$script.'
'.$token, $timeout));
}
/**
* Get the remote working directory path for the task.
*
* @return string
*/
protected function path()
{
return $this->user === 'root'
? '/root/.cloud'
: '/home/cloud/.cloud';
}
/**
* Get the remote path to the script.
*
* @return string
*/
protected function scriptFile()
{
return $this->path().'/'.$this->id.'.sh';
}
/**
* Get the remote path to the output.
*
* @return string
*/
protected function outputFile()
{
return $this->path().'/'.$this->id.'.out';
}
/**
* Create a Process instance for the given script.
*
* @param string $script
* @param int $timeout
* @return Process
*/
protected function toProcess($script, $timeout)
{
return (new Process(
SecureShellCommand::forScript(
$this->provisionable->ipAddress(),
$this->provisionable->port(),
$this->provisionable->ownerKeyPath(),
$this->user,
$script
)
))->setTimeout($timeout);
}
}