diff --git a/agent/app/api/v2/container.go b/agent/app/api/v2/container.go index 0a9c868c8694..c790170373d8 100644 --- a/agent/app/api/v2/container.go +++ b/agent/app/api/v2/container.go @@ -731,6 +731,26 @@ func (b *BaseApi) ComposeUpdate(c *gin.Context) { helper.Success(c) } +// @Tags Container Compose +// @Summary Load compose environment variables +// @Accept json +// @Param request body dto.FilePath true "request" +// @Success 200 {array} string +// @Security ApiKeyAuth +// @Security Timestamp +// @Router /containers/compose/env [post] +func (b *BaseApi) LoadComposeEnv(c *gin.Context) { + var req dto.FilePath + if err := helper.CheckBindAndValidate(&req, c); err != nil { + return + } + data, err := containerService.LoadComposeEnv(req.Path) + if err != nil { + helper.InternalServer(c, err) + } + helper.SuccessWithData(c, data) +} + // @Tags Container // @Summary Container logs // @Param container query string false "容器名称" diff --git a/agent/app/service/container.go b/agent/app/service/container.go index 7dce6705dbf4..831d7bbc098a 100644 --- a/agent/app/service/container.go +++ b/agent/app/service/container.go @@ -59,9 +59,15 @@ type IContainerService interface { ListNetwork() ([]dto.Options, error) PageVolume(req dto.SearchWithPage) (int64, interface{}, error) ListVolume() ([]dto.Options, error) + PageCompose(req dto.SearchWithPage) (int64, interface{}, error) + LoadComposeEnv(name string) (string, error) CreateCompose(req dto.ComposeCreate) error ComposeOperation(req dto.ComposeOperation) error + TestCompose(req dto.ComposeCreate) (bool, error) + ComposeUpdate(req dto.ComposeUpdate) error + ComposeLogClean(req dto.ComposeLogClean) error + ContainerCreate(req dto.ContainerOperate, inThread bool) error ContainerCreateByCommand(req dto.ContainerCreateByCommand) error ContainerUpdate(req dto.ContainerOperate) error @@ -76,14 +82,12 @@ type IContainerService interface { ContainerOperation(req dto.ContainerOperation) error DownloadContainerLogs(containerType, container, since, tail string, c *gin.Context) error ContainerStats(id string) (*dto.ContainerStats, error) + Inspect(req dto.InspectReq) (string, error) DeleteNetwork(req dto.BatchDelete) error CreateNetwork(req dto.NetworkCreate) error DeleteVolume(req dto.BatchDelete) error CreateVolume(req dto.VolumeCreate) error - TestCompose(req dto.ComposeCreate) (bool, error) - ComposeUpdate(req dto.ComposeUpdate) error - ComposeLogClean(req dto.ComposeLogClean) error Prune(req dto.ContainerPrune) error LoadUsers(req dto.OperationWithName) []string diff --git a/agent/app/service/container_compose.go b/agent/app/service/container_compose.go index f4f5cfa8b854..3d5370e59d6c 100644 --- a/agent/app/service/container_compose.go +++ b/agent/app/service/container_compose.go @@ -342,6 +342,15 @@ func (u *ContainerService) ComposeLogClean(req dto.ComposeLogClean) error { }) } +func (u *ContainerService) LoadComposeEnv(name string) (string, error) { + envFilePath := path.Join(path.Dir(name), ".env") + file, err := os.ReadFile(envFilePath) + if err != nil { + return "", err + } + return string(file), nil +} + func (u *ContainerService) loadPath(req *dto.ComposeCreate) error { if req.From == "template" || req.From == "edit" { dir := fmt.Sprintf("%s/docker/compose/%s", global.Dir.DataDir, req.Name) diff --git a/agent/router/ro_container.go b/agent/router/ro_container.go index b64a8ea1625d..112d64be0243 100644 --- a/agent/router/ro_container.go +++ b/agent/router/ro_container.go @@ -46,6 +46,7 @@ func (s *ContainerRouter) InitRouter(Router *gin.RouterGroup) { baRouter.POST("/compose/search", baseApi.SearchCompose) baRouter.POST("/compose", baseApi.CreateCompose) + baRouter.POST("/compose/env", baseApi.LoadComposeEnv) baRouter.POST("/compose/test", baseApi.TestCompose) baRouter.POST("/compose/operate", baseApi.OperatorCompose) baRouter.POST("/compose/clean/log", baseApi.CleanComposeLog) diff --git a/frontend/src/api/modules/container.ts b/frontend/src/api/modules/container.ts index d8ebe931b3aa..37b994b76d06 100644 --- a/frontend/src/api/modules/container.ts +++ b/frontend/src/api/modules/container.ts @@ -201,6 +201,9 @@ export const composeUpdate = (params: Container.ComposeUpdate) => { export const dockerOperate = (operation: string) => { return http.post(`/containers/docker/operate`, { operation: operation }, TimeoutEnum.T_3M); }; +export const loadComposeEnv = (path: string) => { + return http.post(`/containers/compose/env`, { path: path }); +}; export const loadDaemonJson = () => { return http.get(`/containers/daemonjson`); }; diff --git a/frontend/src/views/container/compose/index.vue b/frontend/src/views/container/compose/index.vue index 971b34d3991e..4f07f2c618f5 100644 --- a/frontend/src/views/container/compose/index.vue +++ b/frontend/src/views/container/compose/index.vue @@ -25,7 +25,7 @@ @@ -281,7 +281,7 @@ label-position="top" :model="form" :rules="rules" - v-loading="loading" + v-loading="detailLoading" > @@ -382,6 +382,7 @@ import { containerListStats, inspect, listComposeTemplate, + loadComposeEnv, searchCompose, testCompose, upCompose, @@ -619,6 +620,9 @@ const changePath = async () => { }; const loadDir = async (path: string) => { form.path = path; + await loadComposeEnv(path).then((res) => { + form.env = res.data || ''; + }); }; const handleClose = () => { search(true);