Add version to job archive

This commit is contained in:
Jan Eitzinger 2023-03-27 13:24:06 +02:00
parent ff8f013b56
commit 8447d011ad
3 changed files with 37 additions and 7 deletions

View File

@ -8,12 +8,15 @@ import (
"encoding/json"
"fmt"
"github.com/ClusterCockpit/cc-backend/pkg/log"
"github.com/ClusterCockpit/cc-backend/pkg/lrucache"
"github.com/ClusterCockpit/cc-backend/pkg/schema"
)
const Version = 1
type ArchiveBackend interface {
Init(rawConfig json.RawMessage) error
Init(rawConfig json.RawMessage) (int, error)
LoadJobMeta(job *schema.Job) (*schema.JobMeta, error)
@ -50,9 +53,11 @@ func Init(rawConfig json.RawMessage) error {
return fmt.Errorf("unkown archive backend '%s''", kind.Kind)
}
if err := ar.Init(rawConfig); err != nil {
version, err := ar.Init(rawConfig)
if err != nil {
return err
}
log.Infof("Load archive version %d", version)
return initClusterConfig()
}

View File

@ -61,31 +61,47 @@ func loadJobMeta(filename string) (*schema.JobMeta, error) {
return DecodeJobMeta(bufio.NewReader(f))
}
func (fsa *FsArchive) Init(rawConfig json.RawMessage) error {
func (fsa *FsArchive) Init(rawConfig json.RawMessage) (int, error) {
var config FsArchiveConfig
if err := json.Unmarshal(rawConfig, &config); err != nil {
log.Errorf("fsBackend Init()- %v", err)
return err
return 0, err
}
if config.Path == "" {
err := fmt.Errorf("fsBackend Init()- empty path")
log.Errorf("fsBackend Init()- %v", err)
return err
return 0, err
}
fsa.path = config.Path
b, err := os.ReadFile(fmt.Sprintf("%s/version.txt", fsa.path))
if err != nil {
fmt.Println("Err")
return 0, err
}
version, err := strconv.Atoi(string(b))
if err != nil {
log.Errorf("fsBackend Init()- %v", err)
return 0, err
}
if version != Version {
return version, fmt.Errorf("Unsupported version %d, need %d", version, Version)
}
entries, err := os.ReadDir(fsa.path)
if err != nil {
log.Errorf("fsBackend Init()- %v", err)
return err
return 0, err
}
for _, de := range entries {
fsa.clusters = append(fsa.clusters, de.Name())
}
return nil
return version, nil
}
func (fsa *FsArchive) LoadJobData(job *schema.Job) (schema.JobData, error) {

View File

@ -7,6 +7,7 @@ package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
@ -17,6 +18,8 @@ import (
"github.com/ClusterCockpit/cc-backend/pkg/units"
)
const Version = 1
var ar FsArchive
func loadJobData(filename string) (*JobData, error) {
@ -169,6 +172,10 @@ func main() {
flag.StringVar(&srcPath, "s", "./var/job-archive", "Specify the source job archive path. Default is ./var/job-archive")
flag.StringVar(&dstPath, "d", "./var/job-archive-new", "Specify the destination job archive path. Default is ./var/job-archive-new")
if _, err := os.Stat(fmt.Sprintf("%s/version.txt", srcPath)); !errors.Is(err, os.ErrNotExist) {
log.Fatal("Archive version exists!")
}
srcConfig := fmt.Sprintf("{\"path\": \"%s\"}", srcPath)
err := ar.Init(json.RawMessage(srcConfig))
if err != nil {
@ -248,4 +255,6 @@ func main() {
}
}()
}
os.WriteFile(fmt.Sprintf("%s/version.txt", dstPath), []byte(fmt.Sprintf("%d", Version)), 0644)
}