Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #188 from gao-feng/pause
Browse files Browse the repository at this point in the history
support Pause container for hyper
  • Loading branch information
gnawux committed Feb 4, 2016
2 parents ae3a477 + 01e5327 commit c44ffae
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
45 changes: 45 additions & 0 deletions client/pause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package client

import (
"fmt"
"net/url"
"strings"

"github.com/hyperhq/hyper/engine"
gflag "github.com/jessevdk/go-flags"
)

func (cli *HyperClient) HyperCmdPause(args ...string) error {
var parser = gflag.NewParser(nil, gflag.Default|gflag.IgnoreUnknown)
parser.Usage = "pause Pod\n\nPause the pod"
args, err := parser.ParseArgs(args)
if err != nil {
if !strings.Contains(err.Error(), "Usage") {
return err
} else {
return nil
}
}
if len(args) == 0 {
return fmt.Errorf("Can not accept the 'pause' command without Pod ID!")
}

v := url.Values{}
v.Set("podId", args[0])

body, _, err := readBody(cli.call("POST", "/pod/pause?"+v.Encode(), nil, nil))
if err != nil {
return err
}

out := engine.NewOutput()
if _, err = out.AddEnv(); err != nil {
return err
}

if _, err := out.Write(body); err != nil {
return err
}
out.Close()
return nil
}
44 changes: 44 additions & 0 deletions client/unpause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package client

import (
"fmt"
"net/url"
"strings"

"github.com/hyperhq/hyper/engine"
gflag "github.com/jessevdk/go-flags"
)

