Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Oct 17, 2017
1 parent 8525c7b commit 5d96643
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 352 deletions.
9 changes: 3 additions & 6 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ func main() {
app.Version = versioninfo.VERSION

app.Commands = []*cli.Command{
commands.DeployCommand(),
commands.RemoveCommand(),
commands.ReallocCommand(),
commands.BuildCommand(),
commands.LambdaCommand(),
commands.ContainerCommand(),
commands.PodCommand(),
commands.NodeCommand(),
commands.ContainerCommand(),
commands.ImageCommand(),
commands.LambdaCommand(),
}

app.Flags = commands.GlobalFlags()
Expand Down
167 changes: 154 additions & 13 deletions commands/container.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package commands

import (
"io"

log "github.com/Sirupsen/logrus"
pb "github.com/projecteru2/core/rpc/gen"
"golang.org/x/net/context"
Expand All @@ -14,34 +16,173 @@ func ContainerCommand() *cli.Command {
Usage: "container commands",
Subcommands: []*cli.Command{
&cli.Command{
Name: "get",
Usage: "get container(s)",
Action: getContainers,
Name: "get",
Usage: "get container(s)",
ArgsUsage: containerArgsUsage,
Action: getContainers,
},
&cli.Command{
Name: "remove",
Usage: "remove container(s)",
ArgsUsage: containerArgsUsage,
Action: removeContainers,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Usage: "ignore or not before stop hook if it was setted and force check",
Aliases: []string{"f"},
Value: false,
},
},
},
&cli.Command{
Name: "realloc",
Usage: "realloc containers resource",
ArgsUsage: containerArgsUsage,
Action: reallocContainers,
Flags: []cli.Flag{
&cli.Float64Flag{
Name: "cpu",
Usage: "cpu increment/decrement",
Aliases: []string{"c"},
Value: 1.0,
},
&cli.Int64Flag{
Name: "mem",
Usage: "memory increment/decrement",
Aliases: []string{"m"},
Value: 134217728,
},
},
},
&cli.Command{
Name: "deploy",
Usage: "deploy containers by a image",
ArgsUsage: specFileURI,
Action: deployContainers,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pod",
Usage: "where to run",
},
&cli.StringFlag{
Name: "entry",
Usage: "which entry",
},
&cli.StringFlag{
Name: "image",
Usage: "which to run",
},
&cli.StringFlag{
Name: "node",
Usage: "which node to run",
Value: "",
},
&cli.IntFlag{
Name: "count",
Usage: "how many",
Value: 1,
},
&cli.StringFlag{
Name: "network",
Usage: "SDN name or host mode",
Value: "host",
},
&cli.Float64Flag{
Name: "cpu",
Usage: "how many cpu",
Value: 1.0,
},
&cli.Int64Flag{
Name: "mem",
Usage: "how many memory in bytes",
Value: 536870912.0,
},
&cli.StringSliceFlag{
Name: "ids",
Usage: "id(s) of container",
Value: &cli.StringSlice{},
Name: "env",
Usage: "set env can use multiple times",
},
},
},
},
}
}

func removeContainers(c *cli.Context) error {
client, err := checkParamsAndGetClient(c)
if err != nil {
return cli.Exit(err, -1)
}
opts := &pb.RemoveContainerOptions{Ids: c.Args().Slice(), Force: c.Bool("force")}

resp, err := client.RemoveContainer(context.Background(), opts)
if err != nil {
return cli.Exit(err, -1)
}
for {
msg, err := resp.Recv()
if err == io.EOF {
break
}

if err != nil {
return cli.Exit(err, -1)
}

if msg.Success {
log.Infof("[RemoveContainer] Success %s", msg.Id[:12])
if msg.Message != "" {
log.Info(msg.Message)
}
} else {
log.Errorf("[RemoveContainer] Failed %s", msg.Message)
}
}
return nil
}

func getContainers(c *cli.Context) error {
ids := c.StringSlice("ids")
conn := setupAndGetGRPCConnection()
client := pb.NewCoreRPCClient(conn)
resp, err := client.GetContainers(context.Background(), &pb.ContainerIDs{
Ids: ids,
})
client, err := checkParamsAndGetClient(c)
if err != nil {
return cli.Exit(err, -1)
}
resp, err := client.GetContainers(context.Background(), &pb.ContainerIDs{Ids: c.Args().Slice()})
if err != nil {
log.Fatalf("[GetContainers] send request failed %v", err)
return cli.Exit(err, -1)
}

for _, container := range resp.GetContainers() {
log.Infof("ID: %s, Name: %s, Pod: %s, Node: %s", container.GetId(), container.GetName(), container.GetPodname(), container.GetNodename())
}
return nil
}

