@@ -2,14 +2,33 @@ package lib
22
33import (
44 "fmt"
5+ "github.com/apex/log"
56 "github.com/aws/aws-sdk-go/aws"
6- // "github.com/aws/aws-sdk-go/aws/session"
7+ "github.com/aws/aws-sdk-go/aws/session"
78 "github.com/aws/aws-sdk-go/service/ecs"
89 "github.com/aws/aws-sdk-go/service/ecs/ecsiface"
910)
1011
11- // FindLatestTaskArn finds the latest task ARN for the specified service within a cluster
12- func FindLatestTaskArn (svc ecsiface.ECSAPI , clusterName , serviceName string ) (string , error ) {
12+ // InitializeSession creates an AWS session for ECS interaction
13+ func InitializeSession (region , profile string ) (* session.Session , error ) {
14+ sess , err := session .NewSessionWithOptions (session.Options {
15+ Profile : profile ,
16+ Config : aws.Config {
17+ },
18+ })
19+ if err != nil {
20+ log .WithError (err ).Error ("Failed to create AWS session" )
21+ return nil , err
22+ }
23+ return sess , nil
24+ }
25+
26+ // FindLatestTaskArn locates the most recent task ARN for a specified ECS service
27+ func FindLatestTaskArn (clusterName , serviceName string ) (string , error ) {
28+ if serviceName == "" {
29+ return "" , fmt .Errorf ("service name cannot be empty" )
30+ }
31+
1332 input := & ecs.ListTasksInput {
1433 Cluster : aws .String (clusterName ),
1534 ServiceName : aws .String (serviceName ),
@@ -18,20 +37,35 @@ func FindLatestTaskArn(svc ecsiface.ECSAPI, clusterName, serviceName string) (st
1837 }
1938
2039 result , err := svc .ListTasks (input )
21- if err != nil || len (result .TaskArns ) == 0 {
40+ if err != nil {
41+ log .WithError (err ).Error ("Error listing tasks" )
42+ return "" , fmt .Errorf ("error listing tasks for service %s on cluster %s: %v" , serviceName , clusterName , err )
43+ }
44+ if len (result .TaskArns ) == 0 {
45+ log .WithFields (log.Fields {
46+ "cluster" : clusterName ,
47+ "service" : serviceName ,
48+ }).Error ("No running tasks found" )
2249 return "" , fmt .Errorf ("no running tasks found for service %s on cluster %s" , serviceName , clusterName )
2350 }
2451
52+ log .WithFields (log.Fields {
53+ "taskArn" : aws .StringValue (result .TaskArns [0 ]),
54+ }).Info ("Found latest task ARN" )
2555 return aws .StringValue (result .TaskArns [0 ]), nil
2656}
2757
28- // ExecuteCommandInContainer executes a specified command in a running container on an ECS Fargate cluster.
29- func ExecuteCommandInContainer (svc ecsiface.ECSAPI , cluster , serviceName , containerName , command string ) error {
30- taskArn , err := FindLatestTaskArn (svc , cluster , serviceName )
58+ // ExecuteCommandInContainer runs a command in a specified container on an ECS Fargate service
59+ func ExecFargate (rofile , cluster , service , container , workDir , containerName , awslogGroup , launchType string , command string ) (exitCode int , err error ) {
60+ if service == "" {
61+ return fmt .Errorf ("service name cannot be empty" )
62+ }
63+
64+ taskArn , err := FindLatestTaskArn (cluster , serviceName )
3165 if err != nil {
3266 return err
3367 }
34-
68+ fmt . Println ( taskArn , "we are here" )
3569 input := & ecs.ExecuteCommandInput {
3670 Cluster : aws .String (cluster ),
3771 Task : aws .String (taskArn ),
@@ -42,8 +76,18 @@ func ExecuteCommandInContainer(svc ecsiface.ECSAPI, cluster, serviceName, contai
4276
4377 _ , err = svc .ExecuteCommand (input )
4478 if err != nil {
79+ log .WithError (err ).WithFields (log.Fields {
80+ "taskArn" : taskArn ,
81+ "containerName" : containerName ,
82+ "command" : command ,
83+ }).Error ("Failed to execute command" )
4584 return fmt .Errorf ("failed to execute command: %v" , err )
4685 }
4786
87+ log .WithFields (log.Fields {
88+ "taskArn" : taskArn ,
89+ "containerName" : containerName ,
90+ "command" : command ,
91+ }).Info ("Command executed successfully" )
4892 return nil
4993}
0 commit comments