dockge/frontend/src/components/StackDeployments.vue
Weapie d8fdeba05d
All checks were successful
Docker image / publish (push) Successful in 59s
Improve Dockge workflows and require approval after image scans
Update the shadcn preset and visual Compose editor. Add deployment previews, history, attention notices, and cleanup safeguards. Use the supplied icon in the app header and authentication screens.
2026-11-12 12:15:02 +01:00

252 lines
12 KiB
Vue

<template>
<div v-if="pending || (managed && history.length)" class="flex flex-wrap items-center gap-2">
<UiButton v-if="pending" variant="secondary" size="sm" @click="reviewOpen = true">
<AppIcon name="rocket" /> {{ $t("deploymentPhase." + pending.phase) }}
</UiButton>
<UiButton v-if="history.length" variant="outline" size="sm" @click="historyOpen = true">
{{ $t("deployHistory") }}
</UiButton>
</div>
<Dialog v-model:open="reviewOpen">
<DialogContent class="max-h-[90dvh] overflow-y-auto sm:max-w-3xl" :close-label="$t('Close')" :aria-describedby="undefined">
<DialogHeader><DialogTitle>{{ $t("deployReview") }}</DialogTitle></DialogHeader>
<p v-if="busy && !review" role="status">{{ $t("resourceLoading") }}</p>
<UiAlert v-if="error" variant="destructive">{{ $t(error) }}</UiAlert>
<template v-if="review">
<p role="status" class="text-sm font-medium">{{ $t("deploymentPhase." + review.phase) }}</p>
<UiAlert v-if="review.error" variant="destructive">
{{ $t(review.error) }}
<code v-if="review.field" class="break-all">{{ review.field }}</code>
</UiAlert>
<p v-if="review.notBefore && delayed" class="text-sm text-muted-foreground">{{ $t("deployAvailableAt", { time: formatTime(review.notBefore) }) }}</p>
<p v-if="review.phase === 'ready'" class="text-sm text-muted-foreground">{{ $t("deployApprovalPrompt") }}</p>
<p v-if="review.phase === 'rejected' && review.cleanup" class="text-sm text-muted-foreground">{{ $t("deployRejectResult", review.cleanup) }}</p>
<ul v-if="review.progress?.length" class="flex flex-wrap gap-1">
<li v-for="(item, index) in review.progress" :key="index">
<UiBadge variant="outline" class="status-badge" :data-status="item.health || item.state">
{{ item.service }} · {{ $t("resourceContainerState." + (item.health || item.state), item.health || item.state) }}
</UiBadge>
</li>
</ul>
<div v-if="review.changes.length" class="grid gap-1">
<h3 class="section-title">{{ $t("deployChanges") }}</h3>
<ul class="divide-y rounded-lg border">
<li v-for="(change, index) in review.changes" :key="index" class="flex flex-wrap items-center gap-2 p-2">
<div class="min-w-0 flex-1 basis-36">
<p class="break-all text-sm font-medium">{{ change.name }}</p>
<code v-if="change.reference" class="break-all text-xs text-muted-foreground">
{{ change.reference }}
<span v-if="change.imageId">· <template v-if="change.previousImageIds?.length">{{ change.previousImageIds.map(id => id.slice(7, 19)).join(", ") }} → </template>{{ change.imageId.slice(7, 19) }}</span>
</code>
</div>
<UiBadge variant="outline">{{ $t("deploymentChange." + change.action) }}</UiBadge>
</li>
</ul>
</div>
<div v-if="review.scans.length" class="grid gap-2">
<h3 class="section-title">{{ $t("imageScanTitle") }}</h3>
<div v-for="scan in review.scans" :key="scan.id" class="grid gap-2 rounded-lg border p-2.5">
<p class="break-all font-mono text-xs">{{ scan.references.join(", ") || scan.id }}</p>
<p v-if="scan.error" class="text-sm text-destructive">{{ $t(scan.error) }}</p>
<p v-if="scan.databaseBuiltAt" class="text-xs text-muted-foreground">{{ $t("imageScanDatabase", { time: formatTime(scan.databaseBuiltAt) }) }}</p>
<div v-if="scan.counts" class="flex flex-wrap gap-1">
<UiBadge v-for="(count, severity) in scan.counts" :key="severity" variant="outline" class="severity-badge" :data-severity="severity">
{{ $t("imageScanSeverity." + severity) }} {{ count }}
</UiBadge>
</div>
<ul v-if="scan.findings.length" class="grid gap-1 border-t pt-2 text-xs">
<li v-for="(finding, index) in scan.findings" :key="index" class="break-words">
{{ finding.id }} · {{ finding.packageName }} {{ finding.installedVersion }}
<span v-if="finding.fixedVersions.length" class="text-muted-foreground">→ {{ finding.fixedVersions.join(", ") }}</span>
</li>
</ul>
</div>
</div>
</template>
<DialogFooter>
<UiButton type="button" variant="outline" @click="reviewOpen = false">{{ $t("Close") }}</UiButton>
<template v-if="pending && ['ready', 'failed'].includes(pending.phase)">
<UiButton type="button" variant="destructive" :disabled="busy" @click="decide(false)">{{ $t("deployReject") }}</UiButton>
<UiButton type="button" :disabled="busy || pending.phase !== 'ready' || delayed" @click="decide(true)">{{ $t("deployApprove") }}</UiButton>
</template>
</DialogFooter>
</DialogContent>
</Dialog>
<Dialog v-model:open="historyOpen">
<DialogContent class="max-h-[90dvh] overflow-y-auto sm:max-w-2xl" :close-label="$t('Close')" :aria-describedby="undefined">
<DialogHeader><DialogTitle>{{ $t("deployHistory") }}</DialogTitle></DialogHeader>
<ul class="divide-y rounded-lg border">
<li v-for="entry in history" :key="entry.id" class="grid gap-2 p-3">
<div class="flex flex-wrap items-center justify-between gap-2">
<div class="grid gap-1">
<p class="text-sm font-medium">{{ $t("deploymentPhase." + entry.phase) }} · {{ $t("deploymentMode." + entry.mode) }}</p>
<p class="text-xs text-muted-foreground">{{ formatTime(entry.finishedAt || entry.startedAt) }}</p>
</div>
<UiButton v-if="entry.phase === 'deployed'" variant="outline" size="sm" :disabled="busy" @click="restore(entry.id)">{{ $t("deployRestoreDraft") }}</UiButton>
</div>
<p v-if="entry.error" class="text-xs text-destructive">{{ $t(entry.error) }}</p>
<code v-for="change in entry.changes.filter(item => item.imageId)" :key="change.name" class="break-all text-xs text-muted-foreground">{{ change.name }} · {{ change.imageId }}</code>
</li>
</ul>
</DialogContent>
</Dialog>
</template>
<script>
export default {
props: {
stackName: { type: String,
default: "" },
endpoint: { type: String,
default: "" },
managed: Boolean,
},
emits: [ "deployed", "restore", "pending" ],
data() {
return { pending: null,
history: [],
reviewId: "",
reviewOpen: false,
historyOpen: false,
error: "",
busy: false,
timer: null,
generation: 0,
now: Date.now() };
},
computed: {
review() {
return this.pending || this.history.find(entry => entry.id === this.reviewId);
},
delayed() {
return Boolean(this.pending?.notBefore && Date.parse(this.pending.notBefore) > this.now);
},
},
watch: {
stackName: "reset",
endpoint: "reset",
pending(value) {
this.$emit("pending", Boolean(value));
},
},
mounted() {
this.reviewOpen = this.$route.query.review === "1";
this.reset();
},
beforeUnmount() {
this.generation++;
clearTimeout(this.timer);
},
methods: {
request(event, ...args) {
return this.$root.getSocket().timeout(15000).emitWithAck("agent", this.endpoint, event, ...args);
},
reset() {
clearTimeout(this.timer);
const generation = ++this.generation;
this.pending = null;
this.history = [];
if (/^[a-z0-9_-]+$/.test(this.stackName)) {
this.poll(generation);
}
},
async poll(generation) {
clearTimeout(this.timer);
try {
const result = await this.request("getStackDeployments", this.stackName);
if (result.ok && generation === this.generation) {
const prior = this.pending;
this.pending = result.pending;
this.history = result.history;
const completed = prior && result.history.find(entry => entry.id === prior.id);
if (completed) {
this.reviewId = completed.id;
if (completed.phase === "deployed") {
this.$emit("deployed", completed);
}
}
}
} catch {
// A reconnect can pick up the persisted review.
} finally {
if (generation === this.generation) {
this.now = Date.now();
this.timer = setTimeout(() => this.poll(generation), this.reviewOpen || this.pending ? 2000 : 12000);
}
}
},
async prepare(event, ...args) {
if (this.busy) {
return;
}
this.reviewOpen = true;
this.error = "";
this.reviewId = "";
this.busy = true;
try {
const [ name, yaml, env, isAdd, revision ] = args;
const mode = { deployStack: "deploy",
startStack: "start",
updateStack: "update",
startService: "service" }[event];
const draft = mode === "deploy" ? { yaml,
env,
isAdd,
revision } : null;
const result = await this.request("prepareStackDeployment", name, mode, draft, mode === "service" ? yaml : null);
if (result.ok && result.deployment) {
this.pending = result.deployment;
this.reviewId = result.deployment.id;
} else {
this.error = result.msg || "deployOperationFailed";
}
void this.poll(this.generation);
} catch {
this.error = "deployReviewUnavailable";
} finally {
this.busy = false;
}
},
async decide(approve) {
this.error = "";
this.busy = true;
try {
const result = await this.request("decideStackDeployment", this.stackName, this.pending.id, approve);
if (result.ok) {
this.pending = result.deployment;
void this.poll(this.generation);
} else {
this.error = result.msg || "deployOperationFailed";
}
} catch {
this.error = "deployOperationFailed";
} finally {
this.busy = false;
}
},
async restore(id) {
this.busy = true;
try {
const result = await this.request("restoreStackDeployment", this.stackName, id);
if (result.ok) {
this.historyOpen = false;
this.$emit("restore", { yaml: result.yaml,
env: result.env });
} else {
this.$root.toastRes(result);
}
} catch {
this.$root.toastError("deployHistoryMissing");
} finally {
this.busy = false;
}
},
formatTime(value) {
return new Date(value).toLocaleString(this.$i18n.locale);
},
},
};
</script>