Skip to content

Commit e7c8cdb

Browse files
author
ssongliu
committed
feat: Add force image pull to container compose editing
1 parent 5db2ccb commit e7c8cdb

5 files changed

Lines changed: 61 additions & 23 deletions

File tree

agent/app/dto/container.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ type ComposeCreate struct {
279279
Path string `json:"path"`
280280
Template uint `json:"template"`
281281
Env string `json:"env"`
282-
PullImage *bool `json:"pullImage,omitempty"`
282+
ForcePull bool `json:"forcePull"`
283283
}
284284
type ComposeOperation struct {
285285
Name string `json:"name" validate:"required"`
@@ -294,6 +294,7 @@ type ComposeUpdate struct {
294294
DetailPath string `json:"detailPath"`
295295
Content string `json:"content" validate:"required"`
296296
Env string `json:"env"`
297+
ForcePull bool `json:"forcePull"`
297298
}
298299
type ComposeLogClean struct {
299300
Name string `json:"name" validate:"required"`

agent/app/service/container_compose.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,9 @@ func (u *ContainerService) CreateCompose(req dto.ComposeCreate) error {
203203
if err := newComposeEnv(req.Path, req.Env); err != nil {
204204
return err
205205
}
206-
pullImages := true
207-
if req.PullImage != nil {
208-
pullImages = *req.PullImage
209-
}
210206
go func() {
211207
taskItem.AddSubTask(i18n.GetMsgByKey("ComposeCreate"), func(t *task.Task) error {
212-
err := compose.UpWithTask(req.Path, t, pullImages)
208+
err := compose.UpWithTask(req.Path, t, req.ForcePull)
213209
t.LogWithStatus(i18n.GetMsgByKey("ComposeCreate"), err)
214210
if err != nil {
215211
_, _ = compose.Down(req.Path)
@@ -280,7 +276,7 @@ func (u *ContainerService) ComposeUpdate(req dto.ComposeUpdate) error {
280276
return err
281277
}
282278

283-
if stdout, err := compose.Up(req.Path); err != nil {
279+
if stdout, err := compose.UpWithForcePull(req.Path, req.ForcePull); err != nil {
284280
global.LOG.Errorf("update failed when handle compose up, std: %s, err: %s, now try to recreate the old compose file", stdout, err)
285281
if err := recreateCompose(string(oldFile), req.Path); err != nil {
286282
return fmt.Errorf("update failed and recreate old compose file also failed, err: %v", err)

agent/utils/compose/compose.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,24 @@ func Up(filePath string) (string, error) {
3636
return cmd.NewCommandMgr(cmd.WithTimeout(20*time.Minute)).RunWithStdoutBashCf("%s %s up -d", global.CONF.DockerConfig.Command, loadFiles(filePath))
3737
}
3838

39-
func UpWithTask(filePath string, task *task.Task, pullImages bool) error {
40-
if !pullImages {
41-
return cmd.NewCommandMgr(cmd.WithTask(*task)).RunBashCf("%s %s up -d", global.CONF.DockerConfig.Command, loadFiles(filePath))
39+
func UpWithForcePull(filePath string, forcePull bool) (string, error) {
40+
if err := checkCmd(); err != nil {
41+
return "", err
42+
}
43+
if err := pullComposeImages(filePath, forcePull, nil); err != nil {
44+
return "", err
45+
}
46+
return cmd.NewCommandMgr(cmd.WithTimeout(20*time.Minute)).RunWithStdoutBashCf("%s %s up -d", global.CONF.DockerConfig.Command, loadFiles(filePath))
47+
}
48+
49+
func UpWithTask(filePath string, task *task.Task, forcePull bool) error {
50+
if err := pullComposeImages(filePath, forcePull, task); err != nil {
51+
return err
4252
}
53+
return cmd.NewCommandMgr(cmd.WithTask(*task)).RunBashCf("%s %s up -d", global.CONF.DockerConfig.Command, loadFiles(filePath))
54+
}
55+
56+
func pullComposeImages(filePath string, forcePull bool, task *task.Task) error {
4357
content, err := os.ReadFile(filePath)
4458
if err != nil {
4559
return err
@@ -57,11 +71,28 @@ func UpWithTask(filePath string, task *task.Task, pullImages bool) error {
5771
if err != nil {
5872
return err
5973
}
60-
errMsg := ""
6174
for _, image := range images {
62-
task.Log(i18n.GetWithName("PullImageStart", image))
63-
if err = dockerCLi.PullImageWithProcess(task, image); err != nil {
64-
errOur := err.Error()
75+
if !forcePull {
76+
if exist, _ := dockerCLi.ImageExists(image); exist {
77+
if task != nil {
78+
task.Log(i18n.GetMsgByKey("UseExistImage"))
79+
}
80+
continue
81+
}
82+
}
83+
84+
if task != nil {
85+
task.Log(i18n.GetWithName("PullImageStart", image))
86+
}
87+
pullErr := error(nil)
88+
if task != nil {
89+
pullErr = dockerCLi.PullImageWithProcess(task, image)
90+
} else {
91+
pullErr = docker.PullImage(image)
92+
}
93+
if pullErr != nil {
94+
errMsg := ""
95+
errOur := pullErr.Error()
6596
if errOur != "" {
6697
if strings.Contains(errOur, "no such host") {
6798
errMsg = i18n.GetMsgByKey("ErrNoSuchHost") + ":"
@@ -72,18 +103,21 @@ func UpWithTask(filePath string, task *task.Task, pullImages bool) error {
72103
}
73104
message := errMsg + errOur
74105
installErr := errors.New(message)
75-
task.LogFailedWithErr(i18n.GetMsgByKey("PullImage"), installErr)
106+
if task != nil {
107+
task.LogFailedWithErr(i18n.GetMsgByKey("PullImage"), installErr)
108+
}
76109
if exist, _ := dockerCLi.ImageExists(image); !exist {
77110
return installErr
78-
} else {
111+
}
112+
if task != nil {
79113
task.Log(i18n.GetMsgByKey("UseExistImage"))
80114
}
81-
} else {
115+
} else if task != nil {
82116
task.Log(i18n.GetMsgByKey("PullImageSuccess"))
83117
}
84118
}
85119

86-
return cmd.NewCommandMgr(cmd.WithTask(*task)).RunBashCf("%s %s up -d", global.CONF.DockerConfig.Command, loadFiles(filePath))
120+
return nil
87121
}
88122

89123
func Down(filePath string) (string, error) {

frontend/src/api/interface/container.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export namespace Container {
329329
path: string;
330330
template: number;
331331
env: string;
332-
pullImage?: boolean;
332+
forcePull: boolean;
333333
}
334334
export interface ComposeOperation {
335335
name: string;
@@ -343,6 +343,7 @@ export namespace Container {
343343
path: string;
344344
content: string;
345345
env: string;
346+
forcePull: boolean;
346347
createdBy: string;
347348
}
348349

frontend/src/views/container/compose/index.vue

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@
257257
<span v-if="currentCompose.createdBy === 'Apps'" class="input-help">
258258
{{ $t('container.composeEnvHelper2') }}
259259
</span>
260+
<div class="mt-2">
261+
<el-checkbox v-model="form.forcePull" :label="$t('container.forcePull')" />
262+
<span class="input-help">{{ $t('container.forcePullHelper') }}</span>
263+
</div>
260264

261265
<el-button type="primary" class="mt-2" @click="onSubmitEdit">
262266
{{ $t('commons.button.save') }}
@@ -336,8 +340,8 @@
336340
<el-input placeholder="key=value" type="textarea" :rows="3" v-model="form.env" />
337341
<span class="envTitle">{{ $t('commons.button.set') }}</span>
338342
<el-form-item>
339-
<el-checkbox v-model="form.pullImage" :label="$t('app.pullImage')" />
340-
<span class="input-help">{{ $t('app.pullImageHelper') }}</span>
343+
<el-checkbox v-model="form.forcePull" :label="$t('container.forcePull')" />
344+
<span class="input-help">{{ $t('container.forcePullHelper') }}</span>
341345
</el-form-item>
342346
</el-form>
343347

@@ -432,7 +436,7 @@ const form = reactive({
432436
file: '',
433437
template: null as number,
434438
env: '',
435-
pullImage: true,
439+
forcePull: false,
436440
});
437441
const rules = reactive({
438442
name: [Rules.requiredInput, Rules.composeName],
@@ -523,6 +527,7 @@ const loadDetail = async (row: Container.ComposeInfo, withRefresh: boolean) => {
523527
if (currentCompose.value?.name === row.name && withRefresh !== true) {
524528
return;
525529
}
530+
form.forcePull = false;
526531
isOnCreate.value = false;
527532
detailLoading.value = true;
528533
currentCompose.value = row;
@@ -562,7 +567,7 @@ const onOpenDialog = async () => {
562567
form.file = '';
563568
form.template = null;
564569
form.env = '';
565-
form.pullImage = true;
570+
form.forcePull = false;
566571
loadPath();
567572
loadTemplates();
568573
};
@@ -690,6 +695,7 @@ const onSubmitEdit = async () => {
690695
content: composeContent.value,
691696
createdBy: currentCompose.value.createdBy,
692697
env: env.value || '',
698+
forcePull: form.forcePull,
693699
};
694700
loading.value = true;
695701
await composeUpdate(param)

0 commit comments

Comments
 (0)