From 2eb595c5ce563ea79e4ab37974237b75c0f2c399 Mon Sep 17 00:00:00 2001 From: Neo Liang Date: Mon, 16 Nov 2020 13:28:56 +0800 Subject: [PATCH 1/7] fix aws ssh issue --- pkg/cmd/ssh_aws.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/ssh_aws.go b/pkg/cmd/ssh_aws.go index df51d33bf..07130fe37 100644 --- a/pkg/cmd/ssh_aws.go +++ b/pkg/cmd/ssh_aws.go @@ -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.") @@ -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 } @@ -242,7 +246,7 @@ func (a *AwsInstanceAttribute) sshPortCheck() { cmd := exec.Command("bash", "-c", ncCmd) output, _ := cmd.CombinedOutput() fmt.Println("=>", string(output)) - if strings.Contains(string(output), "succeeded") { + if strings.Contains(string(output), "open") { fmt.Println("Opened SSH Port on Bastion") return } From 2e20b229422650951ada0cb9ff69027f6a0cb95b Mon Sep 17 00:00:00 2001 From: Neo Liang Date: Fri, 20 Nov 2020 09:27:22 +0800 Subject: [PATCH 2/7] add retry logic in CheckIPPortReachable function, use it in ssh_aws.go --- pkg/cmd/ssh_aws.go | 26 ++------------------------ pkg/cmd/utils.go | 25 +++++++++++++++---------- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/pkg/cmd/ssh_aws.go b/pkg/cmd/ssh_aws.go index 07130fe37..9210c9265 100644 --- a/pkg/cmd/ssh_aws.go +++ b/pkg/cmd/ssh_aws.go @@ -22,7 +22,6 @@ import ( "os/exec" "path/filepath" "strings" - "time" ) // AwsInstanceAttribute stores all the critical information for creating an instance on AWS. @@ -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 @@ -236,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), "open") { - 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") diff --git a/pkg/cmd/utils.go b/pkg/cmd/utils.go index db0407ca8..6db3e5a82 100644 --- a/pkg/cmd/utils.go +++ b/pkg/cmd/utils.go @@ -349,17 +349,22 @@ 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 with 2 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 + for attemptCnt < 6 { + timeout := time.Second * 10 + 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 + } + time.Sleep(time.Second * 10) + attemptCnt++ } return fmt.Errorf("IP %s port %s is not reachable", ip, port) } From 38891b81b415fcb8468db9b8ae9faad44d6e601b Mon Sep 17 00:00:00 2001 From: Neo Liang Date: Fri, 20 Nov 2020 19:38:20 +0800 Subject: [PATCH 3/7] remove extra time.Sleep, fix typo in comment --- pkg/cmd/utils.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/cmd/utils.go b/pkg/cmd/utils.go index 6db3e5a82..1b9c8574e 100644 --- a/pkg/cmd/utils.go +++ b/pkg/cmd/utils.go @@ -349,7 +349,7 @@ func PrintoutObject(objectToPrint interface{}, writer io.Writer, outputFormat st return nil } -//CheckIPPortReachable check whether IP with port is reachable with 2 min +//CheckIPPortReachable check whether IP with port is reachable within 1 min func CheckIPPortReachable(ip string, port string) error { attemptCnt := 0 for attemptCnt < 6 { @@ -363,7 +363,6 @@ func CheckIPPortReachable(ip string, port string) error { fmt.Printf("IP %s port %s is reachable\n", ip, port) return nil } - time.Sleep(time.Second * 10) attemptCnt++ } return fmt.Errorf("IP %s port %s is not reachable", ip, port) From 658ea29f8ab541ee872cfcfd5c8b949a3fcc8af5 Mon Sep 17 00:00:00 2001 From: Neo Liang Date: Fri, 20 Nov 2020 19:41:54 +0800 Subject: [PATCH 4/7] omit error --- pkg/cmd/utils.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/cmd/utils.go b/pkg/cmd/utils.go index 1b9c8574e..828c63dcf 100644 --- a/pkg/cmd/utils.go +++ b/pkg/cmd/utils.go @@ -354,10 +354,7 @@ func CheckIPPortReachable(ip string, port string) error { attemptCnt := 0 for attemptCnt < 6 { timeout := time.Second * 10 - conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout) - if err != nil { - fmt.Println("Connecting error:", err) - } + 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) From de1beb7053074f4209aabfc6f3f4551cdbc65576 Mon Sep 17 00:00:00 2001 From: Neo Liang Date: Sat, 21 Nov 2020 09:12:25 +0800 Subject: [PATCH 5/7] re-trigger the test --- pkg/cmd/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/utils.go b/pkg/cmd/utils.go index 828c63dcf..220369dee 100644 --- a/pkg/cmd/utils.go +++ b/pkg/cmd/utils.go @@ -349,7 +349,7 @@ func PrintoutObject(objectToPrint interface{}, writer io.Writer, outputFormat st return nil } -//CheckIPPortReachable check whether IP with port is reachable within 1 min +//CheckIPPortReachable check whether IP with port is reachable within 1 min,retrigger the test func CheckIPPortReachable(ip string, port string) error { attemptCnt := 0 for attemptCnt < 6 { From 499a4b7b1386e05e429d87318ec1772335e0837d Mon Sep 17 00:00:00 2001 From: Neo Liang Date: Sat, 21 Nov 2020 09:22:00 +0800 Subject: [PATCH 6/7] remove re-trigger test info --- pkg/cmd/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/utils.go b/pkg/cmd/utils.go index 220369dee..828c63dcf 100644 --- a/pkg/cmd/utils.go +++ b/pkg/cmd/utils.go @@ -349,7 +349,7 @@ func PrintoutObject(objectToPrint interface{}, writer io.Writer, outputFormat st return nil } -//CheckIPPortReachable check whether IP with port is reachable within 1 min,retrigger the test +//CheckIPPortReachable check whether IP with port is reachable within 1 min func CheckIPPortReachable(ip string, port string) error { attemptCnt := 0 for attemptCnt < 6 { From f93033b7e1c6fbb67e26755fcbf91f8b7cf2d54d Mon Sep 17 00:00:00 2001 From: Neo Liang Date: Tue, 24 Nov 2020 09:51:11 +0800 Subject: [PATCH 7/7] rename variable name --- pkg/cmd/utils.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/utils.go b/pkg/cmd/utils.go index 828c63dcf..54aa096d4 100644 --- a/pkg/cmd/utils.go +++ b/pkg/cmd/utils.go @@ -351,8 +351,8 @@ func PrintoutObject(objectToPrint interface{}, writer io.Writer, outputFormat st //CheckIPPortReachable check whether IP with port is reachable within 1 min func CheckIPPortReachable(ip string, port string) error { - attemptCnt := 0 - for attemptCnt < 6 { + attemptCount := 0 + for attemptCount < 6 { timeout := time.Second * 10 conn, _ := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout) if conn != nil { @@ -360,7 +360,7 @@ func CheckIPPortReachable(ip string, port string) error { fmt.Printf("IP %s port %s is reachable\n", ip, port) return nil } - attemptCnt++ + attemptCount++ } return fmt.Errorf("IP %s port %s is not reachable", ip, port) }