reverted to on hostname, added zerolog
This commit is contained in:
parent
6b85913316
commit
ae87ccb4b5
@ -2,12 +2,14 @@ package backy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Host defines a host to which to connect
|
// Host defines a host to which to connect
|
||||||
@ -16,7 +18,7 @@ type Host struct {
|
|||||||
ConfigFilePath string
|
ConfigFilePath string
|
||||||
Empty bool
|
Empty bool
|
||||||
Host string
|
Host string
|
||||||
HostName []string
|
HostName string
|
||||||
Port uint16
|
Port uint16
|
||||||
PrivateKeyPath string
|
PrivateKeyPath string
|
||||||
PrivateKeyPassword string
|
PrivateKeyPassword string
|
||||||
@ -77,28 +79,24 @@ type BackupConfig struct {
|
|||||||
* Runs a backup configuration
|
* Runs a backup configuration
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func (command Command) RunCmd() logging.Logging {
|
func (command Command) RunCmd() {
|
||||||
|
|
||||||
var stdoutBuf, stderrBuf bytes.Buffer
|
var cmdArgsStr string
|
||||||
var err error
|
|
||||||
var cmdArgs string
|
|
||||||
for _, v := range command.CmdArgs {
|
for _, v := range command.CmdArgs {
|
||||||
cmdArgs += v
|
cmdArgsStr += fmt.Sprintf(" %s", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
var remoteHost = &command.RemoteHost
|
fmt.Printf("\n\nRunning command: " + command.Cmd + " " + cmdArgsStr + " on host " + command.Host + "...\n\n")
|
||||||
fmt.Printf("\n\nRunning command: " + command.Cmd + " " + cmdArgs + " on host " + command.RemoteHost.Host + "...\n\n")
|
if command.Host != "" {
|
||||||
if command.Remote {
|
|
||||||
|
|
||||||
remoteHost.Port = 22
|
command.RemoteHost.Host = command.Host
|
||||||
remoteHost.Host = command.RemoteHost.Host
|
command.RemoteHost.Port = 22
|
||||||
|
sshc, err := command.RemoteHost.ConnectToSSHHost()
|
||||||
sshClient, err := remoteHost.ConnectToSSHHost()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("ssh dial: %w", err))
|
panic(fmt.Errorf("ssh dial: %w", err))
|
||||||
}
|
}
|
||||||
defer sshClient.Close()
|
defer sshc.Close()
|
||||||
s, err := sshClient.NewSession()
|
s, err := sshc.NewSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("new ssh session: %w", err))
|
panic(fmt.Errorf("new ssh session: %w", err))
|
||||||
}
|
}
|
||||||
@ -109,33 +107,55 @@ func (command Command) RunCmd() logging.Logging {
|
|||||||
cmd += " " + a
|
cmd += " " + a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var stdoutBuf, stderrBuf bytes.Buffer
|
||||||
s.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
|
s.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
|
||||||
s.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
|
s.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
|
||||||
err = s.Run(cmd)
|
err = s.Run(cmd)
|
||||||
|
log.Info().Bytes(fmt.Sprintf("%s stdout", command.Cmd), stdoutBuf.Bytes()).Send()
|
||||||
|
log.Info().Bytes(fmt.Sprintf("%s stderr", command.Cmd), stderrBuf.Bytes()).Send()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return logging.Logging{
|
panic(fmt.Errorf("error when running cmd " + cmd + "\n Error: " + err.Error()))
|
||||||
Output: stdoutBuf.String(),
|
|
||||||
Err: fmt.Errorf("error running " + cmd + ": " + stderrBuf.String()),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// fmt.Printf("Output: %s\n", string(output))
|
|
||||||
} else {
|
} else {
|
||||||
// shell := "/bin/bash"
|
// shell := "/bin/bash"
|
||||||
localCMD := exec.Command(command.Cmd, command.CmdArgs...)
|
var err error
|
||||||
|
if command.Shell != "" {
|
||||||
|
cmdArgsStr = fmt.Sprintf("%s %s", command.Cmd, cmdArgsStr)
|
||||||
|
localCMD := exec.Command(command.Shell, "-c", cmdArgsStr)
|
||||||
|
|
||||||
|
var stdoutBuf, stderrBuf bytes.Buffer
|
||||||
localCMD.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
|
localCMD.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
|
||||||
localCMD.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
|
localCMD.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
|
||||||
err = localCMD.Run()
|
err = localCMD.Run()
|
||||||
|
log.Info().Bytes(fmt.Sprintf("%s stdout", command.Cmd), stdoutBuf.Bytes()).Send()
|
||||||
|
log.Info().Bytes(fmt.Sprintf("%s stderr", command.Cmd), stderrBuf.Bytes()).Send()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return logging.Logging{
|
panic(fmt.Errorf("error when running cmd: %s: %w", command.Cmd, err))
|
||||||
Output: stdoutBuf.String(),
|
}
|
||||||
Err: fmt.Errorf(stderrBuf.String()),
|
return
|
||||||
|
}
|
||||||
|
localCMD := exec.Command(command.Cmd, command.CmdArgs...)
|
||||||
|
|
||||||
|
var stdoutBuf, stderrBuf bytes.Buffer
|
||||||
|
localCMD.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
|
||||||
|
localCMD.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
|
||||||
|
err = localCMD.Run()
|
||||||
|
log.Info().Bytes(fmt.Sprintf("%s stdout", command.Cmd), stdoutBuf.Bytes()).Send()
|
||||||
|
log.Info().Bytes(fmt.Sprintf("%s stderr", command.Cmd), stderrBuf.Bytes()).Send()
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("error when running cmd: %s: %w", command.Cmd, err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return logging.Logging{
|
|
||||||
Output: stdoutBuf.String(),
|
func (config *BackyConfigFile) RunBackyConfig() {
|
||||||
Err: nil,
|
for _, list := range config.CmdLists {
|
||||||
|
for _, cmd := range list {
|
||||||
|
cmdToRun := config.Cmds[cmd]
|
||||||
|
cmdToRun.RunCmd()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,3 +172,110 @@ func NewConfig() *BackyConfigFile {
|
|||||||
Hosts: make(map[string]Host),
|
Hosts: make(map[string]Host),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadAndParseConfigFile() *BackyConfigFile {
|
||||||
|
|
||||||
|
backyConfigFile := NewConfig()
|
||||||
|
|
||||||
|
backyViper := viper.New()
|
||||||
|
backyViper.SetConfigName("backy") // name of config file (without extension)
|
||||||
|
backyViper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
|
||||||
|
backyViper.AddConfigPath(".") // optionally look for config in the working directory
|
||||||
|
backyViper.AddConfigPath("$HOME/.config/backy") // call multiple times to add many search paths
|
||||||
|
err := backyViper.ReadInConfig() // Find and read the config file
|
||||||
|
if err != nil { // Handle errors reading the config file
|
||||||
|
panic(fmt.Errorf("fatal error config file: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
commandsMap := backyViper.GetStringMapString("commands")
|
||||||
|
var cmdNames []string
|
||||||
|
for k := range commandsMap {
|
||||||
|
cmdNames = append(cmdNames, k)
|
||||||
|
}
|
||||||
|
hostConfigsMap := make(map[string]*viper.Viper)
|
||||||
|
|
||||||
|
for _, cmdName := range cmdNames {
|
||||||
|
var backupCmdStruct Command
|
||||||
|
println(cmdName)
|
||||||
|
subCmd := backyViper.Sub(getNestedConfig("commands", cmdName))
|
||||||
|
|
||||||
|
hostSet := subCmd.IsSet("host")
|
||||||
|
host := subCmd.GetString("host")
|
||||||
|
|
||||||
|
cmdSet := subCmd.IsSet("cmd")
|
||||||
|
cmd := subCmd.GetString("cmd")
|
||||||
|
cmdArgsSet := subCmd.IsSet("cmdargs")
|
||||||
|
cmdArgs := subCmd.GetStringSlice("cmdargs")
|
||||||
|
shellSet := subCmd.IsSet("shell")
|
||||||
|
shell := subCmd.GetString("shell")
|
||||||
|
|
||||||
|
if hostSet {
|
||||||
|
println("Host:")
|
||||||
|
println(host)
|
||||||
|
backupCmdStruct.Host = host
|
||||||
|
if backyViper.IsSet(getNestedConfig("hosts", host)) {
|
||||||
|
hostconfig := backyViper.Sub(getNestedConfig("hosts", host))
|
||||||
|
hostConfigsMap[host] = hostconfig
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println("Host is not set")
|
||||||
|
}
|
||||||
|
if cmdSet {
|
||||||
|
println("Cmd:")
|
||||||
|
println(cmd)
|
||||||
|
backupCmdStruct.Cmd = cmd
|
||||||
|
} else {
|
||||||
|
println("Cmd is not set")
|
||||||
|
}
|
||||||
|
if shellSet {
|
||||||
|
println("Shell:")
|
||||||
|
println(shell)
|
||||||
|
backupCmdStruct.Shell = shell
|
||||||
|
} else {
|
||||||
|
println("Shell is not set")
|
||||||
|
}
|
||||||
|
if cmdArgsSet {
|
||||||
|
println("CmdArgs:")
|
||||||
|
for _, arg := range cmdArgs {
|
||||||
|
println(arg)
|
||||||
|
}
|
||||||
|
backupCmdStruct.CmdArgs = cmdArgs
|
||||||
|
} else {
|
||||||
|
println("CmdArgs are not set")
|
||||||
|
}
|
||||||
|
backyConfigFile.Cmds[cmdName] = backupCmdStruct
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdListCfg := backyViper.GetStringMapStringSlice("cmd-lists")
|
||||||
|
var cmdNotFoundSliceErr []error
|
||||||
|
for cmdListName, cmdList := range cmdListCfg {
|
||||||
|
println("Cmd list: ", cmdListName)
|
||||||
|
for _, cmdInList := range cmdList {
|
||||||
|
println("Command in list: " + cmdInList)
|
||||||
|
_, cmdNameFound := backyConfigFile.Cmds[cmdInList]
|
||||||
|
if !backyViper.IsSet(getNestedConfig("commands", cmdInList)) && !cmdNameFound {
|
||||||
|
cmdNotFoundStr := fmt.Sprintf("command definition %s is not in config file\n", cmdInList)
|
||||||
|
cmdNotFoundErr := errors.New(cmdNotFoundStr)
|
||||||
|
cmdNotFoundSliceErr = append(cmdNotFoundSliceErr, cmdNotFoundErr)
|
||||||
|
} else {
|
||||||
|
backyConfigFile.CmdLists[cmdListName] = append(backyConfigFile.CmdLists[cmdListName], cmdInList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, err := range cmdNotFoundSliceErr {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return backyConfigFile
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNestedConfig(nestedConfig, key string) string {
|
||||||
|
return fmt.Sprintf("%s.%s", nestedConfig, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNestedSSHConfig(key string) string {
|
||||||
|
return fmt.Sprintf("hosts.%s.config", key)
|
||||||
|
}
|
||||||
|
@ -30,14 +30,14 @@ type SshConfig struct {
|
|||||||
host string
|
host string
|
||||||
|
|
||||||
// host name to connect to
|
// host name to connect to
|
||||||
hostName []string
|
hostName string
|
||||||
|
|
||||||
user string
|
user string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config SshConfig) GetSSHConfig() (SshConfig, error) {
|
func (config SshConfig) GetSSHConfig() (SshConfig, error) {
|
||||||
hostNames := ssh_config.GetAll(config.host, "HostName")
|
hostNames := ssh_config.Get(config.host, "HostName")
|
||||||
if hostNames == nil {
|
if hostNames == "" {
|
||||||
return SshConfig{}, errors.New("hostname not found")
|
return SshConfig{}, errors.New("hostname not found")
|
||||||
}
|
}
|
||||||
config.hostName = hostNames
|
config.hostName = hostNames
|
||||||
@ -116,22 +116,24 @@ func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) {
|
|||||||
identityFile = filepath.Join(dir, identityFile[2:])
|
identityFile = filepath.Join(dir, identityFile[2:])
|
||||||
}
|
}
|
||||||
remoteConfig.PrivateKeyPath = filepath.Join(identityFile)
|
remoteConfig.PrivateKeyPath = filepath.Join(identityFile)
|
||||||
|
log.Debug().Str("Private key path", remoteConfig.PrivateKeyPath).Send()
|
||||||
}
|
}
|
||||||
remoteConfig.HostName, _ = cfg.GetAll(remoteConfig.Host, "HostName")
|
remoteConfig.HostName, _ = cfg.Get(remoteConfig.Host, "HostName")
|
||||||
if remoteConfig.HostName == nil {
|
remoteConfig.User, _ = cfg.Get(remoteConfig.Host, "User")
|
||||||
|
if remoteConfig.HostName == "" {
|
||||||
port, _ := cfg.Get(remoteConfig.Host, "Port")
|
port, _ := cfg.Get(remoteConfig.Host, "Port")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
port = "22"
|
port = "22"
|
||||||
}
|
}
|
||||||
remoteConfig.HostName[0] = remoteConfig.Host + ":" + port
|
// remoteConfig.HostName[0] = remoteConfig.Host + ":" + port
|
||||||
} else {
|
} else {
|
||||||
for index, hostName := range remoteConfig.HostName {
|
// for index, hostName := range remoteConfig.HostName {
|
||||||
port, _ := cfg.Get(remoteConfig.Host, "Port")
|
port, _ := cfg.Get(remoteConfig.Host, "Port")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
port = "22"
|
port = "22"
|
||||||
}
|
}
|
||||||
remoteConfig.HostName[index] = hostName + ":" + port
|
remoteConfig.HostName = remoteConfig.HostName + ":" + port
|
||||||
}
|
// remoteConfig.HostName[index] = hostName + ":" + port
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add value/option to config for host key and add bool to check for host key
|
// TODO: Add value/option to config for host key and add bool to check for host key
|
||||||
@ -151,14 +153,16 @@ func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) {
|
|||||||
User: remoteConfig.User,
|
User: remoteConfig.User,
|
||||||
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
|
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
|
||||||
HostKeyCallback: hostKeyCallback,
|
HostKeyCallback: hostKeyCallback,
|
||||||
|
// HostKeyAlgorithms: []string{ssh.KeyAlgoECDSA256},
|
||||||
}
|
}
|
||||||
for _, host := range remoteConfig.HostName {
|
// for _, host := range remoteConfig.HostName {
|
||||||
log.Info().Msgf("Connecting to host %s", host)
|
log.Info().Msgf("Connecting to host %s", remoteConfig.HostName)
|
||||||
sshClient, connectErr = ssh.Dial("tcp", host, sshConfig)
|
|
||||||
|
sshClient, connectErr = ssh.Dial("tcp", remoteConfig.HostName, sshConfig)
|
||||||
if connectErr != nil {
|
if connectErr != nil {
|
||||||
log.Fatal().Err(connectErr).Str("host", host).Msg("error when connecting to host")
|
log.Fatal().Str("host", remoteConfig.HostName).Err(connectErr).Send()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// }
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user