Skip to content

Commit

Permalink
Updates & Fixes:
Browse files Browse the repository at this point in the history
- Fixes race condition with multiple app terminals and pterm
- Minor language and formatting changes
  • Loading branch information
AutomationD committed May 15, 2024
1 parent 4c7faa2 commit 2aa093c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
2 changes: 1 addition & 1 deletion internal/commands/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewCmdUp(project *config.Project) *cobra.Command {
cmd.Flags().BoolVar(&o.UseYarn, "use-yarn", false, "execute sls commands using yarn")
cmd.Flags().BoolVar(&o.SkipGen, "skip-gen", false, "skip generating terraform files")
cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown")

cmd.AddCommand(
NewCmdUpInfra(project),
NewCmdUpApps(project),
Expand Down
6 changes: 3 additions & 3 deletions internal/manager/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (e *Manager) Deploy(ui terminal.UI) error {
}

if e.App.SkipDeploy {
s := sg.Add("%s: deploy will be skipped", e.App.Name)
s := sg.Add("%s: deploy is skipped", e.App.Name)
defer func() { s.Abort(); time.Sleep(50 * time.Millisecond) }()
s.Done()
return nil
Expand Down Expand Up @@ -186,11 +186,11 @@ func (e *Manager) Push(ui terminal.UI) error {
sg := ui.StepGroup()
defer sg.Wait()

s := sg.Add("%s: pushing app image...", e.App.Name)
s := sg.Add("%s: pushing docker image...", e.App.Name)
defer func() { s.Abort(); time.Sleep(50 * time.Millisecond) }()

if len(e.App.Image) != 0 {
s.Update("%s: pushing app image... (skipped, using %s) ", e.App.Name, e.App.Image)
s.Update("%s: pushing docker image... (skipped, using %s) ", e.App.Name, e.App.Image)
s.Done()

return nil
Expand Down
57 changes: 28 additions & 29 deletions internal/manager/ecs/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
)

func (e *Manager) deployLocal(w io.Writer) error {
pterm.SetDefaultOutput(w)

svc := e.Project.AWSClient.ECSClient

Expand Down Expand Up @@ -72,7 +71,7 @@ func (e *Manager) deployLocal(w io.Writer) error {
}

logrus.Debugf("oldTaskDef: %s", string(oldTaskDefJson))
pterm.Printfln("Deploying based on task definition: %s:%d", *oldTaskDef.Family, *oldTaskDef.Revision)
pterm.Fprintln(w, fmt.Sprintf("Deploying based on task definition: %s:%d", *oldTaskDef.Family, *oldTaskDef.Revision))

var image string

Expand All @@ -88,12 +87,12 @@ func (e *Manager) deployLocal(w io.Writer) error {
image = e.App.Image
}

pterm.Printfln(`Changed image of container "%s" to : "%s" (was: "%s")`, *container.Name, image, *container.Image)
pterm.Fprintln(w, fmt.Sprintf(`Changed image of container "%s" to : "%s" (was: "%s")`, *container.Name, image, *container.Image))
container.Image = &image
}
}

pterm.Println("Creating new task definition revision")
pterm.Fprintln(w, "Creating new task definition revision")

rtdo, err := svc.RegisterTaskDefinition(&ecs.RegisterTaskDefinitionInput{
ContainerDefinitions: oldTaskDef.ContainerDefinitions,
Expand All @@ -119,31 +118,31 @@ func (e *Manager) deployLocal(w io.Writer) error {
}

logrus.Debugf("newTaskDef: %s", string(newTaskDefJson))
pterm.Printfln("Successfully created revision: %s:%d", *rtdo.TaskDefinition.Family, *rtdo.TaskDefinition.Revision)
pterm.Fprintln(w, fmt.Sprintf("Successfully created revision: %s:%d", *rtdo.TaskDefinition.Family, *rtdo.TaskDefinition.Revision))

if err = e.updateTaskDefinition(&newTaskDef, &oldTaskDef, e.App.ServiceName, "Deploying new task definition"); err != nil {
err := e.getLastContainerLogs(fmt.Sprintf("%s", e.App.ServiceName))
if err = e.updateTaskDefinition(w, &newTaskDef, &oldTaskDef, e.App.ServiceName, "Deploying new task definition"); err != nil {
err := e.getLastContainerLogs(w, fmt.Sprintf("%s", e.App.ServiceName))
if err != nil {
pterm.Println("Failed to get logs:", err)
pterm.Fprintln(w, "Failed to get logs:", err)
}

sr, err := getStoppedReason(e.App.Cluster, e.App.ServiceName, svc)
if err != nil {
return err
}

pterm.Printfln("Container %s couldn't start: %s", e.App.ServiceName, sr)
pterm.Fprintln(w, fmt.Sprintf("Container %s couldn't start: %s", e.App.ServiceName, sr))

pterm.Printfln("Rolling back to old task definition: %s:%d", *oldTaskDef.Family, *oldTaskDef.Revision)
pterm.Fprintln(w, fmt.Sprintf("Rolling back to old task definition: %s:%d", *oldTaskDef.Family, *oldTaskDef.Revision))

e.App.Timeout = 600
logrus.Debugf("Setting timeout to %d seconds", e.App.Timeout)

if err = e.updateTaskDefinition(&oldTaskDef, &newTaskDef, e.App.ServiceName, "Deploying previous task definition"); err != nil {
if err = e.updateTaskDefinition(w, &oldTaskDef, &newTaskDef, e.App.ServiceName, "Deploying previous task definition"); err != nil {
return fmt.Errorf("unable to rollback to old task definition: %w", err)
}

pterm.Println("Rollback successful")
pterm.Fprintln(w, "Rollback successful")

return fmt.Errorf("deployment failed, but service has been rolled back to previous task definition: %s", *oldTaskDef.Family)
}
Expand Down Expand Up @@ -210,11 +209,11 @@ func (e *Manager) redeployLocal(w io.Writer) error {
}
}

if err = e.updateTaskDefinition(td, nil, name, "Redeploying new task definition"); err != nil {
pterm.Println(err)
err := e.getLastContainerLogs(fmt.Sprintf("%s", e.App.ServiceName))
if err = e.updateTaskDefinition(w, td, nil, name, "Redeploying new task definition"); err != nil {
pterm.Fprintln(w, err)
err := e.getLastContainerLogs(w, fmt.Sprintf("%s", e.App.ServiceName))
if err != nil {
pterm.Println("Failed to get logs:", err)
pterm.Fprintln(w, "Failed to get logs:", err)
}
return fmt.Errorf("redeployment failed")
}
Expand All @@ -237,8 +236,8 @@ func getService(name string, cluster string, svc ecsiface.ECSAPI) (*ecs.Describe
return dso, nil
}

func (e *Manager) updateTaskDefinition(newTD *ecs.TaskDefinition, oldTD *ecs.TaskDefinition, serviceName string, title string) error {
pterm.Printfln("Updating ECS service: %s (timeout: %d)", e.App.ServiceName, e.App.Timeout)
func (e *Manager) updateTaskDefinition(w io.Writer, newTD *ecs.TaskDefinition, oldTD *ecs.TaskDefinition, serviceName string, title string) error {
pterm.Fprintln(w, fmt.Sprintf("Updating ECS service: %s (timeout: %d)", e.App.ServiceName, e.App.Timeout))

svc := e.Project.AWSClient.ECSClient

Expand Down Expand Up @@ -274,8 +273,8 @@ func (e *Manager) updateTaskDefinition(newTD *ecs.TaskDefinition, oldTD *ecs.Tas
}
}

pterm.Printfln("Successfully changed task definition to: %s:%d", *newTD.Family, *newTD.Revision)
pterm.Println(title)
pterm.Fprintln(w, fmt.Sprintf("Successfully changed task definition to: %s:%d", *newTD.Family, *newTD.Revision))
pterm.Fprintln(w, title)

waitingTimeout := time.Now().Add(time.Duration(e.App.Timeout) * time.Second)
waiting := true
Expand All @@ -294,7 +293,7 @@ func (e *Manager) updateTaskDefinition(newTD *ecs.TaskDefinition, oldTD *ecs.Tas
}

if waiting && time.Now().After(waitingTimeout) {
pterm.Println("Deployment failed due to timeout")
pterm.Fprintln(w, "Deployment failed due to timeout")
return fmt.Errorf("deployment failed due to timeout")
}

Expand All @@ -312,7 +311,7 @@ func (e *Manager) updateTaskDefinition(newTD *ecs.TaskDefinition, oldTD *ecs.Tas
}

if oldTD != nil {
if err = deregisterTaskDefinition(svc, oldTD); err != nil {
if err = deregisterTaskDefinition(w, svc, oldTD); err != nil {
return err
}
}
Expand Down Expand Up @@ -404,8 +403,8 @@ func getStoppedReason(cluster string, name string, svc ecsiface.ECSAPI) (string,
return *dto.Tasks[0].StoppedReason, nil
}

func deregisterTaskDefinition(svc ecsiface.ECSAPI, td *ecs.TaskDefinition) error {
pterm.Println("Deregister task definition revision")
func deregisterTaskDefinition(w io.Writer, svc ecsiface.ECSAPI, td *ecs.TaskDefinition) error {
pterm.Fprintln(w, "Deregister task definition revision")

_, err := svc.DeregisterTaskDefinition(&ecs.DeregisterTaskDefinitionInput{
TaskDefinition: td.TaskDefinitionArn,
Expand All @@ -414,12 +413,12 @@ func deregisterTaskDefinition(svc ecsiface.ECSAPI, td *ecs.TaskDefinition) error
return err
}

pterm.Printfln("Successfully deregistered revision: %s:%d", *td.Family, *td.Revision)
pterm.Fprintln(w, fmt.Sprintf("Successfully deregistered revision: %s:%d", *td.Family, *td.Revision))

return nil
}

func (e *Manager) getLastContainerLogs(logGroup string) error {
func (e *Manager) getLastContainerLogs(w io.Writer, logGroup string) error {
cwl := e.Project.AWSClient.CloudWatchLogsClient
out, err := cwl.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: &logGroup,
Expand All @@ -435,7 +434,7 @@ func (e *Manager) getLastContainerLogs(logGroup string) error {
return nil
}

pterm.Println("Container logs:")
pterm.Fprintln(w, "Container logs:")

for _, stream := range out.LogStreams {
out, err := cwl.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
Expand All @@ -447,11 +446,11 @@ func (e *Manager) getLastContainerLogs(logGroup string) error {
}

for _, event := range out.Events {
pterm.Println("| " + *event.Message)
pterm.Fprintln(w, "| "+*event.Message)
}
}

pterm.Println()
pterm.Fprintln(w)

return nil
}
4 changes: 2 additions & 2 deletions internal/manager/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ func (e *Manager) Push(ui terminal.UI) error {
sg := ui.StepGroup()
defer sg.Wait()

s := sg.Add("%s: push app image...", e.App.Name)
s := sg.Add("%s: push docker image...", e.App.Name)
defer func() { s.Abort(); time.Sleep(50 * time.Millisecond) }()

if len(e.App.Image) != 0 {
s.Update("%s: pushing app image... (skipped, using %s) ", e.App.Name, e.App.Image)
s.Update("%s: pushing docker image... (skipped, using %s) ", e.App.Name, e.App.Image)
s.Done()

return nil
Expand Down

0 comments on commit 2aa093c

Please sign in to comment.