mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2025-12-16 12:16:16 +01:00
archive-migration: Add check for archive version and rewrite version after migration
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user