mirror of
https://github.com/whyour/qinglong.git
synced 2026-08-06 00:34:33 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c329c8acd4 | |||
| d43d563622 | |||
| aa52cfb29d |
+164
-61
@@ -10,6 +10,7 @@ import Logger from '../loaders/logger';
|
||||
import { writeFileWithLock } from '../shared/utils';
|
||||
import { DependenceTypes } from '../data/dependence';
|
||||
import { FormData } from 'undici';
|
||||
import os from 'os';
|
||||
|
||||
export * from './share';
|
||||
|
||||
@@ -535,43 +536,12 @@ export async function setSystemTimezone(timezone: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to check if a name is a GitHub URL
|
||||
function isGitHubUrl(name: string): boolean {
|
||||
// Support git+https://, git+http://, https://, and http:// URLs
|
||||
// This covers GitHub URLs and other git-compatible repositories
|
||||
return !!name.match(/^(git\+https?:\/\/|https?:\/\/)/i);
|
||||
}
|
||||
|
||||
// Helper function to check if a name is a requirements file
|
||||
function isRequirementsFile(name: string): boolean {
|
||||
return !!name.match(/requirements.*\.(txt|in)$/i);
|
||||
}
|
||||
|
||||
// Helper function to check if a name is a pyproject.toml file
|
||||
function isPyprojectToml(name: string): boolean {
|
||||
return name.endsWith('pyproject.toml');
|
||||
}
|
||||
|
||||
export function getGetCommand(type: DependenceTypes, name: string): string {
|
||||
const trimmedName = name.trim();
|
||||
|
||||
// For Python dependencies installed from GitHub or requirements files,
|
||||
// we can't reliably check if they're installed, so skip the check
|
||||
if (type === DependenceTypes.python3) {
|
||||
if (isGitHubUrl(trimmedName) ||
|
||||
isRequirementsFile(trimmedName) ||
|
||||
isPyprojectToml(trimmedName)) {
|
||||
// Return a command that will always indicate not installed
|
||||
// This ensures GitHub URLs and requirements files are always installed
|
||||
return 'echo ""';
|
||||
}
|
||||
}
|
||||
|
||||
const baseCommands = {
|
||||
[DependenceTypes.nodejs]: `pnpm ls -g | grep "${trimmedName}" | head -1`,
|
||||
[DependenceTypes.nodejs]: `pnpm ls -g | grep "${name}" | head -1`,
|
||||
[DependenceTypes.python3]: `
|
||||
python3 -c "exec('''
|
||||
name='${trimmedName}'
|
||||
name='${name}'
|
||||
try:
|
||||
from importlib.metadata import version
|
||||
print(version(name))
|
||||
@@ -581,7 +551,7 @@ except:
|
||||
spec=u.find_spec(name)
|
||||
print(name if spec else '')
|
||||
''')"`,
|
||||
[DependenceTypes.linux]: `apk info -es ${trimmedName}`,
|
||||
[DependenceTypes.linux]: `apk info -es ${name}`,
|
||||
};
|
||||
|
||||
return baseCommands[type];
|
||||
@@ -601,33 +571,7 @@ export function getInstallCommand(type: DependenceTypes, name: string): string {
|
||||
command = `${command} --prefix=${PYTHON_INSTALL_DIR}`;
|
||||
}
|
||||
|
||||
const trimmedName = name.trim();
|
||||
|
||||
// Handle different installation methods for Python
|
||||
if (type === DependenceTypes.python3) {
|
||||
// Check if it's a GitHub URL (support both git+ and direct URLs)
|
||||
if (isGitHubUrl(trimmedName)) {
|
||||
return `${command} ${trimmedName}`;
|
||||
}
|
||||
// Check if it's a requirements file path
|
||||
if (isRequirementsFile(trimmedName)) {
|
||||
return `${command} -r ${trimmedName}`;
|
||||
}
|
||||
// Check if it's a pyproject.toml file
|
||||
if (isPyprojectToml(trimmedName)) {
|
||||
// For pyproject.toml, install from the directory containing it
|
||||
const pathMatch = trimmedName.match(/^(.+)\/pyproject\.toml$/);
|
||||
if (pathMatch) {
|
||||
// Has a path prefix, use the directory
|
||||
return `${command} ${pathMatch[1]}`;
|
||||
} else {
|
||||
// Just "pyproject.toml", install current directory
|
||||
return `${command} .`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return `${command} ${trimmedName}`;
|
||||
return `${command} ${name.trim()}`;
|
||||
}
|
||||
|
||||
export function getUninstallCommand(
|
||||
@@ -647,3 +591,162 @@ export function getUninstallCommand(
|
||||
export function isDemoEnv() {
|
||||
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);
|
||||
}
|
||||
|
||||
+10
-24
@@ -17,6 +17,7 @@ import {
|
||||
readDirs,
|
||||
rmPath,
|
||||
setSystemTimezone,
|
||||
updateLinuxMirrorFile,
|
||||
} from '../config/util';
|
||||
import {
|
||||
DependenceModel,
|
||||
@@ -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 = 'https://dl-cdn.alpinelinux.org';
|
||||
let targetDomain = 'https://dl-cdn.alpinelinux.org';
|
||||
if (os.platform() !== 'linux') {
|
||||
return;
|
||||
}
|
||||
const content = await fs.promises.readFile('/etc/apk/repositories', {
|
||||
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`;
|
||||
|
||||
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) => {
|
||||
|
||||
@@ -111,76 +111,6 @@ add_cron() {
|
||||
notify_api "$path 新增任务" "$detail"
|
||||
}
|
||||
|
||||
## 自动安装订阅仓库中的Python依赖
|
||||
auto_install_python_deps() {
|
||||
local repo_path="$1"
|
||||
local uniq_path="$2"
|
||||
|
||||
echo -e "\n检测订阅仓库中的Python依赖文件...\n"
|
||||
|
||||
get_token
|
||||
|
||||
# 检查 requirements.txt
|
||||
if [[ -f "${repo_path}/requirements.txt" ]]; then
|
||||
echo -e "发现 requirements.txt,开始自动安装依赖...\n"
|
||||
local req_file="${dir_scripts}/${uniq_path}/requirements.txt"
|
||||
|
||||
# 确保目标目录存在
|
||||
make_dir "${dir_scripts}/${uniq_path}"
|
||||
|
||||
# 复制文件并检查结果
|
||||
if cp -f "${repo_path}/requirements.txt" "${req_file}" 2>/dev/null; then
|
||||
# 调用API添加依赖安装任务
|
||||
local dep_name="${uniq_path}/requirements.txt"
|
||||
local currentTimeStamp=$(date +%s)
|
||||
local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \
|
||||
-X POST \
|
||||
-H "Content-Type: application/json;charset=UTF-8" \
|
||||
-H "Authorization: Bearer ${__ql_token__}" \
|
||||
--data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null)
|
||||
|
||||
local code=$(echo "$result" | jq -r '.code' 2>/dev/null)
|
||||
if [[ "$code" == "200" ]]; then
|
||||
echo -e "已添加 requirements.txt 依赖安装任务\n"
|
||||
else
|
||||
echo -e "添加 requirements.txt 依赖失败,请手动添加\n"
|
||||
fi
|
||||
else
|
||||
echo -e "复制 requirements.txt 失败,跳过自动安装\n"
|
||||
fi
|
||||
fi
|
||||
|
||||
# 检查 pyproject.toml
|
||||
if [[ -f "${repo_path}/pyproject.toml" ]]; then
|
||||
echo -e "发现 pyproject.toml,开始自动安装依赖...\n"
|
||||
local pyproject_file="${dir_scripts}/${uniq_path}/pyproject.toml"
|
||||
|
||||
# 确保目标目录存在
|
||||
make_dir "${dir_scripts}/${uniq_path}"
|
||||
|
||||
# 复制文件并检查结果
|
||||
if cp -f "${repo_path}/pyproject.toml" "${pyproject_file}" 2>/dev/null; then
|
||||
# 调用API添加依赖安装任务
|
||||
local dep_name="${uniq_path}/pyproject.toml"
|
||||
local currentTimeStamp=$(date +%s)
|
||||
local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \
|
||||
-X POST \
|
||||
-H "Content-Type: application/json;charset=UTF-8" \
|
||||
-H "Authorization: Bearer ${__ql_token__}" \
|
||||
--data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null)
|
||||
|
||||
local code=$(echo "$result" | jq -r '.code' 2>/dev/null)
|
||||
if [[ "$code" == "200" ]]; then
|
||||
echo -e "已添加 pyproject.toml 依赖安装任务\n"
|
||||
else
|
||||
echo -e "添加 pyproject.toml 依赖失败,请手动添加\n"
|
||||
fi
|
||||
else
|
||||
echo -e "复制 pyproject.toml 失败,跳过自动安装\n"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
## 更新仓库
|
||||
update_repo() {
|
||||
local url="$1"
|
||||
@@ -207,10 +137,6 @@ update_repo() {
|
||||
|
||||
if [[ $exit_status -eq 0 ]]; then
|
||||
echo -e "拉取 ${uniq_path} 成功...\n"
|
||||
|
||||
# 自动检测并安装Python依赖
|
||||
auto_install_python_deps "${repo_path}" "${uniq_path}"
|
||||
|
||||
diff_scripts "$repo_path" "$author" "$path" "$blackword" "$dependence" "$extensions" "$autoAddCron" "$autoDelCron"
|
||||
else
|
||||
echo -e "拉取 ${uniq_path} 失败,请检查日志...\n"
|
||||
|
||||
@@ -22,9 +22,6 @@ const DependenceModal = ({
|
||||
}) => {
|
||||
const [form] = Form.useForm();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [selectedType, setSelectedType] = useState(
|
||||
DependenceTypes[defaultType as any],
|
||||
);
|
||||
|
||||
const handleOk = async (values: any) => {
|
||||
setLoading(true);
|
||||
@@ -93,7 +90,7 @@ const DependenceModal = ({
|
||||
label={intl.get('依赖类型')}
|
||||
initialValue={DependenceTypes[defaultType as any]}
|
||||
>
|
||||
<Select onChange={(value) => setSelectedType(value)}>
|
||||
<Select>
|
||||
{config.dependenceTypes.map((x, i) => (
|
||||
<Option key={i} value={i}>
|
||||
{x}
|
||||
@@ -124,24 +121,11 @@ const DependenceModal = ({
|
||||
whitespace: true,
|
||||
},
|
||||
]}
|
||||
tooltip={
|
||||
selectedType === DependenceTypes.python3
|
||||
? intl.get(
|
||||
'Python支持多种安装方式:\n1. 包名(如:requests)\n2. GitHub链接(如:git+https://github.com/user/repo.git)\n3. requirements文件路径(如:path/to/requirements.txt)\n4. pyproject.toml文件路径',
|
||||
)
|
||||
: undefined
|
||||
}
|
||||
>
|
||||
<Input.TextArea
|
||||
rows={4}
|
||||
autoSize={{ minRows: 1, maxRows: 5 }}
|
||||
placeholder={
|
||||
selectedType === DependenceTypes.python3
|
||||
? intl.get(
|
||||
'支持包名、GitHub链接、requirements.txt或pyproject.toml路径',
|
||||
)
|
||||
: intl.get('请输入依赖名称')
|
||||
}
|
||||
placeholder={intl.get('请输入依赖名称')}
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item name="remark" label={intl.get('备注')}>
|
||||
|
||||
Reference in New Issue
Block a user