cc-backend/tools/archive-manager/main.go

73 lines
2.3 KiB
Go
Raw Permalink Normal View History

2024-04-11 23:04:30 +02:00
// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
2022-09-06 09:32:25 +02:00
// All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
2023-03-31 11:25:12 +02:00
import (
"encoding/json"
"flag"
"fmt"
2023-05-15 14:32:23 +02:00
"os"
"time"
2023-03-31 11:25:12 +02:00
"github.com/ClusterCockpit/cc-backend/internal/config"
"github.com/ClusterCockpit/cc-backend/pkg/archive"
2023-04-12 12:27:15 +02:00
"github.com/ClusterCockpit/cc-backend/pkg/log"
2023-03-31 11:25:12 +02:00
)
func parseDate(in string) int64 {
const shortForm = "2006-Jan-02"
loc, _ := time.LoadLocation("Local")
if in != "" {
t, err := time.ParseInLocation(shortForm, in, loc)
if err != nil {
fmt.Printf("date parse error %v", err)
os.Exit(0)
}
return t.Unix()
}
return 0
}
2022-09-06 09:32:25 +02:00
func main() {
2023-05-15 14:32:23 +02:00
var srcPath, flagConfigFile, flagLogLevel, flagRemoveCluster, flagRemoveAfter, flagRemoveBefore string
var flagLogDateTime, flagValidate bool
2023-03-31 11:25:12 +02:00
flag.StringVar(&srcPath, "s", "./var/job-archive", "Specify the source job archive path. Default is ./var/job-archive")
2023-04-12 12:27:15 +02:00
flag.BoolVar(&flagLogDateTime, "logdate", false, "Set this flag to add date and time to log messages")
flag.StringVar(&flagLogLevel, "loglevel", "warn", "Sets the logging level: `[debug,info,warn (default),err,fatal,crit]`")
2023-03-31 11:25:12 +02:00
flag.StringVar(&flagConfigFile, "config", "./config.json", "Specify alternative path to `config.json`")
2023-05-15 14:32:23 +02:00
flag.StringVar(&flagRemoveCluster, "remove-cluster", "", "Remove cluster from archive and database")
flag.StringVar(&flagRemoveBefore, "remove-before", "", "Remove all jobs with start time before date (Format: 2006-Jan-04)")
flag.StringVar(&flagRemoveAfter, "remove-after", "", "Remove all jobs with start time after date (Format: 2006-Jan-04)")
2023-05-15 14:32:23 +02:00
flag.BoolVar(&flagValidate, "validate", false, "Set this flag to validate a job archive against the json schema")
2023-03-31 11:25:12 +02:00
flag.Parse()
2023-05-15 14:32:23 +02:00
2023-03-31 11:25:12 +02:00
archiveCfg := fmt.Sprintf("{\"kind\": \"file\",\"path\": \"%s\"}", srcPath)
2023-04-12 12:27:15 +02:00
log.Init(flagLogLevel, flagLogDateTime)
2023-03-31 11:25:12 +02:00
config.Init(flagConfigFile)
2023-04-11 16:26:09 +02:00
if err := archive.Init(json.RawMessage(archiveCfg), false); err != nil {
2023-03-31 11:25:12 +02:00
log.Fatal(err)
}
ar := archive.GetHandle()
2022-09-06 09:32:25 +02:00
2023-05-15 14:32:23 +02:00
if flagValidate {
config.Keys.Validate = true
for job := range ar.Iter(true) {
log.Printf("Validate %s - %d\n", job.Meta.Cluster, job.Meta.JobID)
}
os.Exit(0)
2023-03-31 11:25:12 +02:00
}
2023-05-15 14:32:23 +02:00
if flagRemoveBefore != "" || flagRemoveAfter != "" {
ar.Clean(parseDate(flagRemoveBefore), parseDate(flagRemoveAfter))
os.Exit(0)
}
2023-05-15 14:32:23 +02:00
ar.Info()
2022-09-06 09:32:25 +02:00
}