-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate.php
298 lines (248 loc) · 7.84 KB
/
update.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/*
* 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\Install\Exception\UpdateException;
if (\PHP_SAPI != 'cli') {
throw new \Exception('this script can only be launched with cli sapi');
}
$bootstrapToggle = false;
$bootstraped = false;
$env = 'dev';
// Autoload bootstrap
foreach ($argv as $arg) {
if ($arg === '-b') {
$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')) {
require $file;
} elseif (is_file($file = __DIR__.'/../../bootstrap.php')) {
// Here we are on a thelia/thelia-project
require $file;
} 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);
$thelia->boot();
/***************************************************
* Load Update class
***************************************************/
try {
$update = new \Thelia\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();
while (1) {
if ($web !== null && $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 ($web !== null && $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');
cliOutput('Continue update process anyway ? (Y/n)');
} else {
cliOutput('Continue update process ? (Y/n)');
}
$rep = readStdin(true);
if ($rep == 'y') {
break;
}
if ($rep == 'n') {
cliOutput('Update aborted', 'warning');
exit(0);
}
}
$backup = false;
while (1) {
cliOutput('Would you like to backup the current database before proceeding ? (Y/n)');
$rep = readStdin(true);
if ($rep == 'y') {
$backup = true;
break;
}
if ($rep == 'n') {
$backup = false;
break;
}
}
/***************************************************
* 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) {
while (1) {
cliOutput('Would you like to restore the backup database ? (Y/n)');
$rep = readStdin(true);
if ($rep == 'y') {
cliOutput('Database restore started. Wait, it could take a while...');
if (false === $update->restoreDb()) {
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);
}
if ($rep == 'n') {
exit(0);
}
}
}
}
/***************************************************
* 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 {
$fs->remove($file);
} 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 cache:clear (--env=prod).');
}
cliOutput('Update process finished.', 'info');
exit(0);
/***************************************************
* Utils
***************************************************/
function readStdin($normalize = false)
{
$fr = fopen('php://stdin', 'r');
$input = fgets($fr, 128);
$input = rtrim($input);
fclose($fr);
if ($normalize) {
$input = strtolower(trim($input));
}
return $input;
}
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;
}