Revert "make remote commands run and not fail if an SSH session failed to be created"

This reverts commit 7224661c71.
This commit is contained in:
Andrew Woodlee 2023-07-20 21:20:54 -05:00
parent 7224661c71
commit affdd0abfd
4 changed files with 6 additions and 38 deletions

View File

@ -6,19 +6,6 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
and is generated by [Changie](https://github.com/miniscruff/changie). and is generated by [Changie](https://github.com/miniscruff/changie).
## v0.3.1 - 2023-07-20
### Changed
* If an SSH session failed to be created, the command would fail. This would be caused when restarting the SSH host. The SSH connection is attempted to be created again. If successful, the command is executed normally.
## v0.3.0 - 2023-01-07
### Added
* Getting environment variables and passwords from Vault (not tested yet)
* Vault configuration to config (not tested yet)
* Ability to run scripts from file on local machine on the remote host
* Ability to get ouput in the notification of a list for individual commands or all commands
### Changed
* Make SSH connections close after all commands have been run; reuse previous connections if needed
## 0.2.4 - 2023-02-18 ## 0.2.4 - 2023-02-18
### Added ### Added
* Notifications now display errors and the output of the failed command. * Notifications now display errors and the output of the failed command.

View File

@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const versionStr = "0.3.1" const versionStr = "0.3.0"
var ( var (
versionCmd = &cobra.Command{ versionCmd = &cobra.Command{

View File

@ -64,20 +64,9 @@ func (command *Command) RunCmd(log zerolog.Logger, backyConf *ConfigFile, opts *
} }
} }
commandSession, err := command.RemoteHost.SshClient.NewSession() commandSession, err := command.RemoteHost.SshClient.NewSession()
// Retry connecting to host; if that fails, error. If it does not fail, try to create new session
if err != nil { if err != nil {
connErr := command.RemoteHost.ConnectToSSHHost(opts, backyConf) return nil, err
if connErr != nil {
return nil, fmt.Errorf("error creating session: %v, and error creating new connection to host: %v", err, connErr)
}
commandSession, err = command.RemoteHost.SshClient.NewSession()
if err != nil {
return nil, fmt.Errorf("error creating session: %v", err)
}
} }
defer commandSession.Close() defer commandSession.Close()
injectEnvIntoSSH(envVars, commandSession, opts, log) injectEnvIntoSSH(envVars, commandSession, opts, log)
@ -401,12 +390,15 @@ func (config *ConfigFile) RunListConfig(cron string, opts *ConfigOpts) {
listChan := make(chan *CmdList, configListsLen) listChan := make(chan *CmdList, configListsLen)
results := make(chan string) results := make(chan string)
// This starts up list workers, initially blocked // This starts up 3 workers, initially blocked
// because there are no jobs yet. // because there are no jobs yet.
for w := 1; w <= configListsLen; w++ { for w := 1; w <= configListsLen; w++ {
go cmdListWorker(mTemps, listChan, config, results, opts) go cmdListWorker(mTemps, listChan, config, results, opts)
} }
// Here we send 5 `jobs` and then `close` that
// channel to indicate that's all the work we have.
// configChan <- config.Cmds
for listName, cmdConfig := range config.CmdConfigLists { for listName, cmdConfig := range config.CmdConfigLists {
if cmdConfig.Name == "" { if cmdConfig.Name == "" {
cmdConfig.Name = listName cmdConfig.Name = listName
@ -446,7 +438,6 @@ func (config *ConfigFile) ExecuteCmds(opts *ConfigOpts) {
func (c *ConfigFile) closeHostConnections() { func (c *ConfigFile) closeHostConnections() {
for _, host := range c.Hosts { for _, host := range c.Hosts {
c.Logger.Info().Str("server", host.HostName)
if host.isProxyHost { if host.isProxyHost {
continue continue
} }
@ -454,7 +445,6 @@ func (c *ConfigFile) closeHostConnections() {
if _, err := host.SshClient.NewSession(); err == nil { if _, err := host.SshClient.NewSession(); err == nil {
c.Logger.Info().Msgf("Closing host connection %s", host.HostName) c.Logger.Info().Msgf("Closing host connection %s", host.HostName)
host.SshClient.Close() host.SshClient.Close()
host.SshClient = nil
} }
} }
for _, proxyHost := range host.ProxyHost { for _, proxyHost := range host.ProxyHost {
@ -465,7 +455,6 @@ func (c *ConfigFile) closeHostConnections() {
if _, err := host.SshClient.NewSession(); err == nil { if _, err := host.SshClient.NewSession(); err == nil {
c.Logger.Info().Msgf("Closing connection to proxy host %s", host.HostName) c.Logger.Info().Msgf("Closing connection to proxy host %s", host.HostName)
host.SshClient.Close() host.SshClient.Close()
host.SshClient = nil
} }
} }
} }
@ -475,7 +464,6 @@ func (c *ConfigFile) closeHostConnections() {
if _, err := host.SshClient.NewSession(); err == nil { if _, err := host.SshClient.NewSession(); err == nil {
c.Logger.Info().Msgf("Closing proxy host connection %s", host.HostName) c.Logger.Info().Msgf("Closing proxy host connection %s", host.HostName)
host.SshClient.Close() host.SshClient.Close()
host.SshClient = nil
} }
} }
} }

View File

@ -117,7 +117,6 @@ func (remoteConfig *Host) ConnectToSSHHost(opts *ConfigOpts, config *ConfigFile)
if connectErr != nil { if connectErr != nil {
return connectErr return connectErr
} }
config.Hosts[remoteConfig.Host] = remoteConfig config.Hosts[remoteConfig.Host] = remoteConfig
return nil return nil
} }
@ -135,7 +134,6 @@ func (remoteHost *Host) GetSshUserFromConfig() {
} }
remoteHost.ClientConfig.User = remoteHost.User remoteHost.ClientConfig.User = remoteHost.User
} }
func (remoteHost *Host) GetAuthMethods(opts *ConfigOpts) error { func (remoteHost *Host) GetAuthMethods(opts *ConfigOpts) error {
var signer ssh.Signer var signer ssh.Signer
var err error var err error
@ -267,7 +265,6 @@ func (remoteHost *Host) ConnectThroughBastion(log zerolog.Logger) (*ssh.Client,
} }
func GetKnownHosts(khPath string) (string, error) { func GetKnownHosts(khPath string) (string, error) {
if TS(khPath) != "" { if TS(khPath) != "" {
return resolveDir(khPath) return resolveDir(khPath)
} }
@ -275,7 +272,6 @@ func GetKnownHosts(khPath string) (string, error) {
} }
func GetPrivateKeyPassword(key string, opts *ConfigOpts, log zerolog.Logger) (string, error) { func GetPrivateKeyPassword(key string, opts *ConfigOpts, log zerolog.Logger) (string, error) {
var prKeyPassword string var prKeyPassword string
if strings.HasPrefix(key, "file:") { if strings.HasPrefix(key, "file:") {
privKeyPassFilePath := strings.TrimPrefix(key, "file:") privKeyPassFilePath := strings.TrimPrefix(key, "file:")
@ -302,7 +298,6 @@ func GetPrivateKeyPassword(key string, opts *ConfigOpts, log zerolog.Logger) (st
} }
func GetPassword(pass string, opts *ConfigOpts, log zerolog.Logger) (string, error) { func GetPassword(pass string, opts *ConfigOpts, log zerolog.Logger) (string, error) {
pass = strings.TrimSpace(pass) pass = strings.TrimSpace(pass)
if pass == "" { if pass == "" {
return "", nil return "", nil
@ -334,7 +329,6 @@ func GetPassword(pass string, opts *ConfigOpts, log zerolog.Logger) (string, err
} }
func (remoteConfig *Host) GetProxyJumpFromConfig(hosts map[string]*Host) error { func (remoteConfig *Host) GetProxyJumpFromConfig(hosts map[string]*Host) error {
proxyJump, _ := remoteConfig.SSHConfigFile.SshConfigFile.Get(remoteConfig.Host, "ProxyJump") proxyJump, _ := remoteConfig.SSHConfigFile.SshConfigFile.Get(remoteConfig.Host, "ProxyJump")
if proxyJump == "" { if proxyJump == "" {
proxyJump = remoteConfig.SSHConfigFile.DefaultUserSettings.Get(remoteConfig.Host, "ProxyJump") proxyJump = remoteConfig.SSHConfigFile.DefaultUserSettings.Get(remoteConfig.Host, "ProxyJump")
@ -360,7 +354,6 @@ func (remoteConfig *Host) GetProxyJumpFromConfig(hosts map[string]*Host) error {
} }
func (remoteConfig *Host) GetProxyJumpConfig(hosts map[string]*Host, opts *ConfigOpts) error { func (remoteConfig *Host) GetProxyJumpConfig(hosts map[string]*Host, opts *ConfigOpts) error {
if TS(remoteConfig.ConfigFilePath) == "" { if TS(remoteConfig.ConfigFilePath) == "" {
remoteConfig.useDefaultConfig = true remoteConfig.useDefaultConfig = true
} }