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 all 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)
}
17 changes: 17 additions & 0 deletions pkg/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"io"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"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 +350,18 @@ 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 {
timeout := time.Second * 60
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout)
Comment on lines +356 to +357
Copy link
Contributor

Choose a reason for hiding this comment

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

and this works without having to dial/try multiple times within a certain amount of time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, in my test it tries within 1 min, as I set in the 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
}
return fmt.Errorf("IP %s port %s is not reachable", ip, port)
}