11package service
22
33import (
4+ "context"
45 "encoding/json"
56 "fmt"
67 "os"
@@ -15,6 +16,7 @@ import (
1516 "github.com/1Panel-dev/1Panel/agent/buserr"
1617 "github.com/1Panel-dev/1Panel/agent/constant"
1718 "github.com/1Panel-dev/1Panel/agent/global"
19+ "github.com/1Panel-dev/1Panel/agent/utils/docker"
1820 "github.com/jinzhu/copier"
1921 "github.com/pkg/errors"
2022 "github.com/robfig/cron/v3"
@@ -159,6 +161,14 @@ func (u *CronjobService) Export(req dto.OperateByIDs) (string, error) {
159161 for _ , db := range databases {
160162 item .DBNames = append (item .DBNames , dto.TransHelper {Name : db .Database , DetailName : db .Name })
161163 }
164+ case "shell" :
165+ if cronjob .ScriptMode == "library" {
166+ script , err := scriptRepo .Get (repo .WithByID (cronjob .ScriptID ))
167+ if err != nil {
168+ return "" , err
169+ }
170+ item .ScriptName = script .Name
171+ }
162172 }
163173 item .SourceAccounts , item .DownloadAccount , _ = loadBackupNamesByID (cronjob .SourceAccountIDs , cronjob .DownloadAccountID )
164174 alertInfo , _ := alertRepo .Get (alertRepo .WithByType (cronjob .Type ), alertRepo .WithByProject (strconv .Itoa (int (cronjob .ID ))), repo .WithByStatus (constant .AlertEnable ))
@@ -191,7 +201,6 @@ func (u *CronjobService) Import(req []dto.CronjobTrans) error {
191201 Spec : item .Spec ,
192202 Executor : item .Executor ,
193203 ScriptMode : item .ScriptMode ,
194- Script : item .Script ,
195204 Command : item .Command ,
196205 ContainerName : item .ContainerName ,
197206 User : item .User ,
@@ -265,6 +274,36 @@ func (u *CronjobService) Import(req []dto.CronjobTrans) error {
265274 }
266275 }
267276 cronjob .DBName = strings .Join (dbIDs , "," )
277+ case "shell" :
278+ if len (item .ContainerName ) != 0 {
279+ client , err := docker .NewDockerClient ()
280+ if err != nil {
281+ hasNotFound = true
282+ continue
283+ }
284+ defer client .Close ()
285+ if _ , err := client .ContainerStats (context .Background (), item .ContainerName , false ); err != nil {
286+ hasNotFound = true
287+ continue
288+ }
289+ }
290+ switch item .ScriptMode {
291+ case "library" :
292+ library , _ := scriptRepo .Get (repo .WithByName (item .ScriptName ))
293+ if library .ID == 0 {
294+ hasNotFound = true
295+ continue
296+ }
297+ cronjob .ScriptID = library .ID
298+ case "select" :
299+ if _ , err := os .Stat (item .Script ); err != nil {
300+ hasNotFound = true
301+ continue
302+ }
303+ cronjob .Script = item .Script
304+ case "input" :
305+ cronjob .Script = item .Script
306+ }
268307 }
269308 var acIDs []string
270309 for _ , ac := range item .SourceAccounts {
0 commit comments