|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/golang/glog" |
| 7 | + "github.com/hyperhq/hyper/engine" |
| 8 | + "github.com/hyperhq/runv/hypervisor/types" |
| 9 | +) |
| 10 | + |
| 11 | +func (daemon *Daemon) CmdPause(job *engine.Job) error { |
| 12 | + if len(job.Args) == 0 { |
| 13 | + return fmt.Errorf("Can not execute 'pause' command without pod id!") |
| 14 | + } |
| 15 | + |
| 16 | + podId := job.Args[0] |
| 17 | + glog.V(1).Infof("Pause pod %s", podId) |
| 18 | + return daemon.PausePod(podId) |
| 19 | +} |
| 20 | + |
| 21 | +func (daemon Daemon) PausePod(podId string) error { |
| 22 | + daemon.PodList.RLock() |
| 23 | + glog.V(2).Infof("lock read of PodList") |
| 24 | + pod, ok := daemon.PodList.Get(podId) |
| 25 | + if !ok { |
| 26 | + glog.V(2).Infof("unlock read of PodList") |
| 27 | + daemon.PodList.RUnlock() |
| 28 | + return fmt.Errorf("Can not get Pod info with pod ID(%s)", podId) |
| 29 | + } |
| 30 | + vmId := pod.status.Vm |
| 31 | + glog.V(2).Infof("unlock read of PodList") |
| 32 | + daemon.PodList.RUnlock() |
| 33 | + |
| 34 | + vm, ok := daemon.VmList[vmId] |
| 35 | + if !ok { |
| 36 | + return fmt.Errorf("Can not find VM whose Id is %s!", vmId) |
| 37 | + } |
| 38 | + |
| 39 | + if err := vm.Pause(true); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + pod.status.SetContainerStatus(types.S_POD_PAUSED) |
| 44 | + pod.status.Status = types.S_POD_PAUSED |
| 45 | + vm.Status = types.S_VM_PAUSED |
| 46 | + |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func (daemon Daemon) PauseContainer(container string) error { |
| 51 | + glog.V(1).Infof("Get container id is %s", container) |
| 52 | + podId, err := daemon.GetPodByContainer(container) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + return daemon.PausePod(podId) |
| 58 | +} |
| 59 | + |
| 60 | +func (daemon *Daemon) CmdUnpause(job *engine.Job) error { |
| 61 | + if len(job.Args) == 0 { |
| 62 | + return fmt.Errorf("Can not execute 'pause' command without pod id!") |
| 63 | + } |
| 64 | + |
| 65 | + podId := job.Args[0] |
| 66 | + glog.V(1).Infof("Unpause pod %s", podId) |
| 67 | + return daemon.UnpausePod(podId) |
| 68 | +} |
| 69 | + |
| 70 | +func (daemon *Daemon) UnpausePod(podId string) error { |
| 71 | + daemon.PodList.RLock() |
| 72 | + glog.V(2).Infof("lock read of PodList") |
| 73 | + pod, ok := daemon.PodList.Get(podId) |
| 74 | + if !ok { |
| 75 | + glog.V(2).Infof("unlock read of PodList") |
| 76 | + daemon.PodList.RUnlock() |
| 77 | + return fmt.Errorf("Can not get Pod info with pod ID(%s)", podId) |
| 78 | + } |
| 79 | + vmId := pod.status.Vm |
| 80 | + glog.V(2).Infof("unlock read of PodList") |
| 81 | + daemon.PodList.RUnlock() |
| 82 | + |
| 83 | + if pod.status.Status != types.S_POD_PAUSED { |
| 84 | + return fmt.Errorf("pod is not paused") |
| 85 | + } |
| 86 | + |
| 87 | + vm, ok := daemon.VmList[vmId] |
| 88 | + if !ok { |
| 89 | + return fmt.Errorf("Can not find VM whose Id is %s!", vmId) |
| 90 | + } |
| 91 | + |
| 92 | + if err := vm.Pause(false); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + pod.status.SetContainerStatus(types.S_POD_RUNNING) |
| 97 | + pod.status.Status = types.S_POD_RUNNING |
| 98 | + vm.Status = types.S_VM_ASSOCIATED |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func (daemon *Daemon) UnpauseContainer(container string) error { |
| 104 | + glog.V(1).Infof("Get container id is %s", container) |
| 105 | + podId, err := daemon.GetPodByContainer(container) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + return daemon.UnpausePod(podId) |
| 111 | +} |
0 commit comments