mirror of
https://github.com/samuelncui/yatm.git
synced 2026-09-09 01:26:04 +00:00
feat: add export library
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"github.com/rifflock/lfshook"
|
||||
"github.com/samuelncui/yatm/config"
|
||||
"github.com/samuelncui/yatm/entity"
|
||||
"github.com/samuelncui/yatm/library"
|
||||
"github.com/samuelncui/yatm/resource"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
configOpt = flag.String("config", "./config.yaml", "config file path")
|
||||
typesOpt = flag.String("types", "file,tape,position", "types wants to be exported")
|
||||
outputOpt = flag.String("output", "stdout", "output file path, default use stdout")
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
logWriter, err := rotatelogs.New(
|
||||
"./run.log.%Y%m%d%H%M",
|
||||
rotatelogs.WithLinkName("./run.log"),
|
||||
rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
|
||||
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logrus.AddHook(lfshook.NewHook(
|
||||
lfshook.WriterMap{
|
||||
logrus.InfoLevel: logWriter,
|
||||
logrus.ErrorLevel: logWriter,
|
||||
},
|
||||
&logrus.TextFormatter{},
|
||||
))
|
||||
|
||||
flag.Parse()
|
||||
conf := config.GetConfig(*configOpt)
|
||||
|
||||
db, err := resource.NewDBConn(conf.Database.Dialect, conf.Database.DSN)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
lib := library.New(db)
|
||||
if err := lib.AutoMigrate(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
parts := strings.Split(*typesOpt, ",")
|
||||
|
||||
toEnum := entity.ToEnum(entity.LibraryEntityType_value, entity.LibraryEntityType_NONE)
|
||||
types := make([]entity.LibraryEntityType, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
e := toEnum(strings.ToUpper(strings.TrimSpace(part)))
|
||||
if e == entity.LibraryEntityType_NONE {
|
||||
continue
|
||||
}
|
||||
|
||||
types = append(types, e)
|
||||
}
|
||||
if len(types) == 0 {
|
||||
panic(fmt.Errorf("cannot found types, use 'types' option to specify at least one type"))
|
||||
}
|
||||
|
||||
jsonBuf, err := lib.Export(ctx, types)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("export json fail, %w", err))
|
||||
}
|
||||
|
||||
f := func() io.WriteCloser {
|
||||
if *outputOpt == "stdout" {
|
||||
return os.Stdout
|
||||
}
|
||||
|
||||
f, err := os.Create(*outputOpt)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("open output file fail, path= '%s', %w", *outputOpt, err))
|
||||
}
|
||||
return f
|
||||
}()
|
||||
|
||||
defer f.Close()
|
||||
if _, err := f.Write(jsonBuf); err != nil {
|
||||
panic(fmt.Errorf("write output file fail, %w", err))
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
# domain used by user interface
|
||||
domain: http://127.0.0.1:8080
|
||||
# http service binding
|
||||
listen: 127.0.0.1:8080
|
||||
# debug port binding
|
||||
debug_listen: 127.0.0.1:8081
|
||||
|
||||
# database settings, you can use sqlite or mysql (untested)
|
||||
database:
|
||||
dialect: sqlite
|
||||
dsn: ./tapes.db
|
||||
|
||||
# usable tape drivers, must have sg device mapping.
|
||||
# use `sg_map` command (in sg3_utils package) to get corresponding device with sg device mapping.
|
||||
tape_devices:
|
||||
- /dev/tape/by-id/scsi-HUJ0000000
|
||||
|
||||
# working path settings
|
||||
paths:
|
||||
# to save logs and write reports
|
||||
work: ./
|
||||
# source in backup job creator
|
||||
source: ./
|
||||
# restore job target path
|
||||
target: ./
|
||||
|
||||
# those scripts interact with external software, you may need to change them for your use cases.
|
||||
scripts:
|
||||
# enable hardware encryption on tape driver
|
||||
encrypt: ./scripts/encrypt
|
||||
# format ltfs on lto tape, may need to be changed for different ltfs software.
|
||||
mkfs: ./scripts/mkfs
|
||||
# mount ltfs to specified path, may need to be changed for different ltfs software.
|
||||
mount: ./scripts/mount
|
||||
# umount ltfs path
|
||||
umount: ./scripts/umount
|
||||
# read tape barcode for restore jobs
|
||||
read_info: ./scripts/readinfo
|
||||
+3
-27
@@ -16,6 +16,7 @@ import (
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"github.com/rifflock/lfshook"
|
||||
"github.com/samuelncui/yatm/apis"
|
||||
"github.com/samuelncui/yatm/config"
|
||||
"github.com/samuelncui/yatm/entity"
|
||||
"github.com/samuelncui/yatm/executor"
|
||||
"github.com/samuelncui/yatm/library"
|
||||
@@ -25,26 +26,10 @@ import (
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
Domain string `yaml:"domain"`
|
||||
Listen string `yaml:"listen"`
|
||||
DebugListen string `yaml:"debug_listen"`
|
||||
|
||||
Database struct {
|
||||
Dialect string `yaml:"dialect"`
|
||||
DSN string `yaml:"dsn"`
|
||||
} `yaml:"database"`
|
||||
|
||||
Paths executor.Paths `yaml:"paths"`
|
||||
TapeDevices []string `yaml:"tape_devices"`
|
||||
Scripts executor.Scripts `yaml:"scripts"`
|
||||
}
|
||||
|
||||
var (
|
||||
configPath = flag.String("config", "./config.yaml", "config file path")
|
||||
configOpt = flag.String("config", "./config.yaml", "config file path")
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -66,16 +51,7 @@ func main() {
|
||||
))
|
||||
|
||||
flag.Parse()
|
||||
cf, err := os.Open(*configPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
conf := new(config)
|
||||
if err := yaml.NewDecoder(cf).Decode(conf); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logrus.Infof("read config success, conf= '%+v'", conf)
|
||||
conf := config.GetConfig(*configOpt)
|
||||
|
||||
if conf.DebugListen != "" {
|
||||
go tools.Wrap(context.Background(), func() { tools.NewDebugServer(conf.DebugListen) })
|
||||
|
||||
Reference in New Issue
Block a user