-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathUpdateCommand.php
More file actions
550 lines (458 loc) · 17.7 KB
/
Copy pathUpdateCommand.php
File metadata and controls
550 lines (458 loc) · 17.7 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace NC\Updater;
use Override;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
class UpdateCommand extends Command {
protected ?Updater $updater = null;
protected bool $shouldStop = false;
protected bool $skipBackup = false;
protected bool $skipUpgrade = false;
protected bool $ignoreState = false;
protected bool $skipIntegrityCheck = false;
protected string $urlOverride = '';
protected string $signature = '';
/** Strings of text for stages of updater */
protected array $checkTexts = [
0 => '',
1 => 'Check for expected files',
2 => 'Check for write permissions',
3 => 'Create backup',
4 => 'Downloading',
5 => 'Verify integrity',
6 => 'Extracting',
7 => 'Enable maintenance mode',
8 => 'Replace entry points',
9 => 'Delete old files',
10 => 'Move new files in place',
11 => 'Keep maintenance mode active?',
12 => 'Done',
];
#[Override]
protected function configure(): void {
$this
->setName('update')
->setDescription('Updates the code of a Nextcloud instance')
->setHelp('This command fetches the latest code that is announced via the updater server and safely replaces the existing code with the new one.')
->addOption('no-backup', null, InputOption::VALUE_NONE, 'Skip backup of current Nextcloud version')
->addOption('no-upgrade', null, InputOption::VALUE_NONE, "Don't automatically run occ upgrade")
->addOption('url', null, InputOption::VALUE_OPTIONAL, 'The URL of the Nextcloud release to download')
->addOption('ignore-state', null, InputOption::VALUE_NONE, 'Ignore known state from .step file, do a complete update')
->addOption('no-verify', null, InputOption::VALUE_NONE, 'Skip integrity verification of the downloaded file')
->addOption('signature', null, InputOption::VALUE_OPTIONAL, 'Base64 signature of the archive (use it in combination with --url option)')
;
}
public static function getUpdaterVersion(): string {
if (class_exists(Version::class)) {
$versionClass = new Version();
return $versionClass->get();
}
return 'git';
}
#[Override]
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->skipBackup = (bool)$input->getOption('no-backup');
$this->skipUpgrade = (bool)$input->getOption('no-upgrade');
$this->skipIntegrityCheck = (bool)$input->getOption('no-verify');
$this->urlOverride = (string)$input->getOption('url');
$this->signature = (string)$input->getOption('signature');
$this->ignoreState = (bool)$input->getOption('ignore-state');
$version = static::getUpdaterVersion();
$output->writeln('Nextcloud Updater - version: ' . $version);
$output->writeln('');
// Check if the config.php is at the expected place
try {
$path = dirname(__DIR__); // dirname() because we are inside the lib/ subfolder
$pharPath = \Phar::running(false);
if ($pharPath !== '') {
$path = dirname($pharPath);
}
$this->updater = new Updater($path);
} catch (\Exception $exception) {
// logging here is not possible because we don't know the data directory
$output->writeln($exception->getMessage());
return -1;
}
if (!function_exists('posix_getuid')) {
$output->writeln('The posix extensions are required - see http://php.net/manual/en/book.posix.php');
return -1;
}
$dir = (string)getenv('NEXTCLOUD_CONFIG_DIR');
$configFileName = $dir === ''
? $configFileName = $path . '/../config/config.php'
: $configFileName = rtrim($dir, '/') . '/config.php';
$user = posix_getpwuid(posix_getuid());
$fileowner = fileowner($configFileName);
if ($fileowner === false) {
throw new \Exception('Unable to read configuration file owner');
}
$configUser = posix_getpwuid($fileowner);
if ($user['name'] !== $configUser['name']) {
$output->writeln('Console has to be executed with the user that owns the file config/config.php');
$output->writeln('Current user: ' . $user['name']);
$output->writeln('Owner of config.php: ' . $configUser['name']);
$output->writeln("Try adding 'sudo -u " . $configUser['name'] . " ' to the beginning of the command (without the single quotes)");
return -1;
}
// Check if the updater.log can be written to
try {
$this->updater->log('[info] updater cli is executed');
} catch (\Exception $exception) {
// show logging error to user
$output->writeln($exception->getMessage());
return -1;
}
// Check if already a step is in process
$currentStep = $this->ignoreState ? [] : $this->updater->currentStep();
$stepNumber = 0;
if ($currentStep !== []) {
$stepState = $currentStep['state'] ?? '';
$stepNumber = $currentStep['step'] ?? 0;
$this->updater->log('[info] Step ' . $stepNumber . ' is in state "' . $stepState . '".');
if ($stepState === 'start') {
$output->writeln(
sprintf(
'Step %d is currently in process. Please call this command later or remove the following file to start from scratch: %s',
$stepNumber,
$this->updater->getUpdateStepFileLocation()
)
);
return -1;
}
$output->writeln('Found an ongoing update, continue from step ' . $stepNumber);
}
$this->updater->logVersion();
$output->writeln('Current version is ' . $this->updater->getCurrentVersion() . '.');
// needs to be called that early because otherwise updateAvailable() returns false
if ($this->urlOverride !== '') {
$this->updater->log('[info] Using URL override: ' . $this->urlOverride);
$updateString = 'Update check forced with URL override: ' . $this->urlOverride;
} else {
$updateString = $this->updater->checkForUpdate();
}
if ($this->skipIntegrityCheck) {
$this->updater->log('[warn] Integrity check of the downloaded file will be skipped');
$output->writeln('Integrity check of the downloaded file will be skipped.');
}
$output->writeln('');
$lines = explode('<br />', $updateString);
foreach ($lines as $line) {
// strip HTML
$output->writeln('<info>' . preg_replace('/<[^>]*>/', '', $line) . '</info>');
}
$output->writeln('');
if ($this->urlOverride === '' && !$this->updater->updateAvailable() && $stepNumber === 0) {
$output->writeln('Nothing to do.');
return 0;
}
$questionText = 'Start update';
if ($stepNumber > 0) {
$questionText = 'Continue update';
}
if ($input->isInteractive()) {
$this->showCurrentStatus($output, $stepNumber);
$output->writeln('');
/** @var QuestionHelper */
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion($questionText . '? [y/N] ', false);
if (!$helper->ask($input, $output, $question)) {
$output->writeln('Updater stopped.');
$this->updater->log('[info] updater stopped');
return 0;
}
} else {
$this->updater->log('[info] updater run in non-interactive mode');
$output->writeln('Updater run in non-interactive mode.');
$output->writeln('');
$output->writeln($questionText);
}
$this->updater->log('[info] updater started');
$output->writeln('');
if (function_exists('pcntl_signal')) {
// being able to handle stop/terminate command (Ctrl - C)
pcntl_signal(SIGTERM, $this->stopCommand(...));
pcntl_signal(SIGINT, $this->stopCommand(...));
$output->writeln('Info: Pressing Ctrl-C will finish the currently running step and then stops the updater.');
$output->writeln('');
} else {
$output->writeln('Info: Gracefully stopping the updater via Ctrl-C is not possible - PCNTL extension is not loaded.');
$output->writeln('');
}
// print already executed steps
for ($i = 1; $i <= $stepNumber; $i++) {
if ($i === 11) {
// no need to ask for maintenance mode on CLI - skip it
continue;
}
$output->writeln('<info>[SKIP] ' . $this->checkTexts[$i] . '</info>');
}
$i = $stepNumber;
while ($i < 12) {
$i++;
if ($i === 11) {
// no need to ask for maintenance mode on CLI - skip it
continue;
}
if (function_exists('pcntl_signal_dispatch')) {
pcntl_signal_dispatch();
if ($this->shouldStop) {
break;
}
}
$output->write('[ ] ' . $this->checkTexts[$i] . ' ...');
$result = $this->executeStep($i, $output);
// Move the cursor to the beginning of the line
$output->write("\x0D");
// Erase the line
$output->write("\x1B[2K");
if ($result['proceed'] === true) {
$output->writeln('<info>[✔] ' . $this->checkTexts[$i] . '</info>');
} else {
$output->writeln('<error>[✘] ' . $this->checkTexts[$i] . ' failed</error>');
if ($i === 1) {
if (is_string($result['response'])) {
$output->writeln('<error>' . $result['response'] . '</error>');
} else {
$output->writeln('<error>Unknown files detected within the installation folder. This can be fixed by manually removing (or moving) these files. The following extra files have been found:</error>');
foreach ($result['response'] as $file) {
$output->writeln('<error> ' . $file . '</error>');
}
}
} elseif ($i === 2) {
if (is_string($result['response'])) {
$output->writeln('<error>' . $result['response'] . '</error>');
} else {
$output->writeln('<error>The following places can not be written to:</error>');
foreach ($result['response'] as $file) {
$output->writeln('<error> ' . $file . '</error>');
}
}
} elseif (is_string($result['response'])) {
$output->writeln('<error>' . $result['response'] . '</error>');
} else {
$output->writeln('<error>Something has gone wrong. Please check the log file in the data dir.</error>');
}
break;
}
}
$output->writeln('');
if ($i === 12) {
$this->updater->log('[info] update of code successful.');
$output->writeln('Update of code successful.');
//
// Handle `occ upgrade` run
//
if ($this->skipUpgrade) {
$this->updater->log('[info] "occ upgrade" was skipped');
$this->updater->log('[info] updater finished');
$output->writeln('Please execute "./occ upgrade" manually to finish the upgrade.');
return 0;
}
if ($input->isInteractive()) {
$output->writeln('');
/** @var QuestionHelper */
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Should the "occ upgrade" command be executed? [Y/n] ', true);
if (!$helper->ask($input, $output, $question)) {
$this->updater->log('[info] "occ upgrade" was skipped');
$this->updater->log('[info] updater finished');
$output->writeln('Please execute "./occ upgrade" manually to finish the upgrade.');
return 0;
}
} else {
$this->updater->log('[info] updater run in non-interactive mode - will start "occ upgrade" now');
$output->writeln('Updater run in non-interactive mode - will start "occ upgrade" now.');
$output->writeln('');
}
$occPath = $path . '/../occ';
if (!file_exists($occPath)) {
$this->updater->log('[error] FATAL: "occ" is missing from: ' . $occPath);
$output->writeln('');
throw new \Exception('FATAL: "occ" is missing from: ' . $occPath);
}
if (chmod($occPath, 0755) === false) { # TODO do this in the updater
throw new \Exception('FATAL: Unable to make "occ" executable: ' . $occPath);
}
$occRunCommand = PHP_BINARY . ' ' . $occPath;
$this->updater->log('[info] Starting "occ upgrade"');
system($occRunCommand . ' upgrade -v', $returnValue);
if ($returnValue === 0) {
$this->updater->log('[info] "occ upgrade" finished');
$output->writeln('');
$output->writeln('"occ upgrade" finished');
} else { // something went wrong
$this->updater->log('[info] "occ upgrade" failed - return code: ' . $returnValue);
$output->writeln('');
$output->writeln('"occ upgrade" failed - return code: ' . $returnValue);
$this->updater->log('[info] updater finished - with errors');
return $returnValue;
}
//
// Handle maintenance mode toggle
//
$output->writeln('');
if ($input->isInteractive()) {
/** @var QuestionHelper */
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion($this->checkTexts[11] . ' [y/N] ', false);
if ($helper->ask($input, $output, $question)) {
$this->updater->log('[info] maintenance mode kept active');
$output->writeln('Please execute "./occ maintenance:mode --off" manually to finish the upgrade.');
$this->updater->log('[info] updater finished');
return 0;
}
} else {
$this->updater->log('[info] updater run in non-interactive mode - will disable maintenance mode now');
$output->writeln('Updater run in non-interactive mode - will disable maintenance mode now.');
$output->writeln('');
}
$this->updater->log('[info] Disabling maintenance mode');
$systemOutput = system($occRunCommand . ' maintenance:mode --off', $returnValueMaintenanceMode);
if ($returnValueMaintenanceMode === 0) {
$this->updater->log('[info] maintenance mode disabled');
$output->writeln('');
$output->writeln('Maintenance mode is disabled');
return 0;
}
// something went wrong
$this->updater->log('[info] Disabling maintenance mode failed - return code: ' . $returnValueMaintenanceMode);
$output->writeln('');
$output->writeln('Disabling Maintenance mode failed - return code:' . $returnValueMaintenanceMode);
if ($systemOutput === false) {
$this->updater->log('[info] System call failed');
$output->writeln('System call failed');
} else {
$this->updater->log('[info] occ output: ' . $systemOutput);
$output->writeln('occ output: ' . $systemOutput);
}
$this->updater->log('[info] updater finished - with errors');
return $returnValueMaintenanceMode;
}
if ($this->shouldStop) {
$output->writeln('<error>Update stopped. To resume or retry just execute the updater again.</error>');
} else {
$output->writeln('<error>Update failed. To resume or retry just execute the updater again.</error>');
}
return -1;
}
/**
* @return array{proceed:bool,response:string|list<string>} with options 'proceed' which is a boolean and defines if the step succeeded and an optional 'response' string or array
*/
protected function executeStep(int $step, OutputInterface $output): array {
if (!$this->updater instanceof Updater) {
return ['proceed' => false, 'response' => 'Initialization problem'];
}
$this->updater->log('[info] executeStep request for step "' . $step . '"');
try {
if ($step > 12 || $step < 1) {
throw new \Exception('Invalid step');
}
$this->updater->startStep($step);
switch ($step) {
case 1:
$this->updater->checkForExpectedFilesAndFolders();
break;
case 2:
$this->updater->checkWritePermissions();
break;
case 3:
if ($this->skipBackup === false) {
$this->updater->createBackup();
}
break;
case 4:
// Ensure that we have the same number of characters, that we want to override in the progress method
$output->write(str_pad(' 0%', 5, ' ', STR_PAD_LEFT));
$this->updater->downloadUpdate($this->urlOverride, function (int $progress) use ($output) {
// Move cursor 5 to the left and write the new progress
$output->write("\x1B[5D");
$output->write(str_pad(' ' . $progress . '%', 5, ' ', STR_PAD_LEFT));
});
break;
case 5:
if ($this->skipIntegrityCheck) {
$this->updater->silentLog('[info] Skipping integrity check as requested');
break;
}
$this->updater->verifyIntegrity($this->urlOverride, $this->signature);
break;
case 6:
$this->updater->extractDownload();
break;
case 7:
$this->updater->setMaintenanceMode(true);
break;
case 8:
$this->updater->replaceEntryPoints();
break;
case 9:
$this->updater->deleteOldFiles();
break;
case 10:
$this->updater->moveNewVersionInPlace();
break;
case 11:
// this is not needed in the CLI updater
//$this->updater->setMaintenanceMode(false);
break;
case 12:
$this->updater->finalize();
break;
}
$this->updater->endStep($step);
return ['proceed' => true, 'response' => ''];
} catch (UpdateException $e) {
$data = $e->getData();
try {
$this->updater->log('[error] executeStep request failed with UpdateException');
$this->updater->logException($e);
} catch (LogException $logE) {
$data[] = ' (and writing to log failed also with: ' . $logE->getMessage() . ')';
}
$this->updater->rollbackChanges($step);
return ['proceed' => false, 'response' => $data];
} catch (\Exception $e) {
$message = $e->getMessage();
try {
$this->updater->log('[error] executeStep request failed with other exception');
$this->updater->logException($e);
} catch (LogException $logE) {
$message .= ' (and writing to log failed also with: ' . $logE->getMessage() . ')';
}
$this->updater->rollbackChanges($step);
return ['proceed' => false, 'response' => $message];
}
}
protected function showCurrentStatus(OutputInterface $output, int $stepNumber): void {
$output->writeln('Steps that will be executed:');
$counter = count($this->checkTexts);
for ($i = 1; $i < $counter; $i++) {
if ($i === 11) {
// no need to ask for maintenance mode on CLI - skip it
continue;
}
$statusBegin = '[ ] ';
$statusEnd = '';
if ($i <= $stepNumber) {
$statusBegin = '<info>[✔] ';
$statusEnd = '</info>';
}
$output->writeln($statusBegin . $this->checkTexts[$i] . $statusEnd);
}
}
/**
* gets called by the PCNTL listener once the stop/terminate signal
*/
public function stopCommand(): void {
$this->shouldStop = true;
}
}