1: | <?php |
2: | declare(strict_types=1); |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | use Module\Support\Webapps; |
16: | use Module\Support\Webapps\Traits\PublicRelocatable; |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | class Ghost_Module extends Webapps |
26: | { |
27: | |
28: | const DEFAULT_NODE = '12'; |
29: | const GHOST_CLI_REPO = 'git+https://github.com/msaladna/ghost-cli-lite.git'; |
30: | use PublicRelocatable { |
31: | getAppRoot as getAppRootReal; |
32: | } |
33: | const APP_NAME = 'Ghost'; |
34: | const GHOST_CLI = 'ghost'; |
35: | const DEFAULT_VERSION_LOCK = 'major'; |
36: | |
37: | |
38: | const NODE_VERSIONS = [ |
39: | '0' => self::DEFAULT_NODE, |
40: | '4.0' => '12.10.0', |
41: | '4.5' => '12.22.1', |
42: | '4.6' => '14.16.1', |
43: | '4.21' => '14.18.0', |
44: | '5.0' => '16.19', |
45: | '5.30' => '18.12.1', |
46: | ]; |
47: | |
48: | public function plugin_status(string $hostname, string $path = '', string $plugin = null) |
49: | { |
50: | return error('not supported'); |
51: | } |
52: | |
53: | public function uninstall_plugin(string $hostname, string $path, string $plugin, bool $force = false): bool |
54: | { |
55: | return error('not supported'); |
56: | } |
57: | |
58: | public function disable_all_plugins(string $hostname, string $path = ''): bool |
59: | { |
60: | return error('not supported'); |
61: | } |
62: | |
63: | public function restart(string $hostname, string $path = ''): bool |
64: | { |
65: | if (!$approot = $this->getAppRoot($hostname, $path)) { |
66: | return false; |
67: | } |
68: | |
69: | return \Module\Support\Webapps\Passenger::instantiateContexted($this->getAuthContext(), |
70: | [$approot, 'nodejs'])->restart(); |
71: | } |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | protected function getAppRoot(string $hostname, string $path = ''): ?string |
81: | { |
82: | return $this->getAppRootReal($hostname, $path); |
83: | } |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | public function install(string $hostname, string $path = '', array $opts = array()): bool |
94: | { |
95: | if (!$this->mysql_enabled()) { |
96: | return error('%(what)s must be enabled to install %(app)s', |
97: | ['what' => 'MySQL', 'app' => static::APP_NAME]); |
98: | } |
99: | $available = null; |
100: | if (!$this->hasMemoryAllowance(768, $available)) { |
101: | return error("%(app)s requires at least %(min)d MB memory, `%(found)d' MB provided for account", |
102: | ['app' => 'Ghost', 'min' => 768, 'found' => $available]); |
103: | } |
104: | if (!$this->hasStorageAllowance(500, $available)) { |
105: | return error("%(app)s requires at least %(min)d MB storage. Only %(found).2f' MB provided for account", |
106: | ['app' => 'Ghost', 'min' => 500, 'found' => $available]); |
107: | } |
108: | |
109: | if (!$this->ssh_enabled()) { |
110: | return error('Ghost requires ssh service to be enabled'); |
111: | } |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | if ($path) { |
120: | return error('Ghost may only be installed directly on a subdomain or domain without a child path, e.g. https://domain.com but not https://domain.com/ghost'); |
121: | } |
122: | |
123: | if (!$this->parseInstallOptions($opts, $hostname, $path)) { |
124: | return false; |
125: | } |
126: | |
127: | $docroot = $this->getDocumentRoot($hostname, $path); |
128: | |
129: | if (!$this->platformVersionCheck((string)$opts['version'])) { |
130: | return error("Ghost %s cannot be installed on this platform", $opts['version']); |
131: | } |
132: | |
133: | $nodeVersion = $this->validateNode((string)$opts['version'], $opts['user'] ?? null); |
134: | |
135: | $wrapper = empty($opts['user']) ? $this : \apnscpFunctionInterceptor::factory(Auth::context($opts['user'], |
136: | $this->site)); |
137: | $wrapper->node_make_default($nodeVersion, $docroot); |
138: | |
139: | $args['version'] = $opts['version']; |
140: | |
141: | $db = Webapps\DatabaseGenerator::mysql($this->getAuthContext(), $hostname); |
142: | $db->hostname = '127.0.0.1'; |
143: | $db->connectionLimit = max($db->connectionLimit, 15); |
144: | |
145: | if (!$db->create()) { |
146: | return false; |
147: | } |
148: | |
149: | $args['dbhost'] = $db->hostname; |
150: | $args['dbkind'] = $db->kind; |
151: | $args['dbname'] = $db->database; |
152: | $args['dbuser'] = $db->username; |
153: | $args['dbpassword'] = $db->password; |
154: | |
155: | $fqdn = $this->web_normalize_hostname($hostname); |
156: | $args['uri'] = rtrim($fqdn . '/' . $path, '/'); |
157: | $args['proto'] = empty($opts['ssl']) ? 'http://' : 'https://'; |
158: | $args['debug'] = is_debug() ? '-V' : null; |
159: | if (is_debug()) { |
160: | warn("Disabling debug mode as it causes a maxBuffer exceeded error"); |
161: | $args['debug'] = null; |
162: | } |
163: | |
164: | |
165: | |
166: | $ret = $this->_exec($docroot, |
167: | 'ghost install %(debug)s --process=local --no-prompt --no-stack --no-start --no-color --db=%(dbkind)s --dbhost=%(dbhost)s --dbuser=%(dbuser)s --dbpass=%(dbpassword)s ' . |
168: | '--dbname=%(dbname)s --no-setup-linux-user --no-setup-nginx --url=%(proto)s%(uri)s --mail=sendmail %(version)s', |
169: | $args); |
170: | |
171: | if (!$ret['success']) { |
172: | info('removing temporary files'); |
173: | $this->file_delete($docroot, true); |
174: | $db->rollback(); |
175: | |
176: | return error('failed to download Ghost v%s: %s - possibly out of storage space?', $args['version'], |
177: | $ret['stdout']); |
178: | } |
179: | |
180: | $wrapper->node_make_default($nodeVersion, $docroot); |
181: | |
182: | if (!isset($opts['password'])) { |
183: | $opts['password'] = \Opcenter\Auth\Password::generate(10); |
184: | info("autogenerated password `%s'", $opts['password']); |
185: | } |
186: | |
187: | $username = $this->user_getpwnam($opts['user'] ?? $this->username)['gecos'] ?: $this->username; |
188: | info("setting displayed name to `%s'", $username); |
189: | $opts['url'] = rtrim($hostname . '/' . $path, '/'); |
190: | |
191: | if (!$this->fixSymlink($docroot)) { |
192: | return error("Failed to correct current/ symlink in `%s'", $docroot); |
193: | } |
194: | |
195: | $this->fixThemeLink($docroot); |
196: | |
197: | if (null === ($docroot = $this->remapPublic($hostname, $path))) { |
198: | |
199: | return error("Failed to remap Ghost to public/, manually remap from `%s' - Ghost setup is incomplete!", |
200: | $docroot); |
201: | } |
202: | |
203: | $approot = $this->getAppRoot($hostname, $path); |
204: | |
205: | $config = [ |
206: | 'useMinFiles' => 'true', |
207: | 'caching.frontend.maxAge' => 120, |
208: | 'logging.rotation.enabled' => 'true', |
209: | 'mail.transport' => 'sendmail', |
210: | |
211: | 'database.pool.max' => 5, |
212: | 'paths.contentPath' => "{$approot}/content" |
213: | ]; |
214: | |
215: | foreach ($config as $c => $v) { |
216: | $ret = $this->_exec($approot, 'ghost config set %(c)s %(v)s', ['c' => $c, 'v' => $v]); |
217: | if (!$ret['success']) { |
218: | info('removing temporary files'); |
219: | $this->file_delete($docroot, true); |
220: | $db->rollback(); |
221: | return error("Failed to set configuration `%s': %s", $c, coalesce($ret['stderr'], $ret['stdout'])); |
222: | } |
223: | } |
224: | |
225: | foreach (['tmp', 'public', 'logs'] as $dir) { |
226: | ($this->file_create_directory("{$approot}/{$dir}") && |
227: | $this->file_chown("{$approot}/{$dir}", $opts['user'] ?? $this->username) |
228: | ) || warn("failed to create application directory `%s/%s'", $docroot, $dir); |
229: | } |
230: | |
231: | $this->linkConfiguration($approot, 'production'); |
232: | |
233: | if (!$wrapper->file_put_file_contents($docroot . '/.htaccess', |
234: | '# Enable caching' . "\n" . |
235: | 'UnsetEnv no-cache' . "\n" . |
236: | 'PassengerEnabled on' . "\n" . |
237: | 'PassengerAppEnv production' . "\n" . |
238: | 'PassengerStartupFile current/index.js' . "\n" . |
239: | 'PassengerAppType node' . "\n" . |
240: | 'PassengerAppRoot ' . $approot . "\n" |
241: | )) { |
242: | return error('failed to create .htaccess control - Ghost is not properly setup'); |
243: | } |
244: | |
245: | $this->setInterpreter($docroot, $nodeVersion); |
246: | |
247: | $wrapper->node_do($nodeVersion, null, 'npm install -g --production knex-migrator'); |
248: | $ret = $wrapper->node_do($nodeVersion, "{$approot}/current", 'knex-migrator init', [], ['NODE_ENV' => 'production']); |
249: | if (!$ret['success']) { |
250: | return error('Failed to create initial database configuration - knex-migrator failed: %s', |
251: | $ret['stdout']); |
252: | } |
253: | if (!$this->migrate($approot)) { |
254: | return error('Failed to migrate database configuration - Ghost installation incomplete'); |
255: | } |
256: | $this->change_admin($hostname, $path, [ |
257: | 'email' => $opts['email'], |
258: | 'password' => $opts['password'], |
259: | 'name' => $username |
260: | ]); |
261: | |
262: | $this->notifyInstalled($hostname, $path, $opts); |
263: | |
264: | try { |
265: | |
266: | (new \HTTP\SelfReferential($hostname, $this->site_ip_address()))->get($path); |
267: | } catch (\GuzzleHttp\Exception\RequestException) { } |
268: | |
269: | return info('%(app)s installed - confirmation email with login info sent to %(email)s', |
270: | ['app' => static::APP_NAME, 'email' => $opts['email']]); |
271: | } |
272: | |
273: | private function setInterpreter(string $docroot, string $nodeVersion): bool |
274: | { |
275: | $htaccess = $this->file_get_file_contents("{$docroot}/.htaccess"); |
276: | $new = preg_replace( |
277: | '/\R(?:^PassengerNodejs .*$|$)/m', |
278: | "\n". 'PassengerNodejs ' . $this->getNodeCommand($nodeVersion, $this->file_stat($docroot)['owner']), |
279: | $htaccess, |
280: | 1 |
281: | ); |
282: | return $this->file_put_file_contents("{$docroot}/.htaccess", $new); |
283: | } |
284: | |
285: | |
286: | |
287: | |
288: | |
289: | |
290: | |
291: | |
292: | protected function validateNode(string $version = self::DEFAULT_NODE, string $user = null): ?string |
293: | { |
294: | if ($user) { |
295: | $afi = \apnscpFunctionInterceptor::factory(Auth::context($user, $this->site)); |
296: | } |
297: | $wrapper = $afi ?? $this; |
298: | $nodeVersion = \Opcenter\Versioning::satisfy($version, self::NODE_VERSIONS); |
299: | foreach ([\Opcenter\Versioning::asMajor($nodeVersion), $nodeVersion] as $testVersion) { |
300: | if (($chk = $wrapper->node_installed($testVersion)) && version_compare($chk, $nodeVersion, '>=')) |
301: | { |
302: | $nodeVersion = $chk; |
303: | break; |
304: | } |
305: | } |
306: | |
307: | if (!$wrapper->node_installed($nodeVersion) && !$wrapper->node_install($nodeVersion)) { |
308: | error('failed to install Node %s', $nodeVersion); |
309: | return null; |
310: | } |
311: | |
312: | $wrapper->node_do($nodeVersion, null, 'nvm use --delete-prefix'); |
313: | $ret = $wrapper->node_do($nodeVersion, null, 'npm install -g ' . self::GHOST_CLI_REPO); |
314: | if (!$ret['success']) { |
315: | error('failed to install ghost-cli: %s', $ret['stderr'] ?? 'UNKNOWN ERROR'); |
316: | return null; |
317: | } |
318: | $home = $this->user_get_home($user); |
319: | $stat = $this->file_stat($home); |
320: | if (!$stat || !$this->file_chmod($home, decoct($stat['permissions']) | 0001)) { |
321: | error("failed to query user home directory `%s' for user `%s'", $home, $user); |
322: | return null; |
323: | } |
324: | |
325: | return $nodeVersion; |
326: | } |
327: | |
328: | private function _exec(?string $path, $cmd, array $args = array(), $env = array()) |
329: | { |
330: | |
331: | if (!is_array($args)) { |
332: | $args = func_get_args(); |
333: | array_shift($args); |
334: | } |
335: | |
336: | $wrapper = $this->getApnscpFunctionInterceptorFromDocroot($path ?? '~'); |
337: | $ret = $wrapper->node_do( |
338: | null, |
339: | $path, |
340: | $cmd, |
341: | $args, |
342: | $env + [ |
343: | 'NODE_ENV' => 'production' |
344: | ]); |
345: | if (!strncmp(coalesce($ret['stderr'], $ret['stdout']), 'Error:', strlen('Error:'))) { |
346: | |
347: | $ret['success'] = false; |
348: | if (!$ret['stderr']) { |
349: | $ret['stderr'] = $ret['stdout']; |
350: | } |
351: | } |
352: | |
353: | return $ret; |
354: | } |
355: | |
356: | |
357: | |
358: | |
359: | |
360: | |
361: | |
362: | |
363: | public function get_version(string $hostname, string $path = ''): ?string |
364: | { |
365: | if (!$this->valid($hostname, $path)) { |
366: | return null; |
367: | } |
368: | if ($hostname[0] !== '/') { |
369: | $approot = $this->getAppRoot($hostname, $path); |
370: | } else { |
371: | $approot = Webapps\App\Loader::fromDocroot('ghost', $hostname, $this->getAuthContext())->getAppRoot(); |
372: | } |
373: | $path = $this->domain_fs_path($approot . '/current/package.json'); |
374: | clearstatcache(true, \dirname($path)); |
375: | clearstatcache(true, $path); |
376: | if (!file_exists($path)) { |
377: | warn('missing package.json from Ghost root - cannot detect version'); |
378: | |
379: | return null; |
380: | } |
381: | |
382: | return json_decode(file_get_contents($path))->version; |
383: | } |
384: | |
385: | |
386: | |
387: | |
388: | |
389: | |
390: | |
391: | |
392: | public function valid(string $hostname, string $path = ''): bool |
393: | { |
394: | if (!IS_CLI) { |
395: | return $this->query('ghost_valid', $hostname, $path); |
396: | } |
397: | |
398: | if ($hostname[0] === '/') { |
399: | if (!($path = realpath($this->domain_fs_path($hostname)))) { |
400: | return false; |
401: | } |
402: | $approot = \dirname($path); |
403: | } else { |
404: | $approot = $this->getAppRoot($hostname, $path); |
405: | if (!$approot) { |
406: | return false; |
407: | } |
408: | $approot = $this->domain_fs_path($approot); |
409: | } |
410: | if (is_link($approot . '/current') && readlink($approot . '/current')[0] === '/') { |
411: | $this->fixSymlink($this->file_unmake_path($approot)); |
412: | } |
413: | |
414: | $tests = [ |
415: | 'core/server/ghost-server.js', |
416: | 'core/server/GhostServer.js', |
417: | 'core/core/server/GhostServer.js' |
418: | ]; |
419: | |
420: | foreach ($tests as $test) { |
421: | if (file_exists("{$approot}/current/{$test}")) { |
422: | return true; |
423: | } |
424: | } |
425: | |
426: | return false; |
427: | } |
428: | |
429: | |
430: | |
431: | |
432: | |
433: | |
434: | |
435: | private function fixSymlink(string $approot): bool |
436: | { |
437: | $path = $this->domain_fs_path("{$approot}/current"); |
438: | clearstatcache(true, $path); |
439: | if (!is_link($path)) { |
440: | return error("{$approot}/current missing - can't relink"); |
441: | } |
442: | $link = readlink($path); |
443: | if ($link[0] !== '/') { |
444: | |
445: | $stat = $this->file_stat("{$approot}/current"); |
446: | |
447: | return !empty($stat['referent']) ? true : error("{$approot}/current does not point to an active Ghost install"); |
448: | } |
449: | |
450: | if (0 !== strpos($link, $approot)) { |
451: | return false; |
452: | } |
453: | |
454: | if (!$this->file_delete($approot . '/current') || !$this->file_symlink($link, $approot . '/current')) { |
455: | return false; |
456: | } |
457: | |
458: | return $this->file_chown_symlink($approot . '/current', $this->file_stat($approot)['owner']); |
459: | } |
460: | |
461: | |
462: | |
463: | |
464: | |
465: | |
466: | |
467: | private function fixThemeLink(string $approot): bool |
468: | { |
469: | $path = $this->domain_fs_path("{$approot}/content/themes"); |
470: | if (!file_exists($path)) { |
471: | return warn('Cannot correct theme symlinks, cannot find theme path'); |
472: | } |
473: | $dh = opendir($path); |
474: | while (false !== ($file = readdir($dh))) { |
475: | if ($file === '.' || $file === '..') { |
476: | continue; |
477: | } |
478: | if (!is_link("{$path}/{$file}")) { |
479: | continue; |
480: | } |
481: | $link = readlink("{$path}/{$file}"); |
482: | if (0 !== strpos($link . '/', Web_Module::MAIN_DOC_ROOT . '/')) { |
483: | continue; |
484: | } |
485: | $localpath = $this->file_unmake_path("{$path}/{$file}"); |
486: | $this->file_delete($localpath) && $this->file_symlink($approot . substr($link, |
487: | strlen(Web_Module::MAIN_DOC_ROOT)), |
488: | $localpath); |
489: | } |
490: | closedir($dh); |
491: | |
492: | return true; |
493: | } |
494: | |
495: | |
496: | |
497: | |
498: | |
499: | |
500: | |
501: | |
502: | protected function getNodeCommand(string $version = 'lts', string $user = null): ?string |
503: | { |
504: | if ($user) { |
505: | $afi = \apnscpFunctionInterceptor::factory(Auth::context($user, $this->site)); |
506: | } |
507: | $ret = ($afi ?? $this)->node_do($version, null, 'which node'); |
508: | |
509: | return $ret['success'] ? trim($ret['output']) : null; |
510: | } |
511: | |
512: | |
513: | |
514: | |
515: | |
516: | |
517: | |
518: | |
519: | private function linkConfiguration(string $approot, string $appenv = 'production'): bool |
520: | { |
521: | $this->file_delete($approot . "/current/config.{$appenv}.json"); |
522: | |
523: | return $this->file_symlink($approot . "/config.{$appenv}.json", |
524: | $approot . "/current/config.{$appenv}.json") || |
525: | warn("failed to link configuration {$approot}/config.{$appenv}.json to current/"); |
526: | } |
527: | |
528: | |
529: | |
530: | |
531: | |
532: | |
533: | |
534: | |
535: | private function migrate(string $approot, string $appenv = 'production'): bool |
536: | { |
537: | $this->linkConfiguration($approot, $appenv); |
538: | $this->_exec("$approot/current", 'knex-migrator -v || npm install --no-optional knex-migrator'); |
539: | $ret = $this->_exec("$approot/current", 'knex-migrator migrate'); |
540: | return $ret['success'] ?: error("failed to migrate database in `%s': %s", $approot, |
541: | coalesce($ret['stdout'], $ret['stderr'])); |
542: | } |
543: | |
544: | |
545: | |
546: | |
547: | |
548: | |
549: | |
550: | |
551: | |
552: | |
553: | |
554: | public function change_admin(string $hostname, string $path, array $fields): bool |
555: | { |
556: | $docroot = $this->getAppRoot($hostname, $path); |
557: | if (!$docroot) { |
558: | return warn('failed to change administrator information'); |
559: | } |
560: | $admin = $this->get_admin($hostname, $path); |
561: | |
562: | if (!$admin) { |
563: | return error('cannot determine admin of Ghost install'); |
564: | } |
565: | |
566: | if (isset($fields['password'])) { |
567: | if (!\Opcenter\Auth\Password::strong($fields['password'])) { |
568: | return false; |
569: | } |
570: | $fields['password'] = password_hash($fields['password'], PASSWORD_BCRYPT, ['cost' => 10]); |
571: | } |
572: | if (isset($fields['name'])) { |
573: | $fields['slug'] = str_slug($fields['name']); |
574: | } |
575: | |
576: | $db = $this->connectDB($hostname, $path); |
577: | $q = "UPDATE users SET status = 'active'"; |
578: | foreach (['password', 'email', 'name', 'slug'] as $field) { |
579: | if (!isset($fields[$field])) { |
580: | continue; |
581: | } |
582: | $q .= ", {$field} = '" . $db->escape_string($fields[$field]) . "'"; |
583: | } |
584: | $q .= " WHERE email = '" . $admin . "'"; |
585: | if (false === $db->query($q) || $db->affected_rows() < 1) { |
586: | return error("Failed to change admin user `%s'", $admin); |
587: | } |
588: | if (isset($fields['email'])) { |
589: | info('user login changed to %s', $fields['email']); |
590: | } |
591: | if (isset($fields['password'])) { |
592: | info("user `%s' password changed", $fields['email'] ?? $admin); |
593: | } |
594: | |
595: | return true; |
596: | } |
597: | |
598: | |
599: | |
600: | |
601: | |
602: | |
603: | |
604: | |
605: | public function get_admin(string $hostname, string $path = ''): ?string |
606: | { |
607: | $mysql = $this->connectDB($hostname, $path); |
608: | $rs = $mysql->query('SELECT email FROM users WHERE id = 1'); |
609: | if (!$rs || $rs->num_rows < 1) { |
610: | return null; |
611: | } |
612: | |
613: | return $rs->fetch_object()->email; |
614: | } |
615: | |
616: | private function connectDB($hostname, $path): \MySQL |
617: | { |
618: | $dbconfig = $this->db_config($hostname, $path); |
619: | $host = $dbconfig['host'] === 'localhost.localdomain' ? '127.0.0.1' : $dbconfig['host']; |
620: | |
621: | return \MySQL::stub()->connect($host, $dbconfig['user'], $dbconfig['password'], $dbconfig['db']); |
622: | } |
623: | |
624: | |
625: | |
626: | |
627: | |
628: | |
629: | |
630: | |
631: | public function db_config(string $hostname, string $path = '') |
632: | { |
633: | $approot = $this->getAppRoot($hostname, $path); |
634: | if (!$approot) { |
635: | error('failed to determine Ghost config - ' . $approot); |
636: | |
637: | return []; |
638: | } |
639: | foreach (['development', 'production'] as $env) { |
640: | $path = "{$approot}/config.{$env}.json"; |
641: | if ($this->file_exists($path)) { |
642: | |
643: | $json = json_decode($this->file_get_file_contents($path), true)['database']['connection']; |
644: | if (!$json) { |
645: | continue; |
646: | } |
647: | $json['db'] = $json['database']; |
648: | $json['prefix'] = ''; |
649: | |
650: | return $json; |
651: | } |
652: | } |
653: | |
654: | return []; |
655: | } |
656: | |
657: | |
658: | |
659: | |
660: | |
661: | |
662: | |
663: | |
664: | |
665: | |
666: | public function install_plugin( |
667: | string $hostname, |
668: | string $path, |
669: | string $plugin, |
670: | string $version = 'stable' |
671: | ): bool { |
672: | return error('not supported'); |
673: | } |
674: | |
675: | |
676: | |
677: | |
678: | |
679: | |
680: | |
681: | |
682: | |
683: | public function uninstall(string $hostname, string $path = '', string $delete = 'all'): bool |
684: | { |
685: | $this->kill($hostname, $path); |
686: | |
687: | return parent::uninstall($hostname, $path, $delete); |
688: | } |
689: | |
690: | |
691: | |
692: | |
693: | |
694: | |
695: | |
696: | |
697: | |
698: | public function update_all(string $hostname, string $path = '', string $version = null): bool |
699: | { |
700: | return $this->update($hostname, $path, $version) || error('failed to update all components'); |
701: | } |
702: | |
703: | |
704: | |
705: | |
706: | |
707: | |
708: | |
709: | |
710: | |
711: | public function update(string $hostname, string $path = '', string $version = null): bool |
712: | { |
713: | $approot = $this->getAppRoot($hostname, $path); |
714: | if (!$approot) { |
715: | return error('update failed'); |
716: | } |
717: | |
718: | $docroot = $this->getDocumentRoot($hostname, $path); |
719: | if (!$version) { |
720: | $version = \Opcenter\Versioning::nextVersion($this->get_versions(), |
721: | $this->get_version($hostname, $path)); |
722: | } else if (!\Opcenter\Versioning::valid($version)) { |
723: | return error('invalid version number, %s', $version); |
724: | } |
725: | |
726: | if (!$this->platformVersionCheck($version)) { |
727: | return error("Ghost %s cannot be installed on this platform", $version); |
728: | } |
729: | |
730: | $this->file_chmod($approot, 705); |
731: | |
732: | $oldversion = $this->get_version($hostname, $path); |
733: | if ($oldversion === $version) { |
734: | return info("Ghost is already at current version `%s'", $version); |
735: | } |
736: | $oldNodeVersion = $this->node_version_from_path($approot); |
737: | |
738: | if ($oldNodeVersion !== ($nodeVersion = $this->validateNode($version, $this->getDocrootUser($approot)))) { |
739: | info("Updating Node %(old)s => %(new)s per dependency requirements of Ghost %(ghostver)s", [ |
740: | 'old' => $oldNodeVersion, |
741: | 'new' => $nodeVersion, |
742: | 'ghostver' => $version |
743: | ]); |
744: | |
745: | $wrapper = $this->getApnscpFunctionInterceptorFromDocroot($docroot); |
746: | $wrapper->node_make_default($nodeVersion, $approot); |
747: | |
748: | |
749: | defer($_, fn() => $this->setInterpreter($this->getDocumentRoot($hostname, $path), $nodeVersion)); |
750: | } |
751: | |
752: | if (\Opcenter\Versioning::asMajor($version) !== \Opcenter\Versioning::asMajor($oldversion)) |
753: | { |
754: | |
755: | $ret = $this->_exec($approot, 'ghost update %s --local --no-restart --no-color v%d', [ |
756: | null, |
757: | (int)\Opcenter\Versioning::asMajor($oldversion) |
758: | ]); |
759: | if (!$ret['success']) { |
760: | return error('Ghost upgrade must be manually completed. Run the following command to use the migration assistant: ' . |
761: | 'cd %s && NODE_ENV=production nvm exec ghost update --local -f', $approot); |
762: | } |
763: | } else { |
764: | |
765: | $this->assertLocalVersion($approot, $oldversion, $version); |
766: | } |
767: | |
768: | |
769: | $cmd = 'ghost update %(debug)s --no-restart --local --no-prompt --no-color %(version)s'; |
770: | |
771: | if (is_debug()) { |
772: | warn("Disabling debug mode as it causes a maxBuffer exceeded error"); |
773: | } |
774: | |
775: | $args['debug'] = null; |
776: | $args['version'] = $version; |
777: | $ret = $this->_exec($approot, $cmd, $args); |
778: | |
779: | $this->fixSymlink($approot); |
780: | $this->file_touch("{$approot}/tmp/restart.txt"); |
781: | if (!$ret['success']) { |
782: | $this->setInfo($this->getDocumentRoot($hostname, $path), [ |
783: | 'version' => $this->get_version($hostname, $path), |
784: | 'failed' => true |
785: | ]); |
786: | |
787: | $this->assertLocalVersion($approot, $oldversion, $version); |
788: | $output = coalesce($ret['stderr'], $ret['stdout']); |
789: | if (str_contains($output, 'has been deprecated with ')) { |
790: | return warn("Release %(version)s is deprecated pending upstream acknowledgement. " . |
791: | "Update has not been applied and will be skipped.", ['version' => $version]); |
792: | } |
793: | return error('failed to update Ghost: %s', $output); |
794: | } |
795: | |
796: | $ret = $this->migrate($approot) && ($this->kill($hostname, $path) || true); |
797: | $this->setInterpreter($docroot, $nodeVersion); |
798: | if (!\Opcenter\Versioning::compare($version, $newver = $this->get_version($hostname, $path))) { |
799: | report("Upgrade failed, reported version `%s' is not requested version `%s'", $newver, $version); |
800: | } else if ($ret) { |
801: | |
802: | $this->file_delete("{$approot}/versions/{$oldversion}", true); |
803: | |
804: | $this->_exec($approot, 'npm prune'); |
805: | } |
806: | |
807: | $this->setInfo($docroot, [ |
808: | 'version' => $newver, |
809: | 'failed' => !$ret |
810: | ]); |
811: | |
812: | return $ret; |
813: | } |
814: | |
815: | |
816: | |
817: | |
818: | |
819: | |
820: | |
821: | |
822: | private function assertLocalVersion(string $approot, string $version, string $targetVersion = null): bool |
823: | { |
824: | $json = $this->file_get_file_contents($approot . '/.ghost-cli'); |
825: | $meta = json_decode($json, true); |
826: | if (!is_array($meta)) { |
827: | return error("Failed decoding meta in `%s': %s", $approot, json_last_error_msg()); |
828: | } |
829: | |
830: | if ($targetVersion) { |
831: | $stat = $this->file_stat($approot . '/versions/' . $targetVersion); |
832: | if ($stat && $stat['referent']) { |
833: | $this->file_delete($approot . '/versions/' . $targetVersion); |
834: | } |
835: | } |
836: | |
837: | if (($myver = array_get($meta, 'active-version')) === $version) { |
838: | return true; |
839: | } |
840: | |
841: | info("Version in %(approot)s reported as %(oldver)s - forcing version as %(newver)s", |
842: | ['approot' => $approot, 'oldver' => $myver, 'newver' => $version]); |
843: | $meta['active-version'] = $version; |
844: | |
845: | return $this->file_put_file_contents($approot . '/.ghost-cli', json_encode($meta), true) > 0; |
846: | } |
847: | |
848: | |
849: | |
850: | |
851: | |
852: | |
853: | public function get_versions(): array |
854: | { |
855: | $versions = $this->_getVersions(); |
856: | return array_column($versions, 'version'); |
857: | } |
858: | |
859: | public function get_installable_versions(): array |
860: | { |
861: | return array_filter($this->get_versions(), $this->platformVersionCheck(...)); |
862: | } |
863: | |
864: | |
865: | |
866: | |
867: | |
868: | |
869: | |
870: | private function platformVersionCheck(string $version): bool |
871: | { |
872: | |
873: | return version_compare($version, '5.21', '<') || version_compare($version, '5.24.0', '>='); |
874: | } |
875: | |
876: | |
877: | |
878: | |
879: | |
880: | |
881: | private function _getVersions(): array |
882: | { |
883: | $key = 'ghost.versions'; |
884: | $cache = Cache_Super_Global::spawn(); |
885: | if (false !== ($ver = $cache->get($key))) { |
886: | return (array)$ver; |
887: | } |
888: | $versions = array_filter( |
889: | (new Webapps\VersionFetcher\PackageJson)->fetch('ghost', static function ($item) { |
890: | if (isset($item['deprecated'])) { |
891: | return false; |
892: | } |
893: | return version_compare($item['version'], '5.0.0','<') || version_compare($item['version'], '5.24.1', '>=') |
894: | ? $item : false; |
895: | } |
896: | )); |
897: | |
898: | $cache->set($key, $versions, 43200); |
899: | |
900: | return $versions; |
901: | } |
902: | |
903: | |
904: | |
905: | |
906: | |
907: | |
908: | |
909: | |
910: | |
911: | public function update_plugins(string $hostname, string $path = '', array $plugins = array()): bool |
912: | { |
913: | return error('not implemented'); |
914: | } |
915: | |
916: | |
917: | |
918: | |
919: | |
920: | |
921: | |
922: | |
923: | |
924: | public function update_themes(string $hostname, string $path = '', array $themes = array()): bool |
925: | { |
926: | return error('not implemented'); |
927: | } |
928: | |
929: | |
930: | |
931: | |
932: | public function has_fortification(string $hostname, string $path = '', string $mode = null): bool |
933: | { |
934: | return false; |
935: | } |
936: | |
937: | |
938: | |
939: | |
940: | |
941: | |
942: | |
943: | |
944: | |
945: | |
946: | public function fortify(string $hostname, string $path = '', string $mode = 'max', $args = []): bool |
947: | { |
948: | return error('not implemented'); |
949: | } |
950: | |
951: | |
952: | |
953: | |
954: | |
955: | |
956: | |
957: | |
958: | |
959: | public function unfortify(string $hostname, string $path = ''): bool |
960: | { |
961: | return error('not implemented'); |
962: | } |
963: | } |