forked from sutunam/mage-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmage-patch.php
398 lines (314 loc) · 11 KB
/
mage-patch.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
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
<?php
if (version_compare(phpversion(), '5.3.0', '<')) {
die('PHP version must be at least 5.3.0');
}
require_once dirname(__FILE__). '/Download.php';
require_once dirname(__FILE__). '/Colors.php';
class PatchMage {
protected $_patchData;
protected $_suUser;
protected $_sudoUser;
protected $_allowedPatches;
protected $_continueOnError;
protected $_dryRun = false;
protected $_keepDownloadedPatch = false;
protected $_quiet = false;
protected $downloader;
public function __construct($jsonConfigUrl = null)
{
// $this->_loadJsonData($jsonConfigUrl);
}
/*
* @deprecated
*/
protected function _loadJsonData($url = null)
{
if (!$url) {
$this->_patchData = json_decode(file_get_contents(__DIR__.'/config.json'), true);
if (!$this->_patchData) {
throw new Exception('Error loading config file');
}
return;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonData = curl_exec($ch);
curl_close($ch);
if (!$jsonData) {
throw new Exception('Error downloading config file');
}
$this->_patchData = json_decode($jsonData, true);
}
protected function _getPatchFile ($patchVersions, $version)
{
foreach ($patchVersions as $patchVersion => $patchFile) {
$patchVersion = str_replace('x', '9999', $patchVersion);
$patchVersion = explode('->', $patchVersion);
if (count($patchVersion) == 1) {
$patchVersion[1] = $patchVersion[0].'.99999';
}
if (count($patchVersion) != 2) {
throw new Exception('wrong format');
}
if (!version_compare($patchVersion[0], $version, '<=')) {
continue;
} elseif (!version_compare($patchVersion[1], $version, '>=')) {
continue;
}
return $patchFile;
break;
}
}
/**
*
* @param string $dir Install folder
* @throws Exception
* @return array:string
*/
public function getMagentoVersion ($dir)
{
if (!file_exists($dir.'app/Mage.php')) {
throw new Exception('Mage.php file not found');
}
//require($dir.'app/Mage.php');
//$mageVersion = Mage::getVersion();
$mageVersion = shell_exec('php -r \'require("'.$dir.'app/Mage.php"); echo Mage::getVersion();\'');
// todo : better detection Magento CE/EE ?
$magentoEdition = 'CE';
if (file_exists($dir.'app/code/core/Enterprise/Enterprise/etc/config.xml')) {
$magentoEdition = 'EE';
}
return array($magentoEdition, $mageVersion);
}
protected function _downloadPatch($dir, $patchFile)
{
$url = rtrim($this->_patchData['baseUrl'], '/').'/'.$patchFile;
$patchFilename = $dir.$patchFile;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$fp = fopen ($patchFilename, 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
$curlRet = curl_exec($ch);
fclose($fp);
if (!$curlRet) {
unlink($patchFilename);
throw new Exception(curl_error($ch));
}
curl_close($ch);
return $patchFilename;
}
protected function _applyPatch ($dir, $patchFile)
{
$cwd = getcwd();
$cmd = '/bin/bash '.$patchFile;
if ($this->_suUser) {
$user = $this->_suUser;
if ($this->_suUser == '_') {
$user = posix_getpwuid(fileowner($dir)); // username infos from uid
$user = $user['name'];
if (!$user) {
throw new Exception('Cannot get username of UID '.fileowner($dir));
}
}
$cmd = 'su -c '.escapeshellarg($cmd).' '.$user;
} elseif ($this->_sudoUser) {
$user = $this->_sudoUser;
if ($this->_sudoUser == '_') {
$user = '\\#'.fileowner($dir); //uid of user
}
$cmd = 'sudo -u '.$user.' '.$cmd;
}
if (!chdir($dir)) {
throw new Exception('cannot change current working directory to '.$dir);
}
if ($this->_quiet) {
$cmd .= ' 2>&1 > /dev/null';
}
$ret = 0;
if (!$this->_dryRun) {
passthru($cmd, $ret);
}
chdir($cwd);
if ($ret) {
throw new Exception('Error applying patch');
}
}
public function setSudoUser ($user)
{
$this->_sudoUser = $user;
return $this;
}
public function setSuUser ($user)
{
$this->_suUser = $user;
return $this;
}
public function setAllowedPatches ($patches)
{
if (!is_array($patches)) {
$patches = explode(',', $patches);
}
$this->_allowedPatches = $patches;
return $this;
}
public function setContinueOnError ($bool)
{
if (!$this->_isBoolParam($bool)) {
throw new Exception('Wrong param for continueOnError option.');
}
$this->_continueOnError = !!$bool;
return $this;
}
public function setDryRun ($bool)
{
if (!$this->_isBoolParam($bool)) {
throw new Exception('Wrong param for dry-run option.');
}
$this->_dryRun = !!$bool;
return $this;
}
public function setKeepDownloadedPatch ($bool)
{
if (!$this->_isBoolParam($bool)) {
throw new Exception('Wrong param for keepDownloadedPatch option.');
}
$this->_keepDownloadedPatch = !!$bool;
return $this;
}
public function setQuiet ($bool)
{
if (!$this->_isBoolParam($bool)) {
throw new Exception('Wrong param for quiet option.');
}
$this->_quiet = !!$bool;
return $this;
}
protected function _isBoolParam ($param)
{
return in_array($param, array(0, 1, 'true', 'false'));
}
/**
*
* @param string $mageEdition should be CE or EE
* @return array Patches availables for this edition.
*/
protected function _getAvailablePathList ($mageEdition)
{
return $this->_patchData['patches-'.$mageEdition];
}
public function patch ($dir)
{
$this->downloader = new Downloader($dir);
$dir = rtrim($dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
echo $dir.':'.PHP_EOL;
list($mageEdition, $mageVersion) = $this->getMagentoVersion($dir);
echo 'Magento version: '.$mageEdition.' '.$mageVersion.PHP_EOL;
$appliedPatches = array();
$patches = $this->_getAvailablePathList($mageEdition);
if ($this->_allowedPatches) {
$patches = array_intersect_key($patches, array_flip($this->_allowedPatches));
}
$this->downloader->downloadPatch($mageVersion);
die;
foreach ($patches as $patch => $patchVersions) {
$patchFile = $this->_getPatchFile($patchVersions, $mageVersion);
if (!$patchFile) {
echo 'The patch '.$patch.' is not available for version '.$mageVersion.PHP_EOL;
continue;
}
echo '';
echo PHP_EOL.'Apply patch file: '.$patchFile.PHP_EOL;
$this->_downloadPatch($dir, $patchFile);
try {
$this->_applyPatch($dir, $patchFile);
$appliedPatches[] = $patch;
} catch (Exception $e) {
if ($this->_continueOnError) {
echo "Error applying the patch ".$patch.PHP_EOL;
}
}
if (!$this->_keepDownloadedPatch) {
unlink($dir.$patchFile);
}
}
echo PHP_EOL.'The following patches have been applied :'.PHP_EOL.implode(PHP_EOL, $appliedPatches).PHP_EOL.PHP_EOL;
}
public function multiPatch (array $dirs)
{
foreach ($dirs as $dir) {
$this->patch($dir);
}
}
public function help ()
{
$f = basename(__FILE__);
echo <<<OUTPUT
MagePatch : Upgrade Multiple Magento easily
Usage: php -f $f -- [options] dirs ...
dirs:
Magento directory where the patches will be applied
options:
--sudo|--su USR
Specify user USR who will execute the patch with the sudo or the su
command. If you use the magic '_' value, the patch will be executed by
the owner of the Magento directory, using sudo or su command.
--config URL
Specify URL of the config.json. Default is
https://raw.githubusercontent.com/sutunam/mage-patch/master/config.json
--patches patch-name,...
Restrict the list of the patch to be applied to one or more patch-name,
separated by comma. The patch-names are listed in the config.json.
--continueOnError 1|0 (default 0)
Continue applying patch even if an error is returned by a patch.
--dryRun (1|0) (default 0)
Do not apply any patch. Only find Magento version and check that the
patches can be downloaded (actualy it download them and remove them).
--keepDownloadedPatch 1|0 (default 0)
Download the patch and do not delete it, the patch files stay in the specified directory.
--quiet (0|1) (default 0)
Turn off stdin and stdout output of the patch script.
OUTPUT;
}
}
$dirs = $argv;
unset($dirs[0]);
function extractParams($name, &$params) {
$value = null;
if (false !== $key = array_search($name, $params)) {
$value = $params[$key+1];
unset($params[$key]);
unset($params[$key+1]);
}
return $value;
}
if (!$configUrl = extractParams('--config', $dirs)) {
$configUrl = 'https://raw.githubusercontent.com/serhiirepin/mage-patch/master/config.json';
}
$patch = new PatchMage($configUrl);
if ($su = extractParams('--su', $dirs)) {
$patch->setSuUser($su);
}
if ($sudo = extractParams('--sudo', $dirs)) {
$patch->setSudoUser($sudo);
}
if ($patches = extractParams('--patches', $dirs)) {
$patch->setAllowedPatches($patches);
}
if ($continueOnError = extractParams('--continueOnError', $dirs)) {
$patch->setContinueOnError($continueOnError);
}
if ($dryRun = extractParams('--dryRun', $dirs)) {
$patch->setDryRun($dryRun);
}
if ($kdp = extractParams('--keepDownloadedPatch', $dirs)) {
$patch->setKeepDownloadedPatch($kdp);
}
if ($quiet = extractParams('--quiet', $dirs)) {
$patch->setQuiet($quiet);
}
if (!count($dirs)) {
$patch->help();
} else {
$patch->multiPatch($dirs);
}