11package service
22
33import (
4+ "bytes"
45 "context"
56 "encoding/base64"
67 "encoding/json"
8+ "errors"
79 "fmt"
810 "os"
911 "os/exec"
@@ -16,6 +18,7 @@ import (
1618 "github.com/1Panel-dev/1Panel/agent/app/dto"
1719 "github.com/1Panel-dev/1Panel/agent/global"
1820 "github.com/1Panel-dev/1Panel/agent/i18n"
21+ "github.com/1Panel-dev/1Panel/agent/utils/common"
1922 "github.com/1Panel-dev/1Panel/agent/utils/re"
2023)
2124
@@ -567,3 +570,47 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {
567570
568571 return listFile , nil
569572}
573+
574+ var errLogContainerMissing = errors .New ("log container missing" )
575+
576+ func checkLogContainers (params dto.StreamLog ) error {
577+ ctx , cancel := context .WithTimeout (context .Background (), 15 * time .Second )
578+ defer cancel ()
579+ command := "docker"
580+ args := []string {"container" , "inspect" , "--format" , "{{.Id}}" , params .Container }
581+ if params .Type == "compose" {
582+ command = common .GetDockerComposeCommand ()
583+ args = nil
584+ if command != "docker-compose" {
585+ command = "docker"
586+ args = append (args , "compose" )
587+ }
588+ for _ , file := range strings .Split (params .Compose , "," ) {
589+ if file != "" {
590+ args = append (args , "-f" , file )
591+ }
592+ }
593+ args = append (args , "ps" , "--all" , "--quiet" )
594+ }
595+ cmd := exec .CommandContext (ctx , command , args ... )
596+ var stderr bytes.Buffer
597+ cmd .Stderr = & stderr
598+ output , err := cmd .Output ()
599+ if err != nil {
600+ return logContainerPresenceResult (params .Type , stderr .String (), err )
601+ }
602+ return logContainerPresenceResult (params .Type , string (output ), nil )
603+ }
604+
605+ func logContainerPresenceResult (kind , output string , err error ) error {
606+ if err != nil {
607+ if kind != "compose" && (strings .Contains (output , "No such container:" ) || strings .Contains (output , "No such object:" )) {
608+ return errLogContainerMissing
609+ }
610+ return fmt .Errorf ("check log containers: %w: %s" , err , strings .TrimSpace (output ))
611+ }
612+ if strings .TrimSpace (output ) == "" {
613+ return errLogContainerMissing
614+ }
615+ return nil
616+ }
0 commit comments