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

GRPC interface for Start/Remove container #469

Merged
merged 9 commits into from
Dec 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/api/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (cli *Client) CreatePod(spec interface{}) (string, int, error) {

func (cli *Client) CreateContainer(podID string, spec interface{}) (string, int, error) {
v := url.Values{}
v.Set("podID", podID)
v.Set("podId", podID)
body, statusCode, err := readBody(cli.call("POST", "/container/create?"+v.Encode(), spec, nil))
if err != nil {
return "", statusCode, err
Expand Down
3 changes: 3 additions & 0 deletions client/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ type APIInterface interface {
WinResize(id, tag string, height, width int) error

List(item, pod, vm string, aux bool) (*engine.Env, error)
CreateContainer(podID string, spec interface{}) (string, int, error)
StartContainer(container string) error
GetContainerInfo(container string) (*types.ContainerInfo, error)
GetContainerByPod(podId string) (string, error)
GetExitCode(container, tag string) error
ContainerLogs(container, since string, timestamp, follow, stdout, stderr bool, tail string) (io.ReadCloser, string, error)
KillContainer(container string, sig int) error
StopContainer(container string) error
RemoveContainer(container string) error

GetPodInfo(podName string) (*types.PodInfo, error)
CreatePod(spec interface{}) (string, int, error)
Expand Down
11 changes: 11 additions & 0 deletions client/api/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ func (cli *Client) RmPod(id string) error {
}
return nil
}

func (cli *Client) RemoveContainer(container string) error {
v := url.Values{}
v.Set("container", container)

_, _, err := readBody(cli.call("POST", "/container/remove?"+v.Encode(), nil, nil))
if err != nil {
return err
}
return nil
}
12 changes: 12 additions & 0 deletions client/api/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ func (cli *Client) startPodWithoutTty(v *url.Values) (string, error) {
}
return remoteInfo.Get("ID"), nil
}

func (cli *Client) StartContainer(container string) error {
v := url.Values{}
v.Set("container", container)

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

}
76 changes: 43 additions & 33 deletions client/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,65 @@ package client
import (
"encoding/json"
"fmt"
"strings"
"net/http"

apitype "github.com/hyperhq/hyperd/types"

gflag "github.com/jessevdk/go-flags"
"net/http"
)

func (cli *HyperClient) HyperCmdCreate(args ...string) error {
var opts struct {
Yaml bool `short:"y" long:"yaml" default:"false" default-mask:"-" description:"create a pod based on Yaml file"`
}
var parser = gflag.NewParser(&opts, gflag.Default)
parser.Usage = "create [OPTIONS] POD_FILE\n\nCreate a pod into 'pending' status, but without running it"
args, err := parser.ParseArgs(args)
if err != nil {
if !strings.Contains(err.Error(), "Usage") {
return err
} else {
return nil
}
copt, err := cli.ParseCreateOptions("create", args...)
if copt == nil {
return err
}
if len(args) == 0 {
return fmt.Errorf("\"create\" requires a minimum of 1 argument, please provide POD spec file.\n")
if copt.Remove {
return fmt.Errorf("\"create\" does not support rm parameter")
}
jsonFile := args[0]

jsonbody, err := cli.JsonFromFile(jsonFile, opts.Yaml, false)
if err != nil {
return err
if copt.IsContainer && copt.PodId == "" {
return fmt.Errorf("did not provide the target pod")
}

var tmpPod apitype.UserPod
if err := json.Unmarshal([]byte(jsonbody), &tmpPod); err != nil {
return err
}
podId, statusCode, err := cli.client.CreatePod(&tmpPod)
if err != nil {
if statusCode == http.StatusNotFound {
err = cli.PullImages(&tmpPod)
if !copt.IsContainer {
var tmpPod apitype.UserPod
err := json.Unmarshal(copt.JsonBytes, &tmpPod)
if err != nil {
return fmt.Errorf("failed to read json: %v", err)
}
podId, statusCode, err := cli.client.CreatePod(&tmpPod)
if err != nil {
if statusCode == http.StatusNotFound {
err = cli.PullImages(&tmpPod)
if err != nil {
return err
}
podId, statusCode, err = cli.client.CreatePod(&tmpPod)
}
if err != nil {
return err
}
podId, statusCode, err = cli.client.CreatePod(&tmpPod)
}
fmt.Printf("Pod ID is %s\n", podId)
} else {
var tmpContainer apitype.UserContainer
err := json.Unmarshal(copt.JsonBytes, &tmpContainer)
if err != nil {
return fmt.Errorf("failed to read json: %v", err)
}
cid, statusCode, err := cli.client.CreateContainer(copt.PodId, &tmpContainer)
if err != nil {
return err
if statusCode == http.StatusNotFound {
err = cli.PullImage(tmpContainer.Image)
if err != nil {
return err
}
cid, statusCode, err = cli.client.CreateContainer(copt.PodId, &tmpContainer)
}
if err != nil {
return err
}
}
fmt.Printf("Container ID is %s\n", cid)
}
fmt.Printf("Pod ID is %s\n", podId)

return nil
}
27 changes: 19 additions & 8 deletions client/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
)

func (cli *HyperClient) HyperCmdRm(args ...string) error {
var parser = gflag.NewParser(nil, gflag.Default)
parser.Usage = "rm POD [POD...]\n\nRemove one or more pods"
var opts struct {
Container bool `short:"c" long:"container" default:"false" default-mask:"-" description:"stop container"`
}
var parser = gflag.NewParser(&opts, gflag.Default)
parser.Usage = "rm [OPTIONS] CONTAINER|POD [CONTAINER|POD...]\n\nRemove one or more containers/pods"
args, err := parser.ParseArgs(args)
if err != nil {
if !strings.Contains(err.Error(), "Usage") {
Expand All @@ -21,13 +24,21 @@ func (cli *HyperClient) HyperCmdRm(args ...string) error {
if len(args) == 0 {
return fmt.Errorf("\"rm\" requires a minimum of 1 argument, please provide POD ID.\n")
}
pods := args
for _, id := range pods {
err := cli.client.RmPod(id)
if err == nil {
fmt.Fprintf(cli.out, "Pod(%s) is successful to be deleted!\n", id)
for _, id := range args {
if opts.Container {
err := cli.client.RemoveContainer(id)
if err == nil {
fmt.Fprintf(cli.out, "container %s is successfully deleted!\n", id)
} else {
fmt.Fprintf(cli.err, "container %s delete failed: %v\n", id, err)
}
} else {
fmt.Fprintf(cli.out, "%v\n", err)
err := cli.client.RmPod(id)
if err == nil {
fmt.Fprintf(cli.out, "Pod(%s) is successfully deleted!\n", id)
} else {
fmt.Fprintf(cli.err, "Pod(%s) delete failed: %v\n", id, err)
}
}
}
return nil
Expand Down
Loading