From 2aa093c0816f022219e9b1db55e533a31540ad92 Mon Sep 17 00:00:00 2001 From: Dmitry Kireev Date: Wed, 15 May 2024 08:03:57 +0000 Subject: [PATCH] Updates & Fixes: - Fixes race condition with multiple app terminals and pterm - Minor language and formatting changes --- internal/commands/up.go | 2 +- internal/manager/ecs/ecs.go | 6 ++-- internal/manager/ecs/native.go | 57 +++++++++++++++++----------------- internal/manager/helm/helm.go | 4 +-- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/internal/commands/up.go b/internal/commands/up.go index 0992a5ef..802e521b 100644 --- a/internal/commands/up.go +++ b/internal/commands/up.go @@ -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), diff --git a/internal/manager/ecs/ecs.go b/internal/manager/ecs/ecs.go index 9c4dd44a..b893dd0b 100644 --- a/internal/manager/ecs/ecs.go +++ b/internal/manager/ecs/ecs.go @@ -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 @@ -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 diff --git a/internal/manager/ecs/native.go b/internal/manager/ecs/native.go index bc0200b9..b30241f7 100644 --- a/internal/manager/ecs/native.go +++ b/internal/manager/ecs/native.go @@ -19,7 +19,6 @@ import ( ) func (e *Manager) deployLocal(w io.Writer) error { - pterm.SetDefaultOutput(w) svc := e.Project.AWSClient.ECSClient @@ -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 @@ -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, @@ -119,12 +118,12 @@ 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) @@ -132,18 +131,18 @@ func (e *Manager) deployLocal(w io.Writer) error { 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) } @@ -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") } @@ -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 @@ -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 @@ -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") } @@ -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 } } @@ -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, @@ -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, @@ -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{ @@ -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 } diff --git a/internal/manager/helm/helm.go b/internal/manager/helm/helm.go index 6a05bba1..ca8e2732 100644 --- a/internal/manager/helm/helm.go +++ b/internal/manager/helm/helm.go @@ -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