Skip to content

Commit c59105f

Browse files
authored
feat: Encapsulate command execution methods (#8401)
1 parent e6433d6 commit c59105f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+786
-711
lines changed

agent/app/api/v2/terminal.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func loadMapFromDockerTop(containerID string) map[string]string {
165165
pidMap := make(map[string]string)
166166
sudo := cmd.SudoHandleCmd()
167167

168-
stdout, err := cmd.Execf("%s docker top %s -eo pid,command ", sudo, containerID)
168+
stdout, err := cmd.RunDefaultWithStdoutBashCf("%s docker top %s -eo pid,command ", sudo, containerID)
169169
if err != nil {
170170
return pidMap
171171
}
@@ -192,7 +192,7 @@ func killBash(containerID, comm string, pidMap map[string]string) {
192192
}
193193
}
194194
if !isOld && command == comm {
195-
_, _ = cmd.Execf("%s kill -9 %s", sudo, pid)
195+
_, _ = cmd.RunDefaultWithStdoutBashCf("%s kill -9 %s", sudo, pid)
196196
}
197197
}
198198
}

agent/app/dto/cronjob.go

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type CronjobCreate struct {
4040
SourceAccountIDs string `json:"sourceAccountIDs"`
4141
DownloadAccountID uint `json:"downloadAccountID"`
4242
RetainCopies int `json:"retainCopies" validate:"number,min=1"`
43+
RetryTimes int `json:"retryTimes" validate:"number,min=0"`
44+
Timeout uint `json:"timeout" validate:"number,min=1"`
4345
Secret string `json:"secret"`
4446

4547
AlertCount uint `json:"alertCount"`
@@ -72,6 +74,8 @@ type CronjobUpdate struct {
7274
SourceAccountIDs string `json:"sourceAccountIDs"`
7375
DownloadAccountID uint `json:"downloadAccountID"`
7476
RetainCopies int `json:"retainCopies" validate:"number,min=1"`
77+
RetryTimes int `json:"retryTimes" validate:"number,min=0"`
78+
Timeout uint `json:"timeout" validate:"number,min=1"`
7579
Secret string `json:"secret"`
7680

7781
AlertCount uint `json:"alertCount"`
@@ -122,6 +126,8 @@ type CronjobInfo struct {
122126
IsDir bool `json:"isDir"`
123127
SourceDir string `json:"sourceDir"`
124128
RetainCopies int `json:"retainCopies"`
129+
RetryTimes int `json:"retryTimes"`
130+
Timeout uint `json:"timeout"`
125131

126132
SourceAccounts []string `json:"sourceAccounts"`
127133
DownloadAccount string `json:"downloadAccount"`

agent/app/model/cronjob.go

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type Cronjob struct {
3232

3333
SourceAccountIDs string `json:"sourceAccountIDs"`
3434
DownloadAccountID uint `json:"downloadAccountID"`
35+
RetryTimes uint `json:"retryTimes"`
36+
Timeout uint `json:"timeout"`
3537
RetainCopies uint64 `json:"retainCopies"`
3638

3739
Status string `json:"status"`

agent/app/service/ai.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (u *AIToolService) LoadDetail(name string) (string, error) {
7373
if err != nil {
7474
return "", err
7575
}
76-
stdout, err := cmd.Execf("docker exec %s ollama show %s", containerName, name)
76+
stdout, err := cmd.RunDefaultWithStdoutBashCf("docker exec %s ollama show %s", containerName, name)
7777
if err != nil {
7878
return "", err
7979
}
@@ -107,7 +107,8 @@ func (u *AIToolService) Create(req dto.OllamaModelName) error {
107107
}
108108
go func() {
109109
taskItem.AddSubTask(i18n.GetWithName("OllamaModelPull", req.Name), func(t *task.Task) error {
110-
return cmd.ExecShellWithTask(taskItem, time.Hour, "docker", "exec", containerName, "ollama", "pull", info.Name)
110+
cmdMgr := cmd.NewCommandMgr(cmd.WithTask(*taskItem), cmd.WithTimeout(time.Hour))
111+
return cmdMgr.Run("docker", "exec", containerName, "ollama", "pull", info.Name)
111112
}, nil)
112113
taskItem.AddSubTask(i18n.GetWithName("OllamaModelSize", req.Name), func(t *task.Task) error {
113114
itemSize, err := loadModelSize(info.Name, containerName)
@@ -133,7 +134,7 @@ func (u *AIToolService) Close(name string) error {
133134
if err != nil {
134135
return err
135136
}
136-
stdout, err := cmd.Execf("docker exec %s ollama stop %s", containerName, name)
137+
stdout, err := cmd.RunDefaultWithStdoutBashCf("docker exec %s ollama stop %s", containerName, name)
137138
if err != nil {
138139
return fmt.Errorf("handle ollama stop %s failed, stdout: %s, err: %v", name, stdout, err)
139140
}
@@ -162,7 +163,8 @@ func (u *AIToolService) Recreate(req dto.OllamaModelName) error {
162163
}
163164
go func() {
164165
taskItem.AddSubTask(i18n.GetWithName("OllamaModelPull", req.Name), func(t *task.Task) error {
165-
return cmd.ExecShellWithTask(taskItem, time.Hour, "docker", "exec", containerName, "ollama", "pull", req.Name)
166+
cmdMgr := cmd.NewCommandMgr(cmd.WithTask(*taskItem), cmd.WithTimeout(time.Hour))
167+
return cmdMgr.Run("docker", "exec", containerName, "ollama", "pull", req.Name)
166168
}, nil)
167169
taskItem.AddSubTask(i18n.GetWithName("OllamaModelSize", req.Name), func(t *task.Task) error {
168170
itemSize, err := loadModelSize(modelInfo.Name, containerName)
@@ -191,7 +193,7 @@ func (u *AIToolService) Delete(req dto.ForceDelete) error {
191193
}
192194
for _, item := range ollamaList {
193195
if item.Status != constant.StatusDeleted {
194-
stdout, err := cmd.Execf("docker exec %s ollama rm %s", containerName, item.Name)
196+
stdout, err := cmd.RunDefaultWithStdoutBashCf("docker exec %s ollama rm %s", containerName, item.Name)
195197
if err != nil && !req.ForceDelete {
196198
return fmt.Errorf("handle ollama rm %s failed, stdout: %s, err: %v", item.Name, stdout, err)
197199
}
@@ -208,7 +210,7 @@ func (u *AIToolService) Sync() ([]dto.OllamaModelDropList, error) {
208210
if err != nil {
209211
return nil, err
210212
}
211-
stdout, err := cmd.Execf("docker exec %s ollama list", containerName)
213+
stdout, err := cmd.RunDefaultWithStdoutBashCf("docker exec %s ollama list", containerName)
212214
if err != nil {
213215
return nil, err
214216
}
@@ -380,7 +382,7 @@ func LoadContainerName() (string, error) {
380382
}
381383

382384
func loadModelSize(name string, containerName string) (string, error) {
383-
stdout, err := cmd.Execf("docker exec %s ollama list | grep %s", containerName, name)
385+
stdout, err := cmd.RunDefaultWithStdoutBashCf("docker exec %s ollama list | grep %s", containerName, name)
384386
if err != nil {
385387
return "", err
386388
}

agent/app/service/app_utils.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,9 @@ func runScript(task *task.Task, appInstall *model.AppInstall, operate string) er
995995
}
996996
logStr := i18n.GetWithName("ExecShell", operate)
997997
task.LogStart(logStr)
998-
out, err := cmd.ExecScript(scriptPath, workDir)
998+
999+
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(10*time.Minute), cmd.WithScriptPath(scriptPath))
1000+
out, err := cmdMgr.RunWithStdout("bash")
9991001
if err != nil {
10001002
if out != "" {
10011003
err = errors.New(out)

agent/app/service/backup_redis.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func handleRedisBackup(redisInfo *repo.RootInfo, parentTask *task.Task, backupDi
9696
}
9797
}
9898

99-
stdout, err := cmd.Execf("docker exec %s redis-cli -a %s --no-auth-warning save", redisInfo.ContainerName, redisInfo.Password)
99+
stdout, err := cmd.RunDefaultWithStdoutBashCf("docker exec %s redis-cli -a %s --no-auth-warning save", redisInfo.ContainerName, redisInfo.Password)
100100
if err != nil {
101101
return errors.New(string(stdout))
102102
}
@@ -109,14 +109,14 @@ func handleRedisBackup(redisInfo *repo.RootInfo, parentTask *task.Task, backupDi
109109
return nil
110110
}
111111
if strings.HasSuffix(fileName, ".aof") {
112-
stdout1, err := cmd.Execf("docker cp %s:/data/appendonly.aof %s/%s", redisInfo.ContainerName, backupDir, fileName)
112+
stdout1, err := cmd.RunDefaultWithStdoutBashCf("docker cp %s:/data/appendonly.aof %s/%s", redisInfo.ContainerName, backupDir, fileName)
113113
if err != nil {
114114
return errors.New(string(stdout1))
115115
}
116116
return nil
117117
}
118118

119-
stdout1, err1 := cmd.Execf("docker cp %s:/data/dump.rdb %s/%s", redisInfo.ContainerName, backupDir, fileName)
119+
stdout1, err1 := cmd.RunDefaultWithStdoutBashCf("docker cp %s:/data/dump.rdb %s/%s", redisInfo.ContainerName, backupDir, fileName)
120120
if err1 != nil {
121121
return errors.New(string(stdout1))
122122
}

agent/app/service/backup_website.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func handleWebsiteRecover(website *model.Website, recoverFile string, isRollback
189189
t.LogFailedWithErr(taskName, err)
190190
return err
191191
}
192-
stdout, err := cmd.Execf("docker exec -i %s nginx -s reload", nginxInfo.ContainerName)
192+
stdout, err := cmd.RunDefaultWithStdoutBashCf("docker exec -i %s nginx -s reload", nginxInfo.ContainerName)
193193
if err != nil {
194194
return errors.New(stdout)
195195
}

agent/app/service/clam.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (c *ClamService) LoadBaseInfo() (dto.ClamBaseInfo, error) {
8484
}
8585

8686
if baseInfo.IsActive {
87-
version, err := cmd.Exec("clamdscan --version")
87+
version, err := cmd.RunDefaultWithStdoutBashC("clamdscan --version")
8888
if err == nil {
8989
if strings.Contains(version, "/") {
9090
baseInfo.Version = strings.TrimPrefix(strings.Split(version, "/")[0], "ClamAV ")
@@ -96,7 +96,7 @@ func (c *ClamService) LoadBaseInfo() (dto.ClamBaseInfo, error) {
9696
_ = StopAllCronJob(false)
9797
}
9898
if baseInfo.FreshIsActive {
99-
version, err := cmd.Exec("freshclam --version")
99+
version, err := cmd.RunDefaultWithStdoutBashC("freshclam --version")
100100
if err == nil {
101101
if strings.Contains(version, "/") {
102102
baseInfo.FreshVersion = strings.TrimPrefix(strings.Split(version, "/")[0], "ClamAV ")
@@ -111,13 +111,13 @@ func (c *ClamService) LoadBaseInfo() (dto.ClamBaseInfo, error) {
111111
func (c *ClamService) Operate(operate string) error {
112112
switch operate {
113113
case "start", "restart", "stop":
114-
stdout, err := cmd.Execf("systemctl %s %s", operate, c.serviceName)
114+
stdout, err := cmd.RunDefaultWithStdoutBashCf("systemctl %s %s", operate, c.serviceName)
115115
if err != nil {
116116
return fmt.Errorf("%s the %s failed, err: %s", operate, c.serviceName, stdout)
117117
}
118118
return nil
119119
case "fresh-start", "fresh-restart", "fresh-stop":
120-
stdout, err := cmd.Execf("systemctl %s %s", strings.TrimPrefix(operate, "fresh-"), freshClamService)
120+
stdout, err := cmd.RunDefaultWithStdoutBashCf("systemctl %s %s", strings.TrimPrefix(operate, "fresh-"), freshClamService)
121121
if err != nil {
122122
return fmt.Errorf("%s the %s failed, err: %s", operate, c.serviceName, stdout)
123123
}
@@ -344,7 +344,7 @@ func (c *ClamService) HandleOnce(req dto.OperateByID) error {
344344
}
345345
}
346346
global.LOG.Debugf("clamdscan --fdpass %s %s -l %s", strategy, clam.Path, logFile)
347-
stdout, err := cmd.Execf("clamdscan --fdpass %s %s -l %s", strategy, clam.Path, logFile)
347+
stdout, err := cmd.RunDefaultWithStdoutBashCf("clamdscan --fdpass %s %s -l %s", strategy, clam.Path, logFile)
348348
if err != nil {
349349
global.LOG.Errorf("clamdscan failed, stdout: %v, err: %v", stdout, err)
350350
}

agent/app/service/container.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ func (u *ContainerService) ContainerCreateByCommand(req dto.ContainerCreateByCom
340340
}
341341
go func() {
342342
taskItem.AddSubTask(i18n.GetWithName("ContainerCreate", containerName), func(t *task.Task) error {
343-
return cmd.ExecShellWithTask(taskItem, 5*time.Minute, "bash", "-c", req.Command)
343+
cmdMgr := cmd.NewCommandMgr(cmd.WithTask(*taskItem), cmd.WithTimeout(5*time.Minute))
344+
return cmdMgr.RunBashC(req.Command)
344345
}, nil)
345346
_ = taskItem.Execute()
346347
}()

0 commit comments

Comments
 (0)