github.com/georgethebeatle/containerd@v0.2.5/supervisor/update.go (about)

     1  package supervisor
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/docker/containerd/runtime"
     7  )
     8  
     9  // UpdateTask holds needed parameters to update a container resource constraints
    10  type UpdateTask struct {
    11  	baseTask
    12  	ID        string
    13  	State     runtime.State
    14  	Resources *runtime.Resource
    15  }
    16  
    17  func (s *Supervisor) updateContainer(t *UpdateTask) error {
    18  	i, ok := s.containers[t.ID]
    19  	if !ok {
    20  		return ErrContainerNotFound
    21  	}
    22  	container := i.container
    23  	if t.State != "" {
    24  		switch t.State {
    25  		case runtime.Running:
    26  			if err := container.Resume(); err != nil {
    27  				return err
    28  			}
    29  			s.notifySubscribers(Event{
    30  				ID:        t.ID,
    31  				Type:      StateResume,
    32  				Timestamp: time.Now(),
    33  			})
    34  		case runtime.Paused:
    35  			if err := container.Pause(); err != nil {
    36  				return err
    37  			}
    38  			s.notifySubscribers(Event{
    39  				ID:        t.ID,
    40  				Type:      StatePause,
    41  				Timestamp: time.Now(),
    42  			})
    43  		default:
    44  			return ErrUnknownContainerStatus
    45  		}
    46  		return nil
    47  	}
    48  	if t.Resources != nil {
    49  		return container.UpdateResources(t.Resources)
    50  	}
    51  	return nil
    52  }
    53  
    54  // UpdateProcessTask holds needed parameters to update a container
    55  // process terminal size or close its stdin
    56  type UpdateProcessTask struct {
    57  	baseTask
    58  	ID         string
    59  	PID        string
    60  	CloseStdin bool
    61  	Width      int
    62  	Height     int
    63  }
    64  
    65  func (s *Supervisor) updateProcess(t *UpdateProcessTask) error {
    66  	i, ok := s.containers[t.ID]
    67  	if !ok {
    68  		return ErrContainerNotFound
    69  	}
    70  	processes, err := i.container.Processes()
    71  	if err != nil {
    72  		return err
    73  	}
    74  	var process runtime.Process
    75  	for _, p := range processes {
    76  		if p.ID() == t.PID {
    77  			process = p
    78  			break
    79  		}
    80  	}
    81  	if process == nil {
    82  		return ErrProcessNotFound
    83  	}
    84  	if t.CloseStdin {
    85  		if err := process.CloseStdin(); err != nil {
    86  			return err
    87  		}
    88  	}
    89  	if t.Width > 0 || t.Height > 0 {
    90  		if err := process.Resize(t.Width, t.Height); err != nil {
    91  			return err
    92  		}
    93  	}
    94  	return nil
    95  }