archive-migration: Add check for archive version and rewrite version after migration

This commit is contained in:
2025-12-04 15:08:03 +01:00
parent 7cff8bbfd2
commit 7da01975f7
2 changed files with 52 additions and 11 deletions

View File

@@ -5,9 +5,12 @@
package main
import (
"bufio"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
cclog "github.com/ClusterCockpit/cc-lib/ccLogger"
)
@@ -41,17 +44,33 @@ func main() {
cclog.Fatalf("Archive path does not exist: %s", archivePath)
}
// Check archive version
if err := checkVersion(archivePath); err != nil {
cclog.Fatalf("Version check failed: %v", err)
}
// Display warning for non-dry-run mode
if !dryRun {
cclog.Warn("WARNING: This will modify files in the archive!")
cclog.Warn("It is strongly recommended to backup your archive first.")
cclog.Warn("Run with --dry-run first to preview changes.")
cclog.Info("")
fmt.Print("Are you sure you want to continue? [y/N]: ")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
cclog.Fatalf("Error reading input: %v", err)
}
if strings.ToLower(strings.TrimSpace(input)) != "y" {
cclog.Info("Aborted by user.")
os.Exit(0)
}
}
// Run migration
migrated, failed, err := migrateArchive(archivePath, dryRun, numWorkers)
if err != nil {
cclog.Errorf("Migration completed with errors: %s", err.Error())
if failed > 0 {
@@ -62,6 +81,28 @@ func main() {
if dryRun {
cclog.Infof("Dry run completed: %d jobs would be migrated", migrated)
} else {
if err := updateVersion(archivePath); err != nil {
cclog.Errorf("Failed to update archive version: %v", err)
os.Exit(1)
}
cclog.Infof("Migration completed successfully: %d jobs migrated", migrated)
}
}
func checkVersion(archivePath string) error {
versionFile := filepath.Join(archivePath, "version.txt")
data, err := os.ReadFile(versionFile)
if err != nil {
return fmt.Errorf("failed to read version.txt: %v", err)
}
versionStr := strings.TrimSpace(string(data))
if versionStr != "2" {
return fmt.Errorf("archive version is %s, expected 2", versionStr)
}
return nil
}
func updateVersion(archivePath string) error {
versionFile := filepath.Join(archivePath, "version.txt")
return os.WriteFile(versionFile, []byte("3\n"), 0644)
}