安装 linux 依赖自动识别 alpine 和 debian

This commit is contained in:
whyour
2025-12-11 01:53:50 +08:00
parent f942b83eb6
commit b9380c53d6
6 changed files with 237 additions and 40 deletions
+38 -10
View File
@@ -22,6 +22,8 @@ import {
} from '../config/util';
import dayjs from 'dayjs';
import taskLimit from '../shared/pLimit';
import { detectOS } from '../config/util';
import { LINUX_DEPENDENCE_COMMAND } from '../config/const';
@Service()
export default class DependenceService {
@@ -159,8 +161,19 @@ export default class DependenceService {
const docs = await DependenceModel.findAll({ where: { id: ids } });
for (const doc of docs) {
taskLimit.removeQueuedDependency(doc);
const depInstallCommand = getInstallCommand(doc.type, doc.name);
const depUnInstallCommand = getUninstallCommand(doc.type, doc.name);
let depInstallCommand = getInstallCommand(doc.type, doc.name);
let depUnInstallCommand = getUninstallCommand(doc.type, doc.name);
const isLinuxDependence = doc.type === DependenceTypes.linux;
if (isLinuxDependence) {
const osType = await detectOS();
if (!osType) {
continue;
}
const linuxCommand = LINUX_DEPENDENCE_COMMAND[osType];
depInstallCommand = `${linuxCommand.install} ${doc.name.trim()}`;
depUnInstallCommand = `${linuxCommand.uninstall} ${doc.name.trim()}`;
}
const pids = await Promise.all([
getPid(depInstallCommand),
getPid(depUnInstallCommand),
@@ -217,7 +230,17 @@ export default class DependenceService {
if (taskLimit.firstDependencyId !== dependency.id) {
return resolve(null);
}
const isNodeDependence = dependency.type === DependenceTypes.nodejs;
const isLinuxDependence = dependency.type === DependenceTypes.linux;
const isPythonDependence = dependency.type === DependenceTypes.python3;
const osType = await detectOS();
let linuxCommand = {} as typeof LINUX_DEPENDENCE_COMMAND.Alpine;
if (isLinuxDependence) {
if (!osType) {
return resolve(null);
}
linuxCommand = LINUX_DEPENDENCE_COMMAND[osType];
}
taskLimit.removeQueuedDependency(dependency);
const depIds = [dependency.id!];
@@ -230,9 +253,14 @@ export default class DependenceService {
? 'installDependence'
: 'uninstallDependence';
let depName = dependency.name.trim();
const command = isInstall
let depRunCommand = isInstall
? getInstallCommand(dependency.type, depName)
: getUninstallCommand(dependency.type, depName);
if (isLinuxDependence) {
depRunCommand = isInstall
? linuxCommand.install
: linuxCommand.uninstall;
}
const actionText = isInstall ? '安装' : '删除';
const startTime = dayjs();
@@ -248,8 +276,12 @@ export default class DependenceService {
// 判断是否已经安装过依赖
if (isInstall && !force) {
const getCommand = getGetCommand(dependency.type, depName);
let getCommand = getGetCommand(dependency.type, depName);
const depVersionStr = versionDependenceCommandTypes[dependency.type];
if (isLinuxDependence) {
getCommand = `${linuxCommand.info} ${depName}`;
}
let depVersion = '';
if (depName.includes(depVersionStr)) {
const symbolRegx = new RegExp(
@@ -261,10 +293,6 @@ export default class DependenceService {
depVersion = _depVersion;
}
}
const isNodeDependence = dependency.type === DependenceTypes.nodejs;
const isLinuxDependence = dependency.type === DependenceTypes.linux;
const isPythonDependence =
dependency.type === DependenceTypes.python3;
const depInfo = (await promiseExecSuccess(getCommand))
.replace(/\s{2,}/, ' ')
.replace(/\s+$/, '');
@@ -273,7 +301,7 @@ export default class DependenceService {
depInfo &&
((isNodeDependence && depInfo.split(' ')?.[0] === depName) ||
(isLinuxDependence &&
depInfo.toLocaleLowerCase().includes('apt-manual-installed')) ||
linuxCommand.check(depInfo.toLocaleLowerCase())) ||
isPythonDependence) &&
(!depVersion || depInfo.includes(depVersion))
) {
+10 -24
View File
@@ -37,6 +37,7 @@ import ScheduleService, { TaskCallbacks } from './schedule';
import SockService from './sock';
import os from 'os';
import dayjs from 'dayjs';
import { updateLinuxMirrorFile } from '../config/util';
@Service()
export default class SystemService {
@@ -214,33 +215,11 @@ export default class SystemService {
onEnd?: () => void,
) {
const oDoc = await this.getSystemConfig();
await this.updateAuthDb({
...oDoc,
info: { ...oDoc.info, ...info },
});
let defaultDomain = 'http://deb.debian.org';
let targetDomain = 'http://deb.debian.org';
if (os.platform() !== 'linux') {
return;
}
const content = await fs.promises.readFile('/etc/apt/sources.list', {
encoding: 'utf-8',
});
const domainMatch = content.match(/(http.*)\/debian /);
if (domainMatch) {
defaultDomain = domainMatch[1];
}
if (info.linuxMirror) {
targetDomain = info.linuxMirror;
}
const command = `sed -i 's/${defaultDomain.replace(
/\//g,
'\\/',
)}/${targetDomain.replace(
/\//g,
'\\/',
)}/g' /etc/apt/sources.list && apt update`;
const command = await updateLinuxMirrorFile(info.linuxMirror || '');
let hasError = false;
this.scheduleService.runTask(
command,
{
@@ -254,8 +233,15 @@ export default class SystemService {
message: 'update linux mirror end',
});
onEnd?.();
if (!hasError) {
await this.updateAuthDb({
...oDoc,
info: { ...oDoc.info, ...info },
});
}
},
onError: async (message: string) => {
hasError = true;
this.sockService.sendMessage({ type: 'updateLinuxMirror', message });
},
onLog: async (message: string) => {