Skip to content

Commit

Permalink
file separation
Browse files Browse the repository at this point in the history
  • Loading branch information
sgsullivan committed Aug 4, 2022
1 parent e632036 commit 0b08431
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 48 deletions.
48 changes: 0 additions & 48 deletions befehl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ import (
"github.com/sgsullivan/befehl/helpers/waitgroup"
)

type Options struct {
PrivateKeyFile string
SshUser string
LogDir string
}

type Instance struct {
options *Options
sshKey ssh.Signer
}

func New(options *Options) *Instance {
return &Instance{
options: options,
Expand All @@ -50,14 +39,6 @@ func (instance *Instance) Execute(hostsFile, payload string, routines int) error
}
}

func (instance *Instance) getPrivKeyFile() string {
if instance.options.PrivateKeyFile != "" {
return instance.options.PrivateKeyFile
}

return os.Getenv("HOME") + "/.ssh/id_rsa"
}

func (instance *Instance) populateSshKeyEncrypted(privKeyBytes *pem.Block) error {
fmt.Printf("enter private key password: ")
password, err := gopass.GetPasswd()
Expand Down Expand Up @@ -112,13 +93,6 @@ func (instance *Instance) populateSshKey() error {
}
}

func (instance *Instance) getSshUser() string {
if instance.options.SshUser != "" {
return instance.options.SshUser
}
return "root"
}

func (instance *Instance) buildHostLists(hostsFilePath string) ([]string, error) {
hostsFile, err := os.Open(hostsFilePath)
if err != nil {
Expand All @@ -137,17 +111,6 @@ func (instance *Instance) buildHostLists(hostsFilePath string) ([]string, error)
return hostsList, scanner.Err()
}

func (instance *Instance) getSshClientConfig() *ssh.ClientConfig {
return &ssh.ClientConfig{
User: instance.getSshUser(),
Auth: []ssh.AuthMethod{
ssh.PublicKeys(instance.sshKey),
},
Timeout: time.Duration(10) * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
}

func (instance *Instance) executePayloadOnHosts(payload []byte, hostsFilePath string, routines int) error {
hostsList, err := instance.buildHostLists(hostsFilePath)
if err != nil {
Expand Down Expand Up @@ -243,17 +206,6 @@ func (instance *Instance) runPayload(wg *sync.WaitGroup, host string, payload []
}
}

func (instance *Instance) getLogDir() string {
if instance.options.LogDir != "" {
return instance.options.LogDir
}
return os.Getenv("HOME") + "/befehl/logs"
}

func (instance *Instance) getLogFilePath(host string) string {
return instance.getLogDir() + "/" + host
}

func (instance *Instance) prepareLogDir() error {
logDir := instance.getLogDir()
if !filesystem.PathExists(logDir) {
Expand Down
45 changes: 45 additions & 0 deletions getters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package befehl

import (
"os"
"time"

"golang.org/x/crypto/ssh"
)

func (instance *Instance) getSshUser() string {
if instance.options.SshUser != "" {
return instance.options.SshUser
}
return "root"
}

func (instance *Instance) getSshClientConfig() *ssh.ClientConfig {
return &ssh.ClientConfig{
User: instance.getSshUser(),
Auth: []ssh.AuthMethod{
ssh.PublicKeys(instance.sshKey),
},
Timeout: time.Duration(10) * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
}

func (instance *Instance) getLogDir() string {
if instance.options.LogDir != "" {
return instance.options.LogDir
}
return os.Getenv("HOME") + "/befehl/logs"
}

func (instance *Instance) getLogFilePath(host string) string {
return instance.getLogDir() + "/" + host
}

func (instance *Instance) getPrivKeyFile() string {
if instance.options.PrivateKeyFile != "" {
return instance.options.PrivateKeyFile
}

return os.Getenv("HOME") + "/.ssh/id_rsa"
}
16 changes: 16 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package befehl

import (
"golang.org/x/crypto/ssh"
)

type Options struct {
PrivateKeyFile string
SshUser string
LogDir string
}

type Instance struct {
options *Options
sshKey ssh.Signer
}

0 comments on commit 0b08431

Please sign in to comment.