Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

fix aws ssh issue #446

Merged
merged 7 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions pkg/cmd/ssh_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"
)

// AwsInstanceAttribute stores all the critical information for creating an instance on AWS.
Expand Down Expand Up @@ -73,7 +72,8 @@ func sshToAWSNode(targetReader TargetReader, nodeName, path, user, pathSSKeypair

a.createNodeHostSecurityGroup()

a.sshPortCheck()
err := CheckIPPortReachable(a.BastionIP, "22")
checkError(err)

bastionNode := user + "@" + a.BastionIP
node := user + "@" + nodeName
Expand Down Expand Up @@ -150,7 +150,7 @@ func (a *AwsInstanceAttribute) createBastionHostSecurityGroup() {
}

func (a *AwsInstanceAttribute) createNodeHostSecurityGroup() {
// add shh rule to ec2 instance
// add ssh rule to ec2 instance
arguments := fmt.Sprintf("ec2 authorize-security-group-ingress --group-id %s --protocol tcp --port 22 --cidr %s/32", a.SecurityGroupID, a.BastionPrivIP)
operate("aws", arguments)
fmt.Println("Opened SSH Port on Node.")
Expand Down Expand Up @@ -181,6 +181,10 @@ func (a *AwsInstanceAttribute) createBastionHostInstance() {
a.getBastionHostInstance()
if a.BastionInstanceID != "" {
fmt.Println("Bastion Host exists, skipping creation.")
arguments := "ec2 describe-instances --instance-id " + a.BastionInstanceID + " --query Reservations[*].Instances[*].PrivateIpAddress"
a.BastionPrivIP = strings.Trim(operate("aws", arguments), "\n")
arguments = "ec2 describe-instances --instance-id " + a.BastionInstanceID + " --query Reservations[*].Instances[*].PublicIpAddress"
a.BastionIP = strings.Trim(operate("aws", arguments), "\n")
return
}

Expand Down Expand Up @@ -232,28 +236,6 @@ func (a *AwsInstanceAttribute) createBastionHostInstance() {
a.BastionPrivIP = strings.Trim(operate("aws", arguments), "\n")
}

// Bastion SSH port check
func (a *AwsInstanceAttribute) sshPortCheck() {
// waiting 60 seconds for SSH port open
fmt.Println("Waiting 60 seconds for Bastion SSH port open")
attemptCnt := 0
for attemptCnt < 6 {
ncCmd := fmt.Sprintf("timeout 10 nc -vtnz %s 22", a.BastionIP)
cmd := exec.Command("bash", "-c", ncCmd)
output, _ := cmd.CombinedOutput()
fmt.Println("=>", string(output))
if strings.Contains(string(output), "succeeded") {
fmt.Println("Opened SSH Port on Bastion")
return
}
time.Sleep(time.Second * 10)
attemptCnt++
}
fmt.Println("SSH Port Open on Bastion TimeOut")
a.cleanupAwsBastionHost()
os.Exit(0)
}

// cleanupAwsBastionHost cleans up the bastion host for the targeted cluster.
func (a *AwsInstanceAttribute) cleanupAwsBastionHost() {
fmt.Println("(4/4) Cleanup")
Expand Down
21 changes: 11 additions & 10 deletions pkg/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,18 @@ func PrintoutObject(objectToPrint interface{}, writer io.Writer, outputFormat st
return nil
}

//CheckIPPortReachable check whether IP with port is reachable with 1 min
//CheckIPPortReachable check whether IP with port is reachable within 1 min
func CheckIPPortReachable(ip string, port string) error {
timeout := time.Second * 60
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout)
if err != nil {
fmt.Println("Connecting error:", err)
}
if conn != nil {
defer conn.Close()
fmt.Printf("IP %s port %s is reachable\n", ip, port)
return nil
attemptCnt := 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would rather name it attemptCount or just attempt.

for attemptCnt < 6 {
timeout := time.Second * 10
conn, _ := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout)
if conn != nil {
defer conn.Close()
fmt.Printf("IP %s port %s is reachable\n", ip, port)
return nil
}
attemptCnt++
}
return fmt.Errorf("IP %s port %s is not reachable", ip, port)
}