Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions agent/app/service/container_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,8 @@ func (u *ContainerService) CreateCompose(req dto.ComposeCreate) error {
}
go func() {
taskItem.AddSubTask(i18n.GetMsgByKey("ComposeCreate"), func(t *task.Task) error {
cmd := getComposeCmd(req.Path, "up")
out, err := cmd.CombinedOutput()
taskItem.Log(i18n.GetWithName("ComposeCreateRes", string(out)))
err := compose.UpWithTask(req.Path, t)
t.LogWithStatus(i18n.GetMsgByKey("ComposeCreate"), err)
if err != nil {
_, _ = compose.Down(req.Path)
return err
Expand Down
64 changes: 63 additions & 1 deletion agent/utils/compose/compose.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package compose

import (
"errors"
"fmt"
"os"
"path"
"strings"
"time"

"github.com/1Panel-dev/1Panel/agent/app/task"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/i18n"
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
"github.com/1Panel-dev/1Panel/agent/utils/common"
"time"
"github.com/1Panel-dev/1Panel/agent/utils/docker"
"github.com/goccy/go-yaml"
)

func checkCmd() error {
Expand All @@ -27,6 +37,58 @@
return stdout, err
}

func UpWithTask(filePath string, task *task.Task) error {

Check failure on line 40 in agent/utils/compose/compose.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 25 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZq6M0M5QrMLa3I2sDyT&open=AZq6M0M5QrMLa3I2sDyT&pullRequest=11069
content, err := os.ReadFile(filePath)
if err != nil {
return err
}
env, _ := os.ReadFile(path.Join(path.Dir(filePath), ".env"))
var compose docker.ComposeProject
if err := yaml.Unmarshal(content, &compose); err != nil {
return fmt.Errorf("parse docker-compose file failed: %v", err)
}
images, err := docker.GetImagesFromDockerCompose(env, content)
if err != nil {
return err
}
dockerCLi, err := docker.NewClient()
if err != nil {
return err
}
errMsg := ""
for _, image := range images {
task.Log(i18n.GetWithName("PullImageStart", image))
if err = dockerCLi.PullImageWithProcess(task, image); err != nil {
errOur := err.Error()
if errOur != "" {
if strings.Contains(errOur, "no such host") {
errMsg = i18n.GetMsgByKey("ErrNoSuchHost") + ":"
}
if strings.Contains(errOur, "Error response from daemon") {
errMsg = i18n.GetMsgByKey("PullImageTimeout") + ":"
}
}
message := errMsg + errOur
installErr := errors.New(message)
task.LogFailedWithErr(i18n.GetMsgByKey("PullImage"), installErr)
if exist, _ := dockerCLi.ImageExists(image); !exist {
return installErr
} else {
task.Log(i18n.GetMsgByKey("UseExistImage"))
}
} else {
task.Log(i18n.GetMsgByKey("PullImageSuccess"))
}
}

dockerCommand := global.CONF.DockerConfig.Command
if dockerCommand == "docker-compose" {
return cmd.NewCommandMgr(cmd.WithTask(*task)).Run("docker-compose", "-f", filePath, "up", "-d")
} else {
return cmd.NewCommandMgr(cmd.WithTask(*task)).Run("docker", "compose", "-f", filePath, "up", "-d")
}
}

func Down(filePath string) (string, error) {
if err := checkCmd(); err != nil {
return "", err
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/views/container/compose/create/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@
</span>
</template>
</DrawerPro>
<TaskLog ref="taskLogRef" width="70%" />
<TaskLog ref="taskLogRef" width="70%">
<template #task-footer>
<el-button @click="handleClose">{{ $t('commons.table.backToList') }}</el-button>
<el-button type="primary" @click="closeTask">{{ $t('commons.table.keepEdit') }}</el-button>
</template>
</TaskLog>
<FileList ref="fileRef" @choose="loadDir" />
</template>

Expand Down Expand Up @@ -167,8 +172,12 @@ const changeFrom = () => {

const handleClose = () => {
emit('search');
taskLogRef.value?.handleClose();
drawerVisible.value = false;
};
const closeTask = () => {
taskLogRef.value?.handleClose();
};

const loadPath = async () => {
const pathRes = await loadBaseDir();
Expand Down
119 changes: 0 additions & 119 deletions frontend/src/views/container/compose/edit/index.vue

This file was deleted.

55 changes: 35 additions & 20 deletions frontend/src/views/container/compose/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
{{ $t('container.createCompose') }}
</el-button>
</template>
<template #rightToolBar>
<TableRefresh @search="search()" />
<TableSetting title="container-refresh" @search="search()" />
</template>
<template #main>
<el-row v-if="data.length > 0" :gutter="20" class="row-box">
<el-col :span="6">
<el-col :span="7">
<el-card>
<el-input
v-model="searchName"
:placeholder="$t('commons.button.search')"
clearable
class="w-4/5"
@clear="search"
@keyup.enter="search"
>
Expand All @@ -29,17 +34,32 @@
</template>
</el-input>

<ComplexTable :show-header="false" @row-click="loadDetail" :data="data">
<el-table class="mt-2" :show-header="false" @row-click="loadDetail" :data="data">
<el-table-column prop="name">
<template #default="{ row }">
<div class="cursor-pointer">
<div class="font-medium truncate">
<div class="font-medium text-base">
{{ row.name }}
</div>
<div class="mb-1">
<el-text class="w-12" link size="small" type="info">
{{ loadFrom(row) }}
</el-text>
<el-divider direction="vertical" />
<el-text link size="small" type="info" class="ml-2">
{{ row.createdAt }}
</el-text>
<el-divider direction="vertical" />
<el-text v-if="row.containerCount === 0" type="danger" size="small">
<el-text
link
v-if="row.containerCount === 0"
type="danger"
size="small"
>
{{ $t('container.exited') }}
</el-text>
<el-text
link
v-else
:type="
row.containerCount === row.runningCount ? 'success' : 'warning'
Expand All @@ -50,21 +70,16 @@
$t('container.running', [row.runningCount, row.containerCount])
}}
</el-text>
<el-divider direction="vertical" />
<el-button
link
type="primary"
icon="Folder"
:disabled="!currentCompose?.workdir"
@click="openComposeFolder"
/>
</div>
<div class="mt-1 mb-2">
<el-tag size="small" type="info">{{ loadFrom(row) }}</el-tag>
<el-tag size="small" type="info" class="ml-2">
{{ row.createdAt }}
</el-tag>
</div>
<el-button
plain
round
size="small"
:disabled="!currentCompose?.workdir"
@click="openComposeFolder"
>
{{ $t('home.dir') }}
</el-button>
<el-button
plain
round
Expand Down Expand Up @@ -101,10 +116,10 @@
</div>
</template>
</el-table-column>
</ComplexTable>
</el-table>
</el-card>
</el-col>
<el-col :span="18">
<el-col :span="17">
<el-card v-if="currentCompose" v-loading="detailLoading">
<el-table
v-if="composeContainers.length > 0"
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/container/container/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@
<TerminalDialog ref="dialogTerminalRef" />

<PortJumpDialog ref="dialogPortJumpRef" />
<TaskLog ref="taskLogRef" width="70%" />
<TaskLog ref="taskLogRef" width="70%" @close="search" />
</div>
</template>

Expand Down
Loading