From e5c96a8a58dbbf2032f501a70d3d2a2dbf90361b Mon Sep 17 00:00:00 2001 From: CMGS Date: Mon, 9 Oct 2017 14:26:49 +0800 Subject: [PATCH] add build command --- .gitignore | 1 + build.yaml.example | 21 +++++++++ cli.go | 1 + commands/build.go | 108 +++++++++++++++++++++++++++++++++++++++++++++ commands/deploy.go | 20 ++++----- commands/main.go | 2 + 6 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 build.yaml.example create mode 100644 commands/build.go diff --git a/.gitignore b/.gitignore index a7e2398..46ffd26 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ erucli vendor/* test.yaml cli +build.yaml diff --git a/build.yaml.example b/build.yaml.example new file mode 100644 index 0000000..f66de04 --- /dev/null +++ b/build.yaml.example @@ -0,0 +1,21 @@ +stages: + - test + - build +builds: + test: + base: alpine:3.6 + repo: git@github.com:CMGS/cli.git + version: c9fa493eacae4288c6f226c79752ebc103e2ba62 + commands: + - echo '1' + - ls -al + - touch /tmp/testbuild + - echo CMGS > /tmp/testbuild + cache: + /tmp/testbuild: /testCMGS + build: + base: alpine:latest + commands: + - echo '2' + - cat /testCMGS + working_dir: /tmp diff --git a/cli.go b/cli.go index 3235583..e5db038 100644 --- a/cli.go +++ b/cli.go @@ -25,6 +25,7 @@ func main() { commands.DeployCommand(), commands.RemoveCommand(), commands.ReallocCommand(), + commands.BuildCommand(), } app.Flags = commands.GlobalFlags() diff --git a/commands/build.go b/commands/build.go new file mode 100644 index 0000000..4d59598 --- /dev/null +++ b/commands/build.go @@ -0,0 +1,108 @@ +package commands + +import ( + "io" + "io/ioutil" + "strings" + + log "github.com/Sirupsen/logrus" + "github.com/projecteru2/cli/utils" + pb "github.com/projecteru2/core/rpc/gen" + "golang.org/x/net/context" + "google.golang.org/grpc" + cli "gopkg.in/urfave/cli.v2" + yaml "gopkg.in/yaml.v2" +) + +func BuildCommand() *cli.Command { + return &cli.Command{ + Name: "build", + Usage: "build a image", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "name", + Usage: "name of image", + }, + &cli.StringFlag{ + Name: "tag", + Usage: "tag of image", + Value: "latest", + }, + &cli.StringFlag{ + Name: "user", + Usage: "user of image", + Value: "", + DefaultText: "root", + }, + &cli.IntFlag{ + Name: "uid", + Usage: "uid of image", + Value: 0, + DefaultText: "1", + }, + }, + Action: run, + } +} + +func build(c *cli.Context, conn *grpc.ClientConn) { + opts := generateBuildOpts(c) + client := pb.NewCoreRPCClient(conn) + resp, err := client.BuildImage(context.Background(), opts) + if err != nil { + log.Fatalf("[Build] send request failed %v", err) + } + for { + msg, err := resp.Recv() + if err == io.EOF { + break + } + + if err != nil { + log.Fatalf("[Build] Message invalid %v", err) + } + + if msg.Error != "" { + log.Errorf("[Build] Error %d %s", msg.ErrorDetail.Code, msg.ErrorDetail.Message) + } else { + log.Infof("[Build] %s %s %s", msg.Status, msg.Stream, msg.Progress) + + } + } +} + +func generateBuildOpts(c *cli.Context) *pb.BuildImageOptions { + if c.NArg() != 1 { + log.Fatal("[Build] no spec") + } + specURI := c.Args().First() + log.Debugf("[Build] Deploy %s", specURI) + 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("[Build] read spec failed %v", err) + } + specs := &pb.Builds{} + if err = yaml.Unmarshal(data, specs); err != nil { + log.Fatalf("[Build] unmarshal specs failed %v", err) + } + + name := c.String("name") + user := c.String("user") + uid := int32(c.Int("uid")) + tag := c.String("tag") + + opts := &pb.BuildImageOptions{ + Name: name, + User: user, + Uid: uid, + Tag: tag, + Builds: specs, + } + return opts +} diff --git a/commands/deploy.go b/commands/deploy.go index 87b57de..caec184 100644 --- a/commands/deploy.go +++ b/commands/deploy.go @@ -20,10 +20,10 @@ import ( func deploy(c *cli.Context, conn *grpc.ClientConn) { pod, entry, image, network, cpu, mem, envs, count := getDeployParams(c) if c.NArg() != 1 { - log.Fatal("[RawDeploy] no spec") + log.Fatal("[Deploy] no spec") } specURI := c.Args().First() - log.Debugf("[RawDeploy] Deploy %s", specURI) + log.Debugf("[Deploy] Deploy %s", specURI) var data []byte var err error if strings.HasPrefix(specURI, "http") { @@ -32,13 +32,13 @@ func deploy(c *cli.Context, conn *grpc.ClientConn) { data, err = ioutil.ReadFile(specURI) } if err != nil { - log.Fatalf("[RawDeploy] read spec failed %v", err) + log.Fatalf("[Deploy] read spec failed %v", err) } client := pb.NewCoreRPCClient(conn) opts := generateDeployOpts(data, pod, entry, image, network, cpu, mem, envs, count) resp, err := client.CreateContainer(context.Background(), opts) if err != nil { - log.Fatalf("[RawDeploy] send request failed %v", err) + log.Fatalf("[Deploy] send request failed %v", err) } for { msg, err := resp.Recv() @@ -47,19 +47,19 @@ func deploy(c *cli.Context, conn *grpc.ClientConn) { } if err != nil { - log.Fatalf("[RawDeploy] Message invalid %v", err) + log.Fatalf("[Deploy] Message invalid %v", err) } if msg.Success { - log.Infof("[RawDeploy] Success %s %s %s %v %d", msg.Id, msg.Name, msg.Nodename, msg.Cpu, msg.Memory) + log.Infof("[Deploy] Success %s %s %s %v %d", msg.Id, msg.Name, msg.Nodename, msg.Cpu, msg.Memory) if len(msg.Hook) > 0 { - log.Infof("[RawDeploy] Hook output \n%s", msg.Hook) + log.Infof("[Deploy] Hook output \n%s", msg.Hook) } for name, publish := range msg.Publish { - log.Infof("[RawDeploy] Bound %s ip %s", name, publish) + log.Infof("[Deploy] Bound %s ip %s", name, publish) } } else { - log.Errorf("[RawDeploy] Failed %v", msg.Error) + log.Errorf("[Deploy] Failed %v", msg.Error) } } } @@ -74,7 +74,7 @@ func getDeployParams(c *cli.Context) (string, string, string, string, float64, i envs := c.StringSlice("env") count := int32(c.Int("count")) if pod == "" || entry == "" || image == "" { - log.Fatal("[RawDeploy] no pod or entry or image") + log.Fatal("[Deploy] no pod or entry or image") } return pod, entry, image, network, cpu, mem, envs, count } diff --git a/commands/main.go b/commands/main.go index f29b0ac..bee831d 100644 --- a/commands/main.go +++ b/commands/main.go @@ -67,6 +67,8 @@ func run(c *cli.Context) error { remove(c, conn) } else if c.Command.Name == "realloc" { realloc(c, conn) + } else if c.Command.Name == "build" { + build(c, conn) } else { log.Fatal("Not support yet") }