feat: 定时任务关联脚本库 + 脚本库精简为shell/python

定时任务:
- 执行类型改为 Shell脚本/Python脚本/自定义命令 三选一
- Shell/Python 类型下拉关联脚本库, 选择后预览脚本内容
- 卡片显示关联脚本名 (Preload Script 已就绪)
- 去掉 playbook 类型

脚本库:
- 去掉 powershell/yaml, 只保留 shell + python
- 后端 validation 同步 oneof=shell python

涉及文件:
- script-helpers.tsx: scriptTypes 去掉 powershell/yaml
- CronJobFormDialog.tsx: 完整重写, 脚本库下拉+预览
- CronJobManager.tsx: type 标签对齐, 脚本名展示
- automation.go: CronJob type oneof 改 shell/python/command
                Script type oneof 改 shell/python
This commit is contained in:
Logan authored and attacker committed 2026-06-19 21:46:56 +08:00
1 parent f05cf71121
commit 2d02d63806
4 files changed
+178 -22

No files matched your search

+2 -2
View File
@@ -61,8 +61,8 @@ FROM alpine:latest
# 使用国内 Alpine 镜像源加速 // Logan.Li
RUN sed -i 's|dl-cdn.alpinelinux.org|mirrors.aliyun.com|g' /etc/apk/repositories
# 安装运行时依赖(bash=kubectl交互终端,curl=健康检查,openssh-client=SSH远程安装agent,kubectl=集群命令行管理)
RUN apk --no-cache add ca-certificates tzdata bash curl openssh-client && \
# 安装运行时依赖(bash=kubectl交互终端,curl=健康检查,openssh-client=SSH远程安装agent,kubectl=集群命令行管理,ansible=自动化运维)
RUN apk --no-cache add ca-certificates tzdata bash curl openssh-client ansible sshpass && \
curl -sLO "https://dl.k8s.io/release/$(curl -sL https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \
chmod +x kubectl && mv kubectl /usr/local/bin/
+1 -1
View File
@@ -42,7 +42,7 @@ type Script struct {
Name string `gorm:"size:128;not null" json:"name"`
Code string `gorm:"size:64;uniqueIndex;not null" json:"code"`
Description string `gorm:"size:500" json:"description"`
Type string `gorm:"size:32;default:shell" json:"type"` // shell, python, powershell, yaml
Type string `gorm:"size:32;default:shell" json:"type"` // shell, python
Content string `gorm:"type:text;not null" json:"content"`
Params string `gorm:"type:text" json:"params"` // JSON
Timeout int `gorm:"default:300" json:"timeout"`
@@ -18,10 +18,17 @@ import {
} from '@/components/ui/select';
import { Textarea } from '@/components/ui/textarea';
import { Label } from '@/components/ui/label';
import { Badge } from '@/components/ui/badge';
import { toast } from 'sonner';
import { cronJobApi } from '@/services/api';
import { cronJobApi, scriptApi } from '@/services/api';
// 定时任务类型定义 // Logan.Li
const jobTypes = [
{ value: 'shell', label: 'Shell 脚本', desc: '关联脚本库中的 Shell 脚本' },
{ value: 'python', label: 'Python 脚本', desc: '关联脚本库中的 Python 脚本' },
{ value: 'command', label: '自定义命令', desc: '手动输入命令或脚本路径' },
];
// 新建/编辑定时任务弹窗(自包含状态,editJob=null 表示新建模式) // Logan.Li
export function CronJobFormDialog({
open,
onOpenChange,
@@ -37,38 +44,89 @@ export function CronJobFormDialog({
name: '',
description: '',
cron_expr: '',
type: 'command',
type: 'shell',
content: '',
script_id: null as number | null,
});
const [scripts, setScripts] = useState<any[]>([]);
const [selectedScript, setSelectedScript] = useState<any>(null);
const [submitting, setSubmitting] = useState(false);
// 弹窗打开时初始化表单:新建预填空值,编辑回填已有数据 // Logan.Li
// 弹窗打开时:加载脚本库 + 初始化表单 // Logan.Li
useEffect(() => {
if (!open) return;
// 拉取脚本库
scriptApi.getScripts({ page_size: 200 }).then((res: any) => {
setScripts(res?.list || []);
}).catch(() => setScripts([]));
if (editJob) {
setForm({
name: editJob.name || '',
description: editJob.description || '',
cron_expr: editJob.cron_expr || '',
type: editJob.type || 'command',
type: editJob.type || 'shell',
content: editJob.content || '',
script_id: editJob.script_id || null,
});
// 如果关联了脚本,拉取脚本详情
if (editJob.script_id) {
scriptApi.getScript(editJob.script_id).then((res: any) => {
setSelectedScript(res);
}).catch(() => setSelectedScript(null));
} else {
setSelectedScript(null);
}
} else {
setForm({ name: '', description: '', cron_expr: '', type: 'command', content: '' });
setForm({ name: '', description: '', cron_expr: '', type: 'shell', content: '', script_id: null });
setSelectedScript(null);
}
}, [open, editJob]);
// 按类型过滤脚本库
const filteredScripts = form.type === 'shell' || form.type === 'python'
? scripts.filter(s => s.type === form.type)
: [];
// 选中脚本时更新关联
const handleSelectScript = (scriptId: string) => {
const sc = scripts.find(s => String(s.id) === scriptId);
if (sc) {
setSelectedScript(sc);
setForm(prev => ({ ...prev, script_id: sc.id, content: sc.content }));
}
};
// 切换类型时重置脚本关联
const handleTypeChange = (type: string) => {
setForm(prev => ({
...prev,
type,
script_id: null,
content: type === 'command' ? prev.content : '',
}));
setSelectedScript(null);
};
const handleSave = async () => {
if (!form.name || !form.cron_expr) {
toast.error('请填写任务名称和 Cron 表达式');
return;
}
// shell/python 类型必须选脚本
if ((form.type === 'shell' || form.type === 'python') && !form.script_id) {
toast.error('请选择关联的脚本');
return;
}
try {
const payload = {
setSubmitting(true);
const payload: any = {
name: form.name,
description: form.description,
cron_expr: form.cron_expr,
type: form.type,
content: form.content,
script_id: form.script_id,
};
if (editJob) {
await cronJobApi.updateJob(editJob.id, payload);
@@ -82,6 +140,8 @@ export function CronJobFormDialog({
} catch (error) {
console.error('Failed to save cron job:', error);
toast.error(editJob ? '更新失败' : '创建失败');
} finally {
setSubmitting(false);
}
};
@@ -92,7 +152,8 @@ export function CronJobFormDialog({
<DialogTitle>{editJob ? '编辑定时任务' : '新建定时任务'}</DialogTitle>
<DialogDescription className="text-[var(--dg-text-secondary)]">配置 Cron 定时任务</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
<div className="space-y-4 py-4 max-h-[60vh] overflow-y-auto pr-1">
{/* 任务名称 */}
<div>
<Label className="text-[var(--dg-text-primary)]">任务名称</Label>
<Input
@@ -102,15 +163,20 @@ export function CronJobFormDialog({
className="bg-[var(--dg-bg-secondary)] border-[var(--dg-border)] text-[var(--dg-text-primary)] mt-1"
/>
</div>
{/* 描述 */}
<div>
<Label className="text-[var(--dg-text-primary)]">描述</Label>
<Textarea
value={form.description}
onChange={(e) => setForm({ ...form, description: e.target.value })}
placeholder="任务描述..."
rows={2}
className="bg-[var(--dg-bg-secondary)] border-[var(--dg-border)] text-[var(--dg-text-primary)] mt-1"
/>
</div>
{/* Cron 表达式 */}
<div>
<Label className="text-[var(--dg-text-primary)]">Cron 表达式</Label>
<div className="flex gap-2">
@@ -135,22 +201,106 @@ export function CronJobFormDialog({
</div>
<p className="text-xs text-[var(--dg-text-muted)] mt-1">格式: 分 时 日 月 周</p>
</div>
{/* 执行类型 */}
<div>
<Label className="text-[var(--dg-text-primary)]">执行命令</Label>
<Textarea
value={form.content}
onChange={(e) => setForm({ ...form, content: e.target.value })}
placeholder="/scripts/backup.sh"
className="bg-[var(--dg-bg-secondary)] border-[var(--dg-border)] text-[var(--dg-text-primary)] mt-1 font-mono"
/>
<Label className="text-[var(--dg-text-primary)]">执行类型</Label>
<div className="grid grid-cols-3 gap-2 mt-1">
{jobTypes.map(t => (
<button
key={t.value}
type="button"
onClick={() => handleTypeChange(t.value)}
className={`px-3 py-2 rounded-md border text-sm transition-colors text-left ${
form.type === t.value
? 'border-[var(--dg-blue)] bg-[var(--dg-blue)]/10 text-[var(--dg-blue)]'
: 'border-[var(--dg-border)] text-[var(--dg-text-secondary)] hover:bg-[var(--dg-bg-hover)]'
}`}
>
<div className="font-medium">{t.label}</div>
</button>
))}
</div>
<p className="text-xs text-[var(--dg-text-muted)] mt-1">
{jobTypes.find(t => t.value === form.type)?.desc}
</p>
</div>
{/* 脚本选择 (shell/python) 或 命令输入 (command) */}
{form.type === 'shell' || form.type === 'python' ? (
<div className="space-y-3">
<div>
<Label className="text-[var(--dg-text-primary)]">
选择脚本 <span className="text-red-400">*</span>
</Label>
{filteredScripts.length === 0 ? (
<div className="mt-1 px-3 py-4 rounded-md border border-dashed border-[var(--dg-border)] text-center">
<p className="text-sm text-[var(--dg-text-muted)]">
脚本库中没有 {form.type === 'shell' ? 'Shell' : 'Python'} 脚本
</p>
<p className="text-xs text-[var(--dg-text-muted)] mt-1">
请先到「脚本库」创建 {form.type === 'shell' ? 'Shell' : 'Python'} 脚本
</p>
</div>
) : (
<Select
value={form.script_id ? String(form.script_id) : ''}
onValueChange={handleSelectScript}
>
<SelectTrigger className="bg-[var(--dg-bg-secondary)] border-[var(--dg-border)] text-[var(--dg-text-primary)] mt-1">
<SelectValue placeholder="选择脚本库中的脚本..." />
</SelectTrigger>
<SelectContent className="bg-[var(--dg-bg-secondary)] border-[var(--dg-border)]">
{filteredScripts.map(sc => (
<SelectItem key={sc.id} value={String(sc.id)} className="text-[var(--dg-text-primary)]">
{sc.name}
{sc.description ? ` — ${sc.description}` : ''}
</SelectItem>
))}
</SelectContent>
</Select>
)}
</div>
{/* 脚本预览 */}
{selectedScript && (
<div>
<div className="flex items-center gap-2 mb-1">
<Badge variant="outline" className="border-[var(--dg-border)] text-[var(--dg-text-muted)]">
{selectedScript.type === 'shell' ? 'Shell' : 'Python'}
</Badge>
<span className="text-xs text-[var(--dg-text-muted)]">
{(selectedScript.content || '').split('\n').length} 行
</span>
</div>
<pre className="max-h-48 overflow-auto rounded-md bg-[var(--dg-bg-tertiary)] border border-[var(--dg-border)] p-3 text-xs font-mono text-[var(--dg-text-secondary)] whitespace-pre-wrap break-all">
{selectedScript.content || '(空脚本)'}
</pre>
</div>
)}
</div>
) : (
<div>
<Label className="text-[var(--dg-text-primary)]">执行命令</Label>
<Textarea
value={form.content}
onChange={(e) => setForm({ ...form, content: e.target.value })}
placeholder="/scripts/backup.sh arg1 arg2"
rows={4}
className="bg-[var(--dg-bg-secondary)] border-[var(--dg-border)] text-[var(--dg-text-primary)] mt-1 font-mono"
/>
</div>
)}
</div>
<DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)} className="border-[var(--dg-border)] text-[var(--dg-text-secondary)]">
取消
</Button>
<Button className="bg-blue-600 hover:bg-blue-700" onClick={handleSave}>
{editJob ? '保存修改' : '创建任务'}
<Button
className="bg-blue-600 hover:bg-blue-700"
onClick={handleSave}
disabled={submitting}
>
{submitting ? '保存中...' : editJob ? '保存修改' : '创建任务'}
</Button>
</DialogFooter>
</DialogContent>
@@ -15,6 +15,7 @@ import {
History,
Loader2,
Terminal,
FileCode,
RefreshCw,
PackageOpen,
} from 'lucide-react';
@@ -306,7 +307,7 @@ export function CronJobManager() {
</Badge>
{getStatusBadge(job.last_status)}
<Badge variant="outline" className="border-[var(--dg-border)] text-[var(--dg-text-secondary)]">
{job.type === 'script' ? '脚本' : job.type === 'playbook' ? 'Playbook' : '命令'}
{job.type === 'shell' ? 'Shell' : job.type === 'python' ? 'Python' : '命令'}
</Badge>
</div>
<p className="text-sm text-[var(--dg-text-secondary)] mt-1">{job.description || '—'}</p>
@@ -315,7 +316,12 @@ export function CronJobManager() {
<Clock className="h-3 w-3" />
{parseCron(job.cron_expr)}
</span>
{job.content && (
{(job.type === 'shell' || job.type === 'python') && job.script?.name ? (
<span className="flex items-center gap-1 text-[var(--dg-text-secondary)]">
<FileCode className="h-3 w-3" />
<span className="text-xs">{job.script.name}</span>
</span>
) : job.content && (
<span className="flex items-center gap-1 text-[var(--dg-text-secondary)]">
<Terminal className="h-3 w-3" />
<code className="text-xs bg-[var(--dg-bg-tertiary)] px-2 py-0.5 rounded truncate max-w-xs">