Skip to content

Commit

Permalink
switch to kingpin for parsing command line args.
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Nov 9, 2017
1 parent 49c065b commit f275fea
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 92 deletions.
55 changes: 54 additions & 1 deletion Gopkg.lock

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

77 changes: 43 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,28 @@ ecspresso is a deployment tool for Amazon ECS.

(pronounced same as "espresso")

# Usage
## Usage

```
$ ecspresso -h
Usage of ecspresso:
-cluster string
ECS cluster name(required)
-config string
Config file
-region string
aws region
-service string
ECS service name(required)
-task-definition string
task definition path(required)
-timeout int
timeout (sec) (default 300)
```
usage: ecspresso --config=CONFIG [<flags>] <command> [<args> ...]
ecspresso works as below.
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
--config=CONFIG config file
- Register a new task definition from JSON file.
- JSON file is same format as `aws ecs describe-task-definition` output.
- Replace `{{ env "FOO" "bar" }}` syntax in the JSON file to environment variable "FOO".
- If "FOO" is not defined, replaced by "bar"
- Replace `{{ must_env "FOO" }}` syntax in the JSON file to environment variable "FOO".
- If "FOO" is not defined, abort immediately.
- Update a service definition.
- Wait a service stable.
Commands:
help [<command>...]
Show help.
deploy [<flags>]
deploy service
status [<flags>]
show status of service
rollback [<flags>]
rollback service
```

### Configuration file

Expand All @@ -46,19 +39,35 @@ task_definition: myTask.json
timeout: 5m
```
Keys are equal to comand line options.
ecspresso works as below.
- Register a new task definition from JSON file.
- JSON file is same format as `aws ecs describe-task-definition` output.
- Replace `{{ env "FOO" "bar" }}` syntax in the JSON file to environment variable "FOO".
- If "FOO" is not defined, replaced by "bar"
- Replace `{{ must_env "FOO" }}` syntax in the JSON file to environment variable "FOO".
- If "FOO" is not defined, abort immediately.
- Update a service definition.
- Wait a service stable.

## Example

```
$ ecspresso -region ap-northeast-1 -cluster default -service myService -task-definition myTask.json
2017/11/07 09:07:12 myService/default Starting ecspresso
2017/11/07 09:07:12 myService/default Creating a new task definition by app.json
2017/11/07 09:07:12 myService/default Registering a new task definition...
2017/11/07 09:07:15 myService/default Task definition is registered myService:2
2017/11/07 09:07:15 myService/default Updating service...
2017/11/07 09:07:16 myService/default Waiting for service stable...(it will takea few minutes)
2017/11/07 09:10:02 myService/default Service is stable now. Completed!
$ ecspresso deploy --config preview.yaml
2017/11/09 23:20:13 myService/default Starting deploy
Service: myService
Cluster: default
TaskDefinition: myService:3
Deployments:
PRIMARY myService:3 desired:1 pending:0 running:1
Events:
2017/11/09 23:20:13 myService/default Creating a new task definition by task-definition/myService.json
2017/11/09 23:20:13 myService/default Registering a new task definition...
2017/11/09 23:20:13 myService/default Task definition is registered myService:4
2017/11/09 23:20:13 myService/default Updating service...
2017/11/09 23:20:13 myService/default Waiting for service stable...(it will take a few minutes)
2017/11/09 23:23:23 myService/default PRIMARY myService:4 desired:1 pending:0 running:1
2017/11/09 23:23:29 myService/default Service is stable now. Completed!
```
# LICENCE
Expand Down
62 changes: 31 additions & 31 deletions cmd/ecspresso/main.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
package main

import (
"flag"
"log"
"os"
"time"

"github.com/kayac/ecspresso"
config "github.com/kayac/go-config"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

func main() {
os.Exit(_main())
}

func _main() int {
var (
region, conf, service, cluster, path string
timeout int
)

flag.StringVar(&region, "region", os.Getenv("AWS_REGION"), "aws region")
flag.StringVar(&conf, "config", "", "Config file")
flag.StringVar(&service, "service", "", "ECS service name(required)")
flag.StringVar(&cluster, "cluster", "", "ECS cluster name(required)")
flag.StringVar(&path, "task-definition", "", "task definition path(required)")
flag.IntVar(&timeout, "timeout", 300, "timeout (sec)")
flag.Parse()

c := ecspresso.Config{
Region: region,
Service: service,
Cluster: cluster,
TaskDefinitionPath: path,
Timeout: time.Duration(timeout) * time.Second,
}
if conf != "" {
if err := config.Load(&c, conf); err != nil {
log.Println("Cloud not load config file", conf, err)
return 1
}
conf := kingpin.Flag("config", "config file").Required().String()

deploy := kingpin.Command("deploy", "deploy service")
deployDryRun := deploy.Flag("dry-run", "dry-run").Bool()

status := kingpin.Command("status", "show status of service")
statusEvents := status.Flag("events", "show events num").Default("2").Int()

rollback := kingpin.Command("rollback", "rollback service")
rollbackDryRun := rollback.Flag("dry-run", "dry-run").Bool()

sub := kingpin.Parse()

c := ecspresso.NewDefaultConfig()
if err := config.Load(c, *conf); err != nil {
log.Println("Cloud not load config file", conf, err)
kingpin.Usage()
return 1
}
if err := (&c).Validate(); err != nil {
app, err := ecspresso.NewApp(c)
if err != nil {
log.Println(err)
flag.PrintDefaults()
return 1
}

if err := ecspresso.Run(&c); err != nil {
log.Println("FAILED:", err)
switch sub {
case "deploy":
err = app.Deploy(*deployDryRun)
case "status":
err = app.Status(*statusEvents)
case "rollback":
err = app.Rollback(*rollbackDryRun)
}
if err != nil {
log.Printf("%s FAILED. %s", sub, err)
return 1
}

Expand Down
8 changes: 8 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ecspresso

import (
"errors"
"os"
"time"
)

Expand All @@ -25,3 +26,10 @@ func (c *Config) Validate() error {
}
return nil
}

func NewDefaultConfig() *Config {
return &Config{
Region: os.Getenv("AWS_REGION"),
Timeout: 300 * time.Second,
}
}
Loading

0 comments on commit f275fea

Please sign in to comment.