This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from gao-feng/pause
support Pause container for hyper
- Loading branch information
Showing
7 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters