mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-28 09:02:12 +08:00
* feat(cli): add unified Commander CLI for QingLong 2.x * fix(cli): publish via npm and address security review feedback * ci(cli): package npm artifacts and remove evaluation collateral * test(cli): use a fixed shell fixture for log retention * refactor(cli): separate remote npm client from panel tools * feat(cli): cover active panel OpenAPI resources * docs(cli): unify authentication and skill guidance * refactor(cli): isolate internal commands and generate Commander help * refactor(cli): organize remote and internal modules by responsibility * ci(cli): publish verified npm archives from master * fix(cli): publish under the whyour npm scope * ci: use npm trusted publishing for both packages * docs: introduce the published CLI on the project homepage * fix(cli): preserve server log truncation and correct login hints * fix(cli): accept dashboard record request bodies * fix(cli): preserve stdin for local task execution * fix(cli): resolve task executables after changing directory * fix(cli): preserve shell function tasks and sanitize test failures * fix(cli): preserve shell hook state and resolve workdir after hooks * fix(cli): preserve cleanup across shared shell task timeouts * fix(cli): isolate shell control descriptors and reap timed-out descendants
117 lines
4.0 KiB
JavaScript
117 lines
4.0 KiB
JavaScript
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs/promises');
|
|
const { randomUUID } = require('node:crypto');
|
|
const { createContext } = require('../../dist/internal/runtime/context');
|
|
const { installPanelDependencies } = require('../../dist/internal/maintenance/operator');
|
|
const { reloadPanel } = require('../../dist/internal/maintenance/upgrade');
|
|
assert.equal(process.env.QL_PANEL_INTEGRATION, '1');
|
|
(async () => {
|
|
let token;
|
|
const api = async (endpoint, method = 'GET', body) => {
|
|
const response = await fetch(`http://127.0.0.1:5700/api/${endpoint}`, {
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
},
|
|
body: body === undefined ? undefined : JSON.stringify(body),
|
|
signal: AbortSignal.timeout(10000),
|
|
});
|
|
const result = await response.json();
|
|
assert.equal(result.code, 200, `${method} ${endpoint}: ${result.code}`);
|
|
return result;
|
|
};
|
|
const before = (await api('system')).data;
|
|
assert.equal(before.version, '2.19.0');
|
|
assert.equal(before.isInitialized, false);
|
|
assert.equal(process.env.QL_CLI_ROOT, '/opt/qinglong-cli');
|
|
const verifySelection = async () => {
|
|
for (const [name, module] of [
|
|
['task', 'runner'],
|
|
['ql', 'compat'],
|
|
]) {
|
|
const entry = `/root/bin/${name}`;
|
|
assert.equal((await fs.lstat(entry)).isSymbolicLink(), false);
|
|
assert.ok(
|
|
(await fs.readFile(entry, 'utf8')).includes(
|
|
`/opt/qinglong-cli/dist/${module}.js`,
|
|
),
|
|
);
|
|
}
|
|
};
|
|
await verifySelection();
|
|
|
|
const credentials = {
|
|
username: `upgrade-${randomUUID()}`,
|
|
password: randomUUID(),
|
|
};
|
|
await api('user/init', 'PUT', credentials);
|
|
token = (await api('user/login', 'POST', credentials)).data.token;
|
|
await fs.writeFile(
|
|
'/ql/data/scripts/upgrade-fixture.js',
|
|
'console.log("UPGRADE_PRESERVED_TASK");',
|
|
);
|
|
const task = (
|
|
await api('crons', 'POST', {
|
|
name: 'upgrade retained task',
|
|
command: 'task upgrade-fixture.js',
|
|
schedule: '0 0 1 1 *',
|
|
})
|
|
).data;
|
|
await fs.appendFile(
|
|
'/ql/data/config/config.sh',
|
|
'\n# UPGRADE_PRESERVED_CONFIG\n',
|
|
);
|
|
const config = await fs.readFile('/ql/data/config/config.sh');
|
|
const dotenv = await fs.readFile('/ql/.env');
|
|
const context = createContext({ root: '/ql' }, process.env);
|
|
await installPanelDependencies(context, '/stage/source');
|
|
const replacement = await reloadPanel(context, 'system', {
|
|
source: '/stage/source',
|
|
static: '/stage/static',
|
|
});
|
|
assert.deepEqual(replacement.retainedBackups, []);
|
|
let after;
|
|
for (let i = 0; i < 60; i++) {
|
|
try {
|
|
after = (await api('system')).data;
|
|
if (after.version === '2.20.1') break;
|
|
} catch {}
|
|
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
}
|
|
assert.equal(after?.version, '2.20.1');
|
|
await verifySelection();
|
|
token = (await api('user/login', 'POST', credentials)).data.token;
|
|
const persisted = (await api(`crons/${task.id}`)).data;
|
|
assert.equal(persisted.name, task.name);
|
|
assert.equal(persisted.command, task.command);
|
|
assert.deepEqual(await fs.readFile('/ql/data/config/config.sh'), config);
|
|
assert.deepEqual(await fs.readFile('/ql/.env'), dotenv);
|
|
await api('crons/run', 'PUT', [task.id]);
|
|
let log = '';
|
|
for (let i = 0; i < 60; i++) {
|
|
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
log = (await api(`crons/${task.id}/log`)).data;
|
|
if (log.includes('UPGRADE_PRESERVED_TASK') && /完成|执行结束/.test(log))
|
|
break;
|
|
}
|
|
assert.match(log, /UPGRADE_PRESERVED_TASK/);
|
|
assert.match(log, /完成|执行结束/);
|
|
console.log(
|
|
JSON.stringify({
|
|
from: before.version,
|
|
to: after.version,
|
|
tsCommandSelectionPreserved: true,
|
|
accountPreserved: true,
|
|
taskPreserved: true,
|
|
scriptExecuted: true,
|
|
configurationPreserved: true,
|
|
dotenvPreserved: true,
|
|
backupsCleaned: true,
|
|
}),
|
|
);
|
|
})().catch((error) => {
|
|
console.error(error.stack);
|
|
process.exitCode = 1;
|
|
});
|