Skip to content

Commit

Permalink
add build command
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Oct 9, 2017
1 parent c8c121e commit e5c96a8
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ erucli
vendor/*
test.yaml
cli
build.yaml
21 changes: 21 additions & 0 deletions build.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
stages:
- test
- build
builds:
test:
base: alpine:3.6
repo: [email protected]: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
1 change: 1 addition & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
commands.DeployCommand(),
commands.RemoveCommand(),
commands.ReallocCommand(),
commands.BuildCommand(),
}

app.Flags = commands.GlobalFlags()
Expand Down
108 changes: 108 additions & 0 deletions commands/build.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 10 additions & 10 deletions commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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()
Expand All @@ -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)
}
}
}
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions commands/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit e5c96a8

Please sign in to comment.