Repository navigation
Expand file tree
/
Copy pathinstall-update.php
More file actions
158 lines (144 loc) · 6.18 KB
/
Copy pathinstall-update.php
File metadata and controls
158 lines (144 loc) · 6.18 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
<?php
/*
* Copyright 2026 M. Bryce Hudson
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Install (or undo) a downloaded update from the command line.
*
* php install-update.php install what is staged
* php install-update.php --finish swap in any file an install had to defer
* php install-update.php --rollback restore the newest backup in storage/backups
* php install-update.php --status what is staged, and what the last run did
*
* Why this exists as a separate file: the app installs updates fine when the
* web server owns its files, but not when it does not — and it cannot fix a tree
* that a half-finished update already broke, because the code that would do the
* fixing is inside that tree. This shim loads ONE file (src/Install.php), uses
* no autoloader and no framework, and therefore keeps working when nothing else
* does. It is also the escape hatch for a Linux host where www-data cannot write
* the app folder: run it as the user who can.
*
* Nothing here is reachable over HTTP: `install-update.php` is a plain script
* with no side effects on GET, it refuses to do anything without a staged plan,
* and every path it writes goes through the same refusals as the web path.
*/
if (PHP_SAPI !== 'cli') {
http_response_code(403);
header('Content-Type: text/plain');
echo "install-update.php is a command-line tool.\n\n"
. "Run it from a terminal in the app folder:\n"
. " php install-update.php\n";
exit;
}
$installer = __DIR__ . '/src/Install.php';
if (!is_file($installer)) {
fwrite(STDERR, "src/Install.php is missing — this copy cannot install an update.\n");
exit(1);
}
require $installer;
$arg = isset($argv[1]) ? strtolower(trim((string) $argv[1])) : 'install';
function line($text = '')
{
echo $text . "\n";
}
function fail($text)
{
fwrite(STDERR, "\n" . $text . "\n");
exit(1);
}
line('');
line('Autonomous Loop Studio — update installer (command line)');
line(str_repeat('-', 58));
// Storage permission check: a non-writable storage/ is the #1 cause of
// "everything broke after an update". Print the fix, ready to copy.
$storageDir = __DIR__ . '/storage';
$writable = is_dir($storageDir) && is_writable($storageDir);
line('Storage folder: ' . ($writable ? 'writable (ok)' : 'NOT writable by this user'));
if (!$writable) {
$user = 'www-data'; // change to your web user (apache, nginx, ...) if needed
line('');
line('Fix it (copy and paste):');
line(' sudo chown -R ' . $user . ':' . $user . ' ' . $storageDir);
line(' sudo find ' . $storageDir . ' -type d -exec chmod 775 {} \\;');
line(' sudo find ' . $storageDir . ' -type f -exec chmod 664 {} \\;');
line('');
line('Or run the bundled helper:');
line(' sudo sh tools/fix-permissions.sh');
line('');
}
try {
if ($arg === '--status' || $arg === 'status') {
$pre = \Src\Install::preflight();
line('This copy: v' . ($pre['currentVersion'] ?: '?'));
line('Staged release: ' . ($pre['targetVersion'] ? 'v' . $pre['targetVersion'] : '(nothing downloaded)'));
line('Can install: ' . ($pre['ok'] ? 'yes' : 'no — ' . implode(' ', $pre['problems'])));
$pending = \Src\Install::pendingSwaps();
line('Waiting swaps: ' . ($pending ? implode(', ', $pending) . ' (run --finish)' : '(none)'));
$snaps = \Src\Install::snapshots();
line('Backups: ' . ($snaps ? count($snaps) . ' in storage/backups' : '(none)'));
foreach ($snaps as $s) {
line(' · ' . $s['name'] . ' (v' . ($s['version'] ?: '?') . ', ' . $s['files'] . ' files, '
. date('Y-m-d H:i', $s['at']) . ')');
}
$log = \Src\Install::tailLog(10);
if ($log !== '') {
line('');
line('Last lines of storage/updates/install.log:');
line($log);
}
line('');
exit(0);
}
if ($arg === '--finish' || $arg === 'finish') {
// The web installer runs INSIDE api/index.php, and Windows will not let
// a process rename the script it is executing. That one file is written
// as .router-new-index.php and left for somebody who is not holding it:
// this command, spawned automatically at the end of a web install.
$res = \Src\Install::finish();
if ($res['swapped']) {
line('Swapped in: ' . implode(', ', $res['swapped']));
}
if (!$res['ok']) {
line('Still held by the running server: ' . implode(', ', $res['pending']));
line('Stop Apache, then run this again: php install-update.php --finish');
line('');
exit(1);
}
if (!$res['swapped']) {
line('Nothing was waiting to be swapped in.');
}
line('');
exit(0);
}
if ($arg === '--rollback' || $arg === 'rollback') {
$dir = isset($argv[2]) ? (string) $argv[2] : null;
line('Restoring ' . ($dir ?: 'the newest backup') . '…');
$res = \Src\Install::rollback($dir);
line('Done. ' . $res['restored'] . ' file(s) restored, ' . $res['unchanged'] . ' already correct.');
line('This copy now reports v' . ($res['to'] ?: '?') . '.');
line('');
line('Restart the web server (or reload the page) to pick it up.');
line('');
exit(0);
}
// Default: install.
$pre = \Src\Install::preflight();
if (!$pre['ok']) {
fail('Cannot install yet: ' . implode(' ', $pre['problems']));
}
line('Installing v' . $pre['targetVersion'] . ' over v' . ($pre['currentVersion'] ?: '?') . '.');
line('A rollback copy is written to storage/backups first. Nothing under storage/ is touched.');
line('');
$res = \Src\Install::apply();
line('Installed ' . $res['installed'] . ' file(s). This copy now reports v' . ($res['to'] ?: '?') . '.');
line('Rollback copy: ' . $res['backup']);
line('');
line('If anything looks wrong: php install-update.php --rollback');
line('');
exit(0);
} catch (\Throwable $e) {
fail('Failed: ' . $e->getMessage() . "\n\n"
. "The app tree is as it was before the failing file. To undo the whole install:\n"
. " php install-update.php --rollback");
}