more log() cleanups

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-01-05 20:52:42 -06:00
parent cfd827bbdd
commit 096a4b17d1
3 changed files with 64 additions and 53 deletions

16
args.go
View File

@ -7,6 +7,8 @@ import (
) )
var INFO log.LogFlag var INFO log.LogFlag
var RUN log.LogFlag
var SSH log.LogFlag
func init() { func init() {
INFO.B = false INFO.B = false
@ -15,4 +17,18 @@ func init() {
INFO.Short = "shell" INFO.Short = "shell"
INFO.Desc = "general info" INFO.Desc = "general info"
INFO.Register() INFO.Register()
RUN.B = false
RUN.Name = "RUN"
RUN.Subsystem = "shell"
RUN.Short = "shell"
RUN.Desc = "Run() info"
RUN.Register()
SSH.B = false
SSH.Name = "SSH"
SSH.Subsystem = "shell"
SSH.Short = "shell"
SSH.Desc = "ssh() info"
SSH.Register()
} }

42
run.go
View File

@ -1,19 +1,19 @@
package shell package shell
import "strings" import (
import "time" "strings"
import "os/exec" "time"
import "bytes" "os/exec"
import "io" "bytes"
import "fmt" "io"
import "os" "fmt"
import "bufio" "os"
"bufio"
import "github.com/svent/go-nbreader" "github.com/svent/go-nbreader"
// import "github.com/davecgh/go-spew/spew"
import "log" "go.wit.com/log"
// import "go.wit.com/log" )
var msecDelay int = 20 // check every 20 milliseconds var msecDelay int = 20 // check every 20 milliseconds
@ -39,7 +39,7 @@ func (cmd *Shell) Run(cmdline string) string {
} }
func (cmd *Shell) InitProcess(cmdline string) { func (cmd *Shell) InitProcess(cmdline string) {
log.Println("shell.InitProcess() START " + cmdline) log.Log(RUN, "shell.InitProcess() START " + cmdline)
cmd.Cmdline = Chomp(cmdline) // this is like 'chomp' in perl cmd.Cmdline = Chomp(cmdline) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd.Cmdline) cmdArgs := strings.Fields(cmd.Cmdline)
@ -50,7 +50,7 @@ func (cmd *Shell) InitProcess(cmdline string) {
} }
if (cmdArgs[0] == "cd") { if (cmdArgs[0] == "cd") {
if (len(cmdArgs) > 1) { if (len(cmdArgs) > 1) {
log.Println("os.Chdir()", cmd) log.Log(RUN, "os.Chdir()", cmd)
os.Chdir(cmdArgs[1]) os.Chdir(cmdArgs[1])
} }
handleError(nil, 0) handleError(nil, 0)
@ -85,7 +85,7 @@ func (cmd *Shell) FileCreate(out string) {
// NOTE: this might cause problems: // NOTE: this might cause problems:
// always remove the newlines at the end ? // always remove the newlines at the end ?
func (cmd *Shell) Exec(cmdline string) { func (cmd *Shell) Exec(cmdline string) {
log.Println("shell.Run() START " + cmdline) log.Log(RUN, "shell.Run() START " + cmdline)
cmd.InitProcess(cmdline) cmd.InitProcess(cmdline)
if (cmd.Error != nil) { if (cmd.Error != nil) {
@ -114,25 +114,25 @@ func (cmd *Shell) Exec(cmdline string) {
if (err != nil) { if (err != nil) {
cmd.Fail = true cmd.Fail = true
cmd.Error = err cmd.Error = err
log.Println("process.Wait() END err =", err.Error()) log.Log(RUN, "process.Wait() END err =", err.Error())
} else { } else {
log.Println("process.Wait() END") log.Log(RUN, "process.Wait() END")
} }
return return
} }
// nonblocking read until file errors // nonblocking read until file errors
func (cmd *Shell) Capture(f *File) { func (cmd *Shell) Capture(f *File) {
// log.Debugln("nbrREADER() START") log.Log(RUN, "nbrREADER() START")
if (cmd.Buffer == nil) { if (cmd.Buffer == nil) {
cmd.Buffer = new(bytes.Buffer) cmd.Buffer = new(bytes.Buffer)
} }
if (cmd.Buffer == nil) { if (cmd.Buffer == nil) {
// log.Debugln("f.Buffer == nil")
// log.Debugln("SHOULD DIE HERE")
f.Dead = false f.Dead = false
cmd.Error = fmt.Errorf("could not make buffer") cmd.Error = fmt.Errorf("could not make buffer")
log.Error(cmd.Error, "f.Buffer == nil")
log.Error(cmd.Error, "SHOULD DIE HERE")
cmd.Done = true cmd.Done = true
} }
@ -155,7 +155,7 @@ func (cmd *Shell) Capture(f *File) {
// returns true if filehandle buffer is empty // returns true if filehandle buffer is empty
func (cmd *Shell) ReadToBuffer(f *File) bool { func (cmd *Shell) ReadToBuffer(f *File) bool {
// log.Debugln("ReadToBuffer() START") log.Log(RUN, "ReadToBuffer() START")
nbr := f.Fnbreader nbr := f.Fnbreader
oneByte := make([]byte, 1024) oneByte := make([]byte, 1024)
if (nbr == nil) { if (nbr == nil) {

59
ssh.go
View File

@ -1,19 +1,14 @@
package shell package shell
import "log" import (
import "fmt" "fmt"
import "io/ioutil" "io/ioutil"
import "time" "time"
// import "strings" "golang.org/x/crypto/ssh"
// import "path/filepath" "github.com/tmc/scp"
// import "os" "go.wit.com/log"
// import "bufio" )
// import "os/user"
// import "runtime"
import "golang.org/x/crypto/ssh"
import "github.com/tmc/scp"
var sshHostname string var sshHostname string
var sshPort int var sshPort int
@ -30,32 +25,32 @@ func SSHclientSet(hostname string, port int, username string, pass string, keyfi
} }
func SSHclientSCP(localfile string, remotefile string) { func SSHclientSCP(localfile string, remotefile string) {
log.Println("shell.SSHclientSCP() START") log.Log(SSH, "shell.SSHclientSCP() START")
log.Println("shell.SSHclientSCP() \tlocalfile =", localfile) log.Log(SSH, "shell.SSHclientSCP() \tlocalfile =", localfile)
log.Println("shell.SSHclientSCP() \tremotefile =", remotefile) log.Log(SSH, "shell.SSHclientSCP() \tremotefile =", remotefile)
sess := SSH(sshHostname, sshPort, sshUsername, sshPassword, sshKeyfile) sess := mySsh(sshHostname, sshPort, sshUsername, sshPassword, sshKeyfile)
err := scp.CopyPath(localfile, remotefile, sess) err := scp.CopyPath(localfile, remotefile, sess)
sess.Close() sess.Close()
log.Println("shell.SSHclientSCP() \tscp.CopyPath() err =", err) log.Log(SSH, "shell.SSHclientSCP() \tscp.CopyPath() err =", err)
log.Println("shell.SSHclientSCP() END") log.Log(SSH, "shell.SSHclientSCP() END")
} }
func SSHclientRun(cmd string) { func SSHclientRun(cmd string) {
log.Println("shell.SSHclientRun() START cmd =", cmd) log.Log(SSH, "shell.SSHclientRun() START cmd =", cmd)
sess := SSH(sshHostname, sshPort, sshUsername, sshPassword, sshKeyfile) sess := mySsh(sshHostname, sshPort, sshUsername, sshPassword, sshKeyfile)
err := sess.Run(cmd) err := sess.Run(cmd)
sess.Close() sess.Close()
log.Println("shell.SSHclientRun() END err =", err) log.Log(SSH, "shell.SSHclientRun() END err =", err)
} }
func SSH(hostname string, port int, username string, pass string, keyfile string) *ssh.Session { func mySsh(hostname string, port int, username string, pass string, keyfile string) *ssh.Session {
// get host public key // get host public key
// hostKey := getHostKey(host) // hostKey := getHostKey(host)
// log.Println("hostkey =", hostKey) // log.Log(SSH, "hostkey =", hostKey)
publicKey, err := PublicKeyFile(keyfile) publicKey, err := PublicKeyFile(keyfile)
if (err != nil) { if (err != nil) {
log.Println("PublicKeyFile() error =", err) log.Log(SSH, "PublicKeyFile() error =", err)
} }
// ssh client config // ssh client config
@ -87,14 +82,14 @@ func SSH(hostname string, port int, username string, pass string, keyfile string
// connect // connect
client, err := ssh.Dial("tcp", hostname+":"+sport, &config) client, err := ssh.Dial("tcp", hostname+":"+sport, &config)
if err != nil { if err != nil {
log.Fatal(err) log.Error(err)
} }
// defer client.Close() // defer client.Close()
// start session // start session
sess, err := client.NewSession() sess, err := client.NewSession()
if err != nil { if err != nil {
log.Fatal(err) log.Error(err)
} }
// defer sess.Close() // defer sess.Close()
@ -103,12 +98,12 @@ func SSH(hostname string, port int, username string, pass string, keyfile string
func Scp(sess *ssh.Session, localfile string, remotefile string) { func Scp(sess *ssh.Session, localfile string, remotefile string) {
err := scp.CopyPath(localfile, remotefile, sess) err := scp.CopyPath(localfile, remotefile, sess)
log.Println("scp.CopyPath() err =", err) log.Log(SSH, "scp.CopyPath() err =", err)
} }
func PublicKeyFile(file string) (ssh.AuthMethod, error) { func PublicKeyFile(file string) (ssh.AuthMethod, error) {
buffer, err := ioutil.ReadFile(file) buffer, err := ioutil.ReadFile(file)
log.Println("buffer =", string(buffer)) log.Log(SSH, "buffer =", string(buffer))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -150,10 +145,10 @@ func getHostKey(host string) ssh.PublicKey {
// 9enFJdMhb8eHN/6qfHSU/jww2Mo=|pcsWQCvAyve9QXBhjL+w/LhkcHU= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMQx8BJXxD+vk3wyjy7Irzw4FA6xxJvqUP7Hb+Z+ygpOuidYj9G8x6gHEXFUnABn5YirePrWh5tNsk4Rqs48VwU= // 9enFJdMhb8eHN/6qfHSU/jww2Mo=|pcsWQCvAyve9QXBhjL+w/LhkcHU= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMQx8BJXxD+vk3wyjy7Irzw4FA6xxJvqUP7Hb+Z+ygpOuidYj9G8x6gHEXFUnABn5YirePrWh5tNsk4Rqs48VwU=
hostKey, _, _, _, err = ssh.ParseAuthorizedKey([]byte("9enFJdMhb8eHN/6qfHSU/jww2Mo=|pcsWQCvAyve9QXBhjL+w/LhkcHU= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMQx8BJXxD+vk3wyjy7Irzw4FA6xxJvqUP7Hb+Z+ygpOuidYj9G8x6gHEXFUnABn5YirePrWh5tNsk4Rqs48VwU=")) hostKey, _, _, _, err = ssh.ParseAuthorizedKey([]byte("9enFJdMhb8eHN/6qfHSU/jww2Mo=|pcsWQCvAyve9QXBhjL+w/LhkcHU= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMQx8BJXxD+vk3wyjy7Irzw4FA6xxJvqUP7Hb+Z+ygpOuidYj9G8x6gHEXFUnABn5YirePrWh5tNsk4Rqs48VwU="))
log.Println("hostkey err =", err) log.Log(SSH, "hostkey err =", err)
log.Println("hostkey =", hostKey) log.Log(SSH, "hostkey =", hostKey)
if hostKey == nil { if hostKey == nil {
log.Println("no hostkey found err =", err) log.Log(SSH, "no hostkey found err =", err)
log.Fatalf("no hostkey found for %s", host) log.Fatalf("no hostkey found for %s", host)
} }