Skip to content

Commit

Permalink
Create a function to executte a single command on a host specified in…
Browse files Browse the repository at this point in the history
… argument.
  • Loading branch information
Acanthostega committed Sep 18, 2013
1 parent e2e40ba commit e3d0e8d
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 101 deletions.
135 changes: 135 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package commands

import (
"github.com/ElricleNecro/TOD/checker"
"github.com/ElricleNecro/TOD/formatter"
"github.com/ElricleNecro/TOD/ssh"
)

// This function runs a single command on the host on argument.
func OneCommand(
host *formatter.Host,
user *formatter.User,
command string,
timeout int,
disconnected chan<- *formatter.Host,
) (string, error) {

// create a session object
session := ssh.New(
user,
host,
)

// check the connection to the host
if is, err := checker.IsConnected(host, timeout); !is || (err != nil) {

// disconnect
Disconnecter(
"Can't connect to host "+host.Hostname,
host,
disconnected,
)

// exit the loop
return "", err
}

// display that host is connected
formatter.ColoredPrintln(
formatter.Green,
false,
"Host",
host.Hostname,
"seems to be online!",
)

// Attempt a connection to the host
err := session.Connect()

// check the host can be called
if err != nil {

// disconnect
Disconnecter(
"Can't connect to host "+host.Hostname,
host,
disconnected,
)

// Close the session
session.Close()

// exit the loop
return "", err
}

// add a session to connect to host
_, err = session.AddSession()
if err != nil {

// disconnect
Disconnecter(
"Problem when adding a session to the host !",
host,
disconnected,
)

// Close the session
session.Close()

// exit the loop
return "", err
}

// execute the command on the host
formatter.ColoredPrintln(
formatter.Green,
false,
"Execute command on", host.Hostname,
)
output, err2 := session.Run(command)
if err2 != nil {

// disconnect
Disconnecter(
"An error occurred during the execution of the command !\n"+
"The command was: "+command+
"and the host is: "+host.Hostname+
"\nError information: "+err2.Error(),
host,
disconnected,
)

// Close the session
session.Close()

// exit the loop
return "", err2
}

// Close the session
session.Close()

// return nil if good
return output, nil
}

// Function to execute a disconnection of host with a command.
func Disconnecter(
message string,
host *formatter.Host,
disconnected chan<- *formatter.Host,
) {

// display
formatter.ColoredPrintln(
formatter.Red,
false,
message,
)

// dispatch remaining work to other hosts
disconnected <- host

}
109 changes: 8 additions & 101 deletions exec/exec.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package exec

import (
"github.com/ElricleNecro/TOD/checker"
"github.com/ElricleNecro/TOD/commands"
"github.com/ElricleNecro/TOD/configuration"
"github.com/ElricleNecro/TOD/formatter"
"github.com/ElricleNecro/TOD/log_command"
"github.com/ElricleNecro/TOD/ssh"
"strconv"
"time"
)
Expand Down Expand Up @@ -40,87 +39,17 @@ loop:
// number of the command
host.CommandNumber = i + 1

// create a session object
session := ssh.New(
host.Commands[i].User,
// Execute the command on the specified host
output, err := commands.OneCommand(
host,
host.Commands[i].User,
host.Commands[i].Command,
config.Timeout,
disconnected,
)

// check the connection to the host
if is, err := checker.IsConnected(host, config.Timeout); !is || (err != nil) {

// disconnect
Disconnecter(
"Can't connect to host "+host.Hostname,
host,
disconnected,
)

// exit the loop
break loop
}

// display that host is connected
formatter.ColoredPrintln(
formatter.Green,
false,
"Host",
host.Hostname,
"seems to be online!",
)

// Attempt a connection to the host
err := session.Connect()

// check the host can be called
// check the command as executed correctly, else exit loop
if err != nil {

// disconnect
Disconnecter(
"Can't connect to host "+host.Hostname,
host,
disconnected,
)

// exit the loop
break loop
}

// add a session to connect to host
_, err = session.AddSession()
if err != nil {

// disconnect
Disconnecter(
"Problem when adding a session to the host !",
host,
disconnected,
)

// exit the loop
break loop
}

// execute the command on the host
formatter.ColoredPrintln(
formatter.Green,
false,
"Execute command on", host.Hostname,
)
output, err2 := session.Run(host.Commands[i].Command)
if err2 != nil {

// disconnect
Disconnecter(
"An error occurred during the execution of the command !\n"+
"The command was: "+host.Commands[i].Command+
"and the host is: "+host.Hostname+
"\nError information: "+err2.Error(),
host,
disconnected,
)

// exit the loop
break loop
}

Expand All @@ -136,9 +65,6 @@ loop:
i,
)

// Close the session
session.Close()

// for now print the result of the command
if !config.NoResults {
formatter.ColoredPrintln(
Expand Down Expand Up @@ -197,25 +123,6 @@ func Waiter(host *formatter.Host) {

}

// Function to execute a disconnection of host with a command.
func Disconnecter(
message string,
host *formatter.Host,
disconnected chan<- *formatter.Host,
) {

// display
formatter.ColoredPrintln(
formatter.Red,
false,
message,
)

// dispatch remaining work to other hosts
disconnected <- host

}

// Function to dispatch an host on other. Set variables to allow a good synchronisation
// between go routines.
func Disconnection(
Expand Down

0 comments on commit e3d0e8d

Please sign in to comment.