server/remote/challenge-sync-airbus/main.go

231 lines
6.4 KiB
Go

package main
import (
"flag"
"io/ioutil"
"log"
"os"
"os/signal"
"path"
"path/filepath"
"strconv"
"syscall"
"time"
"gopkg.in/fsnotify.v1"
)
var (
TeamsDir string
skipInitialSync bool
)
func main() {
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
flag.BoolVar(&skipInitialSync, "skipinitialsync", skipInitialSync, "Skip the initial synchronization")
daemon := flag.Bool("watch", false, "Enable daemon mode by watching the directory")
tspath := flag.String("timestamp-file", "./REMOTE/timestamp", "Path to the file storing the last timestamp")
exercicespath := flag.String("exercices-file", "./REMOTE/exercices-bindings.json", "Path to the file containing the ID bindings")
coeff := flag.Float64("global-coeff", 10.0, "Coefficient to use to multiply all scores before passing them to the other platform")
flag.Parse()
api := AirbusAPI{
BaseURL: "https://portal.european-cybercup.lan/api",
}
if v, exists := os.LookupEnv("AIRBUS_BASEURL"); exists {
api.BaseURL = v
}
if v, exists := os.LookupEnv("AIRBUS_TOKEN"); exists {
api.Token = v
}
if v, exists := os.LookupEnv("AIRBUS_SESSIONID"); exists {
var err error
api.SessionID, err = strconv.ParseInt(v, 10, 64)
if err != nil {
log.Fatal("AIRBUS_SESSIONID is invalid: ", err.Error())
}
}
if v, exists := os.LookupEnv("AIRBUS_SESSIONUUID"); exists {
api.SessionUUID = v
}
log.SetPrefix("[challenge-sync-airbus] ")
TeamsDir = path.Clean(TeamsDir)
// Load the timestamp
ts, err := loadTS(*tspath)
if err != nil {
log.Fatal("Unable to open timestamp file: ", err.Error())
}
// Load exercices bindings
exbindings, err := ReadExercicesBindings(*exercicespath)
if err != nil {
log.Fatal("Unable to open exercices bindings file: ", err.Error())
}
// Load teams.json
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
if err != nil {
log.Fatal("Unable to open teams bindings file: ", err.Error())
}
w := Walker{
LastSync: ts,
Exercices: exbindings,
Teams: teamsbindings,
API: api,
Coeff: *coeff,
}
if !skipInitialSync {
// Iterate over teams scores
err = filepath.WalkDir(TeamsDir, w.WalkScoreSync)
if err != nil {
log.Printf("Something goes wrong during walking")
}
// save current timestamp for teams
err = saveTS(*tspath, w.LastSync)
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
}
}
if daemon != nil && *daemon {
// Watch teams.json and scores.json
log.Println("Registering directory events...")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
if err := watchsubdir(watcher, TeamsDir); err != nil {
log.Fatal(err)
}
// Register SIGUSR1, SIGUSR2
interrupt1 := make(chan os.Signal, 1)
signal.Notify(interrupt1, syscall.SIGHUP)
interrupt2 := make(chan os.Signal, 1)
signal.Notify(interrupt2, syscall.SIGUSR1)
interrupt3 := make(chan os.Signal, 1)
signal.Notify(interrupt3, syscall.SIGUSR2)
ticker := time.NewTicker(5 * time.Second)
watchedNotify := fsnotify.Create
for {
select {
case <-interrupt1:
log.Println("SIGHUP received, reloading files...")
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
if err != nil {
log.Println("Unable to open teams bindings file: ", err.Error())
return
}
w.Teams = teamsbindings
// save current timestamp for teams
err = saveTS(*tspath, w.LastSync)
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
}
log.Println("SIGHUP treated.")
case <-interrupt2:
log.Println("SIGUSR1 received, resynching all teams")
// Iterate over teams scores
err = filepath.WalkDir(TeamsDir, w.WalkScoreSync)
if err != nil {
log.Printf("Something goes wrong during walking")
}
// save current timestamp for teams
err = saveTS(*tspath, w.LastSync)
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
}
log.Println("SIGUSR1 treated.")
case <-interrupt3:
log.Println("SIGUSR2 received, resynching all teams from teams.json")
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
if err != nil {
log.Println("Unable to open teams bindings file: ", err.Error())
return
}
w.Teams = teamsbindings
// FIXME
log.Println("SIGUSR2 treated.")
case ev := <-watcher.Events:
if d, err := os.Lstat(ev.Name); err == nil && ev.Op&fsnotify.Create == fsnotify.Create && d.Mode().IsDir() && d.Mode()&os.ModeSymlink == 0 && d.Name() != ".tmp" {
// Register new subdirectory
if err := watchsubdir(watcher, ev.Name); err != nil {
log.Println(err)
}
} else if ev.Op&watchedNotify == watchedNotify && d.Mode().IsRegular() {
if *debugINotify {
log.Println("Treating event:", ev, "for", ev.Name)
}
if filepath.Base(ev.Name) == "scores.json" {
go w.treat(ev.Name)
} else if filepath.Base(ev.Name) == "teams.json" {
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
if err != nil {
log.Println("Unable to open teams bindings file: ", err.Error())
return
}
w.Teams = teamsbindings
}
} else if ev.Op&fsnotify.Write == fsnotify.Write {
log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
watchedNotify = fsnotify.Write
} else if *debugINotify {
log.Println("Skipped event:", ev, "for", ev.Name)
}
case err := <-watcher.Errors:
log.Println("error:", err)
case <-ticker.C:
// save current timestamp for teams
err = saveTS(*tspath, w.LastSync)
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
}
}
}
}
// save current timestamp for teams
err = saveTS(*tspath, w.LastSync)
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
}
}
func watchsubdir(watcher *fsnotify.Watcher, pathname string) error {
log.Println("Watch new directory:", pathname)
if err := watcher.Add(pathname); err != nil {
return err
}
if ds, err := ioutil.ReadDir(pathname); err != nil {
return err
} else {
for _, d := range ds {
p := path.Join(pathname, d.Name())
if d.IsDir() && d.Name() != ".tmp" && d.Mode()&os.ModeSymlink == 0 {
if err := watchsubdir(watcher, p); err != nil {
return err
}
}
}
return nil
}
}