Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
feat: add pull progress popover when deploying project (#512)
Kyle Mendellcd77c8943d02
diff --git a/backend/internal/api/project_handler.go b/backend/internal/api/project_handler.goindex 4bdaea6ce..d17e794f7 100644--- a/backend/internal/api/project_handler.go+++ b/backend/internal/api/project_handler.go@@ -4,7 +4,6 @@ import ("context""errors""fmt"- "io""net/http""strings""sync"@@ -244,12 +243,19 @@ func (h *ProjectHandler) PullProjectImages(c *gin.Context) {return}- if err := h.projectService.PullProjectImages(c.Request.Context(), projectID, io.Discard); err != nil {- c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": fmt.Sprintf("Failed to pull images: %v", err)})+ c.Writer.Header().Set("Content-Type", "application/x-json-stream")+ c.Writer.Header().Set("Cache-Control", "no-cache")+ c.Writer.Header().Set("Connection", "keep-alive")+ c.Writer.Header().Set("X-Accel-Buffering", "no")++ _, _ = fmt.Fprintln(c.Writer, `{"status":"starting project image pull"}`)++ if err := h.projectService.PullProjectImages(c.Request.Context(), projectID, c.Writer); err != nil {+ _, _ = fmt.Fprintf(c.Writer, `{"error":%q}`+"\n", err.Error())return}- c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"message": "Images pulled successfully"}})+ _, _ = fmt.Fprintln(c.Writer, `{"status":"complete"}`)}func (h *ProjectHandler) UpdateProject(c *gin.Context) {diff --git a/backend/internal/services/project_service.go b/backend/internal/services/project_service.goindex cd4c2d98b..bd55e6aaa 100644--- a/backend/internal/services/project_service.go+++ b/backend/internal/services/project_service.go@@ -426,8 +426,13 @@ func (s *ProjectService) DeployProject(ctx context.Context, projectID string, usif err := s.updateProjectStatusInternal(ctx, projectID, models.ProjectStatusDeploying); err != nil {return fmt.Errorf("failed to update project status to deploying: %w", err)}- if err := projects.ComposeUp(ctx, project, project.Services.GetProfiles()); err != nil {+ // Pre-pull all images using auth-aware ImageService+ if perr := s.PullProjectImages(ctx, projectID, io.Discard); perr != nil {+ slog.Warn("pre-pull images failed (continuing to compose up)", "projectID", projectID, "error", perr)+ }++ if err := projects.ComposeUp(ctx, project, project.Services.GetProfiles()); err != nil {slog.Error("compose up failed", "projectName", project.Name, "projectID", projectID, "error", err)if containers, psErr := s.GetProjectServices(ctx, projectID); psErr == nil {slog.Info("containers after failed deploy", "projectID", projectID, "containers", containers)@@ -592,8 +597,19 @@ func (s *ProjectService) PullProjectImages(ctx context.Context, projectID stringreturn fmt.Errorf("failed to load compose project: %w", lerr)}- if err := projects.ComposePull(ctx, compProj); err != nil {- return fmt.Errorf("failed to pull images via compose api: %w", err)+ images := map[string]struct{}{}+ for _, svc := range compProj.Services {+ img := strings.TrimSpace(svc.Image)+ if img == "" {+ continue+ }+ images[img] = struct{}{}+ }++ for img := range images {+ if err := s.imageService.PullImage(ctx, img, progressWriter, systemUser); err != nil {+ return fmt.Errorf("failed to pull image %s: %w", img, err)+ }}return nil}diff --git a/frontend/src/lib/components/action-buttons.svelte b/frontend/src/lib/components/action-buttons.svelteindex dbfc0a655..1b7435732 100644--- a/frontend/src/lib/components/action-buttons.svelte+++ b/frontend/src/lib/components/action-buttons.svelte@@ -80,6 +80,9 @@});let pullPopoverOpen = $state(false);+ let deployPullPopoverOpen = $state(false);+ let projectPulling = $state(false); // only for Project Pull button/popover+ let deployPulling = $state(false); // only for Deploy popoverlet pullProgress = $state(0);let pullStatusText = $state('');let pullError = $state('');@@ -120,9 +123,18 @@}}- function buildPullApiUrl(): string {- const envId = getCurrentEnvironmentId();- return `/api/environments/${envId}/projects/${id}/pull`;+ function isDownloadingLine(data: any): boolean {+ const status = String(data?.status ?? '').toLowerCase();+ const pd = data?.progressDetail;+ // Open if we see byte progress or any of the common pull statuses+ if (pd && (typeof pd.total === 'number' || typeof pd.current === 'number')) return true;+ return (+ status.includes('downloading') ||+ status.includes('extracting') ||+ status.includes('pulling fs layer') ||+ status.includes('download complete') ||+ status.includes('pull complete')+ );}function getCurrentEnvironmentId(): string {@@ -216,17 +228,89 @@}async function handleDeploy() {+ resetPullState();setLoading('start', true);- await handleApiResultWithCallbacks({- result: await tryCatch(environmentAPI.deployProject(id)),- message: m.action_failed_generic({ action: m.common_start(), type }),- setLoadingState: (value) => setLoading('start', value),- onSuccess: async () => {- itemState = 'running';- toast.success(m.action_started_success({ type }));- onActionComplete('running');+ let openedPopover = false;+ let hadError = false;++ try {+ const { pulled } = await environmentAPI.deployProjectMaybePull(id, (data) => {+ if (!data) return;++ if (!openedPopover && isDownloadingLine(data)) {+ deployPullPopoverOpen = true;+ deployPulling = true;+ pullStatusText = m.images_pull_initiating();+ openedPopover = true;+ }++ if (data.error) {+ const errMsg = typeof data.error === 'string' ? data.error : data.error.message || m.images_pull_stream_error();+ pullError = errMsg;+ pullStatusText = m.images_pull_failed_with_error({ error: errMsg });+ hadError = true;+ return;+ }++ if (data.status) pullStatusText = data.status;++ if (data.id) {+ const currentLayer = layerProgress[data.id] || { current: 0, total: 0, status: '' };+ currentLayer.status = data.status || currentLayer.status;+ if (data.progressDetail) {+ const { current, total } = data.progressDetail;+ if (typeof current === 'number') currentLayer.current = current;+ if (typeof total === 'number') currentLayer.total = total;+ }+ layerProgress[data.id] = currentLayer;+ }++ calculateOverallProgress();+ });++ // If popover was shown, finish/close it nicely+ if (openedPopover) {+ calculateOverallProgress();+ if (hadError) throw new Error(pullError || m.images_pull_failed());++ if (pullProgress < 100) {+ const allDone = Object.values(layerProgress).every(+ (l) =>+ l.status &&+ (l.status.toLowerCase().includes('complete') ||+ l.status.toLowerCase().includes('already exists') ||+ l.status.toLowerCase().includes('downloaded newer image'))+ );+ if (allDone && Object.keys(layerProgress).length > 0) {+ pullProgress = 100;+ }+ }+ pullStatusText = m.images_pulled_success();+ toast.success(m.images_pulled_success());+ await invalidateAll();++ setTimeout(() => {+ deployPullPopoverOpen = false;+ deployPulling = false;+ resetPullState();+ }, 1500);}- });++ // Deploy already completed successfully+ itemState = 'running';+ toast.success(m.action_started_success({ type }));+ onActionComplete('running');+ } catch (e: any) {+ const message = e?.message || m.action_failed_generic({ action: m.common_start(), type });+ if (openedPopover) {+ pullError = message;+ pullStatusText = m.images_pull_failed_with_error({ error: message });+ deployPulling = false;+ }+ toast.error(message);+ } finally {+ setLoading('start', false);+ }}async function handleStop() {@@ -276,93 +360,56 @@async function handleProjectPull() {resetPullState();- isLoading.pull = true;+ projectPulling = true;pullPopoverOpen = true;pullStatusText = m.images_pull_initiating();let wasSuccessful = false;try {- const response = await fetch(buildPullApiUrl(), {- method: 'POST',- headers: {- 'Content-Type': 'application/json'+ await environmentAPI.pullProjectImages(id, (data) => {+ if (!data) return;++ if (data.error) {+ const errMsg = typeof data.error === 'string' ? data.error : data.error.message || m.images_pull_stream_error();+ pullError = errMsg;+ pullStatusText = m.images_pull_failed_with_error({ error: errMsg });+ return;}- });-- if (!response.ok || !response.body) {- const errorData = await response.json().catch(() => ({- error: m.images_pull_server_error()- }));- const errorMessage =- typeof errorData.error === 'string'- ? errorData.error- : errorData.message || `${m.images_pull_server_error()}: HTTP ${response.status}`;- throw new Error(errorMessage);- }- const reader = response.body.getReader();- const decoder = new TextDecoder();- let buffer = '';+ if (data.status) pullStatusText = data.status;- while (true) {- const { done, value } = await reader.read();- if (done) {- pullStatusText = m.images_pull_processing_final_layers();- break;- }+ if (data.id) {+ const currentLayer = layerProgress[data.id] || { current: 0, total: 0, status: '' };+ currentLayer.status = data.status || currentLayer.status;- buffer += decoder.decode(value, { stream: true });- const lines = buffer.split('\n');- buffer = lines.pop() || '';-- for (const line of lines) {- if (line.trim() === '') continue;- try {- const data = JSON.parse(line);-- if (data.error) {- console.error('Error in stream:', data.error);- pullError = typeof data.error === 'string' ? data.error : data.error.message || m.images_pull_stream_error();- pullStatusText = m.images_pull_failed_with_error({ error: pullError });- continue;- }-- pullStatusText = data.status || pullStatusText;- if (data.id) {- const currentLayer = layerProgress[data.id] || { current: 0, total: 0, status: '' };- currentLayer.status = data.status || currentLayer.status;-- if (data.progressDetail) {- currentLayer.current = data.progressDetail.current || currentLayer.current;- currentLayer.total = data.progressDetail.total || currentLayer.total;- }- layerProgress[data.id] = currentLayer;- }- calculateOverallProgress();- } catch (e: any) {- console.warn('Failed to parse stream line or process data:', line, e);+ if (data.progressDetail) {+ const { current, total } = data.progressDetail;+ if (typeof current === 'number') currentLayer.current = current;+ if (typeof total === 'number') currentLayer.total = total;}+ layerProgress[data.id] = currentLayer;}- }+ calculateOverallProgress();+ });++ // Stream finishedcalculateOverallProgress();if (!pullError && pullProgress < 100) {- const allLayersCompleteOrExisting = Object.values(layerProgress).every(+ const allDone = Object.values(layerProgress).every((l) =>l.status &&(l.status.toLowerCase().includes('complete') ||l.status.toLowerCase().includes('already exists') ||l.status.toLowerCase().includes('downloaded newer image')));- if (allLayersCompleteOrExisting && Object.keys(layerProgress).length > 0) {+ if (allDone && Object.keys(layerProgress).length > 0) {pullProgress = 100;}}- if (pullError) {- throw new Error(pullError);- }+ if (pullError) throw new Error(pullError);wasSuccessful = true;pullProgress = 100;@@ -372,18 +419,17 @@setTimeout(() => {pullPopoverOpen = false;- isLoading.pull = false;+ projectPulling = false;resetPullState();}, 2000);} catch (error: any) {- console.error('Pull images error:', error);- const message = error.message || m.images_pull_failed();+ const message = error?.message || m.images_pull_failed();pullError = message;pullStatusText = m.images_pull_failed_with_error({ error: message });toast.error(message);} finally {if (!wasSuccessful) {- isLoading.pull = false;+ projectPulling = false;}}}@@ -391,11 +437,21 @@<div class="flex items-center gap-2">{#if !isRunning}- <ArcaneButton- action={type === 'container' ? 'start' : 'deploy'}- onclick={type === 'container' ? () => handleStart() : () => handleDeploy()}- loading={uiLoading.start}- />+ {#if type === 'container'}+ <ArcaneButton action="start" onclick={() => handleStart()} loading={uiLoading.start} />+ {:else}+ <ProgressPopover+ bind:open={deployPullPopoverOpen}+ bind:progress={pullProgress}+ title={m.progress_pulling_images()}+ statusText={pullStatusText}+ error={pullError}+ loading={deployPulling}+ icon={DownloadIcon}+ >+ <ArcaneButton action="deploy" onclick={() => handleDeploy()} loading={uiLoading.start} />+ </ProgressPopover>+ {/if}{/if}{#if isRunning}@@ -420,10 +476,10 @@title={m.progress_pulling_images()}statusText={pullStatusText}error={pullError}- loading={uiLoading.pulling}+ loading={projectPulling}icon={DownloadIcon}>- <ArcaneButton action="pull" onclick={() => handlePull()} loading={uiLoading.pulling} />+ <ArcaneButton action="pull" onclick={() => handlePull()} loading={projectPulling} /></ProgressPopover>{:else}<ArcaneButton action="pull" onclick={() => handlePull()} loading={uiLoading.pulling} />diff --git a/frontend/src/lib/services/api/environment-api-service.ts b/frontend/src/lib/services/api/environment-api-service.tsindex aa209bfe8..ad527f882 100644--- a/frontend/src/lib/services/api/environment-api-service.ts+++ b/frontend/src/lib/services/api/environment-api-service.ts@@ -332,9 +332,77 @@ export class EnvironmentAPIService extends BaseAPIService {return this.handleResponse(this.api.post(`/environments/${envId}/projects/${projectName}/redeploy`));}- async pullProjectImages(projectName: string): Promise<Project> {+ private isDownloadingStatus(status?: string): boolean {+ if (!status) return false;+ const s = status.toLowerCase();+ return (+ s.includes('downloading') ||+ s.includes('extracting') ||+ s.includes('pull complete') ||+ s.includes('download complete') ||+ s.includes('pulling fs layer')+ );+ }++ private async streamProjectPull(projectId: string, onLine?: (data: any) => void): Promise<boolean> {const envId = await this.getCurrentEnvironmentId();- return this.handleResponse(this.api.post(`/environments/${envId}/projects/${projectName}/pull`));+ const url = `/api/environments/${envId}/projects/${projectId}/pull`;++ const res = await fetch(url, { method: 'POST' });+ if (!res.ok || !res.body) {+ throw new Error(`Failed to start project image pull (${res.status})`);+ }++ const reader = res.body.getReader();+ const decoder = new TextDecoder();+ let buffer = '';+ let pulled = false;++ while (true) {+ const { value, done } = await reader.read();+ if (done) break;++ buffer += decoder.decode(value, { stream: true });+ const lines = buffer.split('\n');+ buffer = lines.pop() || '';++ for (const line of lines) {+ const trimmed = line.trim();+ if (!trimmed) continue;+ try {+ const obj = JSON.parse(trimmed);++ // Detect if any actual download happened+ if (!pulled) {+ const status = obj?.status as string | undefined;+ const total = obj?.progressDetail?.total as number | undefined;+ if (this.isDownloadingStatus(status) || (typeof total === 'number' && total > 0)) {+ pulled = true;+ }+ }++ onLine?.(obj);+ } catch {+ // ignore malformed line+ }+ }+ }+ return pulled;+ }++ pullProjectImages(projectId: string): Promise<void>;+ pullProjectImages(projectId: string, onLine: (data: any) => void): Promise<void>;+ async pullProjectImages(projectId: string, onLine?: (data: any) => void): Promise<void> {+ await this.streamProjectPull(projectId, onLine);+ }++ async deployProjectMaybePull(+ projectId: string,+ onPullLine?: (data: any) => void+ ): Promise<{ pulled: boolean; project: Project }> {+ const pulled = await this.streamProjectPull(projectId, onPullLine);+ const project = await this.deployProject(projectId);+ return { pulled, project };}async destroyProject(projectName: string, removeVolumes = false, removeFiles = false): Promise<void> {