update.go 1.6 KB

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