Skip to content
Permalink
Browse files

Merge pull request #38 from Songmu/wait

add `wait` subcommand and add `--no-wait` option to `deploy`, `create` and `rollback` subcommands
  • Loading branch information...
fujiwara committed Sep 11, 2019
2 parents 8bb1d28 + e2b33fb commit 837bf0998687fac917aeb0130a89dcbbccd1e4f7
Showing with 63 additions and 1 deletion.
  1. +11 −1 cmd/ecspresso/main.go
  2. +45 −0 ecspresso.go
  3. +7 −0 options.go
@@ -27,12 +27,14 @@ func _main() int {
DesiredCount: deploy.Flag("tasks", "desired count of tasks").Default("-1").Int64(),
SkipTaskDefinition: deploy.Flag("skip-task-definition", "skip register a new task definition").Bool(),
ForceNewDeployment: deploy.Flag("force-new-deployment", "force a new deployment of the service").Bool(),
NoWait: deploy.Flag("no-wait", "exit ecspresso immediately after just deployed without waiting for service stable").Bool(),
}

create := kingpin.Command("create", "create service")
createOption := ecspresso.CreateOption{
DryRun: create.Flag("dry-run", "dry-run").Bool(),
DesiredCount: create.Flag("tasks", "desired count of tasks").Default("1").Int64(),
NoWait: create.Flag("no-wait", "exit ecspresso immediately after just created without waiting for service stable").Bool(),
}

status := kingpin.Command("status", "show status of service")
@@ -42,8 +44,9 @@ func _main() int {

rollback := kingpin.Command("rollback", "rollback service")
rollbackOption := ecspresso.RollbackOption{
DryRun: rollback.Flag("dry-run", "dry-run").Bool(),
DryRun: rollback.Flag("dry-run", "dry-run").Bool(),
DeregisterTaskDefinition: rollback.Flag("deregister-task-definition", "deregister rolled back task definition").Bool(),
NoWait: rollback.Flag("no-wait", "exit ecspresso immediately after just rollbacked without waiting for service stable").Bool(),
}

delete := kingpin.Command("delete", "delete service")
@@ -62,6 +65,11 @@ func _main() int {
Count: run.Flag("count", "the number of tasks (max 10)").Default("1").Int64(),
}

wait := kingpin.Command("wait", "wait until service stable")
waitOption := ecspresso.WaitOption{
DesiredCount: wait.Flag("tasks", "desired count of tasks").Default("-1").Int64(),
}

sub := kingpin.Parse()
if sub == "version" {
fmt.Println("ecspresso", Version)
@@ -93,6 +101,8 @@ func _main() int {
err = app.Delete(deleteOption)
case "run":
err = app.Run(runOption)
case "wait":
err = app.Wait(waitOption)
default:
kingpin.Usage()
return 1
@@ -241,6 +241,10 @@ func (d *App) Create(opt CreateOption) error {
}
d.Log("Service is created")

if *opt.NoWait {
return nil
}

start := time.Now()
time.Sleep(delayForServiceChanged) // wait for service created
if err := d.WaitServiceStable(ctx, start); err != nil {
@@ -416,6 +420,12 @@ func (d *App) Deploy(opt DeployOption) error {
if err := d.UpdateService(ctx, tdArn, count, *opt.ForceNewDeployment, sv); err != nil {
return errors.Wrap(err, "deploy failed")
}

if *opt.NoWait {
d.Log("Service is deployed.")
return nil
}

time.Sleep(delayForServiceChanged) // wait for service updated
if err := d.WaitServiceStable(ctx, time.Now()); err != nil {
return errors.Wrap(err, "deploy failed")
@@ -448,6 +458,12 @@ func (d *App) Rollback(opt RollbackOption) error {
if err := d.UpdateService(ctx, targetArn, sv.DesiredCount, false, sv); err != nil {
return errors.Wrap(err, "rollback failed")
}

if *opt.NoWait {
d.Log("Service is rollbacked.")
return nil
}

time.Sleep(delayForServiceChanged) // wait for service updated
if err := d.WaitServiceStable(ctx, time.Now()); err != nil {
return errors.Wrap(err, "rollback failed")
@@ -472,6 +488,35 @@ func (d *App) Rollback(opt RollbackOption) error {
return nil
}

func (d *App) Wait(opt WaitOption) error {
ctx, cancel := d.Start()
defer cancel()

d.Log("Waiting for the service stable")
sv, err := d.DescribeServiceStatus(ctx, 0)
if err != nil {
return errors.Wrap(err, "wait failed")
}

var count *int64
if sv.SchedulingStrategy != nil && *sv.SchedulingStrategy == "DAEMON" {
count = nil
} else if *opt.DesiredCount == KeepDesiredCount {
count = sv.DesiredCount
} else {
count = opt.DesiredCount
}
if count != nil {
d.Log("desired count:", *count)
}
if err := d.WaitServiceStable(ctx, time.Now()); err != nil {
return errors.Wrap(err, "the service still unstable")
}

d.Log("Service is stable now. Completed!")
return nil
}

func (d *App) FindRollbackTarget(ctx context.Context, taskDefinitionArn string) (string, error) {
var found bool
var nextToken *string
@@ -3,13 +3,15 @@ package ecspresso
type CreateOption struct {
DryRun *bool
DesiredCount *int64
NoWait *bool
}

type DeployOption struct {
DryRun *bool
DesiredCount *int64
SkipTaskDefinition *bool
ForceNewDeployment *bool
NoWait *bool
}

type StatusOption struct {
@@ -19,6 +21,7 @@ type StatusOption struct {
type RollbackOption struct {
DryRun *bool
DeregisterTaskDefinition *bool
NoWait *bool
}

type DeleteOption struct {
@@ -34,3 +37,7 @@ type RunOption struct {
SkipTaskDefinition *bool
Count *int64
}

type WaitOption struct {
DesiredCount *int64
}

0 comments on commit 837bf09

Please sign in to comment.
You can’t perform that action at this time.