Files
slacker-bot/main.go
T
2026-11-01 11:47:41 -05:00

69 lines
1.6 KiB
Go

package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
)
func main() {
cfgPath := flag.String("config", "/etc/slackspawn/config.toml", "path to the config file")
mode := flag.String("mode", ModeMaster, "master (runs the Slack bot) or slave (runs containers for a master)")
flag.Parse()
cfg, err := LoadConfig(*cfgPath, *mode)
if err != nil {
log.Fatal(err)
}
if os.Geteuid() != 0 {
log.Print("warning: not running as root; starting containers will fail")
}
local, err := NewLocal(cfg)
if err != nil {
log.Fatal(err)
}
log.Printf("node %s: %s total, %s for containers", local.Name(), local.cap.Total, local.cap.Usable)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if *mode == ModeSlave {
if err := RunSlave(ctx, cfg, local); err != nil {
log.Fatal(err)
}
return
}
store, err := OpenStore(filepath.Join(cfg.StateDir, "state.json"))
if err != nil {
log.Fatal(err)
}
if seeded, err := store.SeedPermissions(cfg.Permissions); err != nil {
log.Fatal(err)
} else if !seeded {
log.Print("permissions: using the saved ones; [permissions] in the config only seeds a fresh state")
}
mgr := NewManager(cfg, store, local)
bot, err := NewBot(cfg, store, mgr)
if err != nil {
log.Fatal(err)
}
if cfg.Cluster.Listen != "" {
go func() {
if err := mgr.ServeCluster(ctx); err != nil {
log.Fatalf("cluster: %v", err)
}
}()
}
log.Print("connected to Slack, waiting for messages")
if err := bot.Run(ctx); err != nil && ctx.Err() == nil {
log.Fatal(err)
}
}