@lethevimlet/sshift 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -9,7 +9,6 @@ const { getDataDir } = require('../../utils/config');
|
|
|
9
9
|
const SSL_CERT_FILE = 'ssl-cert.pem';
|
|
10
10
|
const SSL_KEY_FILE = 'ssl-key.pem';
|
|
11
11
|
const UPDATE_MARKER_FILE = '.updating';
|
|
12
|
-
const RESTART_MARKER_FILE = '.restart-after-update';
|
|
13
12
|
|
|
14
13
|
/**
|
|
15
14
|
* Register system endpoints
|
|
@@ -17,21 +16,24 @@ const RESTART_MARKER_FILE = '.restart-after-update';
|
|
|
17
16
|
* @param {Object} io - Socket.IO instance
|
|
18
17
|
*/
|
|
19
18
|
function registerSystemEndpoints(app, io) {
|
|
20
|
-
// Clean up stale update marker
|
|
19
|
+
// Clean up stale update marker from previous update attempt
|
|
21
20
|
const dataDir = getDataDir();
|
|
22
21
|
const staleUpdateMarker = path.join(dataDir, UPDATE_MARKER_FILE);
|
|
23
|
-
const
|
|
22
|
+
const staleUpdateScript = path.join(dataDir, '.sshift-update.sh');
|
|
23
|
+
const stalePsScript = path.join(dataDir, '.sshift-update.ps1');
|
|
24
24
|
try {
|
|
25
25
|
if (fs.existsSync(staleUpdateMarker)) {
|
|
26
26
|
fs.unlinkSync(staleUpdateMarker);
|
|
27
27
|
console.log('[UPDATE] Cleaned up stale update marker');
|
|
28
28
|
}
|
|
29
|
-
if (fs.existsSync(
|
|
30
|
-
fs.unlinkSync(
|
|
31
|
-
|
|
29
|
+
if (fs.existsSync(staleUpdateScript)) {
|
|
30
|
+
fs.unlinkSync(staleUpdateScript);
|
|
31
|
+
}
|
|
32
|
+
if (fs.existsSync(stalePsScript)) {
|
|
33
|
+
fs.unlinkSync(stalePsScript);
|
|
32
34
|
}
|
|
33
35
|
} catch (e) {
|
|
34
|
-
console.error('[UPDATE] Failed to clean up marker
|
|
36
|
+
console.error('[UPDATE] Failed to clean up marker file:', e.message);
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
// API: Get version
|
|
@@ -131,7 +133,6 @@ function registerSystemEndpoints(app, io) {
|
|
|
131
133
|
app.get('/api/update-status', (req, res) => {
|
|
132
134
|
const dataDir = getDataDir();
|
|
133
135
|
const updateMarker = path.join(dataDir, UPDATE_MARKER_FILE);
|
|
134
|
-
const restartMarker = path.join(dataDir, RESTART_MARKER_FILE);
|
|
135
136
|
|
|
136
137
|
// Read package.json for current version
|
|
137
138
|
let version = 'unknown';
|
|
@@ -145,13 +146,11 @@ function registerSystemEndpoints(app, io) {
|
|
|
145
146
|
|
|
146
147
|
// Check if we're in the middle of an update
|
|
147
148
|
const isUpdating = fs.existsSync(updateMarker);
|
|
148
|
-
const restartRequested = fs.existsSync(restartMarker);
|
|
149
149
|
|
|
150
150
|
res.json({
|
|
151
151
|
version,
|
|
152
152
|
updating: isUpdating,
|
|
153
|
-
|
|
154
|
-
ready: !isUpdating // Server is ready if not updating
|
|
153
|
+
ready: !isUpdating
|
|
155
154
|
});
|
|
156
155
|
});
|
|
157
156
|
|
|
@@ -194,20 +193,6 @@ function registerSystemEndpoints(app, io) {
|
|
|
194
193
|
const platform = process.platform;
|
|
195
194
|
const dataDir = getDataDir();
|
|
196
195
|
|
|
197
|
-
// Determine the install script based on platform
|
|
198
|
-
let installScript;
|
|
199
|
-
if (platform === 'win32') {
|
|
200
|
-
installScript = path.join(__dirname, '../../../../sshift-install.ps1');
|
|
201
|
-
} else {
|
|
202
|
-
installScript = path.join(__dirname, '../../../../sshift-install.sh');
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Check if install script exists
|
|
206
|
-
if (!fs.existsSync(installScript)) {
|
|
207
|
-
res.status(500).json({ error: 'Install script not found' });
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
196
|
// Write update marker to indicate update in progress
|
|
212
197
|
const updateMarker = path.join(dataDir, UPDATE_MARKER_FILE);
|
|
213
198
|
try {
|
|
@@ -221,48 +206,62 @@ function registerSystemEndpoints(app, io) {
|
|
|
221
206
|
console.error('[UPDATE] Failed to write update marker:', e.message);
|
|
222
207
|
}
|
|
223
208
|
|
|
224
|
-
// Write a restart marker file to indicate we want to restart after update
|
|
225
|
-
const restartMarker = path.join(dataDir, RESTART_MARKER_FILE);
|
|
226
|
-
try {
|
|
227
|
-
fs.writeFileSync(restartMarker, 'true');
|
|
228
|
-
} catch (e) {
|
|
229
|
-
console.error('[UPDATE] Failed to write restart marker:', e.message);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
209
|
// Send response immediately, then update in background
|
|
233
210
|
res.json({ message: 'Update started. Server will restart automatically.' });
|
|
234
211
|
|
|
235
|
-
//
|
|
236
|
-
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
212
|
+
// Write a temporary update script and run it detached
|
|
213
|
+
const nodeExe = process.execPath;
|
|
214
|
+
const sshiftScript = process.argv[1];
|
|
215
|
+
const updateScriptPath = path.join(dataDir, '.sshift-update.sh');
|
|
216
|
+
|
|
217
|
+
if (platform === 'win32') {
|
|
218
|
+
const psScript = `npm install -g @lethevimlet/sshift@latest; & '${nodeExe}' '${sshiftScript}'`;
|
|
219
|
+
fs.writeFileSync(updateScriptPath.replace('.sh', '.ps1'), psScript, 'utf8');
|
|
220
|
+
} else {
|
|
221
|
+
const updateScript = `#!/bin/sh
|
|
222
|
+
npm install -g @lethevimlet/sshift@latest
|
|
223
|
+
exec "${nodeExe}" "${sshiftScript}"
|
|
224
|
+
`;
|
|
225
|
+
fs.writeFileSync(updateScriptPath, updateScript, 'utf8');
|
|
226
|
+
fs.chmodSync(updateScriptPath, 0o755);
|
|
227
|
+
}
|
|
242
228
|
|
|
243
229
|
console.log('[UPDATE] Starting update process...');
|
|
244
230
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
231
|
+
let spawnFailed = false;
|
|
232
|
+
let spawnCommand, spawnArgs;
|
|
233
|
+
if (platform === 'win32') {
|
|
234
|
+
spawnCommand = 'powershell';
|
|
235
|
+
spawnArgs = ['-ExecutionPolicy', 'Bypass', '-File', updateScriptPath.replace('.sh', '.ps1')];
|
|
236
|
+
} else {
|
|
237
|
+
spawnCommand = '/bin/sh';
|
|
238
|
+
spawnArgs = [updateScriptPath];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const updateProcess = spawn(spawnCommand, spawnArgs, {
|
|
242
|
+
cwd: dataDir,
|
|
243
|
+
detached: true,
|
|
244
|
+
stdio: 'ignore',
|
|
245
|
+
env: { ...process.env }
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
updateProcess.on('error', (err) => {
|
|
249
|
+
console.error('[UPDATE] Failed to start update process:', err.message);
|
|
250
|
+
spawnFailed = true;
|
|
251
|
+
// Clean up marker since update failed
|
|
252
|
+
try { fs.unlinkSync(updateMarker); } catch (e) { /* ignore */ }
|
|
265
253
|
});
|
|
254
|
+
|
|
255
|
+
updateProcess.unref();
|
|
256
|
+
|
|
257
|
+
setTimeout(() => {
|
|
258
|
+
if (spawnFailed) {
|
|
259
|
+
console.error('[UPDATE] Update process failed to start. Keeping server running.');
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
console.log('[UPDATE] Exiting for update...');
|
|
263
|
+
process.exit(0);
|
|
264
|
+
}, 500);
|
|
266
265
|
} catch (err) {
|
|
267
266
|
console.error('Error triggering update:', err);
|
|
268
267
|
res.status(500).json({ error: 'Failed to trigger update' });
|