仰望星辰工作室

miaopanel

miaopanel/ web/src/components/docker/DockerCreateModal.vue 4.2 KB · 129 行 原始文件
1<script setup lang="ts">
2import { inject, ref } from 'vue'
3import { api } from '../../api'
4import type { DockerCreateResult } from '../../types'
5
6const emit = defineEmits<{ (e: 'close'): void; (e: 'created'): void }>()
7const notify = inject<(text: string, kind?: 'ok' | 'error') => void>('notify')!
8
9const mode = ref<'run' | 'compose'>('run')
10const content = ref('')
11const project = ref('')
12const busy = ref(false)
13const result = ref<DockerCreateResult | null>(null)
14
15const runHint = 'docker run -d --name web -p 8080:80 nginx:latest'
16const composeHint = `services:
17 web:
18 image: nginx:latest
19 ports:
20 - "8080:80"`
21
22async function submit() {
23 if (busy.value) return
24 const body = content.value.trim()
25 if (!body) {
26 notify('请粘贴命令或 compose 内容', 'error')
27 return
28 }
29 busy.value = true
30 try {
31 const res = await api.post<DockerCreateResult>('api/v1/docker/containers', {
32 mode: mode.value,
33 content: body,
34 project: project.value.trim(),
35 })
36 result.value = res
37 emit('created')
38 notify('容器创建完成')
39 } catch (e) {
40 notify((e as Error).message, 'error')
41 } finally {
42 busy.value = false
43 }
44}
45
46function close() {
47 if (busy.value) return
48 emit('close')
49}
50
51function useExample() {
52 content.value = mode.value === 'run' ? runHint : composeHint
53}
54</script>
55
56<template>
57 <div class="overlay" @click.self="close">
58 <div class="modal" style="width: min(780px, 100%)">
59 <div class="modal-head">
60 <h3>新建容器</h3>
61 <button class="icon-btn" :disabled="busy" title="关闭" @click="close">×</button>
62 </div>
63
64 <div class="tabs" style="margin-bottom: 12px">
65 <button class="tab" :class="{ active: mode === 'run' }" @click="mode = 'run'">docker run</button>
66 <button class="tab" :class="{ active: mode === 'compose' }" @click="mode = 'compose'">compose YAML</button>
67 </div>
68
69 <p class="hint">
70 <template v-if="mode === 'run'">
71 粘贴完整的 <code>docker run</code> 命令。必须带 <code>-d</code>,不能包含
72 <code>;</code>、<code>&amp;&amp;</code>、管道或重定向;命令会以 root 身份执行。
73 </template>
74 <template v-else>
75 粘贴 compose YAML,面板会写入数据目录并执行 <code>docker compose up -d</code>。
76 不支持 privileged,执行后不提供项目管理页。
77 </template>
78 </p>
79
80 <textarea
81 v-model="content"
82 class="textarea mono"
83 :rows="mode === 'run' ? 5 : 10"
84 :placeholder="mode === 'run' ? runHint : composeHint"
85 :disabled="busy"
86 ></textarea>
87
88 <label v-if="mode === 'compose'" class="field" style="margin-top: 10px">
89 项目名称(可选)
90 <input v-model="project" class="input mono" placeholder="留空自动生成,例如 blog" :disabled="busy" />
91 </label>
92
93 <div class="row" style="margin-top: 8px">
94 <button class="btn btn-ghost btn-sm" :disabled="busy" @click="useExample">填入示例</button>
95 <div class="spacer"></div>
96 <span class="hint">绑定的域名流量会经过 nginx 与 WAF;直接映射的端口绕过 WAF。</span>
97 </div>
98
99 <div v-if="result" class="alert ok" style="margin-top: 12px">
100 <b>{{ result.mode === 'run' ? '命令执行完成' : `compose 项目 ${result.stack} 已启动` }}</b>
101 <div v-if="result.path" class="mono" style="margin-top: 4px; font-size: 12px">{{ result.path }}</div>
102 <pre class="create-output mono">{{ result.output }}</pre>
103 </div>
104
105 <div class="modal-foot">
106 <div class="spacer"></div>
107 <button class="btn btn-ghost" :disabled="busy" @click="close">{{ result ? '关闭' : '取消' }}</button>
108 <button v-if="!result" class="btn btn-primary" :disabled="busy" @click="submit">
109 <span v-if="busy" class="spin"></span>
110 {{ busy ? '创建中…' : '创建' }}
111 </button>
112 </div>
113 </div>
114 </div>
115</template>
116
117<style scoped>
118.create-output {
119 margin: 8px 0 0;
120 padding: 8px 10px;
121 max-height: 180px;
122 overflow: auto;
123 border-radius: 8px;
124 background: #f7fcfb;
125 border: 1px solid var(--line);
126 font-size: 12px;
127 white-space: pre-wrap;
128}
129</style>