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

cleanup function will return the code which ssh returns #445

Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 13 additions & 3 deletions pkg/cmd/ssh_openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type OpenstackInstanceAttribute struct {

//sshToOpenstackNode ssh to openstack node
func sshToOpenstackNode(nodeName, path, user, pathSSKeypair string, sshPublicKey []byte, myPublicIP string) {
sshStatus := 0
a := &OpenstackInstanceAttribute{}
a.InstanceID = nodeName
var err error
Expand Down Expand Up @@ -68,7 +69,13 @@ func sshToOpenstackNode(nodeName, path, user, pathSSKeypair string, sshPublicKey
operate("openstack", "server add floating ip "+a.InstanceID+" "+a.FIP)
time.Sleep(5000)

defer a.cleanUpOpenstack()
defer a.cleanUpOpenstack(&sshStatus)

err = CheckIPPortReachable(a.FIP, "22")

if err != nil {
sshStatus = 1
}

node := user + "@" + a.FIP
fmt.Println("(4/5) Establishing SSH connection")
Expand All @@ -89,11 +96,14 @@ func sshToOpenstackNode(nodeName, path, user, pathSSKeypair string, sshPublicKey
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Println(err)
if exitError, ok := err.(*exec.ExitError); ok {
sshStatus = exitError.ExitCode()
}
}
}

//cleanUpOpenstack clean the resource added to ssh to openstack node
func (a *OpenstackInstanceAttribute) cleanUpOpenstack() {
func (a *OpenstackInstanceAttribute) cleanUpOpenstack(exitStatus *int) {
fmt.Println("")
fmt.Println("(5/5) Cleanup")

Expand All @@ -102,5 +112,5 @@ func (a *OpenstackInstanceAttribute) cleanUpOpenstack() {

fmt.Println("Delete the floating IP")
operate("openstack", "floating ip delete "+a.FIP)

os.Exit(*exitStatus)
}
18 changes: 18 additions & 0 deletions pkg/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
"runtime"
"strings"
"time"

gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardenerlogger "github.com/gardener/gardener/pkg/logger"
Expand Down Expand Up @@ -348,3 +349,20 @@ func PrintoutObject(objectToPrint interface{}, writer io.Writer, outputFormat st
}
return nil
}

//CheckIPPortReachable check whether IP with port is reachable with 1 min
func CheckIPPortReachable(ip string, port string) error {
attemptCnt := 0
for attemptCnt < 6 {
ncCmd := fmt.Sprintf("timeout 10 nc -vtnz %s %s", ip, port)
cmd := exec.Command("bash", "-c", ncCmd)
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Stop calling bash
  • Use exec. CommandContext to pass a context with a timeout to achieve the same behavior instead of calling timeout
  • Does this work on windows? I'd rather prefer a native-go solution

output, _ := cmd.CombinedOutput()
if strings.Contains(string(output), "open") {
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)
}