-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (94 loc) · 3.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"github.com/AlecAivazis/survey/v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"log"
"os"
"os/exec"
)
type Pivot struct {
task ecs.Task
container ecs.Container
}
func main() {
var i string
var region string
var cluster string
// Flags
flag.StringVar(&i, "i", "", ".PEM file location.")
flag.StringVar(®ion, "region", "", "Example: eu-west-2")
flag.StringVar(&cluster, "cluster", "", "The name of your cluster.")
flag.Parse()
sess, err := session.NewSession()
if err != nil {
log.Fatal("Error creating session ", err)
}
if region == "" {
survey.AskOne(&survey.Input{Message: "Region",}, ®ion)
}
sess.Config.Region = aws.String(region)
ecsSvc := ecs.New(sess)
if cluster == "" {
clusters, err := ecsSvc.ListClusters(&ecs.ListClustersInput{})
if err == nil {
var clusterArnStrings []string
for _, arn := range clusters.ClusterArns {
clusterArnStrings = append(clusterArnStrings, *arn)
}
survey.AskOne(&survey.Select{Message: "Select a task", Options: clusterArnStrings}, &cluster)
} else {
survey.AskOne(&survey.Input{Message: "Cluster",}, &cluster)
}
}
list, _ := ecsSvc.ListTasks(&ecs.ListTasksInput{
Cluster: &cluster,
MaxResults: aws.Int64(100),
})
tasks, err := ecsSvc.DescribeTasks(&ecs.DescribeTasksInput{
Cluster: &cluster,
Tasks: list.TaskArns,
})
if err != nil {
log.Fatal(err.Error())
}
var tasksStringArr []string
containersMap := make(map[string]Pivot)
for _, task := range tasks.Tasks {
for _, container := range task.Containers {
tasksStringArr = append(tasksStringArr, *task.Group + ": "+ *container.Name + " (" + *container.ContainerArn + ")")
containersMap[*task.Group + ": "+ *container.Name + " (" + *container.ContainerArn + ")"] = Pivot{task: *task, container: *container};
}
}
var taskArn string
survey.AskOne(&survey.Select{Message: "Select a task", Options: tasksStringArr}, &taskArn)
selected := containersMap[taskArn]
ecsInstance, err := ecsSvc.DescribeContainerInstances(&ecs.DescribeContainerInstancesInput{Cluster: &cluster, ContainerInstances: []*string{selected.task.ContainerInstanceArn}})
if err != nil {
log.Fatal(err.Error())
}
ec2Svc := ec2.New(sess)
ec2Instance, _ := ec2Svc.DescribeInstances(&ec2.DescribeInstancesInput{InstanceIds: []*string{ecsInstance.ContainerInstances[0].Ec2InstanceId}})
//ssmSvc := ssm.New(sess)
//startSession, err := ssmSvc.StartSession(&ssm.StartSessionInput{Target: ec2Instance.Reservations[0].Instances[0].InstanceId})
//fmt.Println(startSession)
command := exec.Command(
"ssh", "-t", "-i", i,
"ec2-user@" + *ec2Instance.Reservations[0].Instances[0].PublicDnsName,
"docker", "exec", "-it",
*selected.container.RuntimeId,
"/bin/sh",
)
command.Stdout = os.Stdout
command.Stdin = os.Stdin
command.Stderr = os.Stderr
err = command.Start()
if err == nil {
command.Wait()
} else {
log.Fatal(err.Error())
}
}