-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfiniteDeploy.php
277 lines (236 loc) · 9.37 KB
/
InfiniteDeploy.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
use phpseclib3\Net\SFTP;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
class InfiniteDeploy
{
public $build = true;
public $deploy = true;
public $gate = true;
public $verbose = false;
public $quiet = false;
public $config;
public $selectedTargets = [];
private $remoteUsername;
private $remotePassword;
private $twig;
public function __construct(array $argv, array $config)
{
$this->config = $config;
$this->validateConfig();
foreach (array_slice($argv, 1) as $argument) {
if ($argument == '--no-build') { $this->build = false; continue; }
if ($argument == '--no-deploy') { $this->deploy = false; continue; }
if ($argument == '--no-gate') { $this->gate = false; continue; }
if ($argument == '--verbose') { $this->verbose = true; continue; }
if ($argument == '--quiet') { $this->quiet = true; continue; }
if ($argument == '--all') {
$this->selectedTargets = $config['targets'];
continue;
}
if (array_key_exists($argument, $config['targets'])) {
$this->selectedTargets[$argument] = $config['targets'][$argument];
continue;
}
$this->showUsageAndExit();
}
if (!$this->selectedTargets) {
$this->showUsageAndExit();
}
// Get a username/password for remote deploys.
if ($this->deploy) {
echo 'Remote username: ';
$this->remoteUsername = rtrim(fgets(STDIN), "\r\n");
echo 'Remote password: ';
$ph = popen('stty -echo; read REPLY; echo $REPLY', 'r');
$this->remotePassword = rtrim(fgets($ph), "\r\n");
pclose($ph);
echo "\n";
// Confirm that this username/password combo works on every target, so that we can fail early if they're wrong
foreach ($this->selectedTargets as $target => $meta) {
if (!($session = $this->connect($target))) {
$this->error("Username/password failed on $target");
}
$session->disconnect();
}
}
$this->twig = new Environment(new ArrayLoader);
}
/**
* Unless --no-gate was passed, print the given $title and return true.
*/
public function gate(string $title): bool
{
if ($this->gate && !$this->quiet) {
echo "$title\n";
}
return $this->gate;
}
/**
* Unless --no-build was passed, print the given $title and return true.
*/
public function build(string $title): bool
{
if ($this->build && !$this->quiet) {
echo "$title\n";
}
return $this->build;
}
/**
* Unless --no-deploy was passed, print the given $title and return true.
*/
public function deploy(string $title): bool
{
if ($this->deploy && !$this->quiet) {
echo "$title\n";
}
return $this->deploy;
}
public function runScripts(string $key, array $substitutions = []): void
{
if (!isset($this->config['scripts'][$key])) {
throw new \RuntimeException("$key scripts not defined");
}
foreach ($this->config['scripts'][$key] as $line) {
$template = $this->twig->createTemplate($line);
$line = $template->render($substitutions);
$this->runCmd($line);
}
}
public function runCmd(string $command): void
{
if ($this->verbose) {
echo "$command\n";
passthru($command, $code);
if ($code) { exit($code); }
} else {
$ph = popen($command . ' 2>&1', 'r');
$output = stream_get_contents($ph);
$code = pclose($ph);
if ($code) {
$this->error("$command\n$output", $code);
}
}
}
public function connect($target): ?SFTP
{
if (!isset($this->config['targets'][$target]['remote'])) {
throw new \InvalidArgumentException('No such remote target: '.$target);
}
list($server, $port) = $this->config['targets'][$target]['remote'];
$session = new SFTP($server, $port);
if (!$session->login($this->remoteUsername, $this->remotePassword)) {
$session->disconnect();
return null;
}
$session->setTimeout(0);
return $session;
}
public function upload(SFTP $session, string $localFilename, ?string $remoteFilename = null): void
{
if ($remoteFilename === null) {
$remoteFilename = basename($localFilename);
}
if (!$this->quiet) {
$size = filesize($localFilename);
$startTime = microtime(true);
$nextCheck = $startTime;
$scaledSize = $size;
$scale = 0;
while ($scaledSize >= 1024 && $scale < 4) {
$scale++;
$scaledSize /= 1024;
}
$progressCallback = function ($sent, $force = false) use ($startTime, &$nextCheck, $size, $scale) {
if (microtime(true) < $nextCheck && !$force) {
return;
}
$nextCheck += 1;
$timeTaken = microtime(true) - $startTime;
$barWidth = floor($sent / $size * 40);
printf(
"\r[%s%s] %.2f/%.2f%s",
str_repeat('=', $barWidth),
str_repeat(' ', 40 - $barWidth),
$sent / pow(1024, $scale),
$size / pow(1024, $scale),
['B', 'kB', 'MB', 'GB', 'TB'][$scale]
);
if ($timeTaken >= 1) {
$ss = floor($timeTaken / $sent * $size - $timeTaken);
$mm = floor($ss / 60);
$ss = $ss % 60;
printf(" %02d:%02d remaining", $mm, $ss);
}
};
} else {
$progressCallback = null;
}
$session->put($localFilename, $remoteFilename, SFTP::SOURCE_LOCAL_FILE, -1, -1, $progressCallback);
if (!$this->quiet) {
$progressCallback($size, true);
echo "\n";
}
}
public function remoteSudo(SFTP $session, $script): void
{
$command = sprintf("echo %s | sudo -kSp '[sudo] Automatically entering password\n' bash -c %s", escapeshellarg($this->remotePassword), escapeshellarg($script));
$output = $session->exec($command, $this->verbose ? function ($line) { echo $line; } : null);
if ($session->getExitStatus()) {
if (!$this->verbose) { fwrite(STDERR, $output); }
$session->disconnect();
exit($session->getExitStatus());
}
}
private function showUsageAndExit(): void
{
global $argv;
fwrite(STDERR, "Usage: $argv[0] [target1] [target2] ...
Flags:
--all: Deploy to all targets
--no-build: Don't build the package (deploy existing package only)
--no-deploy: Skip the deploy phase (build package only)
--no-gate: Skip the validation phase
--verbose: Print more output
--quiet: Print less output
Targets:
");
foreach ($this->config['targets'] as $target => $meta) {
fwrite(STDERR, " $target: $meta[domain]\n");
}
exit(1);
}
private function validateConfig(): void
{
if (!isset($this->config['targets'])) { $this->error("config.targets must exist"); }
if (!is_array($this->config['targets'])) { $this->error("config.targets must be an array"); }
if (!$this->config['targets']) { $this->error("config.targets must not be empty"); }
foreach ($this->config['targets'] as $target => $meta) {
if ($target === '') { $this->error("config.targets must not have an empty key"); }
if (!is_array($meta)) { $this->error("config.targets[$target] must be an array"); }
if (!isset($meta['remote'])) { $this->error("config.targets[$target].remote must exist"); }
if (!is_array($meta['remote']) || count($meta['remote']) !== 2 || !is_string($meta['remote'][0]) || !is_int($meta['remote'][1])) {
$this->error("config.targets[$target].remote must be an array with two elements (IP address and port number)");
}
if (!isset($meta['domain'])) { $this->error("config.targets[$target].domain must exist"); }
if (!is_string($meta['domain'])) { $this->error("config.targets[$target].domain must be a string"); }
}
if (isset($this->config['scripts'])) {
if (!is_array($this->config['scripts'])) { $this->error("config.scripts must be an array"); }
foreach ($this->config['scripts'] as $key => $scripts) {
if ($key === '') { $this->error("config.scripts must not have an empty key"); }
if (!is_array($scripts)) { $this->error("config.scripts[$key] must be an array"); }
foreach ($scripts as $index => $line) {
if (!is_string($line)) {
$this->error("config.scripts[$key][$index] must be a string");
}
}
}
}
}
private function error($message, $code = 1)
{
fprintf(STDERR, "%s\n", $message);
exit($code);
}
}