Skip to content

Commit

Permalink
Merge pull request #46 from stormcat24/feature/variables
Browse files Browse the repository at this point in the history
Support for passing parameter
  • Loading branch information
Akinori Yamada committed Dec 14, 2015
2 parents 68da5cd + 838a37a commit 000e9dd
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 98 deletions.
81 changes: 50 additions & 31 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ test-service:
#### Keep desired_count at updating service

If you modify value of `desired_count` by AWS Management Console or aws-cli, you'll fear override value of `desired_count` by ecs-formation. This value should be flexibly changed in the operation.

If `keep_desired_count` is `true`, keep current `desired_count` at updating service.

```bash
Expand All @@ -121,7 +121,7 @@ test-service:
desired_count: 1
keep_desired_count: true
```


#### Manage Task Definitions

Expand Down Expand Up @@ -216,6 +216,24 @@ chain_elb:
standby_elb: test-internal-elb-standby
```
### Others
#### Passing custom parameters
You can use custom parameters. Define parameters in yaml file(task, service, bluegreen) as follows.
```Ruby
nginx:
image: stormcat24/nginx:${NGINX_VERSION}
ports:
- 80:${NGINX_PORT}
```
You can set value for these parameters by using `-p` option.
```bash
ecs-formation task -p NGINX_VERSION=1.0 -p NGINX_PORT=80 plan your-web-task
```
License
===
See [LICENSE](LICENSE).
Expand Down
30 changes: 20 additions & 10 deletions bluegreen/bluegreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stormcat24/ecs-formation/aws"
"github.com/stormcat24/ecs-formation/logger"
"github.com/stormcat24/ecs-formation/service"
"github.com/stormcat24/ecs-formation/util"
"github.com/str1ngs/ansi/color"
"gopkg.in/yaml.v2"
"io/ioutil"
Expand All @@ -19,20 +20,21 @@ type BlueGreenController struct {
ClusterController *service.ServiceController
blueGreenMap map[string]*BlueGreen
TargetResource string
params map[string]string
}

func NewBlueGreenController(manager *aws.AwsManager, projectDir string, targetResource string) (*BlueGreenController, error) {
func NewBlueGreenController(manager *aws.AwsManager, projectDir string, targetResource string, params map[string]string) (*BlueGreenController, error) {

ccon, errcc := service.NewServiceController(manager, projectDir, "")

if errcc != nil {
return nil, errcc
ccon, err := service.NewServiceController(manager, projectDir, "", params)
if err != nil {
return nil, err
}

con := &BlueGreenController{
manager: manager,
ClusterController: ccon,
TargetResource: targetResource,
params: params,
}

defs, errs := con.searchBlueGreen(projectDir)
Expand All @@ -59,11 +61,16 @@ func (self *BlueGreenController) searchBlueGreen(projectDir string) (map[string]

for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".yml") {
content, _ := ioutil.ReadFile(clusterDir + "/" + file.Name())
content, err := ioutil.ReadFile(clusterDir + "/" + file.Name())
if err != nil {
return nil, err
}

mergedYaml := util.MergeYamlWithParameters(content, self.params)
tokens := filePattern.FindStringSubmatch(file.Name())
name := tokens[1]

bg, err := CreateBlueGreen(content)
bg, err := CreateBlueGreen(mergedYaml)
if err != nil {
return merged, err
}
Expand All @@ -74,11 +81,14 @@ func (self *BlueGreenController) searchBlueGreen(projectDir string) (map[string]
return merged, nil
}

func CreateBlueGreen(data []byte) (*BlueGreen, error) {
func CreateBlueGreen(data string) (*BlueGreen, error) {

bg := &BlueGreen{}
err := yaml.Unmarshal(data, bg)
return bg, err
if err := yaml.Unmarshal([]byte(data), bg); err != nil {
return nil, errors.New(fmt.Sprintf("%v\n\n%v", err.Error(), data))
}

return bg, nil
}

func (self *BlueGreenController) GetBlueGreenMap() map[string]*BlueGreen {
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
machine:
environment:
GO_VERSION: 1.5.1
GO_VERSION: 1.5.2

dependencies:
pre:
Expand Down
13 changes: 8 additions & 5 deletions operation/bluegreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ var commandBluegreen = cli.Command{
Name: "json-output, jo",
Usage: "Output json",
},
cli.StringSliceFlag{
Name: "params, p",
Usage: "parameters",
},
},
Action: doBluegreen,
}
Expand All @@ -58,7 +62,7 @@ func doBluegreen(c *cli.Context) {
os.Exit(1)
}

operation, errSubCommand := createOperation(c.Args())
operation, errSubCommand := createOperation(c)

if errSubCommand != nil {
logger.Main.Error(color.Red(errSubCommand.Error()))
Expand All @@ -71,15 +75,14 @@ func doBluegreen(c *cli.Context) {
os.Exit(1)
}

bgController, errbgc := bluegreen.NewBlueGreenController(awsManager, projectDir, operation.TargetResource)
if errbgc != nil {
logger.Main.Error(color.Red(errbgc.Error()))
bgController, err := bluegreen.NewBlueGreenController(awsManager, projectDir, operation.TargetResource, operation.Params)
if err != nil {
logger.Main.Error(color.Red(err.Error()))
os.Exit(1)
}

jsonOutput := c.Bool("json-output")
bgPlans, err := createBlueGreenPlans(bgController, jsonOutput)

if err != nil {
logger.Main.Error(color.Red(err.Error()))
os.Exit(1)
Expand Down
9 changes: 8 additions & 1 deletion operation/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/codegangsta/cli"
"github.com/stormcat24/ecs-formation/aws"

"github.com/stormcat24/ecs-formation/util"
"log"
"os"
"strings"
Expand All @@ -13,6 +14,7 @@ import (
type Operation struct {
SubCommand string
TargetResource string
Params map[string]string
}

var Commands = []cli.Command{
Expand Down Expand Up @@ -40,7 +42,9 @@ func buildAwsManager() (*aws.AwsManager, error) {
return aws.NewAwsManager(region), nil
}

func createOperation(args cli.Args) (Operation, error) {
func createOperation(c *cli.Context) (Operation, error) {

args := c.Args()

if len(args) == 0 {
return Operation{}, fmt.Errorf("subcommand is not specified.")
Expand All @@ -54,9 +58,12 @@ func createOperation(args cli.Args) (Operation, error) {
targetResource = args[1]
}

params := c.StringSlice("params")

return Operation{
SubCommand: sub,
TargetResource: targetResource,
Params: util.ParseKeyValues(params),
}, nil
} else {
return Operation{}, fmt.Errorf("'%s' is invalid subcommand.", sub)
Expand Down
20 changes: 13 additions & 7 deletions operation/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import (
)

var commandService = cli.Command{
Name: "service",
Usage: "Manage ECS services on cluster",
Description: `
Manage services on ECS cluster.
`,
Name: "service",
Usage: "Manage ECS services on cluster",
Description: "Manage services on ECS cluster.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "json-output, jo",
Usage: "Output json",
},
cli.StringSliceFlag{
Name: "params, p",
Usage: "parameters",
},
},
Action: doService,
}
Expand All @@ -35,7 +37,7 @@ func doService(c *cli.Context) {
os.Exit(1)
}

operation, errSubCommand := createOperation(c.Args())
operation, errSubCommand := createOperation(c)

if errSubCommand != nil {
logger.Main.Error(color.Red(errSubCommand.Error()))
Expand All @@ -49,7 +51,11 @@ func doService(c *cli.Context) {
}

jsonOutput := c.Bool("json-output")
clusterController, err := service.NewServiceController(awsManager, projectDir, operation.TargetResource)
clusterController, err := service.NewServiceController(awsManager, projectDir, operation.TargetResource, operation.Params)
if err != nil {
logger.Main.Error(color.Red(err.Error()))
os.Exit(1)
}

plans, err := createClusterPlans(clusterController, projectDir, jsonOutput)

Expand Down
Loading

0 comments on commit 000e9dd

Please sign in to comment.