func (cli *HyperClient) HyperCmdUnpause(args ...string) error {
var parser = gflag.NewParser(nil, gflag.Default|gflag.IgnoreUnknown)
parser.Usage = "unpause Pod\n\nUnpause the pod"
args, err := parser.ParseArgs(args)
if err != nil {
if !strings.Contains(err.Error(), "Usage") {
return err
} else {
return nil
}
}
if len(args) == 0 {
return fmt.Errorf("Can not accept the 'unpause' command without Pod ID!")
}

v := url.Values{}
v.Set("podId", args[0])

body, _, err := readBody(cli.call("POST", "/pod/unpause?"+v.Encode(), nil, nil))
if err != nil {
return err
}
out := engine.NewOutput()
if _, err = out.AddEnv(); err != nil {
return err
}

if _, err := out.Write(body); err != nil {
return err
}
out.Close()
return nil
}
5 changes: 5 additions & 0 deletions daemon/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ func (daemon *Daemon) CmdCommit(job *engine.Job) error {
message := job.Args[4]
pause := job.Args[5]

if pause != "no" {
daemon.PauseContainer(containerId)
defer daemon.UnpauseContainer(containerId)
}

cli := daemon.DockerCli
imgId, _, err := cli.SendContainerCommit(containerId, repo, author, change, message, pause)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func (daemon *Daemon) Install(eng *engine.Engine) error {
"build": daemon.CmdBuild,
"commit": daemon.CmdCommit,
"rename": daemon.CmdRename,
"pause": daemon.CmdPause,
"unpause": daemon.CmdUnpause,
"push": daemon.CmdPush,
"podCreate": daemon.CmdPodCreate,
"podStart": daemon.CmdPodStart,
Expand Down
7 changes: 7 additions & 0 deletions daemon/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ func showVM(v *hypervisor.Vm) string {
case types.S_VM_IDLE:
status = "idle"
break
case types.S_VM_PAUSED:
status = "pasued"
break
default:
status = ""
break
Expand All @@ -176,6 +179,8 @@ func showPod(pod *hypervisor.PodStatus) string {
if pod.Type == "kubernetes" {
status = "failed(kubernetes)"
}
case types.S_POD_PAUSED:
status = "paused"
case types.S_POD_SUCCEEDED:
status = "succeeded"
if pod.Type == "kubernetes" {
Expand Down Expand Up @@ -214,6 +219,8 @@ func showContainer(c *hypervisor.Container) string {
status = "failed"
case types.S_POD_SUCCEEDED:
status = "succeeded"
case types.S_POD_PAUSED:
status = "paused"
default:
status = ""
}
Expand Down
111 changes: 111 additions & 0 deletions daemon/pause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package daemon

import (
"fmt"

"github.com/golang/glog"
"github.com/hyperhq/hyper/engine"
"github.com/hyperhq/runv/hypervisor/types"
)

func (daemon *Daemon) CmdPause(job *engine.Job) error {
if len(job.Args) == 0 {
return fmt.Errorf("Can not execute 'pause' command without pod id!")
}

podId := job.Args[0]
glog.V(1).Infof("Pause pod %s", podId)
return daemon.PausePod(podId)
}

func (daemon Daemon) PausePod(podId string) error {
daemon.PodList.RLock()
glog.V(2).Infof("lock read of PodList")
pod, ok := daemon.PodList.Get(podId)
if !ok {
glog.V(2).Infof("unlock read of PodList")
daemon.PodList.RUnlock()
return fmt.Errorf("Can not get Pod info with pod ID(%s)", podId)
}
vmId := pod.status.Vm
glog.V(2).Infof("unlock read of PodList")
daemon.PodList.RUnlock()

vm, ok := daemon.VmList[vmId]
if !ok {
return fmt.Errorf("Can not find VM whose Id is %s!", vmId)
}

if err := vm.Pause(true); err != nil {
return err
}

pod.status.SetContainerStatus(types.S_POD_PAUSED)
pod.status.Status = types.S_POD_PAUSED
vm.Status = types.S_VM_PAUSED

return nil
}

func (daemon Daemon) PauseContainer(container string) error {
glog.V(1).Infof("Get container id is %s", container)
podId, err := daemon.GetPodByContainer(container)
if err != nil {
return err
}

return daemon.PausePod(podId)
}

func (daemon *Daemon) CmdUnpause(job *engine.Job) error {
if len(job.Args) == 0 {
return fmt.Errorf("Can not execute 'pause' command without pod id!")
}

podId := job.Args[0]
glog.V(1).Infof("Unpause pod %s", podId)
return daemon.UnpausePod(podId)
}

func (daemon *Daemon) UnpausePod(podId string) error {
daemon.PodList.RLock()
glog.V(2).Infof("lock read of PodList")
pod, ok := daemon.PodList.Get(podId)
if !ok {
glog.V(2).Infof("unlock read of PodList")
daemon.PodList.RUnlock()
return fmt.Errorf("Can not get Pod info with pod ID(%s)", podId)
}
vmId := pod.status.Vm
glog.V(2).Infof("unlock read of PodList")
daemon.PodList.RUnlock()

if pod.status.Status != types.S_POD_PAUSED {
return fmt.Errorf("pod is not paused")
}

vm, ok := daemon.VmList[vmId]
if !ok {
return fmt.Errorf("Can not find VM whose Id is %s!", vmId)
}

if err := vm.Pause(false); err != nil {
return err
}

pod.status.SetContainerStatus(types.S_POD_RUNNING)
pod.status.Status = types.S_POD_RUNNING
vm.Status = types.S_VM_ASSOCIATED

return nil
}

func (daemon *Daemon) UnpauseContainer(container string) error {
glog.V(1).Infof("Get container id is %s", container)
podId, err := daemon.GetPodByContainer(container)
if err != nil {
return err
}

return daemon.UnpausePod(podId)
}
30 changes: 30 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,34 @@ func postContainerRename(eng *engine.Engine, version version.Version, w http.Res
return writeJSONEnv(w, http.StatusOK, env)
}

func postPodPause(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
}

job := eng.Job("pause", r.Form.Get("podId"))
if err := job.Run(); err != nil {
return err
}

w.WriteHeader(http.StatusNoContent)
return nil
}

func postPodUnpause(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
}

job := eng.Job("unpause", r.Form.Get("podId"))
if err := job.Run(); err != nil {
return err
}

w.WriteHeader(http.StatusNoContent)
return nil
}

func postPodCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil {
return err
Expand Down Expand Up @@ -1188,6 +1216,8 @@ func createRouter(eng *engine.Engine, logging, enableCors bool, corsHeaders stri
"/pod/labels": postPodLabels,
"/pod/start": postPodStart,
"/pod/stop": postStop,
"/pod/pause": postPodPause,
"/pod/unpause": postPodUnpause,
"/service/add": postServiceAdd,
"/service/update": postServiceUpdate,
"/tty/resize": postTtyResize,
Expand Down

0 comments on commit c44ffae

Please sign in to comment.