In preparation for parallelism, emit all log lines prefixed by hostname

This commit is contained in:
Pim van Pelt
2025-07-07 00:51:47 +02:00
parent c8df809c29
commit 90f5ec4e26
2 changed files with 17 additions and 15 deletions

View File

@ -64,6 +64,8 @@ func main() {
keyFile = findDefaultSSHKey()
if keyFile == "" {
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)
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
commands := deviceConfig.Commands
@ -111,12 +113,12 @@ func main() {
}
if user == "" {
fmt.Printf("No user specified for %s, skipping\n", hostname)
fmt.Printf("%s: No user specified, skipping\n", hostname)
continue
}
if len(commands) == 0 {
fmt.Printf("No commands specified for %s, skipping\n", hostname)
fmt.Printf("%s: No commands specified, skipping\n", hostname)
continue
}
@ -125,7 +127,7 @@ func main() {
// Connect and backup
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
}
@ -133,9 +135,9 @@ func main() {
backup.Disconnect()
if err != nil {
fmt.Printf("Backup failed for %s: %v\n", hostname, err)
fmt.Printf("%s: Backup failed: %v\n", hostname, err)
} else {
fmt.Printf("Backup completed for %s\n", hostname)
fmt.Printf("%s: Backup completed\n", hostname)
successCount++
}
}

View File

@ -179,7 +179,7 @@ func (rb *RouterBackup) Connect() error {
}
rb.client = client
fmt.Printf("Successfully connected to %s\n", targetHost)
fmt.Printf("%s: Successfully connected to %s\n", rb.hostname, targetHost)
return nil
}
@ -249,11 +249,11 @@ func (rb *RouterBackup) BackupCommands(commands []string, excludePatterns []stri
hasErrors := false
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)
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
continue
}
@ -261,7 +261,7 @@ func (rb *RouterBackup) BackupCommands(commands []string, excludePatterns []stri
// Append to temporary file
file, err := os.OpenFile(tempPath, os.O_APPEND|os.O_WRONLY, 0644)
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
continue
}
@ -274,12 +274,12 @@ func (rb *RouterBackup) BackupCommands(commands []string, excludePatterns []stri
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 {
// Remove .new suffix and log error
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")
}
@ -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)
}
fmt.Printf("Output saved to %s\n", finalPath)
fmt.Printf("%s: Output saved to %s\n", rb.hostname, finalPath)
return nil
}
@ -297,7 +297,7 @@ func (rb *RouterBackup) BackupCommands(commands []string, excludePatterns []stri
func (rb *RouterBackup) Disconnect() {
if rb.client != nil {
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 {
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
}
}