func reallocContainers(c *cli.Context) error {
client, err := checkParamsAndGetClient(c)
if err != nil {
return cli.Exit(err, -1)
}
opts := &pb.ReallocOptions{Ids: c.Args().Slice(), Cpu: c.Float64("cpu"), Mem: c.Int64("mem")}

resp, err := client.ReallocResource(context.Background(), opts)
if err != nil {
return cli.Exit(err, -1)
}
for {
msg, err := resp.Recv()
if err == io.EOF {
break
}

if err != nil {
return cli.Exit(err, -1)
}

if msg.Success {
log.Infof("[Realloc] Success %s", msg.Id[:12])
} else {
log.Errorf("[Realloc] Failed %s", msg.Id[:12])
}
}
return nil
}
69 changes: 8 additions & 61 deletions commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,32 @@ import (
pb "github.com/projecteru2/core/rpc/gen"
coreutils "github.com/projecteru2/core/utils"
"golang.org/x/net/context"
"google.golang.org/grpc"
cli "gopkg.in/urfave/cli.v2"
"gopkg.in/yaml.v2"
)

func deploy(c *cli.Context, conn *grpc.ClientConn) {
if c.NArg() != 1 {
log.Fatal("[Deploy] no spec")
func deployContainers(c *cli.Context) error {
client, err := checkParamsAndGetClient(c)
if err != nil {
return cli.Exit(err, -1)
}
specURI := c.Args().First()
log.Debugf("[Deploy] Deploy %s", specURI)

pod, node, entry, image, network, cpu, mem, envs, count := getDeployParams(c)
var data []byte
var err error
if strings.HasPrefix(specURI, "http") {
data, err = utils.GetSpecFromRemote(specURI)
} else {
data, err = ioutil.ReadFile(specURI)
}
if err != nil {
log.Fatalf("[Deploy] read spec failed %v", err)
return cli.Exit(err, -1)
}
client := pb.NewCoreRPCClient(conn)
opts := generateDeployOpts(data, pod, node, entry, image, network, cpu, mem, envs, count)
resp, err := client.CreateContainer(context.Background(), opts)
if err != nil {
log.Fatalf("[Deploy] send request failed %v", err)
return cli.Exit(err, -1)
}
for {
msg, err := resp.Recv()
Expand All @@ -48,7 +46,7 @@ func deploy(c *cli.Context, conn *grpc.ClientConn) {
}

if err != nil {
log.Fatalf("[Deploy] Message invalid %v", err)
return cli.Exit(err, -1)
}

if msg.Success {
Expand All @@ -63,6 +61,7 @@ func deploy(c *cli.Context, conn *grpc.ClientConn) {
log.Errorf("[Deploy] Failed %v", msg.Error)
}
}
return nil
}

func getDeployParams(c *cli.Context) (string, string, string, string, string, float64, int64, []string, int32) {
Expand Down Expand Up @@ -140,55 +139,3 @@ func generateDeployOpts(data []byte, pod, node, entry, image, network string, cp
}
return opts
}

//DeployCommand for running deploy task
func DeployCommand() *cli.Command {
return &cli.Command{
Name: "deploy",
Usage: "deploy containers by a image",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pod",
Usage: "where to run",
},
&cli.StringFlag{
Name: "entry",
Usage: "which entry",
},
&cli.StringFlag{
Name: "image",
Usage: "which to run",
},
&cli.StringFlag{
Name: "node",
Usage: "which node to run",
Value: "",
},
&cli.IntFlag{
Name: "count",
Usage: "how many",
Value: 1,
},
&cli.StringFlag{
Name: "network",
Usage: "SDN name or host mode",
Value: "host",
},
&cli.Float64Flag{
Name: "cpu",
Usage: "how many cpu",
Value: 1.0,
},
&cli.Int64Flag{
Name: "mem",
Usage: "how many memory in bytes",
Value: 536870912.0,
},
&cli.StringSliceFlag{
Name: "env",
Usage: "set env can use multiple times",
},
},
Action: run,
}
}
Loading

0 comments on commit 5d96643

Please sign in to comment.