2020-04-10 22:33:30 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/pkg/errors"
|
2020-04-10 23:55:36 +00:00
|
|
|
"io/ioutil"
|
2020-04-10 22:33:30 +00:00
|
|
|
"os"
|
2020-04-10 22:37:10 +00:00
|
|
|
"sync"
|
2020-04-10 22:33:30 +00:00
|
|
|
)
|
|
|
|
|
2020-04-11 01:07:57 +00:00
|
|
|
const stateFileLocation = "data/.states.json"
|
|
|
|
|
|
|
|
var stateMutex sync.Mutex
|
2020-04-10 22:33:30 +00:00
|
|
|
|
2020-04-11 01:07:57 +00:00
|
|
|
// Checks if the state tracking file exists, if not it is generated.
|
|
|
|
func ensureStateFileExists() (bool, error) {
|
|
|
|
stateMutex.Lock()
|
|
|
|
defer stateMutex.Unlock()
|
2020-04-10 22:37:10 +00:00
|
|
|
|
2020-04-11 01:07:57 +00:00
|
|
|
if _, err := os.Stat(stateFileLocation); err != nil {
|
2020-04-10 22:33:30 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return false, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-04-11 01:07:57 +00:00
|
|
|
// Returns the state of the servers.
|
|
|
|
func getServerStates() (map[string]string, error) {
|
2020-04-10 22:33:30 +00:00
|
|
|
// Check if the states file exists.
|
2020-04-11 01:07:57 +00:00
|
|
|
exists, err := ensureStateFileExists()
|
2020-04-10 22:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-04-10 22:37:10 +00:00
|
|
|
// Request a lock after we check if the file exists.
|
2020-04-11 01:07:57 +00:00
|
|
|
stateMutex.Lock()
|
|
|
|
defer stateMutex.Unlock()
|
2020-04-10 22:37:10 +00:00
|
|
|
|
2020-04-10 22:33:30 +00:00
|
|
|
// Return an empty map if the file does not exist.
|
|
|
|
if !exists {
|
|
|
|
return map[string]string{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the states file.
|
2020-04-11 01:07:57 +00:00
|
|
|
f, err := os.Open(stateFileLocation)
|
2020-04-10 22:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Convert the json object to a map.
|
|
|
|
states := map[string]string{}
|
2020-04-10 23:55:36 +00:00
|
|
|
if err := json.NewDecoder(f).Decode(&states); err != nil {
|
2020-04-10 22:33:30 +00:00
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return states, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveServerStates .
|
|
|
|
func SaveServerStates() error {
|
|
|
|
// Get the states of all servers on the daemon.
|
|
|
|
states := map[string]string{}
|
|
|
|
for _, s := range GetServers().All() {
|
|
|
|
states[s.Uuid] = s.State
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the map to a json object.
|
|
|
|
data, err := json.Marshal(states)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-04-11 01:07:57 +00:00
|
|
|
stateMutex.Lock()
|
|
|
|
defer stateMutex.Unlock()
|
2020-04-10 22:37:10 +00:00
|
|
|
|
2020-04-10 22:33:30 +00:00
|
|
|
// Write the data to the file
|
2020-04-11 01:07:57 +00:00
|
|
|
if err := ioutil.WriteFile(stateFileLocation, data, 0644); err != nil {
|
2020-04-10 22:33:30 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-04-10 23:55:36 +00:00
|
|
|
return nil
|
2020-04-10 22:33:30 +00:00
|
|
|
}
|