Skip to content

Commit

Permalink
Merge pull request #5 from kayac/show-events-on-deploy
Browse files Browse the repository at this point in the history
show events on deploy.
  • Loading branch information
fujiwara authored Dec 26, 2017
2 parents 2836341 + 53a73f8 commit 037b1be
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
31 changes: 22 additions & 9 deletions ecspresso.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

var isTerminal = isatty.IsTerminal(os.Stdout.Fd())
var TerminalWidth = 90

const KeepDesiredCount = -1

Expand Down Expand Up @@ -60,12 +61,14 @@ func (d *App) DescribeServiceStatus(ctx context.Context, events int) (*ecs.Servi
if i >= events {
break
}
fmt.Println(" ", formatEvent(event))
for _, line := range formatEvent(event, TerminalWidth) {
fmt.Println(line)
}
}
return s, nil
}

func (d *App) DescribeServiceDeployments(ctx context.Context) (int, error) {
func (d *App) DescribeServiceDeployments(ctx context.Context, startedAt time.Time) (int, error) {
out, err := d.ecs.DescribeServicesWithContext(ctx, d.DescribeServicesInput())
if err != nil {
return 0, err
Expand All @@ -74,10 +77,20 @@ func (d *App) DescribeServiceDeployments(ctx context.Context) (int, error) {
return 0, nil
}
s := out.Services[0]
lines := 0
for _, dep := range s.Deployments {
lines++
d.Log(formatDeployment(dep))
}
return len(s.Deployments), nil
for _, event := range s.Events {
if (*event.CreatedAt).After(startedAt) {
for _, line := range formatEvent(event, TerminalWidth) {
fmt.Println(line)
lines++
}
}
}
return lines, nil
}

func NewApp(conf *Config) (*App, error) {
Expand Down Expand Up @@ -146,8 +159,9 @@ func (d *App) Create(opt CreateOption) error {
}
d.Log("Service is created")

start := time.Now()
time.Sleep(3 * time.Second) // wait for service created
if err := d.WaitServiceStable(ctx); err != nil {
if err := d.WaitServiceStable(ctx, start); err != nil {
return errors.Wrap(err, "create failed")
}

Expand Down Expand Up @@ -199,7 +213,7 @@ func (d *App) Deploy(opt DeployOption) error {
if err := d.UpdateService(ctx, tdArn, count); err != nil {
return errors.Wrap(err, "deploy failed")
}
if err := d.WaitServiceStable(ctx); err != nil {
if err := d.WaitServiceStable(ctx, time.Now()); err != nil {
return errors.Wrap(err, "deploy failed")
}

Expand Down Expand Up @@ -229,7 +243,7 @@ func (d *App) Rollback(opt RollbackOption) error {
if err := d.UpdateService(ctx, targetArn, service.DesiredCount); err != nil {
return errors.Wrap(err, "rollback failed")
}
if err := d.WaitServiceStable(ctx); err != nil {
if err := d.WaitServiceStable(ctx, time.Now()); err != nil {
return errors.Wrap(err, "rollback failed")
}

Expand Down Expand Up @@ -278,9 +292,8 @@ func (d *App) Log(v ...interface{}) {
log.Println(args...)
}

func (d *App) WaitServiceStable(ctx context.Context) error {
func (d *App) WaitServiceStable(ctx context.Context, startedAt time.Time) error {
d.Log("Waiting for service stable...(it will take a few minutes)")

waitCtx, cancel := context.WithCancel(ctx)
defer cancel()

Expand All @@ -297,7 +310,7 @@ func (d *App) WaitServiceStable(ctx context.Context) error {
fmt.Print(aec.EraseLine(aec.EraseModes.All), aec.PreviousLine(1))
}
}
lines, _ = d.DescribeServiceDeployments(waitCtx)
lines, _ = d.DescribeServiceDeployments(waitCtx, startedAt)
}
}
}()
Expand Down
16 changes: 13 additions & 3 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@ func formatDeployment(d *ecs.Deployment) string {
)
}

func formatEvent(e *ecs.ServiceEvent) string {
return fmt.Sprintf("%s: %s",
e.CreatedAt.In(timezone).Format(time.RFC3339),
func formatEvent(e *ecs.ServiceEvent, chars int) []string {
line := fmt.Sprintf("%s %s",
e.CreatedAt.In(timezone).Format("2006/01/02 15:04:05"),
*e.Message,
)
lines := []string{}
n := len(line)/chars + 1
for i := 0; i < n; i++ {
if i == n-1 {
lines = append(lines, line[i*chars:])
} else {
lines = append(lines, line[i*chars:(i+1)*chars])
}
}
return lines
}

0 comments on commit 037b1be

Please sign in to comment.