165 lines
3.4 KiB
Go
165 lines
3.4 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent"
|
|
"github.com/pkg/errors"
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
)
|
|
|
|
type Controller struct {
|
|
previousState *agent.State
|
|
filename string
|
|
}
|
|
|
|
// Name implements node.Controller.
|
|
func (c *Controller) Name() string {
|
|
return "persistence-controller"
|
|
}
|
|
|
|
// Reconcile implements node.Controller.
|
|
func (c *Controller) Reconcile(ctx context.Context, state *agent.State) (*agent.State, error) {
|
|
defer func() {
|
|
c.previousState = state
|
|
}()
|
|
|
|
switch {
|
|
// If state did not change, return
|
|
case state == c.previousState:
|
|
return state, nil
|
|
|
|
// If first cycle, load state from file system
|
|
case c.previousState == nil:
|
|
state, err := c.loadState(ctx)
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return state, nil
|
|
}
|
|
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return state, nil
|
|
|
|
// If state has changed, save it
|
|
case state != c.previousState:
|
|
logger.Info(ctx, "saving state", logger.F("stateFile", c.filename))
|
|
|
|
if err := c.writeState(ctx, state); err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
}
|
|
|
|
return state, nil
|
|
}
|
|
|
|
func (c *Controller) loadState(ctx context.Context) (*agent.State, error) {
|
|
data, err := ioutil.ReadFile(c.filename)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
state := agent.NewState()
|
|
|
|
if err := json.Unmarshal(data, state); err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return state, nil
|
|
}
|
|
|
|
func (c *Controller) writeState(ctx context.Context, state *agent.State) error {
|
|
dir, file := filepath.Split(c.filename)
|
|
if dir == "" {
|
|
dir = "."
|
|
}
|
|
|
|
f, err := ioutil.TempFile(dir, file)
|
|
if err != nil {
|
|
return errors.Errorf("cannot create temp file: %v", err)
|
|
}
|
|
|
|
defer func() {
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
if err := os.Remove(f.Name()); err != nil {
|
|
logger.Error(ctx, "could not remove temporary file", logger.E(errors.WithStack(err)))
|
|
}
|
|
}()
|
|
|
|
defer func() {
|
|
if err := f.Close(); err != nil {
|
|
if errors.Is(err, os.ErrClosed) {
|
|
return
|
|
}
|
|
|
|
logger.Error(ctx, "could not close temporary file", logger.E(errors.WithStack(err)))
|
|
}
|
|
}()
|
|
|
|
data, err := json.Marshal(state)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
name := f.Name()
|
|
if err := ioutil.WriteFile(name, data, os.ModePerm); err != nil {
|
|
return errors.Errorf("cannot write data to temporary file %q: %v", name, err)
|
|
}
|
|
|
|
if err := f.Sync(); err != nil {
|
|
return errors.Errorf("can't flush temporary file %q: %v", name, err)
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
return errors.Errorf("can't close temporary file %q: %v", name, err)
|
|
}
|
|
|
|
// get the file mode from the original file and use that for the replacement
|
|
// file, too.
|
|
destInfo, err := os.Stat(c.filename)
|
|
|
|
switch {
|
|
case os.IsNotExist(err):
|
|
// Do nothing
|
|
|
|
case err != nil:
|
|
return errors.WithStack(err)
|
|
|
|
default:
|
|
sourceInfo, err := os.Stat(name)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if sourceInfo.Mode() != destInfo.Mode() {
|
|
if err := os.Chmod(name, destInfo.Mode()); err != nil {
|
|
return fmt.Errorf("can't set filemode on temporary file %q: %v", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := os.Rename(name, c.filename); err != nil {
|
|
return fmt.Errorf("cannot replace %q with temporary file %q: %v", c.filename, name, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func NewController(filename string) *Controller {
|
|
return &Controller{
|
|
filename: filename,
|
|
}
|
|
}
|
|
|
|
var _ agent.Controller = &Controller{}
|