create corrosion config

This commit is contained in:
Pavel Sviderski
2024-09-26 17:00:30 +10:00
parent 2f7463ebe3
commit 9fcc319c3a
8 changed files with 191 additions and 49 deletions
+104
View File
@@ -0,0 +1,104 @@
package corrosion
import (
"fmt"
"github.com/BurntSushi/toml"
"net/netip"
"os"
"os/user"
"path/filepath"
"strconv"
)
const (
DefaultUser = "uncloud"
DefaultGossipPort = 51001
DefaultAPIPort = 51002
)
// Config represents the Corrosion config.
type Config struct {
DB DBConfig `toml:"db"`
Gossip GossipConfig `toml:"gossip"`
API APIConfig `toml:"api"`
Admin AdminConfig `toml:"admin"`
}
type DBConfig struct {
Path string `toml:"path"`
SchemaPaths []string `toml:"schema_paths"`
}
type GossipConfig struct {
Addr netip.AddrPort `toml:"addr"`
Plaintext bool `toml:"plaintext"`
}
type APIConfig struct {
Addr netip.AddrPort `toml:"addr"`
}
type AdminConfig struct {
Path string `toml:"path"`
}
func (c *Config) Write(path, owner string) error {
data, err := toml.Marshal(c)
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
if err := os.WriteFile(path, data, 0600); err != nil {
return err
}
if owner != "" {
usr, err := user.Lookup(owner)
if err != nil {
return fmt.Errorf("lookup user %q: %w", owner, err)
}
uid, err := strconv.Atoi(usr.Uid)
if err != nil {
return fmt.Errorf("parse %q user ID (UID) %q: %w", owner, usr.Uid, err)
}
gid, err := strconv.Atoi(usr.Gid)
if err != nil {
return fmt.Errorf("parse %q user group ID (GID) %q: %w", owner, usr.Gid, err)
}
if err = os.Chown(path, uid, gid); err != nil {
return fmt.Errorf("chown %q: %w", path, err)
}
}
return nil
}
func MkDataDir(dir, owner string) error {
parent, _ := filepath.Split(dir)
// Use 0711 for parent directories to allow `owner` to access its nested data directory.
if err := os.MkdirAll(parent, 0711); err != nil {
return fmt.Errorf("create directory %q: %w", parent, err)
}
if err := os.Mkdir(dir, 0700); err != nil {
if !os.IsExist(err) {
return fmt.Errorf("create directory %q: %w", dir, err)
}
}
if owner != "" {
usr, err := user.Lookup(owner)
if err != nil {
return fmt.Errorf("lookup user %q: %w", owner, err)
}
uid, err := strconv.Atoi(usr.Uid)
if err != nil {
return fmt.Errorf("parse %q user ID (UID) %q: %w", owner, usr.Uid, err)
}
gid, err := strconv.Atoi(usr.Gid)
if err != nil {
return fmt.Errorf("parse %q user group ID (GID) %q: %w", owner, usr.Gid, err)
}
if err = os.Chown(dir, uid, gid); err != nil {
return fmt.Errorf("chown %q: %w", dir, err)
}
}
return nil
}
+5
View File
@@ -0,0 +1,5 @@
package corrosion
type Service interface {
Start() error
}
+36
View File
@@ -0,0 +1,36 @@
package corrosion
import (
"fmt"
"log/slog"
"os/exec"
)
const DefaultSystemdUnit = "uncloud-corrosion.service"
type SystemdService struct {
DataDir string
Unit string
User string
}
func DefaultSystemdService(dataDir string) *SystemdService {
return &SystemdService{
DataDir: dataDir,
Unit: DefaultSystemdUnit,
User: DefaultUser,
}
}
func (s *SystemdService) Configure() error {
// TODO: create config.toml and schema dir in DataDir.
return nil
}
func (s *SystemdService) Start() error {
if _, err := exec.Command("systemctl", "start", s.Unit).Output(); err != nil {
return fmt.Errorf("systemctl start %s: %w", s.Unit, err)
}
slog.Info("Corrosion systemd service started.", "unit", s.Unit)
return nil
}