mirror of
https://github.com/whyour/qinglong.git
synced 2026-08-12 03:10:48 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a66cdbf17 | ||
|
|
3f880e6610 | ||
|
|
a2d313fe1b | ||
|
|
4cda665886 |
@@ -10,7 +10,6 @@ import Logger from '../loaders/logger';
|
|||||||
import { writeFileWithLock } from '../shared/utils';
|
import { writeFileWithLock } from '../shared/utils';
|
||||||
import { DependenceTypes } from '../data/dependence';
|
import { DependenceTypes } from '../data/dependence';
|
||||||
import { FormData } from 'undici';
|
import { FormData } from 'undici';
|
||||||
import os from 'os';
|
|
||||||
|
|
||||||
export * from './share';
|
export * from './share';
|
||||||
|
|
||||||
@@ -591,162 +590,3 @@ export function getUninstallCommand(
|
|||||||
export function isDemoEnv() {
|
export function isDemoEnv() {
|
||||||
return process.env.DeployEnv === 'demo';
|
return process.env.DeployEnv === 'demo';
|
||||||
}
|
}
|
||||||
|
|
||||||
// OS detection for Linux mirror configuration
|
|
||||||
let osType: 'Debian' | 'Ubuntu' | 'Alpine' | undefined;
|
|
||||||
|
|
||||||
async function getOSReleaseInfo(): Promise<string> {
|
|
||||||
try {
|
|
||||||
const osRelease = await fs.readFile('/etc/os-release', 'utf8');
|
|
||||||
return osRelease;
|
|
||||||
} catch (error) {
|
|
||||||
Logger.error(`Failed to read /etc/os-release: ${error}`);
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isDebian(osReleaseInfo: string): boolean {
|
|
||||||
return osReleaseInfo.includes('Debian');
|
|
||||||
}
|
|
||||||
|
|
||||||
function isUbuntu(osReleaseInfo: string): boolean {
|
|
||||||
return osReleaseInfo.includes('Ubuntu');
|
|
||||||
}
|
|
||||||
|
|
||||||
function isAlpine(osReleaseInfo: string): boolean {
|
|
||||||
return osReleaseInfo.includes('Alpine');
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function detectOS(): Promise<
|
|
||||||
'Debian' | 'Ubuntu' | 'Alpine' | undefined
|
|
||||||
> {
|
|
||||||
if (osType) return osType;
|
|
||||||
const platform = os.platform();
|
|
||||||
|
|
||||||
if (platform === 'linux') {
|
|
||||||
const osReleaseInfo = await getOSReleaseInfo();
|
|
||||||
// Check Ubuntu before Debian since Ubuntu is based on Debian
|
|
||||||
if (isUbuntu(osReleaseInfo)) {
|
|
||||||
osType = 'Ubuntu';
|
|
||||||
} else if (isDebian(osReleaseInfo)) {
|
|
||||||
osType = 'Debian';
|
|
||||||
} else if (isAlpine(osReleaseInfo)) {
|
|
||||||
osType = 'Alpine';
|
|
||||||
} else {
|
|
||||||
Logger.error(`Unknown Linux Distribution: ${osReleaseInfo}`);
|
|
||||||
console.error(`Unknown Linux Distribution: ${osReleaseInfo}`);
|
|
||||||
}
|
|
||||||
} else if (platform === 'darwin') {
|
|
||||||
osType = undefined;
|
|
||||||
} else {
|
|
||||||
Logger.error(`Unsupported platform: ${platform}`);
|
|
||||||
console.error(`Unsupported platform: ${platform}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return osType;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getCurrentMirrorDomain(
|
|
||||||
filePath: string,
|
|
||||||
): Promise<string | null> {
|
|
||||||
try {
|
|
||||||
const fileContent = await fs.readFile(filePath, 'utf8');
|
|
||||||
const lines = fileContent.split('\n');
|
|
||||||
for (const line of lines) {
|
|
||||||
if (line.trim().startsWith('#')) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const match = line.match(/https?:\/\/[^\/]+/);
|
|
||||||
if (match) {
|
|
||||||
return match[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
} catch (error) {
|
|
||||||
Logger.error(`Failed to read mirror configuration file ${filePath}: ${error}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeRegExp(string: string): string {
|
|
||||||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
||||||
}
|
|
||||||
|
|
||||||
async function replaceDomainInFile(
|
|
||||||
filePath: string,
|
|
||||||
oldDomainWithScheme: string,
|
|
||||||
newDomainWithScheme: string,
|
|
||||||
): Promise<void> {
|
|
||||||
// Ensure the new domain has a trailing slash before replacement
|
|
||||||
if (!newDomainWithScheme.endsWith('/')) {
|
|
||||||
newDomainWithScheme += '/';
|
|
||||||
}
|
|
||||||
|
|
||||||
let fileContent = await fs.readFile(filePath, 'utf8');
|
|
||||||
// Escape special regex characters in the old domain
|
|
||||||
const escapedOldDomain = escapeRegExp(oldDomainWithScheme);
|
|
||||||
let updatedContent = fileContent.replace(
|
|
||||||
new RegExp(escapedOldDomain, 'g'),
|
|
||||||
newDomainWithScheme,
|
|
||||||
);
|
|
||||||
|
|
||||||
await writeFileWithLock(filePath, updatedContent);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function _updateLinuxMirror(
|
|
||||||
osType: string,
|
|
||||||
mirrorDomainWithScheme: string,
|
|
||||||
): Promise<string> {
|
|
||||||
let filePath: string, currentDomainWithScheme: string | null;
|
|
||||||
switch (osType) {
|
|
||||||
case 'Debian':
|
|
||||||
filePath = '/etc/apt/sources.list.d/debian.sources';
|
|
||||||
currentDomainWithScheme = await getCurrentMirrorDomain(filePath);
|
|
||||||
if (currentDomainWithScheme) {
|
|
||||||
await replaceDomainInFile(
|
|
||||||
filePath,
|
|
||||||
currentDomainWithScheme,
|
|
||||||
mirrorDomainWithScheme || 'http://deb.debian.org',
|
|
||||||
);
|
|
||||||
return 'apt-get update';
|
|
||||||
} else {
|
|
||||||
throw Error(`Current mirror domain not found.`);
|
|
||||||
}
|
|
||||||
case 'Ubuntu':
|
|
||||||
filePath = '/etc/apt/sources.list.d/ubuntu.sources';
|
|
||||||
currentDomainWithScheme = await getCurrentMirrorDomain(filePath);
|
|
||||||
if (currentDomainWithScheme) {
|
|
||||||
await replaceDomainInFile(
|
|
||||||
filePath,
|
|
||||||
currentDomainWithScheme,
|
|
||||||
mirrorDomainWithScheme || 'http://archive.ubuntu.com',
|
|
||||||
);
|
|
||||||
return 'apt-get update';
|
|
||||||
} else {
|
|
||||||
throw Error(`Current mirror domain not found.`);
|
|
||||||
}
|
|
||||||
case 'Alpine':
|
|
||||||
filePath = '/etc/apk/repositories';
|
|
||||||
currentDomainWithScheme = await getCurrentMirrorDomain(filePath);
|
|
||||||
if (currentDomainWithScheme) {
|
|
||||||
await replaceDomainInFile(
|
|
||||||
filePath,
|
|
||||||
currentDomainWithScheme,
|
|
||||||
mirrorDomainWithScheme || 'http://dl-cdn.alpinelinux.org',
|
|
||||||
);
|
|
||||||
return 'apk update';
|
|
||||||
} else {
|
|
||||||
throw Error(`Current mirror domain not found.`);
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
throw Error('Unsupported OS type for updating mirrors.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function updateLinuxMirrorFile(mirror: string): Promise<string> {
|
|
||||||
const detectedOS = await detectOS();
|
|
||||||
if (!detectedOS) {
|
|
||||||
throw Error(`Unknown Linux Distribution`);
|
|
||||||
}
|
|
||||||
return await _updateLinuxMirror(detectedOS, mirror);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -65,7 +65,11 @@ export default async (src: string = 'deps') => {
|
|||||||
|
|
||||||
const source = path.join(config.rootPath, src);
|
const source = path.join(config.rootPath, src);
|
||||||
const watcher = chokidar.watch(source, {
|
const watcher = chokidar.watch(source, {
|
||||||
ignored: /(^|[\/\\])\../, // ignore dotfiles
|
ignored: [
|
||||||
|
/(^|[\/\\])\../, // ignore dotfiles
|
||||||
|
/(^|[\/\\])node_modules([\/\\]|$)/, // ignore node_modules
|
||||||
|
/(^|[\/\\])\.git([\/\\]|$)/, // ignore .git
|
||||||
|
],
|
||||||
persistent: true,
|
persistent: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+24
-10
@@ -17,7 +17,6 @@ import {
|
|||||||
readDirs,
|
readDirs,
|
||||||
rmPath,
|
rmPath,
|
||||||
setSystemTimezone,
|
setSystemTimezone,
|
||||||
updateLinuxMirrorFile,
|
|
||||||
} from '../config/util';
|
} from '../config/util';
|
||||||
import {
|
import {
|
||||||
DependenceModel,
|
DependenceModel,
|
||||||
@@ -215,11 +214,33 @@ export default class SystemService {
|
|||||||
onEnd?: () => void,
|
onEnd?: () => void,
|
||||||
) {
|
) {
|
||||||
const oDoc = await this.getSystemConfig();
|
const oDoc = await this.getSystemConfig();
|
||||||
|
await this.updateAuthDb({
|
||||||
|
...oDoc,
|
||||||
|
info: { ...oDoc.info, ...info },
|
||||||
|
});
|
||||||
|
let defaultDomain = 'https://dl-cdn.alpinelinux.org';
|
||||||
|
let targetDomain = 'https://dl-cdn.alpinelinux.org';
|
||||||
if (os.platform() !== 'linux') {
|
if (os.platform() !== 'linux') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const command = await updateLinuxMirrorFile(info.linuxMirror || '');
|
const content = await fs.promises.readFile('/etc/apk/repositories', {
|
||||||
let hasError = false;
|
encoding: 'utf-8',
|
||||||
|
});
|
||||||
|
const domainMatch = content.match(/(http.*)\/alpine\/.*/);
|
||||||
|
if (domainMatch) {
|
||||||
|
defaultDomain = domainMatch[1];
|
||||||
|
}
|
||||||
|
if (info.linuxMirror) {
|
||||||
|
targetDomain = info.linuxMirror;
|
||||||
|
}
|
||||||
|
const command = `sed -i 's/${defaultDomain.replace(
|
||||||
|
/\//g,
|
||||||
|
'\\/',
|
||||||
|
)}/${targetDomain.replace(
|
||||||
|
/\//g,
|
||||||
|
'\\/',
|
||||||
|
)}/g' /etc/apk/repositories && apk update -f`;
|
||||||
|
|
||||||
this.scheduleService.runTask(
|
this.scheduleService.runTask(
|
||||||
command,
|
command,
|
||||||
{
|
{
|
||||||
@@ -233,15 +254,8 @@ export default class SystemService {
|
|||||||
message: 'update linux mirror end',
|
message: 'update linux mirror end',
|
||||||
});
|
});
|
||||||
onEnd?.();
|
onEnd?.();
|
||||||
if (!hasError) {
|
|
||||||
await this.updateAuthDb({
|
|
||||||
...oDoc,
|
|
||||||
info: { ...oDoc.info, ...info },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
onError: async (message: string) => {
|
onError: async (message: string) => {
|
||||||
hasError = true;
|
|
||||||
this.sockService.sendMessage({ type: 'updateLinuxMirror', message });
|
this.sockService.sendMessage({ type: 'updateLinuxMirror', message });
|
||||||
},
|
},
|
||||||
onLog: async (message: string) => {
|
onLog: async (message: string) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user