-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate.php
More file actions
379 lines (308 loc) · 10.7 KB
/
Copy pathupdate.php
File metadata and controls
379 lines (308 loc) · 10.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
<?php
declare(strict_types=1);
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Thelia\Core\Install\Exception\UpdateException;
if (\PHP_SAPI !== 'cli') {
throw new Exception('this script can only be launched with cli sapi');
}
// How many times an unusable answer is asked again before the script gives up.
const UPDATE_MAX_INPUT_ATTEMPTS = 3;
$bootstrapToggle = false;
$bootstraped = false;
$env = 'dev';
// Autoload bootstrap
foreach ($argv as $arg) {
if ('-b' === $arg) {
$bootstrapToggle = true;
continue;
}
if (preg_match_all('/--env=(\S+)/', $arg, $matchs)) {
$env = $matchs[1][0];
}
if ($bootstrapToggle) {
require __DIR__.\DIRECTORY_SEPARATOR.$arg;
$bootstraped = true;
}
}
if (!$bootstraped) {
if (isset($bootstrapFile)) {
require $bootstrapFile;
} elseif (is_file($file = __DIR__.'/../vendor/autoload.php')) {
// A thelia/thelia checkout: setup/ sits at the root, next to vendor/.
require $file;
} elseif (is_file($file = __DIR__.'/../../bootstrap.php') && is_file($autoload = __DIR__.'/../../vendor/autoload.php')) {
// A thelia-project install: the script sits in local/setup/, and the project
// root holds bootstrap.php next to vendor/. bootstrap.php only defines the
// path constants and deliberately leaves the autoloader alone, so both have
// to be loaded, and in that order: vendor/autoload.php pulls in the core
// bootstrap.php, which would otherwise read THELIA_ROOT off vendor/thelia/core.
// public/index.php and bin/install of the project load them the same way.
require $file;
require $autoload;
} else {
echo 'No autoload file found. Please use the -b argument to include yours';
exit(1);
}
}
if (is_file(dirname(__DIR__)."/.env.{$env}.local")) {
(new Symfony\Component\Dotenv\Dotenv())->bootEnv(dirname(__DIR__)."/.env.{$env}.local");
} elseif (is_file(dirname(__DIR__).'/.env')) {
(new Symfony\Component\Dotenv\Dotenv())->bootEnv(dirname(__DIR__).'/.env');
} elseif (is_file($file = __DIR__.'/../../bootstrap.php')) {
// Here we are on a thelia/thelia-project
(new Symfony\Component\Dotenv\Dotenv())->bootEnv(dirname(__DIR__).'/../.env');
}
$thelia = new App\Kernel($_ENV['APP_ENV'], false);
try {
$thelia->boot();
} catch (Throwable $bootFailure) {
// The code was just updated, and the compiled container and the generated
// Propel models on disk still describe the previous release: booting on them
// fails as soon as a bundle touches a model the old schema did not have.
// Both are rebuilt from the new schema, then the kernel starts again. A shop
// that is already up to date boots first time and keeps its caches.
$staleEnvironment = $_ENV['APP_ENV'];
cliOutput(sprintf('Boot failed on the previous release caches (%s), rebuilding them', $bootFailure->getMessage()), 'info');
foreach ([THELIA_CACHE_DIR.$staleEnvironment, THELIA_ROOT.'var'.DS.'propel'.DS.$staleEnvironment] as $staleDirectory) {
if (is_dir($staleDirectory)) {
cliOutput(sprintf('Removing : %s', $staleDirectory), 'info');
(new Filesystem())->remove($staleDirectory);
}
}
$thelia = new App\Kernel($_ENV['APP_ENV'], false);
$thelia->boot();
}
/*
* Load Update class
*/
try {
$update = new Thelia\Core\Install\Update(false);
} catch (UpdateException $ex) {
cliOutput($ex->getMessage(), 'error');
exit(2);
}
/*
* Check if update is needed
*/
if ($update->isLatestVersion()) {
cliOutput('You already have the latest version of Thelia : '.$update->getCurrentVersion(), 'success');
exit(3);
}
$current = $update->getCurrentVersion();
$files = $update->getLatestVersion();
$web = $update->getWebVersion();
if (null !== $web && $files !== $web) {
cliOutput(sprintf(
'Thelia server is reporting the current stable release version is %s ',
$web,
), 'warning');
}
cliOutput(sprintf(
'You are going to update Thelia from version %s to version %s.',
$current,
$files,
), 'info');
if (null !== $web && $files < $web) {
cliOutput(sprintf(
'Your files belongs to version %s, which is not the latest stable release.',
$files,
), 'warning');
cliOutput(
'It is recommended to upgrade your files first then run this script again.'.\PHP_EOL
.'The latest version is available at http://thelia.net/#download .',
'warning',
);
$question = 'Continue update process anyway ? (Y/n)';
} else {
$question = 'Continue update process ? (Y/n)';
}
if (!askConfirmation($question)) {
cliOutput('Update aborted', 'warning');
exit(0);
}
$backup = askConfirmation('Would you like to backup the current database before proceeding ? (Y/n)');
/*
* Update
*/
$updateError = null;
try {
// backup db
if (true === $backup) {
try {
$update->backupDb();
cliOutput(sprintf('Your database has been backed up. The sql file : %s', $update->getBackupFile()), 'info');
} catch (Exception $e) {
cliOutput('Sorry, your database can\'t be backed up. Reason : '.$e->getMessage(), 'error');
exit(4);
}
}
// update
$update->process($backup);
} catch (UpdateException $ex) {
$updateError = $ex;
}
foreach ($update->getMessages() as $message) {
cliOutput($message[0], $message[1]);
}
if (null === $updateError) {
cliOutput(sprintf('Thelia as been successfully updated to version %s', $update->getCurrentVersion()), 'success');
if ($update->hasPostInstructions()) {
cliOutput('===================================');
cliOutput($update->getPostInstructions());
cliOutput('===================================');
}
} else {
cliOutput(sprintf('Sorry, an unexpected error has occured : %s', $updateError->getMessage()), 'error');
echo $updateError->getTraceAsString().\PHP_EOL;
echo 'Trace: '.\PHP_EOL;
foreach ($update->getLogs() as $log) {
cliOutput(sprintf('[%s] %s'.\PHP_EOL, $log[0], $log[1]), 'error');
}
if (true === $backup) {
// Say what the restore does before asking. It rewrites every table from the
// backup, so it undoes the versions the run did manage to apply and drops
// everything written since the backup was taken.
$appliedVersions = $update->getUpdatedVersions();
if ([] !== $appliedVersions) {
cliOutput(sprintf(
'Applied to the database before the failure: %s.',
implode(', ', $appliedVersions),
), 'warning');
}
cliOutput(
'Restoring rewrites every table from the backup: those versions, and anything '
.'written since the backup was taken, are lost.',
'warning',
);
if (askConfirmation('Would you like to restore the backup database ? (Y/n)')) {
cliOutput('Database restore started. Wait, it could take a while...');
if (false === $update->restoreDb()) {
cliOutput($update->getRestoreFailure() ?? 'The backup could not be restored.', 'error');
cliOutput(sprintf(
'Sorry, your database can\'t be restore. Try to do it manually : %s',
$update->getBackupFile(),
), 'error');
exit(5);
}
cliOutput('Database successfully restore.');
exit(5);
}
}
}
/*
* Try to delete cache
*/
$finder = new Finder();
$fs = new Filesystem();
$hasDeleteError = false;
$finder->files()->in(THELIA_CACHE_DIR);
cliOutput(sprintf('Try to delete cache in : %s', THELIA_CACHE_DIR), 'info');
foreach ($finder as $file) {
try {
// remove() takes a path, a Traversable or an array: a Finder entry is an
// SplFileInfo and would raise a TypeError.
$fs->remove($file->getPathname());
} catch (Symfony\Component\Filesystem\Exception\IOException $ex) {
$hasDeleteError = true;
}
}
if (true === $hasDeleteError) {
cliOutput('The cache has not been cleared properly. Try to run the command manually : '.
'(sudo) php Thelia thelia:cache:clear (--env=prod).');
}
cliOutput('Update process finished.', 'info');
exit(null === $updateError ? 0 : 7);
/*
* Utils
*/
/**
* @return string|false the answer, or false when standard input is exhausted
*/
function readStdin($normalize = false)
{
// Kept open across calls: closing php://stdin makes every later read fail,
// and a piped input then dies on the second question.
static $stream = null;
if (null === $stream) {
$stream = fopen('php://stdin', 'r');
}
if (false === $stream) {
return false;
}
$input = fgets($stream, 128);
// End of input: a pipe that ran out, or no stdin at all.
if (false === $input) {
return false;
}
$input = rtrim($input);
if ($normalize) {
$input = strtolower(trim($input));
}
return $input;
}
function askConfirmation($question): bool
{
for ($attempt = 0; $attempt < UPDATE_MAX_INPUT_ATTEMPTS; ++$attempt) {
cliOutput($question);
$answer = readStdin(true);
if (false === $answer) {
abortUpdate('standard input is exhausted or is not a terminal, no answer can be read.');
}
if ('y' === $answer) {
return true;
}
if ('n' === $answer) {
return false;
}
cliOutput('Please answer y or n.', 'warning');
}
abortUpdate(sprintf('no valid answer after %d attempts.', UPDATE_MAX_INPUT_ATTEMPTS));
}
function abortUpdate($reason): never
{
cliOutput('Update aborted : '.$reason, 'error');
exit(6);
}
function joinPaths()
{
$args = func_get_args();
$paths = [];
foreach ($args as $arg) {
$paths[] = trim($arg, '/\\');
}
$path = implode(\DIRECTORY_SEPARATOR, $paths);
if ('/' === substr($args[0], 0, 1)) {
$path = \DIRECTORY_SEPARATOR.$path;
}
return $path;
}
function cliOutput($message, $type = null): void
{
switch ($type) {
case 'success':
$color = "\033[0;32m";
break;
case 'info':
$color = "\033[0;34m";
break;
case 'error':
$color = "\033[0;31m";
break;
case 'warning':
$color = "\033[1;33m";
break;
default:
$color = "\033[0m";
}
echo \PHP_EOL.$color.$message."\033[0m".\PHP_EOL;
}