In preparation for parallelism, emit all log lines prefixed by hostname
This commit is contained in:
14
src/main.go
14
src/main.go
@ -64,6 +64,8 @@ func main() {
|
|||||||
keyFile = findDefaultSSHKey()
|
keyFile = findDefaultSSHKey()
|
||||||
if keyFile == "" {
|
if keyFile == "" {
|
||||||
log.Fatal("No SSH key found and no password provided")
|
log.Fatal("No SSH key found and no password provided")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Using SSH key: %s\n", keyFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,7 +97,7 @@ func main() {
|
|||||||
totalCount := len(devicesToProcess)
|
totalCount := len(devicesToProcess)
|
||||||
|
|
||||||
for hostname, deviceConfig := range devicesToProcess {
|
for hostname, deviceConfig := range devicesToProcess {
|
||||||
fmt.Printf("\nProcessing device: %s (type: %s)\n", hostname, deviceConfig.Type)
|
fmt.Printf("\n%s: Processing device (type: %s)\n", hostname, deviceConfig.Type)
|
||||||
|
|
||||||
user := deviceConfig.User
|
user := deviceConfig.User
|
||||||
commands := deviceConfig.Commands
|
commands := deviceConfig.Commands
|
||||||
@ -111,12 +113,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if user == "" {
|
if user == "" {
|
||||||
fmt.Printf("No user specified for %s, skipping\n", hostname)
|
fmt.Printf("%s: No user specified, skipping\n", hostname)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(commands) == 0 {
|
if len(commands) == 0 {
|
||||||
fmt.Printf("No commands specified for %s, skipping\n", hostname)
|
fmt.Printf("%s: No commands specified, skipping\n", hostname)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +127,7 @@ func main() {
|
|||||||
|
|
||||||
// Connect and backup
|
// Connect and backup
|
||||||
if err := backup.Connect(); err != nil {
|
if err := backup.Connect(); err != nil {
|
||||||
fmt.Printf("Failed to connect to %s: %v\n", hostname, err)
|
fmt.Printf("%s: Failed to connect: %v\n", hostname, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,9 +135,9 @@ func main() {
|
|||||||
backup.Disconnect()
|
backup.Disconnect()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Backup failed for %s: %v\n", hostname, err)
|
fmt.Printf("%s: Backup failed: %v\n", hostname, err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Backup completed for %s\n", hostname)
|
fmt.Printf("%s: Backup completed\n", hostname)
|
||||||
successCount++
|
successCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
18
src/ssh.go
18
src/ssh.go
@ -179,7 +179,7 @@ func (rb *RouterBackup) Connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rb.client = client
|
rb.client = client
|
||||||
fmt.Printf("Successfully connected to %s\n", targetHost)
|
fmt.Printf("%s: Successfully connected to %s\n", rb.hostname, targetHost)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,11 +249,11 @@ func (rb *RouterBackup) BackupCommands(commands []string, excludePatterns []stri
|
|||||||
hasErrors := false
|
hasErrors := false
|
||||||
|
|
||||||
for i, command := range commands {
|
for i, command := range commands {
|
||||||
fmt.Printf("Running command %d/%d: %s\n", i+1, len(commands), command)
|
fmt.Printf("%s: Running command %d/%d: %s\n", rb.hostname, i+1, len(commands), command)
|
||||||
output, err := rb.RunCommand(command)
|
output, err := rb.RunCommand(command)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error executing '%s': %v\n", command, err)
|
fmt.Printf("%s: Error executing '%s': %v\n", rb.hostname, command, err)
|
||||||
hasErrors = true
|
hasErrors = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -261,7 +261,7 @@ func (rb *RouterBackup) BackupCommands(commands []string, excludePatterns []stri
|
|||||||
// Append to temporary file
|
// Append to temporary file
|
||||||
file, err := os.OpenFile(tempPath, os.O_APPEND|os.O_WRONLY, 0644)
|
file, err := os.OpenFile(tempPath, os.O_APPEND|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to open file for writing: %v\n", err)
|
fmt.Printf("%s: Failed to open file for writing: %v\n", rb.hostname, err)
|
||||||
hasErrors = true
|
hasErrors = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -274,12 +274,12 @@ func (rb *RouterBackup) BackupCommands(commands []string, excludePatterns []stri
|
|||||||
successCount++
|
successCount++
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Summary: %d/%d commands successful\n", successCount, len(commands))
|
fmt.Printf("%s: Summary: %d/%d commands successful\n", rb.hostname, successCount, len(commands))
|
||||||
|
|
||||||
if hasErrors || successCount == 0 {
|
if hasErrors || successCount == 0 {
|
||||||
// Remove .new suffix and log error
|
// Remove .new suffix and log error
|
||||||
if err := os.Remove(tempPath); err != nil {
|
if err := os.Remove(tempPath); err != nil {
|
||||||
fmt.Printf("Failed to remove temporary file %s: %v\n", tempPath, err)
|
fmt.Printf("%s: Failed to remove temporary file %s: %v\n", rb.hostname, tempPath, err)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("device backup incomplete due to command failures")
|
return fmt.Errorf("device backup incomplete due to command failures")
|
||||||
}
|
}
|
||||||
@ -289,7 +289,7 @@ func (rb *RouterBackup) BackupCommands(commands []string, excludePatterns []stri
|
|||||||
return fmt.Errorf("failed to move temporary file to final location: %v", err)
|
return fmt.Errorf("failed to move temporary file to final location: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Output saved to %s\n", finalPath)
|
fmt.Printf("%s: Output saved to %s\n", rb.hostname, finalPath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,7 +297,7 @@ func (rb *RouterBackup) BackupCommands(commands []string, excludePatterns []stri
|
|||||||
func (rb *RouterBackup) Disconnect() {
|
func (rb *RouterBackup) Disconnect() {
|
||||||
if rb.client != nil {
|
if rb.client != nil {
|
||||||
rb.client.Close()
|
rb.client.Close()
|
||||||
fmt.Printf("Disconnected from %s\n", rb.hostname)
|
fmt.Printf("%s: Disconnected\n", rb.hostname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,7 +316,7 @@ func findDefaultSSHKey() string {
|
|||||||
|
|
||||||
for _, keyPath := range defaultKeys {
|
for _, keyPath := range defaultKeys {
|
||||||
if _, err := os.Stat(keyPath); err == nil {
|
if _, err := os.Stat(keyPath); err == nil {
|
||||||
fmt.Printf("Using SSH key: %s\n", keyPath)
|
// Key discovery logging moved to main.go for hostname context
|
||||||
return keyPath
|
return keyPath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user