Initial commit: TaskPool React panel
- React frontend with route-level code splitting - Backend rebranded from Baihu to TaskPool - DB brand migration script and local compatibility
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const { URL } = require('url');
|
||||
|
||||
/**
|
||||
* 内部 API 请求辅助函数
|
||||
*/
|
||||
function request(urlStr, method = 'GET', data = null) {
|
||||
const token = process.env.BHPKG_OPENAPI_TOKEN || process.env.OPENAPI_TOKEN || process.env.BHPKG_NOTIFY_TOKEN;
|
||||
if (!token) {
|
||||
throw new Error(`没有正确配置或缺少 BHPKG_OPENAPI_TOKEN 环境变量以使用 env 函数。请在任务池的任务设置中配置这些 Key。`);
|
||||
}
|
||||
|
||||
const parsedUrl = new URL(urlStr);
|
||||
const protocol = parsedUrl.protocol === 'https:' ? https : http;
|
||||
|
||||
let payload = '';
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
};
|
||||
|
||||
if (data !== null) {
|
||||
payload = JSON.stringify(data);
|
||||
headers['Content-Length'] = Buffer.byteLength(payload);
|
||||
}
|
||||
|
||||
const options = {
|
||||
hostname: parsedUrl.hostname,
|
||||
port: parsedUrl.port,
|
||||
path: parsedUrl.pathname + (parsedUrl.search || ''),
|
||||
method: method,
|
||||
headers: headers
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = protocol.request(options, (res) => {
|
||||
let body = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', (chunk) => body += chunk);
|
||||
res.on('end', () => {
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
try {
|
||||
const parsed = body ? JSON.parse(body) : {};
|
||||
if (parsed && typeof parsed === 'object' && parsed.code !== undefined && parsed.code !== 200) {
|
||||
reject(new Error(`请求失败 [${parsed.code}]: ${parsed.msg || parsed.message || '未知错误'}`));
|
||||
} else {
|
||||
resolve(parsed);
|
||||
}
|
||||
} catch (e) {
|
||||
resolve(body);
|
||||
}
|
||||
} else {
|
||||
let errMsg = body;
|
||||
try {
|
||||
const parsed = JSON.parse(body);
|
||||
errMsg = parsed.msg || parsed.message || body;
|
||||
} catch(e) {}
|
||||
reject(new Error(`请求失败 [${res.statusCode}]: ${errMsg}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (e) => reject(e));
|
||||
if (payload) {
|
||||
req.write(payload);
|
||||
}
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function getEnvsUrl() {
|
||||
const url = process.env.BHPKG_OPENAPI_URL || process.env.OPENAPI_URL;
|
||||
if (url) return url;
|
||||
|
||||
const notifyUrl = process.env.BHPKG_NOTIFY_URL || 'http://localhost:8052/api/v1/notify/send';
|
||||
const targets = ['/api/v1/notify/send/', '/api/v1/notify/send', '/api/v1/notify/', '/api/v1/notify'];
|
||||
for (const target of targets) {
|
||||
if (notifyUrl.includes(target)) {
|
||||
return notifyUrl.replace(target, '/open2api/v1/env');
|
||||
}
|
||||
}
|
||||
return 'http://localhost:8052/open2api/v1/env';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的环境变量列表
|
||||
*/
|
||||
async function getEnvs() {
|
||||
const url = `${getEnvsUrl()}/all`;
|
||||
const res = await request(url, 'GET');
|
||||
return res.data || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据变量名获取环境变量,不存在则返回 null
|
||||
*/
|
||||
async function getEnv(name) {
|
||||
const envs = await getEnvs();
|
||||
for (const env of envs) {
|
||||
if (env.name === name) {
|
||||
return env;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量添加环境变量
|
||||
*/
|
||||
async function addEnvs(envsList) {
|
||||
const url = getEnvsUrl();
|
||||
const addedEnvs = [];
|
||||
for (const env of envsList) {
|
||||
if (!env.name || !env.value) {
|
||||
throw new Error("环境变量必须包含 'name' 和 'value'");
|
||||
}
|
||||
const res = await request(url, 'POST', env);
|
||||
if (res.data) {
|
||||
addedEnvs.push(res.data);
|
||||
}
|
||||
}
|
||||
return addedEnvs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加单个环境变量
|
||||
*/
|
||||
async function addEnv(name, value, remark = "", type = "normal", hidden = true, enabled = true) {
|
||||
const url = getEnvsUrl();
|
||||
const payload = {
|
||||
name,
|
||||
value,
|
||||
remark,
|
||||
type,
|
||||
hidden,
|
||||
enabled
|
||||
};
|
||||
const res = await request(url, 'POST', payload);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 更新环境变量
|
||||
*/
|
||||
async function updateEnv(id, name, value, remark = null, type = null, hidden = null, enabled = null) {
|
||||
const url = `${getEnvsUrl()}/${id}`;
|
||||
const payload = {};
|
||||
if (name !== null) payload.name = name;
|
||||
if (value !== null) payload.value = value;
|
||||
if (remark !== null) payload.remark = remark;
|
||||
if (type !== null) payload.type = type;
|
||||
if (hidden !== null) payload.hidden = hidden;
|
||||
if (enabled !== null) payload.enabled = enabled;
|
||||
|
||||
const res = await request(url, 'PUT', payload);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除环境变量
|
||||
*/
|
||||
async function deleteEnvs(ids) {
|
||||
for (const id of ids) {
|
||||
await deleteEnv(id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 删除指定环境变量
|
||||
*/
|
||||
async function deleteEnv(id) {
|
||||
const url = `${getEnvsUrl()}/${id}`;
|
||||
await request(url, 'DELETE');
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getEnvs,
|
||||
getEnv,
|
||||
addEnvs,
|
||||
addEnv,
|
||||
updateEnv,
|
||||
deleteEnvs,
|
||||
deleteEnv
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
const { notify } = require('./notify');
|
||||
const {
|
||||
getEnvs,
|
||||
getEnv,
|
||||
addEnvs,
|
||||
addEnv,
|
||||
updateEnv,
|
||||
deleteEnvs,
|
||||
deleteEnv
|
||||
} = require('./env');
|
||||
const {
|
||||
getTasks,
|
||||
getTask,
|
||||
updateTask,
|
||||
deleteTask,
|
||||
executeTask,
|
||||
stopTask,
|
||||
getLastResults
|
||||
} = require('./task');
|
||||
|
||||
module.exports = {
|
||||
notify,
|
||||
getEnvs,
|
||||
getEnv,
|
||||
addEnvs,
|
||||
addEnv,
|
||||
updateEnv,
|
||||
deleteEnvs,
|
||||
deleteEnv,
|
||||
getTasks,
|
||||
getTask,
|
||||
updateTask,
|
||||
deleteTask,
|
||||
executeTask,
|
||||
stopTask,
|
||||
getLastResults
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const { URL } = require('url');
|
||||
|
||||
/**
|
||||
* 发送通知的辅助函数 (仅使用 Node.js 标准库)
|
||||
*/
|
||||
function notify(title, text, channelId) {
|
||||
const token = process.env.BHPKG_NOTIFY_TOKEN;
|
||||
const channel = process.env.BHPKG_NOTIFY_CHANNEL;
|
||||
|
||||
if (!token || !channel) {
|
||||
const missing = [];
|
||||
if (!token) missing.push("BHPKG_NOTIFY_TOKEN");
|
||||
if (!channel) missing.push("BHPKG_NOTIFY_CHANNEL");
|
||||
throw new Error(`没有正确配置或缺少 ${missing.join(" 和 ")} 环境变量以使用 notify 函数。请在任务池的任务设置中配置这些 Key。`);
|
||||
}
|
||||
|
||||
const notifyUrl = process.env.BHPKG_NOTIFY_URL || 'http://localhost:8052/api/v1/notify/send';
|
||||
const cid = channelId || channel;
|
||||
|
||||
if (!notifyUrl || !token || !cid) return;
|
||||
|
||||
const parsedUrl = new URL(notifyUrl);
|
||||
const protocol = parsedUrl.protocol === 'https:' ? https : http;
|
||||
|
||||
const data = JSON.stringify({
|
||||
channel_id: cid,
|
||||
title: title || '系统通知',
|
||||
text: text
|
||||
});
|
||||
|
||||
const options = {
|
||||
hostname: parsedUrl.hostname,
|
||||
port: parsedUrl.port,
|
||||
path: parsedUrl.pathname + (parsedUrl.search || ''),
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'notify-token': token,
|
||||
'Content-Length': Buffer.byteLength(data)
|
||||
}
|
||||
};
|
||||
|
||||
const req = protocol.request(options);
|
||||
req.on('error', (e) => {});
|
||||
req.write(data);
|
||||
req.end();
|
||||
}
|
||||
|
||||
module.exports = { notify };
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "taskpool",
|
||||
"version": "1.0.0",
|
||||
"description": "TaskPool internal helper for Node.js",
|
||||
"main": "index.js",
|
||||
"license": "MIT"
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const { URL } = require('url');
|
||||
|
||||
/**
|
||||
* 内部 API 请求辅助函数
|
||||
*/
|
||||
function request(urlStr, method = 'GET', data = null) {
|
||||
const token = process.env.BHPKG_OPENAPI_TOKEN || process.env.OPENAPI_TOKEN || process.env.BHPKG_NOTIFY_TOKEN;
|
||||
if (!token) {
|
||||
throw new Error(`没有正确配置或缺少 BHPKG_OPENAPI_TOKEN 环境变量以使用 task 函数。请在任务池的任务设置中配置这些 Key。`);
|
||||
}
|
||||
|
||||
const parsedUrl = new URL(urlStr);
|
||||
const protocol = parsedUrl.protocol === 'https:' ? https : http;
|
||||
|
||||
let payload = '';
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
};
|
||||
|
||||
if (data !== null) {
|
||||
payload = JSON.stringify(data);
|
||||
headers['Content-Length'] = Buffer.byteLength(payload);
|
||||
}
|
||||
|
||||
const options = {
|
||||
hostname: parsedUrl.hostname,
|
||||
port: parsedUrl.port,
|
||||
path: parsedUrl.pathname + (parsedUrl.search || ''),
|
||||
method: method,
|
||||
headers: headers
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = protocol.request(options, (res) => {
|
||||
let body = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', (chunk) => body += chunk);
|
||||
res.on('end', () => {
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
try {
|
||||
const parsed = body ? JSON.parse(body) : {};
|
||||
if (parsed && typeof parsed === 'object' && parsed.code !== undefined && parsed.code !== 200) {
|
||||
reject(new Error(`请求失败 [${parsed.code}]: ${parsed.msg || parsed.message || '未知错误'}`));
|
||||
} else {
|
||||
resolve(parsed);
|
||||
}
|
||||
} catch (e) {
|
||||
resolve(body);
|
||||
}
|
||||
} else {
|
||||
let errMsg = body;
|
||||
try {
|
||||
const parsed = JSON.parse(body);
|
||||
errMsg = parsed.msg || parsed.message || body;
|
||||
} catch(e) {}
|
||||
reject(new Error(`请求失败 [${res.statusCode}]: ${errMsg}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (e) => reject(e));
|
||||
if (payload) {
|
||||
req.write(payload);
|
||||
}
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function getBaseUrl() {
|
||||
const url = process.env.BHPKG_OPENAPI_URL || process.env.OPENAPI_URL;
|
||||
if (url) {
|
||||
if (url.endsWith('/env')) return url.slice(0, -4);
|
||||
if (url.endsWith('/env/')) return url.slice(0, -5);
|
||||
return url;
|
||||
}
|
||||
|
||||
const notifyUrl = process.env.BHPKG_NOTIFY_URL || 'http://localhost:8052/api/v1/notify/send';
|
||||
const targets = ['/api/v1/notify/send/', '/api/v1/notify/send', '/api/v1/notify/', '/api/v1/notify'];
|
||||
for (const target of targets) {
|
||||
if (notifyUrl.includes(target)) {
|
||||
return notifyUrl.replace(target, '/open2api/v1');
|
||||
}
|
||||
}
|
||||
return 'http://localhost:8052/open2api/v1';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全部任务列表
|
||||
*/
|
||||
async function getTasks() {
|
||||
const url = `${getBaseUrl()}/tasks`;
|
||||
const res = await request(url, 'GET');
|
||||
return res.data || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 获取单个任务信息
|
||||
*/
|
||||
async function getTask(id) {
|
||||
const url = `${getBaseUrl()}/tasks/${id}`;
|
||||
const res = await request(url, 'GET');
|
||||
return res.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 更新指定任务
|
||||
*/
|
||||
async function updateTask(id, name, command, remark, pin_type, trigger_type, schedule, timeout, work_dir, retry_count, retry_interval, random_range, enabled) {
|
||||
const url = `${getBaseUrl()}/tasks/${id}`;
|
||||
const payload = {};
|
||||
if (name !== undefined) payload.name = name;
|
||||
if (command !== undefined) payload.command = command;
|
||||
if (remark !== undefined) payload.remark = remark;
|
||||
if (pin_type !== undefined) payload.pin_type = pin_type;
|
||||
if (trigger_type !== undefined) payload.trigger_type = trigger_type;
|
||||
if (schedule !== undefined) payload.schedule = schedule;
|
||||
if (timeout !== undefined) payload.timeout = timeout;
|
||||
if (work_dir !== undefined) payload.work_dir = work_dir;
|
||||
if (retry_count !== undefined) payload.retry_count = retry_count;
|
||||
if (retry_interval !== undefined) payload.retry_interval = retry_interval;
|
||||
if (random_range !== undefined) payload.random_range = random_range;
|
||||
if (enabled !== undefined) payload.enabled = enabled;
|
||||
|
||||
const res = await request(url, 'PUT', payload);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 删除任务
|
||||
*/
|
||||
async function deleteTask(id) {
|
||||
const url = `${getBaseUrl()}/tasks/${id}`;
|
||||
await request(url, 'DELETE');
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 触发运行指定任务
|
||||
*/
|
||||
async function executeTask(id) {
|
||||
const url = `${getBaseUrl()}/execute/task/${id}`;
|
||||
const res = await request(url, 'POST');
|
||||
return res.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据日志 ID 停止正在运行的任务
|
||||
*/
|
||||
async function stopTask(logId) {
|
||||
const url = `${getBaseUrl()}/tasks/stop/${logId}`;
|
||||
const res = await request(url, 'POST');
|
||||
return res.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最近的任务执行结果列表
|
||||
*/
|
||||
async function getLastResults() {
|
||||
const url = `${getBaseUrl()}/execute/results`;
|
||||
const res = await request(url, 'GET');
|
||||
return res.data || [];
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getTasks,
|
||||
getTask,
|
||||
updateTask,
|
||||
deleteTask,
|
||||
executeTask,
|
||||
stopTask,
|
||||
getLastResults
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name='taskpool',
|
||||
version='1.0.0',
|
||||
description='TaskPool internal helper for Python',
|
||||
packages=['taskpool'],
|
||||
package_dir={'taskpool': 'taskpool'},
|
||||
python_requires='>=3.6',
|
||||
)
|
||||
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
from .notify import notify as _notify
|
||||
from .env import (
|
||||
get_envs,
|
||||
get_env,
|
||||
add_envs,
|
||||
add_env,
|
||||
update_env,
|
||||
delete_envs,
|
||||
delete_env
|
||||
)
|
||||
from .task import (
|
||||
get_tasks,
|
||||
get_task,
|
||||
update_task,
|
||||
delete_task,
|
||||
execute_task,
|
||||
stop_task,
|
||||
get_last_results
|
||||
)
|
||||
|
||||
def notify(title, text):
|
||||
"""
|
||||
发送内建通知。
|
||||
会在调用时校验环境变量:BHPKG_NOTIFY_TOKEN, BHPKG_NOTIFY_CHANNEL
|
||||
"""
|
||||
_TOKEN = os.environ.get("BHPKG_NOTIFY_TOKEN")
|
||||
_CHANNEL = os.environ.get("BHPKG_NOTIFY_CHANNEL")
|
||||
|
||||
if not _TOKEN or not _CHANNEL:
|
||||
missing = []
|
||||
if not _TOKEN: missing.append("BHPKG_NOTIFY_TOKEN")
|
||||
if not _CHANNEL: missing.append("BHPKG_NOTIFY_CHANNEL")
|
||||
|
||||
error_msg = f"缺少必要的环境变量以使用 taskpool 模块: {', '.join(missing)}。请在任务池的任务设置中配置指定的 Key。"
|
||||
raise RuntimeError(error_msg)
|
||||
|
||||
return _notify(title, text)
|
||||
|
||||
__all__ = [
|
||||
'notify',
|
||||
'get_envs',
|
||||
'get_env',
|
||||
'add_envs',
|
||||
'add_env',
|
||||
'update_env',
|
||||
'delete_envs',
|
||||
'delete_env',
|
||||
'get_tasks',
|
||||
'get_task',
|
||||
'update_task',
|
||||
'delete_task',
|
||||
'execute_task',
|
||||
'stop_task',
|
||||
'get_last_results'
|
||||
]
|
||||
@@ -0,0 +1,133 @@
|
||||
import os
|
||||
import json
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
|
||||
def _get_headers():
|
||||
token = os.environ.get("BHPKG_OPENAPI_TOKEN") or os.environ.get("OPENAPI_TOKEN") or os.environ.get("BHPKG_NOTIFY_TOKEN")
|
||||
if not token:
|
||||
raise RuntimeError("没有正确配置或缺少 BHPKG_OPENAPI_TOKEN 环境变量以使用 env 函数。请在任务池的任务设置中配置这些 Key。")
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {token}"
|
||||
}
|
||||
|
||||
def _get_envs_url():
|
||||
url = os.environ.get("BHPKG_OPENAPI_URL") or os.environ.get("OPENAPI_URL")
|
||||
if url:
|
||||
return url
|
||||
|
||||
notify_url = os.environ.get("BHPKG_NOTIFY_URL", "http://localhost:8052/api/v1/notify/send")
|
||||
for target in ["/api/v1/notify/send/", "/api/v1/notify/send", "/api/v1/notify/", "/api/v1/notify"]:
|
||||
if target in notify_url:
|
||||
return notify_url.replace(target, "/open2api/v1/env")
|
||||
|
||||
return "http://localhost:8052/open2api/v1/env"
|
||||
|
||||
def _request(url, method="GET", data=None):
|
||||
headers = _get_headers()
|
||||
payload = None
|
||||
if data is not None:
|
||||
payload = json.dumps(data).encode("utf-8")
|
||||
|
||||
req = urllib.request.Request(url, data=payload, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req) as resp:
|
||||
body = resp.read().decode("utf-8")
|
||||
if not body:
|
||||
return {}
|
||||
parsed = json.loads(body)
|
||||
if isinstance(parsed, dict) and parsed.get("code") is not None and parsed.get("code") != 200:
|
||||
msg = parsed.get("msg") or parsed.get("message") or "未知错误"
|
||||
raise RuntimeError(f"请求失败 [{parsed.get('code')}]: {msg}")
|
||||
return parsed
|
||||
except urllib.error.HTTPError as e:
|
||||
err_body = e.read().decode("utf-8")
|
||||
try:
|
||||
err_json = json.loads(err_body)
|
||||
msg = err_json.get("msg") or err_json.get("message") or err_body
|
||||
except Exception:
|
||||
msg = err_body
|
||||
raise RuntimeError(f"请求失败 [{e.code}]: {msg}")
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"请求发生异常: {e}")
|
||||
|
||||
def get_envs():
|
||||
"""
|
||||
获取所有的环境变量列表。
|
||||
"""
|
||||
url = f"{_get_envs_url()}/all"
|
||||
res = _request(url, "GET")
|
||||
return res.get("data", [])
|
||||
|
||||
def get_env(name):
|
||||
"""
|
||||
根据变量名获取环境变量。如果不存在则返回 None。
|
||||
"""
|
||||
envs = get_envs()
|
||||
for env in envs:
|
||||
if env.get("name") == name:
|
||||
return env
|
||||
return None
|
||||
|
||||
def add_envs(envs_list):
|
||||
"""
|
||||
批量添加环境变量。
|
||||
envs_list: 包含环境变量字典的列表,如 [{"name": "KEY", "value": "VAL", "remark": "备注"}]
|
||||
"""
|
||||
url = _get_envs_url()
|
||||
added_envs = []
|
||||
for env in envs_list:
|
||||
if "name" not in env or "value" not in env:
|
||||
raise ValueError("环境变量必须包含 'name' 和 'value'")
|
||||
res = _request(url, "POST", env)
|
||||
if "data" in res:
|
||||
added_envs.append(res["data"])
|
||||
return added_envs
|
||||
|
||||
def add_env(name, value, remark="", type="normal", hidden=True, enabled=True):
|
||||
"""
|
||||
添加单个环境变量。
|
||||
"""
|
||||
url = _get_envs_url()
|
||||
payload = {
|
||||
"name": name,
|
||||
"value": value,
|
||||
"remark": remark,
|
||||
"type": type,
|
||||
"hidden": hidden,
|
||||
"enabled": enabled
|
||||
}
|
||||
res = _request(url, "POST", payload)
|
||||
return res.get("data")
|
||||
|
||||
def update_env(id, name, value, remark=None, type=None, hidden=None, enabled=None):
|
||||
"""
|
||||
根据 ID 更新环境变量。
|
||||
"""
|
||||
url = f"{_get_envs_url()}/{id}"
|
||||
payload = {}
|
||||
if name is not None: payload["name"] = name
|
||||
if value is not None: payload["value"] = value
|
||||
if remark is not None: payload["remark"] = remark
|
||||
if type is not None: payload["type"] = type
|
||||
if hidden is not None: payload["hidden"] = hidden
|
||||
if enabled is not None: payload["enabled"] = enabled
|
||||
|
||||
res = _request(url, "PUT", payload)
|
||||
return res.get("data")
|
||||
|
||||
def delete_envs(ids):
|
||||
"""
|
||||
批量删除环境变量。
|
||||
"""
|
||||
for fid in ids:
|
||||
delete_env(fid)
|
||||
|
||||
def delete_env(id):
|
||||
"""
|
||||
根据 ID 删除指定的环境变量。
|
||||
"""
|
||||
url = f"{_get_envs_url()}/{id}"
|
||||
_request(url, "DELETE")
|
||||
return True
|
||||
@@ -0,0 +1,32 @@
|
||||
import os
|
||||
import json
|
||||
import urllib.request
|
||||
|
||||
def notify(title, text, channel_id=None):
|
||||
"""
|
||||
发送内建通知。
|
||||
"""
|
||||
token = os.environ.get("BHPKG_NOTIFY_TOKEN")
|
||||
url = os.environ.get("BHPKG_NOTIFY_URL", "http://localhost:8052/api/v1/notify/send")
|
||||
default_channel = os.environ.get("BHPKG_NOTIFY_CHANNEL")
|
||||
|
||||
cid = channel_id or default_channel
|
||||
|
||||
if not url or not token or not cid:
|
||||
return
|
||||
|
||||
payload = {
|
||||
"channel_id": cid,
|
||||
"title": title,
|
||||
"text": text
|
||||
}
|
||||
|
||||
data = json.dumps(payload).encode('utf-8')
|
||||
req = urllib.request.Request(url, data=data, method='POST')
|
||||
req.add_header('Content-Type', 'application/json')
|
||||
req.add_header('notify-token', token)
|
||||
|
||||
|
||||
with urllib.request.urlopen(req) as resp:
|
||||
return resp.read().decode('utf-8')
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
import os
|
||||
import json
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
|
||||
def _get_headers():
|
||||
token = os.environ.get("BHPKG_OPENAPI_TOKEN") or os.environ.get("OPENAPI_TOKEN") or os.environ.get("BHPKG_NOTIFY_TOKEN")
|
||||
if not token:
|
||||
raise RuntimeError("没有正确配置或缺少 BHPKG_OPENAPI_TOKEN 环境变量以使用 task 函数。请在任务池的任务设置中配置这些 Key。")
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {token}"
|
||||
}
|
||||
|
||||
def _get_base_url():
|
||||
url = os.environ.get("BHPKG_OPENAPI_URL") or os.environ.get("OPENAPI_URL")
|
||||
if url:
|
||||
# If openapi_url ends with /env, replace it with nothing or use base
|
||||
if url.endswith("/env"):
|
||||
return url[:-4]
|
||||
elif url.endswith("/env/"):
|
||||
return url[:-5]
|
||||
return url
|
||||
|
||||
notify_url = os.environ.get("BHPKG_NOTIFY_URL", "http://localhost:8052/api/v1/notify/send")
|
||||
for target in ["/api/v1/notify/send/", "/api/v1/notify/send", "/api/v1/notify/", "/api/v1/notify"]:
|
||||
if target in notify_url:
|
||||
return notify_url.replace(target, "/open2api/v1")
|
||||
|
||||
return "http://localhost:8052/open2api/v1"
|
||||
|
||||
def _request(url, method="GET", data=None):
|
||||
headers = _get_headers()
|
||||
payload = None
|
||||
if data is not None:
|
||||
payload = json.dumps(data).encode("utf-8")
|
||||
|
||||
req = urllib.request.Request(url, data=payload, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req) as resp:
|
||||
body = resp.read().decode("utf-8")
|
||||
if not body:
|
||||
return {}
|
||||
parsed = json.loads(body)
|
||||
if isinstance(parsed, dict) and parsed.get("code") is not None and parsed.get("code") != 200:
|
||||
msg = parsed.get("msg") or parsed.get("message") or "未知错误"
|
||||
raise RuntimeError(f"请求失败 [{parsed.get('code')}]: {msg}")
|
||||
return parsed
|
||||
except urllib.error.HTTPError as e:
|
||||
err_body = e.read().decode("utf-8")
|
||||
try:
|
||||
err_json = json.loads(err_body)
|
||||
msg = err_json.get("msg") or err_json.get("message") or err_body
|
||||
except Exception:
|
||||
msg = err_body
|
||||
raise RuntimeError(f"请求失败 [{e.code}]: {msg}")
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"请求发生异常: {e}")
|
||||
|
||||
def get_tasks():
|
||||
"""
|
||||
获取全部任务列表。
|
||||
"""
|
||||
url = f"{_get_base_url()}/tasks"
|
||||
res = _request(url, "GET")
|
||||
return res.get("data", [])
|
||||
|
||||
def get_task(id):
|
||||
"""
|
||||
根据 ID 获取单个任务的详细信息。
|
||||
"""
|
||||
url = f"{_get_base_url()}/tasks/{id}"
|
||||
res = _request(url, "GET")
|
||||
return res.get("data")
|
||||
|
||||
def update_task(id, name=None, command=None, remark=None, pin_type=None, trigger_type=None, schedule=None, timeout=None, work_dir=None, retry_count=None, retry_interval=None, random_range=None, enabled=None):
|
||||
"""
|
||||
根据 ID 更新任务。
|
||||
"""
|
||||
url = f"{_get_base_url()}/tasks/{id}"
|
||||
payload = {}
|
||||
if name is not None: payload["name"] = name
|
||||
if command is not None: payload["command"] = command
|
||||
if remark is not None: payload["remark"] = remark
|
||||
if pin_type is not None: payload["pin_type"] = pin_type
|
||||
if trigger_type is not None: payload["trigger_type"] = trigger_type
|
||||
if schedule is not None: payload["schedule"] = schedule
|
||||
if timeout is not None: payload["timeout"] = timeout
|
||||
if work_dir is not None: payload["work_dir"] = work_dir
|
||||
if retry_count is not None: payload["retry_count"] = retry_count
|
||||
if retry_interval is not None: payload["retry_interval"] = retry_interval
|
||||
if random_range is not None: payload["random_range"] = random_range
|
||||
if enabled is not None: payload["enabled"] = enabled
|
||||
|
||||
res = _request(url, "PUT", payload)
|
||||
return res.get("data")
|
||||
|
||||
def delete_task(id):
|
||||
"""
|
||||
根据 ID 删除指定任务。
|
||||
"""
|
||||
url = f"{_get_base_url()}/tasks/{id}"
|
||||
_request(url, "DELETE")
|
||||
return True
|
||||
|
||||
def execute_task(id):
|
||||
"""
|
||||
触发执行特定任务。
|
||||
"""
|
||||
url = f"{_get_base_url()}/execute/task/{id}"
|
||||
res = _request(url, "POST")
|
||||
return res.get("data")
|
||||
|
||||
def stop_task(log_id):
|
||||
"""
|
||||
根据日志 ID 停止正在运行的任务。
|
||||
"""
|
||||
url = f"{_get_base_url()}/tasks/stop/{log_id}"
|
||||
res = _request(url, "POST")
|
||||
return res.get("data")
|
||||
|
||||
def get_last_results():
|
||||
"""
|
||||
获取最近的执行结果列表。
|
||||
"""
|
||||
url = f"{_get_base_url()}/execute/results"
|
||||
res = _request(url, "GET")
|
||||
return res.get("data", [])
|
||||
Reference in New Issue
Block a user