@@ -12,6 +12,7 @@ import (
1212 "github.com/TylerBrock/colorjson"
1313 "github.com/aws/aws-sdk-go/aws"
1414 "github.com/aws/aws-sdk-go/service/cloudwatchlogs"
15+ "github.com/aws/aws-sdk-go/service/ec2"
1516 "github.com/aws/aws-sdk-go/service/ecs"
1617 "github.com/fatih/color"
1718 "github.com/spf13/cobra"
@@ -180,6 +181,53 @@ func taskDefinitionsRunRun(cmd *cobra.Command, args []string) {
180181 t .Exitln ("task failed to run" )
181182 }
182183
184+ tSplited := strings .Split (aws .StringValue (taskResult .Tasks [0 ].TaskArn ), "/" )
185+ taskID := tSplited [2 ]
186+
187+ if viper .GetBool ("output-ip" ) {
188+ var tasksStatus * ecs.DescribeTasksOutput
189+ var err error
190+
191+ for {
192+ tasksStatus , err = ecsI .DescribeTasks (& ecs.DescribeTasksInput {
193+ Cluster : aws .String (cluster ),
194+ Tasks : []* string {aws .String (taskID )},
195+ })
196+ t .Must (err )
197+
198+ status := aws .StringValue (tasksStatus .Tasks [0 ].LastStatus )
199+ if status != "PENDING" {
200+ break
201+ }
202+
203+ time .Sleep (1 * time .Second )
204+ }
205+
206+ ec2ECSInstance , err := ecsI .DescribeContainerInstances (& ecs.DescribeContainerInstancesInput {
207+ Cluster : aws .String (cluster ),
208+ ContainerInstances : []* string {tasksStatus .Tasks [0 ].ContainerInstanceArn },
209+ })
210+ t .Must (err )
211+
212+ if len (ec2ECSInstance .ContainerInstances ) == 0 {
213+ t .Exitln ("failed to find the EC2 Instance running this task" )
214+ }
215+
216+ ec2Info , err := ec2I .DescribeInstances (& ec2.DescribeInstancesInput {
217+ InstanceIds : []* string {ec2ECSInstance .ContainerInstances [0 ].Ec2InstanceId },
218+ })
219+
220+ if len (ec2Info .Reservations ) == 0 || len (ec2Info .Reservations [0 ].Instances ) == 0 {
221+ t .Exitln ("failed to describe the EC2 Instance running this task" )
222+ }
223+
224+ hostIp := ec2Info .Reservations [0 ].Instances [0 ].PrivateIpAddress
225+
226+ hostPort := tasksStatus .Tasks [0 ].Containers [0 ].NetworkBindings [0 ].HostPort
227+
228+ t .Outf ("%s:%d\n " , aws .StringValue (hostIp ), aws .Int64Value (hostPort ))
229+ }
230+
183231 if ! follow {
184232 t .Exit (nil )
185233 }
@@ -200,9 +248,6 @@ func taskDefinitionsRunRun(cmd *cobra.Command, args []string) {
200248 }()
201249 }
202250
203- tSplited := strings .Split (aws .StringValue (taskResult .Tasks [0 ].TaskArn ), "/" )
204- taskID := tSplited [2 ]
205-
206251 logDriver := td .ContainerDefinitions [0 ].LogConfiguration .LogDriver
207252 if aws .StringValue (logDriver ) != "awslogs" {
208253 t .Exit (nil )
@@ -315,6 +360,9 @@ func init() {
315360
316361 flags .StringVar (& groupTasks , "group" , "" , groupTasksSpec )
317362
363+ flags .Bool ("output-ip" , false , "Return the Private IP and Port of the Container on running Task for EC2 Instances" )
364+ viper .BindPFlag ("output-ip" , taskDefinitionsRunCmd .Flags ().Lookup ("output-ip" ))
365+
318366 flags .StringP ("cluster" , "c" , "default" , clusterSpec )
319367 viper .BindPFlag ("cluster" , taskDefinitionsRunCmd .Flags ().Lookup ("cluster" ))
320368}
0 commit comments