github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/containerd/supervisor/get_containers.go (about)

     1  package supervisor
     2  
     3  import (
     4  
     5     "os"
     6     "log"
     7  
     8     "github.com/docker/containerd/runtime"
     9  )
    10  
    11  // GetContainersTask holds needed parameters to retrieve a list of
    12  // containers
    13  type GetContainersTask struct {
    14  	baseTask
    15  	ID       string
    16  	GetState func(c runtime.Container) (interface{}, error)
    17  
    18  	Containers []runtime.Container
    19  	States     []interface{}
    20  }
    21  
    22  func (s *Supervisor) getContainers(t *GetContainersTask) error {
    23  
    24  	if t.ID != "" {
    25  		ci, ok := s.containers[t.ID]
    26  		if !ok {
    27              logPrintServerGetContainers("getContainers")
    28  			return ErrContainerNotFound
    29  		}
    30  		t.Containers = append(t.Containers, ci.container)
    31  		if t.GetState != nil {
    32  			st, err := t.GetState(ci.container)
    33  			if err != nil {
    34  				return err
    35  			}
    36  			t.States = append(t.States, st)
    37  		}
    38  
    39  		return nil
    40  	}
    41  
    42  	for _, ci := range s.containers {
    43  		t.Containers = append(t.Containers, ci.container)
    44  		if t.GetState != nil {
    45  			st, err := t.GetState(ci.container)
    46  			if err != nil {
    47  				return err
    48  			}
    49  			t.States = append(t.States, st)
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  
    57  func logPrintServerGetContainers(errStr string) {
    58      logFile, logError := os.Open("/home/vagrant/getlogServer.md")
    59      if logError != nil {
    60          logFile, _ = os.Create("/home/vagrant/getlogServer.md")
    61      }
    62      defer logFile.Close()
    63  
    64      debugLog := log.New(logFile, "[Debug]", log.Llongfile)
    65      debugLog.Println(errStr)
    66  }