Skip to content

Commit

Permalink
Add ability to restart existing task
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed May 5, 2023
1 parent f2ed4b2 commit 91bf67b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
49 changes: 49 additions & 0 deletions awscmd/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,55 @@ type OutputEcsRunTask struct {
ID string
}

func EcsRestartTask(ctx context.Context, input *InputEcsRestartTask, w io.Writer) (*OutputEcsRestartTask, error) {
sess, err := NewSession(input.Region)
if err != nil {
return nil, err
}
svc := ecs.New(sess)

// 1. update ecs service with new task arn
fmt.Fprintf(w, "Updating service\n")
updateOut, err := svc.UpdateServiceWithContext(ctx, &ecs.UpdateServiceInput{
Cluster: aws.String(input.Cluster),
Service: aws.String(input.Service),
ForceNewDeployment: aws.Bool(true),
})
if err != nil {
return nil, fmt.Errorf("Failed to restart service: %w", err)
}
var monitoredDeployment *ecs.Deployment = nil
for _, deployment := range updateOut.Service.Deployments {
if *deployment.Status == "PRIMARY" {
monitoredDeployment = deployment
break
}
}

if monitoredDeployment == nil {
return &OutputEcsRestartTask{
Service: *updateOut.Service.ServiceName,
}, fmt.Errorf("Service %s deployed but couldn't fetch primary deployment status", *updateOut.Service.ServiceName)
}

return &OutputEcsRestartTask{
Service: *updateOut.Service.ServiceName,
MonitoredDeploymentID: *monitoredDeployment.Id,
}, nil
}

type InputEcsRestartTask struct {
Region string
Cluster string
Service string
OneOffCommand string
}

type OutputEcsRestartTask struct {
Service string
MonitoredDeploymentID string
}

func EcsTaskWait(ctx context.Context, input *InputEcsTaskWait, w io.Writer) (*OuputEcsTaskWait, error) {
sess, err := NewSession(input.Region)
if err != nil {
Expand Down
48 changes: 48 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,54 @@ func main() {
ctc.Reset,
)

return nil
},
},
{
Name: "restart",
Usage: "Restarts service",
Flags: []cli.Flag{
&cli.StringFlag{Name: "region", Usage: "AWS region", Required: true},
&cli.StringFlag{Name: "cluster", Usage: "ECS cluster ID", Required: true},
&cli.StringFlag{Name: "service", Usage: "ECS service ID", Required: true},
&cli.StringFlag{Name: "timeout", Usage: `Timeout (time units are "ns", "us" (or "µs"), "ms", "s", "m", "h")`, Required: false, Value: "10m"},
},
Action: func(c *cli.Context) error {
timeout, err := time.ParseDuration(c.String("timeout"))
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(c.Context, timeout)
defer cancel()

restartTaskInput := &awscmd.InputEcsRestartTask{
Region: c.String("region"),
Cluster: c.String("cluster"),
Service: c.String("service"),
}
out, err := awscmd.EcsRestartTask(ctx, restartTaskInput, c.App.Writer)
if err != nil {
return err
}
waitInput := &awscmd.InputEcsDeployWait{
Region: c.String("region"),
Cluster: c.String("cluster"),
Service: c.String("service"),
DeploymentID: out.MonitoredDeploymentID,
}
_, err = awscmd.EcsDeployWait(ctx, waitInput, c.App.Writer)
if err != nil {
return err
}

fmt.Fprintf(
c.App.Writer,
"%sDeployment for service `%s` reached stable state%s\n",
ctc.ForegroundGreen,
out.Service,
ctc.Reset,
)

return nil
},
},
Expand Down

0 comments on commit 91bf67b

Please sign in to comment.