2022-04-11 04:35:17 -04:00
|
|
|
package nodestate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/role"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-04-11 04:35:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const nodeStatePath = "/run/state/constellation/node_state.json"
|
|
|
|
|
|
|
|
// NodeState is the state of a constellation node that is required to recover from a reboot.
|
|
|
|
// Can be persisted to disk and reloaded later.
|
|
|
|
type NodeState struct {
|
2022-06-21 11:59:12 -04:00
|
|
|
Role role.Role
|
|
|
|
OwnerID []byte
|
|
|
|
ClusterID []byte
|
2022-04-11 04:35:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromFile reads a NodeState from disk.
|
|
|
|
func FromFile(fileHandler file.Handler) (*NodeState, error) {
|
|
|
|
nodeState := &NodeState{}
|
|
|
|
if err := fileHandler.ReadJSON(nodeStatePath, nodeState); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return nil, fmt.Errorf("loading node state: %w", err)
|
2022-04-11 04:35:17 -04:00
|
|
|
}
|
|
|
|
return nodeState, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToFile writes a NodeState to disk.
|
|
|
|
func (nodeState *NodeState) ToFile(fileHandler file.Handler) error {
|
2022-04-13 03:18:32 -04:00
|
|
|
return fileHandler.WriteJSON(nodeStatePath, nodeState, file.OptMkdirAll)
|
2022-04-11 04:35:17 -04:00
|
|
|
}
|