Some small progress...

This commit is contained in:
Marcel Otte 2021-12-13 23:44:23 +01:00
parent 17ec990dc9
commit 716a3c9d4a
5 changed files with 61 additions and 24 deletions

View File

@ -8,7 +8,7 @@ before:
- go generate ./... - go generate ./...
builds: builds:
- main: ./cmd/backive/main.go - main: ./cmd/backive/main.go
id: backive id: "backive"
binary: backive binary: backive
env: env:
- CGO_ENABLED=0 - CGO_ENABLED=0
@ -19,7 +19,7 @@ builds:
# - windows # - windows
# - darwin # - darwin
- main: ./cmd/backive_udev/main.go - main: ./cmd/backive_udev/main.go
id: backive_udev id: "backive_udev"
binary: backive_udev binary: backive_udev
goarch: goarch:
- amd64 - amd64
@ -30,7 +30,7 @@ builds:
# - windows # - windows
# - darwin # - darwin
- main: ./cmd/backive_ui/main.go - main: ./cmd/backive_ui/main.go
id: backive_ui id: "backive_ui"
binary: backive_ui binary: backive_ui
env: env:
- CGO_ENABLED=0 - CGO_ENABLED=0

View File

@ -1,10 +1,11 @@
package backive package main
import ( import (
"container/list" "container/list"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net" "net"
"os" "os"
"time" "time"
@ -12,6 +13,24 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var logfile os.File
func setupLogging() {
logdir := "/var/log/backive"
logname := "/var/log/backive/backive.log"
if _, err := os.Stat(logdir); err == nil {
//ignore
} else if os.IsNotExist(err) {
os.MkdirAll(logdir, 0755)
}
logfile, err := os.OpenFile(logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Println("Error creating logfile!")
panic("no logfile no info")
}
log.SetOutput(logfile)
}
// Global variables for backive // Global variables for backive
var ( var (
database Database database Database
@ -41,11 +60,16 @@ func (d Database) Save() {
// LoadDb loads the database // LoadDb loads the database
func (d Database) Load() { func (d Database) Load() {
data, err := os.ReadFile(d.path) if _, err := os.Stat(d.path); err == nil {
if err != nil { data, rferr := os.ReadFile(d.path)
panic(err) if rferr != nil {
panic(rferr)
} }
json.Unmarshal(data, &d.data) json.Unmarshal(data, &d.data)
} else if os.IsNotExist(err) {
// no data
}
} }
// Device represents a device, with a name easy to remember and the UUID to identify it, optionally an owner. // Device represents a device, with a name easy to remember and the UUID to identify it, optionally an owner.
@ -88,7 +112,7 @@ type Configuration struct {
Settings Settings `mapstructure:"settings"` Settings Settings `mapstructure:"settings"`
Devices Devices `mapstructure:"devices"` Devices Devices `mapstructure:"devices"`
Backups Backups `mapstructure:"backups"` Backups Backups `mapstructure:"backups"`
vconfig *viper.Viper vconfig viper.Viper
} }
// Settings struct holds the global configuration items // Settings struct holds the global configuration items
@ -107,26 +131,30 @@ type Backups map[string]Backup
// CreateViper creates a viper instance for usage later // CreateViper creates a viper instance for usage later
func (c Configuration) CreateViper() { func (c Configuration) CreateViper() {
vconfig := viper.New() vconfig := viper.New()
// vconfig.Debug()
vconfig.SetConfigName("backive") vconfig.SetConfigName("backive")
vconfig.SetConfigFile("backive.yml")
//vconfig.SetConfigFile("backive.yaml")
vconfig.SetConfigType("yaml") vconfig.SetConfigType("yaml")
vconfig.AddConfigPath("/etc/backive/") // system config vconfig.AddConfigPath("/etc/backive") // system config
vconfig.AddConfigPath("$HOME/.backive/") //vconfig.AddConfigPath("$HOME/.backive/")
vconfig.AddConfigPath(".") vconfig.AddConfigPath(".")
c.vconfig = vconfig c.vconfig = *vconfig
} }
// Load loads the configuration from the disk // Load loads the configuration from the disk
func (c Configuration) Load() { func (c Configuration) Load() {
c.CreateViper() c.CreateViper()
if err := c.vconfig.ReadInConfig(); err != nil { vc := c.vconfig
if err := vc.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok { if _, ok := err.(viper.ConfigFileNotFoundError); ok {
panic(fmt.Errorf("Fatal: No config file could be found")) panic(fmt.Errorf("Fatal: No config file could be found: %w", err))
} }
panic(fmt.Errorf("Fatal error config file: %w ", err)) panic(fmt.Errorf("Fatal error config file: %w ", err))
} }
//Unmarshal all into Configuration type //Unmarshal all into Configuration type
err := c.vconfig.Unmarshal(c) err := vc.Unmarshal(c)
if err != nil { if err != nil {
fmt.Printf("Error occured when loading config: %v\n", err) fmt.Printf("Error occured when loading config: %v\n", err)
panic("No configuration available!") panic("No configuration available!")
@ -173,24 +201,25 @@ func (eh EventHandler) RegisterCallback(cb func(map[string]string)) {
func (eh EventHandler) process() { func (eh EventHandler) process() {
client, err := eh.ls.Accept() client, err := eh.ls.Accept()
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
data := make([]byte, 2048) data := make([]byte, 2048)
for { for {
buf := make([]byte, 512) buf := make([]byte, 512)
nr, err := client.Read(buf) nr, err := client.Read(buf)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
panic(err) log.Fatal(err)
} }
data = append(data, buf[0:nr]...) data = append(data, buf[0:nr]...)
if err == io.EOF { if err == io.EOF {
break break
} }
} }
log.Println(data)
env := map[string]string{} env := map[string]string{}
errjson := json.Unmarshal(data, &env) errjson := json.Unmarshal(data, &env)
if errjson != nil { if errjson != nil {
panic(errjson) log.Fatal(errjson)
} }
for _, v := range eh.callbacks { for _, v := range eh.callbacks {
v(env) v(env)
@ -296,8 +325,9 @@ func (r Runs) LastRun(b Backup) (time.Time, error) {
} }
func main() { func main() {
setupLogging()
// TODO: do proper signal handling! // TODO: do proper signal handling!
fmt.Println("backive starting up...") log.Println("backive starting up...")
// find and load config // find and load config
database.Load() database.Load()
config.Load() config.Load()
@ -312,5 +342,5 @@ func main() {
// cleanup if anything is there to cleanup // cleanup if anything is there to cleanup
database.Save() database.Save()
fmt.Println("backive shuting down.") log.Println("backive shuting down.")
} }

View File

@ -11,7 +11,14 @@ import (
// main Simple main function for the udev callback executable, registered with the udev service. // main Simple main function for the udev callback executable, registered with the udev service.
func main() { func main() {
f, err := os.OpenFile("/tmp/backive/udev.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666) udev_logdir := "/var/log/backive"
udev_logname := "/var/log/backive/udev.log"
if _, err := os.Stat(udev_logdir); err == nil {
//ignore
} else if os.IsNotExist(err) {
os.MkdirAll(udev_logdir, 0755)
}
f, err := os.OpenFile(udev_logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil { if err != nil {
fmt.Println("Error creating logfile!") fmt.Println("Error creating logfile!")
panic("no logfile no info") panic("no logfile no info")

2
go.mod
View File

@ -16,7 +16,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20210930141918-969570ce7c6c // indirect golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
golang.org/x/text v0.3.7 // indirect golang.org/x/text v0.3.7 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.63.2 // indirect gopkg.in/ini.v1 v1.63.2 // indirect

4
go.sum
View File

@ -432,8 +432,8 @@ golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210930141918-969570ce7c6c h1:ayiZ33F3u3LIXB03Y5VKNdaFO79a18Fr+SB30o/KFyw= golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac h1:oN6lz7iLW/YC7un8pq+9bOLyXrprv2+DKfkJY+2LJJw=
golang.org/x/sys v0.0.0-20210930141918-969570ce7c6c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=