allow offline box

This commit is contained in:
shoopea 2022-04-16 20:57:45 +08:00
parent 762c538607
commit fd62711571
4 changed files with 196 additions and 109 deletions

2
app.go
View File

@ -540,6 +540,7 @@ func (a AppConfig) SendSnapshots() (err error) {
for _, src := range a.Sources { for _, src := range a.Sources {
for _, dest := range a.Destinations { for _, dest := range a.Destinations {
if cfg.Box[dest.Box()].online {
if *debugFlag { if *debugFlag {
log.Printf("AppConfig.SendSnapshots : %s : Sending snapshots from %s to %s", a.Name, string(src), string(dest)) log.Printf("AppConfig.SendSnapshots : %s : Sending snapshots from %s to %s", a.Name, string(src), string(dest))
} }
@ -642,6 +643,7 @@ func (a AppConfig) SendSnapshots() (err error) {
} }
} }
} }
}
err = nil err = nil
return return
} }

74
box.go
View File

@ -15,6 +15,7 @@ type Box struct {
Name string `json:"-"` Name string `json:"-"`
ssh *SSHConfig ssh *SSHConfig
zfs *ZFSConfig zfs *ZFSConfig
online bool
} }
func (b *Box) ZFSTakeSnapshot(schedule, path string) (err error) { func (b *Box) ZFSTakeSnapshot(schedule, path string) (err error) {
@ -22,6 +23,11 @@ func (b *Box) ZFSTakeSnapshot(schedule, path string) (err error) {
log.Printf("Box.ZFSTakeSnapshot : %s : Taking snapshot on %s for %s", b.Name, path, schedule) log.Printf("Box.ZFSTakeSnapshot : %s : Taking snapshot on %s for %s", b.Name, path, schedule)
} }
if !b.online {
err = fmt.Errorf("box offline")
return
}
err = b.SnapshotInitialize() err = b.SnapshotInitialize()
if err != nil { if err != nil {
return return
@ -49,9 +55,14 @@ func (b *Box) ZFSGetLastSnapshot(path string) (last Snapshot, err error) {
log.Printf("Box.ZFSGetLastSnapshot : %s : Start %s (%d snapshots)", b.Name, path, len(b.zfs.SnapshotList)) log.Printf("Box.ZFSGetLastSnapshot : %s : Start %s (%d snapshots)", b.Name, path, len(b.zfs.SnapshotList))
} }
if !b.online {
err = fmt.Errorf("box offline")
return
}
err = b.SnapshotInitialize() err = b.SnapshotInitialize()
if err != nil { if err != nil {
return return last, err
} }
b.zfs.M.Lock() b.zfs.M.Lock()
@ -73,6 +84,11 @@ func (b *Box) ZFSIsLastSnapshot(src Snapshot) (is bool, err error) {
log.Printf("Box.ZFSIsLastSnapshot : %s : Start %s", b.Name, string(src)) log.Printf("Box.ZFSIsLastSnapshot : %s : Start %s", b.Name, string(src))
} }
if !b.online {
err = fmt.Errorf("box offline")
return
}
err = b.SnapshotInitialize() err = b.SnapshotInitialize()
if err != nil { if err != nil {
return return
@ -95,6 +111,11 @@ func (b *Box) ZFSGetFirstSnapshot(path string) (first Snapshot, err error) {
log.Printf("Box.ZFSGetFirstSnapshot : %s : Start %s", b.Name, path) log.Printf("Box.ZFSGetFirstSnapshot : %s : Start %s", b.Name, path)
} }
if !b.online {
err = fmt.Errorf("box offline")
return
}
err = b.SnapshotInitialize() err = b.SnapshotInitialize()
if err != nil { if err != nil {
return return
@ -118,6 +139,11 @@ func (b *Box) ZFSGetNextSnapshot(src Snapshot) (next Snapshot, err error) {
log.Printf("Box.ZFSGetNextSnapshot : %s : Start %s", b.Name, string(src)) log.Printf("Box.ZFSGetNextSnapshot : %s : Start %s", b.Name, string(src))
} }
if !b.online {
err = fmt.Errorf("box offline")
return
}
err = b.SnapshotInitialize() err = b.SnapshotInitialize()
if err != nil { if err != nil {
return return
@ -151,6 +177,11 @@ func (b *Box) ZFSUpdateSnapshotList() (err error) {
log.Printf("Box.ZFSUpdateSnapshotList : %s : Start", b.Name) log.Printf("Box.ZFSUpdateSnapshotList : %s : Start", b.Name)
} }
if !b.online {
err = fmt.Errorf("box offline")
return
}
b.zfs.M.Lock() b.zfs.M.Lock()
if b.zfs.SnapshotDeleted || b.zfs.SnapshotAdded { if b.zfs.SnapshotDeleted || b.zfs.SnapshotAdded {
b.zfs.SnapshotInitialized = false b.zfs.SnapshotInitialized = false
@ -166,6 +197,11 @@ func (b *Box) ZFSGetSnapshotList() (snaps []Snapshot, err error) {
log.Printf("Box.ZFSGetSnapshotList : %s : Start", b.Name) log.Printf("Box.ZFSGetSnapshotList : %s : Start", b.Name)
} }
if !b.online {
err = fmt.Errorf("box offline")
return
}
err = b.SnapshotInitialize() err = b.SnapshotInitialize()
if err != nil { if err != nil {
return return
@ -179,6 +215,15 @@ func (b *Box) ZFSGetSnapshotList() (snaps []Snapshot, err error) {
} }
func (b *Box) SnapshotInitialize() (err error) { func (b *Box) SnapshotInitialize() (err error) {
if *debugFlag {
log.Printf("Box.SnapshotInitialize : %s : Start", b.Name)
}
if !b.online {
err = fmt.Errorf("box offline")
return
}
b.zfs.M.Lock() b.zfs.M.Lock()
defer b.zfs.M.Unlock() defer b.zfs.M.Unlock()
@ -227,6 +272,11 @@ func (b *Box) ZFSUpdateList() (err error) {
log.Printf("Box.ZFSUpdateList : %s : Start", b.Name) log.Printf("Box.ZFSUpdateList : %s : Start", b.Name)
} }
if !b.online {
err = fmt.Errorf("box offline")
return
}
b.zfs.M.Lock() b.zfs.M.Lock()
if b.zfs.ZFSDeleted || b.zfs.ZFSAdded { if b.zfs.ZFSDeleted || b.zfs.ZFSAdded {
b.zfs.ZFSInitialized = false b.zfs.ZFSInitialized = false
@ -242,6 +292,10 @@ func (b *Box) ZFSIsZFS(path string) bool {
log.Printf("Box.ZFSIsZFS : %s : Start %s", b.Name, path) log.Printf("Box.ZFSIsZFS : %s : Start %s", b.Name, path)
} }
if !b.online {
return false
}
err := b.ZFSInitialize() err := b.ZFSInitialize()
if err != nil { if err != nil {
return false return false
@ -262,6 +316,11 @@ func (b *Box) ZFSCreateZFS(path string) (err error) {
log.Printf("Box.ZFSCreateZFS : %s : Start %s", b.Name, path) log.Printf("Box.ZFSCreateZFS : %s : Start %s", b.Name, path)
} }
if !b.online {
err = fmt.Errorf("box offline")
return
}
err = b.ZFSInitialize() err = b.ZFSInitialize()
if err != nil { if err != nil {
return return
@ -303,6 +362,14 @@ func (b *Box) ZFSCreateZFS(path string) (err error) {
func (b *Box) ZFSInitialize() (err error) { func (b *Box) ZFSInitialize() (err error) {
b.zfs.M.Lock() b.zfs.M.Lock()
defer b.zfs.M.Unlock() defer b.zfs.M.Unlock()
if *debugFlag {
log.Printf("Box.ZFSInitialize : %s : Start", b.Name)
}
if !b.online {
err = fmt.Errorf("box offline")
return
}
if b.zfs.ZFSInitialized { if b.zfs.ZFSInitialized {
return nil return nil
@ -341,6 +408,11 @@ func (b *Box) ZFSInitialize() (err error) {
} }
func (b *Box) SSHExec(cmd string) (buf *bytes.Buffer, err error) { func (b *Box) SSHExec(cmd string) (buf *bytes.Buffer, err error) {
if !b.online {
err = fmt.Errorf("box offline")
return
}
buf, err = b.ssh.exec(cmd) buf, err = b.ssh.exec(cmd)
return return
} }

View File

@ -53,6 +53,7 @@ func (c *Config) Load() error {
for k, v := range c.Box { for k, v := range c.Box {
v.Name = k v.Name = k
v.online = false
v.zfs = NewZFSConfig() v.zfs = NewZFSConfig()
s := &SSHConfig{ s := &SSHConfig{
logged: false, logged: false,
@ -97,8 +98,8 @@ func (c *Config) Load() error {
log.Printf("Config.Load : ssh.Dial(%s) : %s", k, err) log.Printf("Config.Load : ssh.Dial(%s) : %s", k, err)
} }
return err return err
} } else {
v.online = true
session, err := s.client.NewSession() session, err := s.client.NewSession()
if err != nil { if err != nil {
if *debugFlag { if *debugFlag {
@ -123,6 +124,7 @@ func (c *Config) Load() error {
session.Close() session.Close()
s.logged = true s.logged = true
} }
}
for _, app := range c.Apps { for _, app := range c.Apps {
for _, src := range app.Sources { for _, src := range app.Sources {
@ -132,7 +134,11 @@ func (c *Config) Load() error {
if _, ok := cfg.Box[src.Box()]; !ok { if _, ok := cfg.Box[src.Box()]; !ok {
return fmt.Errorf("No box defined for source : %s", string(src)) return fmt.Errorf("No box defined for source : %s", string(src))
} }
if !cfg.Box[src.Box()].online {
return fmt.Errorf("Source box offline for app : %s", app.Name)
} }
}
var allOffline bool = true
for _, dest := range app.Destinations { for _, dest := range app.Destinations {
if !dest.Valid() { if !dest.Valid() {
return fmt.Errorf("Destination not valid : %s", string(dest)) return fmt.Errorf("Destination not valid : %s", string(dest))
@ -140,7 +146,14 @@ func (c *Config) Load() error {
if _, ok := cfg.Box[dest.Box()]; !ok { if _, ok := cfg.Box[dest.Box()]; !ok {
return fmt.Errorf("No box defined for destination : %s", string(dest)) return fmt.Errorf("No box defined for destination : %s", string(dest))
} }
if cfg.Box[dest.Box()].online {
allOffline = false
} }
}
if allOffline {
return fmt.Errorf("No online destination box for app : %s", app.Name)
}
for val, before := range app.Before { for val, before := range app.Before {
_, err = regexp.Compile(val) _, err = regexp.Compile(val)
if err != nil { if err != nil {

View File

@ -1,6 +1,6 @@
// Code generated by version.sh (@generated) DO NOT EDIT. // Code generated by version.sh (@generated) DO NOT EDIT.
package main package main
var githash = "0083ab6" var githash = "762c538"
var buildstamp = "2022-04-16_07:16:27" var buildstamp = "2022-04-16_12:55:05"
var commits = "28" var commits = "29"
var version = "0083ab6-b28 - 2022-04-16_07:16:27" var version = "762c538-b29 - 2022-04-16_12:55:05"