Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: global env variables sync to remote environments (#2191)
Giulio Savini04c698b5a836
diff --git a/backend/internal/huma/handlers/templates.go b/backend/internal/huma/handlers/templates.goindex b2f51c0de..19d960a0f 100644--- a/backend/internal/huma/handlers/templates.go+++ b/backend/internal/huma/handlers/templates.go@@ -3,6 +3,7 @@ package handlersimport ("context""encoding/json"+ "net/http""github.com/danielgtaylor/huma/v2""github.com/getarcaneapp/arcane/backend/internal/common"@@ -16,7 +17,8 @@ import (// TemplateHandler handles template management endpoints.type TemplateHandler struct {- templateService *services.TemplateService+ templateService *services.TemplateService+ environmentService *services.EnvironmentService}// ============================================================================@@ -150,14 +152,17 @@ type FetchTemplateRegistryOutput struct {Body base.ApiResponse[template.RemoteRegistry]}-type GetGlobalVariablesInput struct{}+type GetGlobalVariablesInput struct {+ EnvironmentID string `path:"id" doc:"Environment ID"`+}type GetGlobalVariablesOutput struct {Body base.ApiResponse[[]env.Variable]}type UpdateGlobalVariablesInput struct {- Body env.Summary+ EnvironmentID string `path:"id" doc:"Environment ID"`+ Body env.Summary}type UpdateGlobalVariablesOutput struct {@@ -169,8 +174,8 @@ type UpdateGlobalVariablesOutput struct {// ============================================================================// RegisterTemplates registers all template management endpoints.-func RegisterTemplates(api huma.API, templateService *services.TemplateService) {- h := &TemplateHandler{templateService: templateService}+func RegisterTemplates(api huma.API, templateService *services.TemplateService, environmentService *services.EnvironmentService) {+ h := &TemplateHandler{templateService: templateService, environmentService: environmentService}// Public endpoints (no auth required in original)huma.Register(api, huma.Operation{@@ -352,9 +357,9 @@ func RegisterTemplates(api huma.API, templateService *services.TemplateService)huma.Register(api, huma.Operation{OperationID: "getGlobalVariables",Method: "GET",- Path: "/templates/variables",+ Path: "/environments/{id}/templates/variables",Summary: "Get global variables",- Description: "Get global template variables",+ Description: "Get global template variables for an environment",Tags: []string{"Templates"},Security: []map[string][]string{{"BearerAuth": {}},@@ -365,9 +370,9 @@ func RegisterTemplates(api huma.API, templateService *services.TemplateService)huma.Register(api, huma.Operation{OperationID: "updateGlobalVariables",Method: "PUT",- Path: "/templates/variables",+ Path: "/environments/{id}/templates/variables",Summary: "Update global variables",- Description: "Update global template variables",+ Description: "Update global template variables for an environment",Tags: []string{"Templates"},Security: []map[string][]string{{"BearerAuth": {}},@@ -822,11 +827,15 @@ func (h *TemplateHandler) FetchRegistry(ctx context.Context, input *FetchTemplat}// GetGlobalVariables returns global template variables.-func (h *TemplateHandler) GetGlobalVariables(ctx context.Context, _ *GetGlobalVariablesInput) (*GetGlobalVariablesOutput, error) {+func (h *TemplateHandler) GetGlobalVariables(ctx context.Context, input *GetGlobalVariablesInput) (*GetGlobalVariablesOutput, error) {if h.templateService == nil {return nil, huma.Error500InternalServerError("service not available")}+ if input.EnvironmentID != "0" {+ return h.getGlobalVariablesForRemoteEnvironmentInternal(ctx, input)+ }+vars, err := h.templateService.GetGlobalVariables(ctx)if err != nil {return nil, huma.Error500InternalServerError((&common.GlobalVariablesRetrievalError{Err: err}).Error())@@ -840,12 +849,38 @@ func (h *TemplateHandler) GetGlobalVariables(ctx context.Context, _ *GetGlobalVa}, nil}+func (h *TemplateHandler) getGlobalVariablesForRemoteEnvironmentInternal(ctx context.Context, input *GetGlobalVariablesInput) (*GetGlobalVariablesOutput, error) {+ if h.environmentService == nil {+ return nil, huma.Error500InternalServerError("environment service not available")+ }++ respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.EnvironmentID, http.MethodGet, "/api/environments/0/templates/variables", nil)+ if err != nil {+ return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())+ }++ if statusCode != http.StatusOK {+ return nil, huma.NewError(statusCode, "remote environment returned error: "+string(respBody), nil)+ }++ var response base.ApiResponse[[]env.Variable]+ if err := json.Unmarshal(respBody, &response); err != nil {+ return nil, huma.Error500InternalServerError("failed to parse remote response: " + err.Error())+ }++ return &GetGlobalVariablesOutput{Body: response}, nil+}+// UpdateGlobalVariables updates global template variables.func (h *TemplateHandler) UpdateGlobalVariables(ctx context.Context, input *UpdateGlobalVariablesInput) (*UpdateGlobalVariablesOutput, error) {if h.templateService == nil {return nil, huma.Error500InternalServerError("service not available")}+ if input.EnvironmentID != "0" {+ return h.updateGlobalVariablesForRemoteEnvironmentInternal(ctx, input)+ }+if err := h.templateService.UpdateGlobalVariables(ctx, input.Body.Variables); err != nil {return nil, huma.Error500InternalServerError((&common.GlobalVariablesUpdateError{Err: err}).Error())}@@ -859,3 +894,30 @@ func (h *TemplateHandler) UpdateGlobalVariables(ctx context.Context, input *Upda},}, nil}++func (h *TemplateHandler) updateGlobalVariablesForRemoteEnvironmentInternal(ctx context.Context, input *UpdateGlobalVariablesInput) (*UpdateGlobalVariablesOutput, error) {+ if h.environmentService == nil {+ return nil, huma.Error500InternalServerError("environment service not available")+ }++ body, err := json.Marshal(input.Body)+ if err != nil {+ return nil, huma.Error500InternalServerError("failed to marshal request body: " + err.Error())+ }++ respBody, statusCode, err := h.environmentService.ProxyRequest(ctx, input.EnvironmentID, http.MethodPut, "/api/environments/0/templates/variables", body)+ if err != nil {+ return nil, huma.Error502BadGateway("failed to proxy request to environment: " + err.Error())+ }++ if statusCode != http.StatusOK {+ return nil, huma.NewError(statusCode, "remote environment returned error: "+string(respBody), nil)+ }++ var response base.ApiResponse[base.MessageResponse]+ if err := json.Unmarshal(respBody, &response); err != nil {+ return nil, huma.Error500InternalServerError("failed to parse remote response: " + err.Error())+ }++ return &UpdateGlobalVariablesOutput{Body: response}, nil+}diff --git a/backend/internal/huma/huma.go b/backend/internal/huma/huma.goindex 4df420842..848422ec7 100644--- a/backend/internal/huma/huma.go+++ b/backend/internal/huma/huma.go@@ -388,7 +388,7 @@ func registerHandlers(api huma.API, svc *Services) {handlers.RegisterOidc(api, authSvc, oidcSvc, cfg)handlers.RegisterEnvironments(api, environmentSvc, settingsSvc, apiKeySvc, eventSvc, cfg)handlers.RegisterContainerRegistries(api, containerRegistrySvc, environmentSvc)- handlers.RegisterTemplates(api, templateSvc)+ handlers.RegisterTemplates(api, templateSvc, environmentSvc)handlers.RegisterImages(api, dockerSvc, imageSvc, imageUpdateSvc, settingsSvc, buildSvc)handlers.RegisterBuildWorkspaces(api, buildWorkspaceSvc)handlers.RegisterImageUpdates(api, imageUpdateSvc)diff --git a/frontend/src/lib/services/template-service.ts b/frontend/src/lib/services/template-service.tsindex 2d3e80d5b..997ea8a6b 100644--- a/frontend/src/lib/services/template-service.ts+++ b/frontend/src/lib/services/template-service.ts@@ -3,6 +3,7 @@ import type { TemplateRegistry, Template, RemoteRegistry, TemplateContentData }import type { Variable } from '$lib/types/variable.type';import type { SearchPaginationSortRequest, Paginated } from '$lib/types/pagination.type';import { transformPaginationParams } from '$lib/utils/params.util';+import { environmentStore } from '$lib/stores/environment.store.svelte';export default class TemplateService extends BaseAPIService {async getTemplates(options?: SearchPaginationSortRequest): Promise<Paginated<Template>> {@@ -106,12 +107,14 @@ export default class TemplateService extends BaseAPIService {}async getGlobalVariables(): Promise<Variable[]> {- const response = await this.api.get('/templates/variables');+ const envId = await environmentStore.getCurrentEnvironmentId();+ const response = await this.api.get(`/environments/${envId}/templates/variables`);return response.data?.data ?? [];}async updateGlobalVariables(variables: Variable[]): Promise<void> {- await this.api.put('/templates/variables', { variables });+ const envId = await environmentStore.getCurrentEnvironmentId();+ await this.api.put(`/environments/${envId}/templates/variables`, { variables });}}