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

Commit

Permalink
cleanup function will return the code which ssh returns
Browse files Browse the repository at this point in the history
  • Loading branch information
neo-liang-sap committed Nov 18, 2020
1 parent 7cd336c commit 84b3723
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 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,8 +69,6 @@ func sshToOpenstackNode(nodeName, path, user, pathSSKeypair string, sshPublicKey
operate("openstack", "server add floating ip "+a.InstanceID+" "+a.FIP)
time.Sleep(5000)

defer a.cleanUpOpenstack()

node := user + "@" + a.FIP
fmt.Println("(4/5) Establishing SSH connection")
fmt.Println("")
Expand All @@ -89,11 +88,15 @@ 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()
}
}
defer a.cleanUpOpenstack(sshStatus)
}

//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 +105,5 @@ func (a *OpenstackInstanceAttribute) cleanUpOpenstack() {

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

os.Exit(exitStatus)
}

0 comments on commit 84b3723

Please sign in to comment.