191 lines
5.6 KiB
Go
191 lines
5.6 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"slackerbot/internal/model"
|
|
"slackerbot/internal/units"
|
|
)
|
|
|
|
const MinStorageMB = 512
|
|
|
|
type StorageConfig struct {
|
|
Min string `toml:"min"`
|
|
Max string `toml:"max"`
|
|
Limit string `toml:"limit"`
|
|
|
|
MinMB, MaxMB, LimitMB int64 `toml:"-"`
|
|
}
|
|
|
|
type DriveConfig struct {
|
|
Path string `toml:"path"`
|
|
Reserve string `toml:"reserve"`
|
|
|
|
ReserveMB int64 `toml:"-"`
|
|
}
|
|
|
|
type Config struct {
|
|
Slack struct {
|
|
AppToken string `toml:"app_token"`
|
|
BotToken string `toml:"bot_token"`
|
|
} `toml:"slack"`
|
|
|
|
System string `toml:"system"`
|
|
|
|
MachinesDir string `toml:"machines_dir"`
|
|
StateDir string `toml:"state_dir"`
|
|
|
|
IdleTimeout time.Duration `toml:"idle_timeout"`
|
|
MaxIdleTimeout time.Duration `toml:"max_idle_timeout"`
|
|
AllowIndefinite bool `toml:"allow_indefinite"`
|
|
BootTimeout time.Duration `toml:"boot_timeout"`
|
|
|
|
AgentCwd string `toml:"agent_cwd"`
|
|
AgentEnv map[string]string `toml:"agent_env"`
|
|
|
|
MaxSendFileMB int64 `toml:"max_send_file_mb"`
|
|
|
|
ROBinds []string `toml:"ro_binds"`
|
|
RWBinds []string `toml:"rw_binds"`
|
|
|
|
Container model.Size `toml:"container"`
|
|
MaxContainer model.Size `toml:"max_container"`
|
|
HostReserve model.Size `toml:"host_reserve"`
|
|
|
|
Storage StorageConfig `toml:"storage"`
|
|
Drives []DriveConfig `toml:"drives"`
|
|
|
|
Cluster ClusterConfig `toml:"cluster"`
|
|
|
|
Permissions model.PermissionsConfig `toml:"permissions"`
|
|
}
|
|
|
|
type ClusterConfig struct {
|
|
Name string `toml:"name"`
|
|
Secret string `toml:"secret"`
|
|
Listen string `toml:"listen"`
|
|
Master string `toml:"master"`
|
|
MasterFingerprint string `toml:"master_fingerprint"`
|
|
}
|
|
|
|
const (
|
|
ModeMaster = "master"
|
|
ModeSlave = "slave"
|
|
)
|
|
|
|
func LoadConfig(path, mode string) (*Config, error) {
|
|
c := &Config{
|
|
MachinesDir: "/var/lib/machines",
|
|
StateDir: "/var/lib/slackerbot",
|
|
IdleTimeout: 6 * time.Hour,
|
|
AllowIndefinite: true,
|
|
BootTimeout: 2 * time.Minute,
|
|
AgentCwd: "/root",
|
|
MaxSendFileMB: 100,
|
|
Container: model.Size{MemoryMB: 2048, CPUs: 1},
|
|
MaxContainer: model.Size{MemoryMB: 8192, CPUs: 4},
|
|
HostReserve: model.Size{MemoryMB: 2048, CPUs: 1},
|
|
Storage: StorageConfig{Min: "5G", Max: "10G", Limit: "50G"},
|
|
}
|
|
if _, err := toml.DecodeFile(path, c); err != nil {
|
|
return nil, fmt.Errorf("reading %s: %w", path, err)
|
|
}
|
|
|
|
if v := os.Getenv("SLACK_APP_TOKEN"); v != "" {
|
|
c.Slack.AppToken = v
|
|
}
|
|
if v := os.Getenv("SLACK_BOT_TOKEN"); v != "" {
|
|
c.Slack.BotToken = v
|
|
}
|
|
for k, v := range c.AgentEnv {
|
|
c.AgentEnv[k] = os.ExpandEnv(v)
|
|
}
|
|
c.Cluster.Secret = os.ExpandEnv(c.Cluster.Secret)
|
|
if v := os.Getenv("CLUSTER_SECRET"); v != "" {
|
|
c.Cluster.Secret = v
|
|
}
|
|
if c.Cluster.Name == "" {
|
|
c.Cluster.Name, _ = os.Hostname()
|
|
}
|
|
|
|
if err := c.loadStorage(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
clustered := (mode == ModeMaster && c.Cluster.Listen != "") || mode == ModeSlave
|
|
switch {
|
|
case mode != ModeMaster && mode != ModeSlave:
|
|
return nil, fmt.Errorf("mode must be %q or %q", ModeMaster, ModeSlave)
|
|
case mode == ModeMaster && (c.Slack.AppToken == "" || c.Slack.BotToken == ""):
|
|
return nil, fmt.Errorf("slack app_token and bot_token are required")
|
|
case mode == ModeMaster && c.Permissions.Validate() != nil:
|
|
return nil, c.Permissions.Validate()
|
|
case mode == ModeSlave && !strings.HasPrefix(c.Cluster.Master, "wss://"):
|
|
return nil, fmt.Errorf("cluster.master must be the master's address, like wss://master.example:7946")
|
|
case clustered && len(c.Cluster.Secret) < 16:
|
|
return nil, fmt.Errorf("cluster.secret must be at least 16 characters, and the same on every node")
|
|
case c.Cluster.Name == "":
|
|
return nil, fmt.Errorf("cluster.name is required")
|
|
case c.System == "":
|
|
return nil, fmt.Errorf("system (the NixOS build) is required")
|
|
case c.Container.MemoryMB <= 0 || c.Container.CPUs <= 0:
|
|
return nil, fmt.Errorf("container memory_mb and cpus must be above zero")
|
|
case c.MaxSendFileMB <= 0 || c.MaxSendFileMB > 1024:
|
|
return nil, fmt.Errorf("max_send_file_mb must be between 1 and 1024 (Slack's limit)")
|
|
case c.IdleTimeout <= 0:
|
|
return nil, fmt.Errorf("idle_timeout must be above zero")
|
|
case !c.Container.Fits(c.MaxContainer):
|
|
return nil, fmt.Errorf("max_container must be at least as big as container")
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (c *Config) loadStorage() error {
|
|
var err error
|
|
s := &c.Storage
|
|
if s.MinMB, err = units.ParseSizeMB(s.Min); err != nil {
|
|
return fmt.Errorf("storage.min: %w", err)
|
|
}
|
|
if s.MaxMB, err = units.ParseSizeMB(s.Max); err != nil {
|
|
return fmt.Errorf("storage.max: %w", err)
|
|
}
|
|
if s.LimitMB, err = units.ParseSizeMB(s.Limit); err != nil {
|
|
return fmt.Errorf("storage.limit: %w", err)
|
|
}
|
|
switch {
|
|
case s.MinMB < MinStorageMB:
|
|
return fmt.Errorf("storage.min must be at least %s", units.FormatMB(MinStorageMB))
|
|
case s.MinMB > s.MaxMB:
|
|
return fmt.Errorf("storage.min (%s) can't be more than storage.max (%s)", s.Min, s.Max)
|
|
case s.MaxMB > s.LimitMB:
|
|
return fmt.Errorf("storage.max (%s) can't be more than storage.limit (%s)", s.Max, s.Limit)
|
|
case len(c.Drives) == 0:
|
|
return fmt.Errorf("add at least one [[drives]] entry (a folder on a btrfs drive)")
|
|
}
|
|
seen := map[string]bool{}
|
|
for i := range c.Drives {
|
|
d := &c.Drives[i]
|
|
if d.Path == "" || !filepath.IsAbs(d.Path) {
|
|
return fmt.Errorf("drives[%d].path must be a full path", i)
|
|
}
|
|
d.Path = filepath.Clean(d.Path)
|
|
if seen[d.Path] {
|
|
return fmt.Errorf("drive %s is listed twice", d.Path)
|
|
}
|
|
seen[d.Path] = true
|
|
if d.Reserve == "" {
|
|
d.Reserve = "0G"
|
|
}
|
|
if d.ReserveMB, err = units.ParseSizeMB(d.Reserve); err != nil {
|
|
return fmt.Errorf("drive %s reserve: %w", d.Path, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|