189 lines
5.6 KiB
Go
189 lines
5.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
type Size struct {
|
|
MemoryMB int `toml:"memory_mb"`
|
|
CPUs float64 `toml:"cpus"`
|
|
}
|
|
|
|
func (s Size) Add(o Size) Size { return Size{s.MemoryMB + o.MemoryMB, s.CPUs + o.CPUs} }
|
|
func (s Size) Sub(o Size) Size { return Size{s.MemoryMB - o.MemoryMB, s.CPUs - o.CPUs} }
|
|
func (s Size) Fits(in Size) bool {
|
|
return s.MemoryMB <= in.MemoryMB && s.CPUs <= in.CPUs+1e-9
|
|
}
|
|
func (s Size) String() string {
|
|
mem := fmt.Sprintf("%d MB", s.MemoryMB)
|
|
if s.MemoryMB >= 1024 && s.MemoryMB%1024 == 0 {
|
|
mem = fmt.Sprintf("%d GB", s.MemoryMB/1024)
|
|
}
|
|
cpu := strconv.FormatFloat(s.CPUs, 'f', -1, 64) + " CPUs"
|
|
if s.CPUs == 1 {
|
|
cpu = "1 CPU"
|
|
}
|
|
return mem + " RAM, " + cpu
|
|
}
|
|
|
|
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"`
|
|
|
|
ROBinds []string `toml:"ro_binds"`
|
|
RWBinds []string `toml:"rw_binds"`
|
|
|
|
Container Size `toml:"container"`
|
|
MaxContainer Size `toml:"max_container"`
|
|
HostReserve Size `toml:"host_reserve"`
|
|
|
|
Storage StorageConfig `toml:"storage"`
|
|
Drives []DriveConfig `toml:"drives"`
|
|
|
|
Cluster ClusterConfig `toml:"cluster"`
|
|
|
|
Permissions 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/slackspawn",
|
|
IdleTimeout: 6 * time.Hour,
|
|
AllowIndefinite: true,
|
|
BootTimeout: 2 * time.Minute,
|
|
AgentCwd: "/root",
|
|
Container: Size{MemoryMB: 2048, CPUs: 1},
|
|
MaxContainer: Size{MemoryMB: 8192, CPUs: 4},
|
|
HostReserve: 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.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 = parseSizeMB(s.Min); err != nil {
|
|
return fmt.Errorf("storage.min: %w", err)
|
|
}
|
|
if s.MaxMB, err = parseSizeMB(s.Max); err != nil {
|
|
return fmt.Errorf("storage.max: %w", err)
|
|
}
|
|
if s.LimitMB, err = 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", 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 = parseSizeMB(d.Reserve); err != nil {
|
|
return fmt.Errorf("drive %s reserve: %w", d.Path, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|