Skip to content

Commit

Permalink
fix: rollout, upコマンドでdeployコンテクストパスを指定できるようになった
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp committed Oct 9, 2018
1 parent e65232e commit 67b6414
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
17 changes: 8 additions & 9 deletions cli/cage/commands/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/loilo-inc/canarycage"
"github.com/urfave/cli"
"io/ioutil"
"os"
)

Expand All @@ -31,8 +30,9 @@ func RollOutCommand() cli.Command {
}
configPath := ""
return cli.Command{
Name: "rollout",
Name: "rollout",
Description: "start rolling out next service with current service",
ArgsUsage: "[deploy context path]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Expand Down Expand Up @@ -128,13 +128,11 @@ func RollOutCommand() cli.Command {
fmt.Fprint(os.Stdout, string(d))
os.Exit(0)
}
if configPath != "" {
d, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatalf("failed to read config file %s due to: %s", configPath, err)
}
if err := json.Unmarshal(d, envars); err != nil {
log.Fatalf("failed to unmarshal json due to: %s", err)
if ctx.NArg() > 0 {
// deployコンテクストを指定した場合
dir := ctx.Args().Get(0)
if err := envars.LoadFromFiles(dir); err != nil {
log.Fatalf(err.Error())
}
}
err := cage.EnsureEnvars(envars)
Expand Down Expand Up @@ -184,6 +182,7 @@ func DryRun(envars *cage.Envars) {
log.Infof("%d roll outs are expected", estimated)
}


func Action(envars *cage.Envars) error {
ses, err := session.NewSession(&aws.Config{
Region: envars.Region,
Expand Down
16 changes: 6 additions & 10 deletions cli/cage/commands/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ import (
)

func UpCommand(ses *session.Session) cli.Command {
context := "."
return cli.Command{
Name: "up",
Flags: []cli.Flag{
cli.StringFlag{
Name: "context, c",
Usage: "dirname that contains `service.json` and `task-definition.json`. default value is `.`",
Value: ".",
Destination: &context,
},
},
ArgsUsage: "[up context path (default=.)]",
Action: func(ctx *cli.Context) {
Up(ecs.New(ses), context)
dir := "."
if ctx.NArg() > 0 {
dir = ctx.Args().Get(0)
}
Up(ecs.New(ses), dir)
},
}
}
Expand Down
49 changes: 48 additions & 1 deletion env.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package cage

import (
"math"
"encoding/base64"
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"math"
"os"
"path/filepath"
)

type Envars struct {
Expand Down Expand Up @@ -88,3 +93,45 @@ func EnsureEnvars(
}
return nil
}

func (e *Envars) LoadFromFiles(dir string) error {
svcPath := filepath.Join(dir, "service.json")
tdPath := filepath.Join(dir, "task-definition.json")
_, noSvc := os.Stat(svcPath)
_, noTd := os.Stat(tdPath)
if noSvc != nil || noTd != nil {
return NewErrorf("roll out context specified at '%s' but no 'service.json' or 'task-definition.json'", dir)
}
var (
svc = &ecs.CreateServiceInput{}
td = &ecs.RegisterTaskDefinitionInput{}
svcBase64 string
tdBase64 string
)
if d ,err := ReadAndUnmarshalJson(svcPath, svc); err != nil {
return NewErrorf("failed to read and unmarshal service.json: %s", err)
} else {
svcBase64 = base64.StdEncoding.EncodeToString(d)
}
if d, err := ReadAndUnmarshalJson(tdPath, td); err != nil {
return NewErrorf("failed to read and unmarshal task-definition.json: %s", err)
} else {
tdBase64 = base64.StdEncoding.EncodeToString(d)
}
e.Cluster = svc.Cluster
e.NextServiceName = svc.ServiceName
e.NextServiceDefinitionBase64 = &svcBase64
e.NextTaskDefinitionBase64 = &tdBase64
return nil
}

func ReadAndUnmarshalJson(path string, dest interface{}) ([]byte, error) {
if d, err := ReadFileAndApplyEnvars(path); err != nil {
return d, err
} else {
if err := json.Unmarshal(d, dest); err != nil {
return d, err
}
return d, nil
}
}
3 changes: 2 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func ReadFileAndApplyEnvars(path string) ([]byte, error) {
if envar, ok := os.LookupEnv(m[1]); ok {
str = strings.Replace(str, m[0], envar, -1)
} else {
log.Warnf("envar literal '%s' found in %s but was not defined", m[0], path)
log.Warnf("envar literal '%s' found in %s but was not defined. filled by empty string", m[0], path)
str = strings.Replace(str, m[0], "", -1)
}
}
return []byte(str), nil
Expand Down

0 comments on commit 67b6414

Please sign in to comment.