Skip to content

Commit

Permalink
feat: get the IPv4 Private IP on tasks run
Browse files Browse the repository at this point in the history
  • Loading branch information
gumieri committed Jun 7, 2024
1 parent be996fe commit e396c88
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions cmd/task_definitions_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/TylerBrock/colorjson"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/fatih/color"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -180,6 +181,53 @@ func taskDefinitionsRunRun(cmd *cobra.Command, args []string) {
t.Exitln("task failed to run")
}

tSplited := strings.Split(aws.StringValue(taskResult.Tasks[0].TaskArn), "/")
taskID := tSplited[2]

if viper.GetBool("output-ip") {
var tasksStatus *ecs.DescribeTasksOutput
var err error

for {
tasksStatus, err = ecsI.DescribeTasks(&ecs.DescribeTasksInput{
Cluster: aws.String(cluster),
Tasks: []*string{aws.String(taskID)},
})
t.Must(err)

status := aws.StringValue(tasksStatus.Tasks[0].LastStatus)
if status != "PENDING" {
break
}

time.Sleep(1 * time.Second)
}

ec2ECSInstance, err := ecsI.DescribeContainerInstances(&ecs.DescribeContainerInstancesInput{
Cluster: aws.String(cluster),
ContainerInstances: []*string{tasksStatus.Tasks[0].ContainerInstanceArn},
})
t.Must(err)

if len(ec2ECSInstance.ContainerInstances) == 0 {
t.Exitln("failed to find the EC2 Instance running this task")
}

ec2Info, err := ec2I.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{ec2ECSInstance.ContainerInstances[0].Ec2InstanceId},
})

if len(ec2Info.Reservations) == 0 || len(ec2Info.Reservations[0].Instances) == 0 {
t.Exitln("failed to describe the EC2 Instance running this task")
}

hostIp := ec2Info.Reservations[0].Instances[0].PrivateIpAddress

hostPort := tasksStatus.Tasks[0].Containers[0].NetworkBindings[0].HostPort

t.Outf("%s:%d\n", aws.StringValue(hostIp), aws.Int64Value(hostPort))
}

if !follow {
t.Exit(nil)
}
Expand All @@ -200,9 +248,6 @@ func taskDefinitionsRunRun(cmd *cobra.Command, args []string) {
}()
}

tSplited := strings.Split(aws.StringValue(taskResult.Tasks[0].TaskArn), "/")
taskID := tSplited[2]

logDriver := td.ContainerDefinitions[0].LogConfiguration.LogDriver
if aws.StringValue(logDriver) != "awslogs" {
t.Exit(nil)
Expand Down Expand Up @@ -315,6 +360,9 @@ func init() {

flags.StringVar(&groupTasks, "group", "", groupTasksSpec)

flags.Bool("output-ip", false, "Return the Private IP and Port of the Container on running Task for EC2 Instances")
viper.BindPFlag("output-ip", taskDefinitionsRunCmd.Flags().Lookup("output-ip"))

flags.StringP("cluster", "c", "default", clusterSpec)
viper.BindPFlag("cluster", taskDefinitionsRunCmd.Flags().Lookup("cluster"))
}

0 comments on commit e396c88

Please sign in to comment.