added zerolog
This commit is contained in:
parent
9d07298eb0
commit
6b85913316
4
go.mod
4
go.mod
@ -6,9 +6,11 @@ go 1.19
|
||||
|
||||
require (
|
||||
github.com/kevinburke/ssh_config v1.2.0
|
||||
github.com/rs/zerolog v1.28.0
|
||||
github.com/spf13/cobra v1.6.1
|
||||
github.com/spf13/viper v1.14.0
|
||||
golang.org/x/crypto v0.4.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||
)
|
||||
|
||||
require (
|
||||
@ -16,6 +18,8 @@ require (
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||
|
18
go.sum
18
go.sum
@ -36,6 +36,7 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
|
||||
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
|
||||
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
@ -46,6 +47,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
@ -62,6 +64,7 @@ github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbS
|
||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
@ -139,6 +142,13 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
||||
@ -152,6 +162,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
|
||||
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
|
||||
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
|
||||
github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
|
||||
@ -314,6 +327,9 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
@ -475,6 +491,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
|
@ -25,9 +25,43 @@ type Host struct {
|
||||
|
||||
type Command struct {
|
||||
Remote bool
|
||||
RemoteHost Host
|
||||
|
||||
// command to run
|
||||
Cmd string
|
||||
Args []string
|
||||
|
||||
// host on which to run cmd
|
||||
Host string
|
||||
|
||||
/*
|
||||
Shell specifies which shell to run the command in, if any
|
||||
Not applicable when host is defined
|
||||
*/
|
||||
Shell string
|
||||
|
||||
RemoteHost Host
|
||||
|
||||
// cmdArgs is an array that holds the arguments to cmd
|
||||
CmdArgs []string
|
||||
}
|
||||
|
||||
type BackyConfigFile struct {
|
||||
/*
|
||||
Cmds holds the commands for a list
|
||||
key is the name of the command
|
||||
*/
|
||||
Cmds map[string]Command
|
||||
|
||||
/*
|
||||
CmdLists holds the lists of commands to be run in order
|
||||
key is the command list name
|
||||
*/
|
||||
CmdLists map[string][]string
|
||||
|
||||
/*
|
||||
Hosts holds the Host config
|
||||
key is the host
|
||||
*/
|
||||
Hosts map[string]Host
|
||||
}
|
||||
|
||||
// BackupConfig is a configuration struct that is used to define backups
|
||||
@ -43,12 +77,12 @@ type BackupConfig struct {
|
||||
* Runs a backup configuration
|
||||
*/
|
||||
|
||||
func (command Command) runCmd() logging.Logging {
|
||||
func (command Command) RunCmd() logging.Logging {
|
||||
|
||||
var stdoutBuf, stderrBuf bytes.Buffer
|
||||
var err error
|
||||
var cmdArgs string
|
||||
for _, v := range command.Args {
|
||||
for _, v := range command.CmdArgs {
|
||||
cmdArgs += v
|
||||
}
|
||||
|
||||
@ -71,7 +105,7 @@ func (command Command) runCmd() logging.Logging {
|
||||
defer s.Close()
|
||||
|
||||
cmd := command.Cmd
|
||||
for _, a := range command.Args {
|
||||
for _, a := range command.CmdArgs {
|
||||
cmd += " " + a
|
||||
}
|
||||
|
||||
@ -87,7 +121,7 @@ func (command Command) runCmd() logging.Logging {
|
||||
// fmt.Printf("Output: %s\n", string(output))
|
||||
} else {
|
||||
// shell := "/bin/bash"
|
||||
localCMD := exec.Command(command.Cmd, command.Args...)
|
||||
localCMD := exec.Command(command.Cmd, command.CmdArgs...)
|
||||
localCMD.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
|
||||
localCMD.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
|
||||
err = localCMD.Run()
|
||||
@ -108,3 +142,13 @@ func (command Command) runCmd() logging.Logging {
|
||||
func New() BackupConfig {
|
||||
return BackupConfig{}
|
||||
}
|
||||
|
||||
// NewConfig initializes new config that holds information
|
||||
// from the config file
|
||||
func NewConfig() *BackyConfigFile {
|
||||
return &BackyConfigFile{
|
||||
Cmds: make(map[string]Command),
|
||||
CmdLists: make(map[string][]string),
|
||||
Hosts: make(map[string]Host),
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package backy
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
@ -11,8 +10,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kevinburke/ssh_config"
|
||||
"github.com/rs/zerolog"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
type SshConfig struct {
|
||||
@ -53,6 +54,44 @@ func (config SshConfig) GetSSHConfig() (SshConfig, error) {
|
||||
}
|
||||
|
||||
func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) {
|
||||
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
|
||||
output.FormatLevel = func(i interface{}) string {
|
||||
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
|
||||
}
|
||||
output.FormatMessage = func(i interface{}) string {
|
||||
return fmt.Sprintf("%s", i)
|
||||
}
|
||||
output.FormatFieldName = func(i interface{}) string {
|
||||
return fmt.Sprintf("%s: ", i)
|
||||
}
|
||||
output.FormatFieldValue = func(i interface{}) string {
|
||||
return strings.ToUpper(fmt.Sprintf("%s", i))
|
||||
}
|
||||
|
||||
fileLogger := &lumberjack.Logger{
|
||||
Filename: "./backy.log",
|
||||
MaxSize: 500, // megabytes
|
||||
MaxBackups: 3,
|
||||
MaxAge: 28, //days
|
||||
Compress: true, // disabled by default
|
||||
}
|
||||
|
||||
// fileOutput := zerolog.ConsoleWriter{Out: fileLogger, TimeFormat: time.RFC3339}
|
||||
// fileOutput.FormatLevel = func(i interface{}) string {
|
||||
// return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
|
||||
// }
|
||||
// fileOutput.FormatMessage = func(i interface{}) string {
|
||||
// return fmt.Sprintf("%s", i)
|
||||
// }
|
||||
// fileOutput.FormatFieldName = func(i interface{}) string {
|
||||
// return fmt.Sprintf("%s: ", i)
|
||||
// }
|
||||
// fileOutput.FormatFieldValue = func(i interface{}) string {
|
||||
// return strings.ToUpper(fmt.Sprintf("%s", i))
|
||||
// }
|
||||
zerolog.TimeFieldFormat = time.RFC1123
|
||||
writers := zerolog.MultiLevelWriter(os.Stdout, fileLogger)
|
||||
log := zerolog.New(writers).With().Timestamp().Logger()
|
||||
|
||||
var sshClient *ssh.Client
|
||||
var connectErr error
|
||||
@ -92,35 +131,32 @@ func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) {
|
||||
port = "22"
|
||||
}
|
||||
remoteConfig.HostName[index] = hostName + ":" + port
|
||||
|
||||
println("HostName: " + remoteConfig.HostName[0])
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add value/option to config for host key and add bool to check for host key
|
||||
hostKeyCallback, err := knownhosts.New(khPath)
|
||||
if err != nil {
|
||||
log.Fatal("could not create hostkeycallback function: ", err)
|
||||
log.Fatal().Err(err).Msg("could not create hostkeycallback function")
|
||||
}
|
||||
privateKey, err := os.ReadFile(remoteConfig.PrivateKeyPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read private key error: %w", err)
|
||||
log.Fatal().Err(err).Msg("read private key error")
|
||||
}
|
||||
signer, err := ssh.ParsePrivateKey(privateKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse private key error: %w", err)
|
||||
log.Fatal().Err(err).Msg("parse private key error")
|
||||
}
|
||||
sshConfig := &ssh.ClientConfig{
|
||||
User: remoteConfig.User,
|
||||
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
|
||||
HostKeyCallback: hostKeyCallback,
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
for _, host := range remoteConfig.HostName {
|
||||
println("Connecting to " + host)
|
||||
log.Info().Msgf("Connecting to host %s", host)
|
||||
sshClient, connectErr = ssh.Dial("tcp", host, sshConfig)
|
||||
if connectErr != nil {
|
||||
panic(fmt.Errorf("error when connecting to host %s: %w", host, connectErr))
|
||||
log.Fatal().Err(connectErr).Str("host", host).Msg("error when connecting to host")
|
||||
}
|
||||
}
|
||||
break
|
||||
|
@ -1,14 +1,5 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/syslog"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Logging struct {
|
||||
Err error
|
||||
Output string
|
||||
@ -17,28 +8,3 @@ type Logging struct {
|
||||
type Logfile struct {
|
||||
LogfilePath string
|
||||
}
|
||||
|
||||
func OpenLogFile(config *viper.Viper) (interface{}, error) {
|
||||
var logFile *os.File
|
||||
var syslogWriter *syslog.Writer
|
||||
var err error
|
||||
logType := config.GetString("global.logging.type")
|
||||
if logType != "" {
|
||||
|
||||
switch logType {
|
||||
case "file":
|
||||
logFile, err = os.OpenFile(config.GetString("global.logging.file"), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return logFile, nil
|
||||
case "syslog":
|
||||
syslogWriter, err = syslog.New(syslog.LOG_SYSLOG, "Backy")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to set logfile: " + err.Error())
|
||||
}
|
||||
return syslogWriter, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("log type not specified; Please set global.logging.type in your config file")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user