2022-05-30 15:25:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-14 16:43:51 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2022-05-30 15:25:07 +00:00
|
|
|
"log"
|
2024-03-14 16:43:51 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-05-30 15:25:07 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/settings"
|
2024-03-17 15:49:15 +00:00
|
|
|
|
|
|
|
"gopkg.in/fsnotify.v1"
|
2022-05-30 15:25:07 +00:00
|
|
|
)
|
|
|
|
|
2024-03-17 15:49:15 +00:00
|
|
|
var PublishNextChangeIndication = true
|
|
|
|
|
2022-05-30 15:25:07 +00:00
|
|
|
// NewDistList creates a distList from the given src directory
|
2024-03-14 16:43:51 +00:00
|
|
|
func NewSettingsDistList(src string) (*distList, error) {
|
2022-05-30 15:25:07 +00:00
|
|
|
list, err := settings.ListNextSettingsFiles()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
var dlist []DistEvent
|
|
|
|
for _, e := range list {
|
|
|
|
dlist = append(dlist, e)
|
2022-05-30 15:25:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
return &distList{List: dlist, Timer: time.NewTimer(time.Minute)}, nil
|
|
|
|
}
|
2022-05-30 15:25:07 +00:00
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
func parseSettingsFilename(fname string) (int64, error) {
|
|
|
|
return strconv.ParseInt(strings.TrimSuffix(fname, ".json"), 10, 64)
|
2022-05-30 15:25:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
func newSettingsFile(l *distList, raw_path string) {
|
|
|
|
bpath := path.Base(raw_path)
|
2022-05-30 15:25:07 +00:00
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
if bpath == "challenge.json" || bpath == "settings.json" {
|
|
|
|
log.Printf("Copying %s to SETTINGDIST...", bpath)
|
|
|
|
// Copy content through tmp file
|
|
|
|
fd, err := os.Open(raw_path)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: Unable to open %s: %s", raw_path, err.Error())
|
2022-05-30 15:25:07 +00:00
|
|
|
return
|
|
|
|
}
|
2024-03-14 16:43:51 +00:00
|
|
|
defer fd.Close()
|
2022-05-30 15:25:07 +00:00
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
tmpfile, err := ioutil.TempFile(TmpSettingsDistDirectory, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: Unable to create temporary file for %s: %s", bpath, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-05-30 15:25:07 +00:00
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
_, err = io.Copy(tmpfile, fd)
|
|
|
|
tmpfile.Close()
|
2022-06-08 10:31:08 +00:00
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: Unable to copy content to temporary file (%s): %s", bpath, err.Error())
|
|
|
|
return
|
2022-06-08 10:31:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
os.Chmod(tmpfile.Name(), 0644)
|
2022-06-08 10:31:08 +00:00
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
if err = os.Rename(tmpfile.Name(), path.Join(SettingsDistDir, bpath)); err != nil {
|
|
|
|
log.Println("ERROR: Unable to move file:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if ts, err := parseSettingsFilename(bpath); err == nil {
|
|
|
|
activateTime := time.Unix(ts, 0)
|
2022-06-08 10:31:08 +00:00
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
log.Printf("Preparing %s: activation time at %s", bpath, activateTime)
|
2022-05-30 15:25:07 +00:00
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
l.AddEvent(&settings.NextSettingsFile{
|
|
|
|
Id: ts,
|
|
|
|
Date: activateTime,
|
2024-03-17 15:49:15 +00:00
|
|
|
}, updateNextSettingsTime)
|
2022-05-30 15:25:07 +00:00
|
|
|
} else {
|
2024-03-14 16:43:51 +00:00
|
|
|
log.Println("WARNING: Unknown file to treat: not a valid timestamp:", err.Error())
|
2022-05-30 15:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 15:49:15 +00:00
|
|
|
func saveSettingsFile(cur_settings *settings.Settings) error {
|
|
|
|
if watchedNotify == fsnotify.Create {
|
|
|
|
err := settings.SaveSettings(path.Join(TmpSettingsDirectory, "settings.json"), cur_settings)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to save settings.json to tmp dir: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Rename(path.Join(TmpSettingsDirectory, "settings.json"), path.Join(settings.SettingsDir, "settings.json"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to move settings.json to dest dir: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := settings.SaveSettings(path.Join(settings.SettingsDir, "settings.json"), cur_settings)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to save settings.json to tmp dir: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-14 16:43:51 +00:00
|
|
|
func treatSettingsFile(e DistEvent) {
|
|
|
|
v, err := settings.ReadNextSettingsFile(path.Join(settings.SettingsDir, fmt.Sprintf("%d.json", e.GetId())), e.GetId())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to read json: %s", err.Error())
|
|
|
|
} else if cur_settings, err := settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile)); err != nil {
|
|
|
|
log.Printf("Unable to read settings.json: %s", err.Error())
|
|
|
|
} else {
|
|
|
|
cur_settings = settings.MergeSettings(*cur_settings, v.Values)
|
|
|
|
|
2024-03-17 15:49:15 +00:00
|
|
|
if err := saveSettingsFile(cur_settings); err != nil {
|
|
|
|
log.Printf(err.Error())
|
2024-03-14 16:43:51 +00:00
|
|
|
} else if err = os.Remove(path.Join(settings.SettingsDir, fmt.Sprintf("%d.json", v.Id))); err != nil {
|
|
|
|
log.Printf("Unable to remove initial diff file (%d.json): %s", v.Id, err.Error())
|
|
|
|
}
|
2022-05-30 15:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-17 15:49:15 +00:00
|
|
|
|
|
|
|
func updateNextSettingsTime(e DistEvent) {
|
|
|
|
if !PublishNextChangeIndication {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("Updating settings.json to include next settings time")
|
|
|
|
|
|
|
|
cur_settings, err := settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to read settings.json: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if e == nil {
|
|
|
|
cur_settings = settings.MergeSettings(*cur_settings, map[string]interface{}{
|
|
|
|
"nextchangetime": nil,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
cur_settings = settings.MergeSettings(*cur_settings, map[string]interface{}{
|
|
|
|
"nextchangetime": *e.GetDate(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err = saveSettingsFile(cur_settings)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(err.Error())
|
|
|
|
}
|
|
|
|
}
|