update store schema when configuring corrosion, create machines table

This commit is contained in:
Pavel Sviderski
2024-09-27 20:10:05 +10:00
parent 69c9167355
commit 3e9718f08c
4 changed files with 50 additions and 35 deletions
+23 -32
View File
@@ -54,7 +54,30 @@ func (c *Config) Write(path, owner string) error {
if err := os.WriteFile(path, data.Bytes(), 0600); err != nil { if err := os.WriteFile(path, data.Bytes(), 0600); err != nil {
return err return err
} }
if err := Chown(path, owner); err != nil {
return 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 err := Chown(dir, owner); err != nil {
return err
}
return nil
}
func Chown(path, owner string) error {
if owner != "" { if owner != "" {
usr, err := user.Lookup(owner) usr, err := user.Lookup(owner)
if err != nil { if err != nil {
@@ -74,35 +97,3 @@ func (c *Config) Write(path, owner string) error {
} }
return nil 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
}
+13 -3
View File
@@ -21,6 +21,7 @@ import (
"uncloud/internal/machine/cluster" "uncloud/internal/machine/cluster"
"uncloud/internal/machine/corrosion" "uncloud/internal/machine/corrosion"
"uncloud/internal/machine/network" "uncloud/internal/machine/network"
"uncloud/internal/machine/store"
) )
const ( const (
@@ -256,7 +257,8 @@ func (m *Machine) configureCorrosion(dataDir string) error {
if err := corrosion.MkDataDir(dataDir, corrosion.DefaultUser); err != nil { if err := corrosion.MkDataDir(dataDir, corrosion.DefaultUser); err != nil {
return fmt.Errorf("create corrosion data directory: %w", err) return fmt.Errorf("create corrosion data directory: %w", err)
} }
corroConfigPath := filepath.Join(dataDir, "config.toml") configPath := filepath.Join(dataDir, "config.toml")
schemaPath := filepath.Join(dataDir, "schema.sql")
// TODO: use a partial list of machine peers for bootstrapping if the cluster is large. // TODO: use a partial list of machine peers for bootstrapping if the cluster is large.
var bootstrap []string var bootstrap []string
@@ -270,7 +272,7 @@ func (m *Machine) configureCorrosion(dataDir string) error {
cfg := corrosion.Config{ cfg := corrosion.Config{
DB: corrosion.DBConfig{ DB: corrosion.DBConfig{
Path: filepath.Join(dataDir, "store.db"), Path: filepath.Join(dataDir, "store.db"),
SchemaPaths: []string{filepath.Join(dataDir, "schema")}, SchemaPaths: []string{schemaPath},
}, },
Gossip: corrosion.GossipConfig{ Gossip: corrosion.GossipConfig{
Addr: netip.AddrPortFrom(m.state.Network.ManagementIP, corrosion.DefaultGossipPort), Addr: netip.AddrPortFrom(m.state.Network.ManagementIP, corrosion.DefaultGossipPort),
@@ -284,9 +286,17 @@ func (m *Machine) configureCorrosion(dataDir string) error {
Path: filepath.Join(dataDir, "admin.sock"), Path: filepath.Join(dataDir, "admin.sock"),
}, },
} }
if err := cfg.Write(corroConfigPath, corrosion.DefaultUser); err != nil { if err := cfg.Write(configPath, corrosion.DefaultUser); err != nil {
return fmt.Errorf("write corrosion config: %w", err) return fmt.Errorf("write corrosion config: %w", err)
} }
if err := os.WriteFile(schemaPath, []byte(store.Schema), 0644); err != nil {
return fmt.Errorf("write corrosion schema: %w", err)
}
if err := corrosion.Chown(schemaPath, corrosion.DefaultUser); err != nil {
return fmt.Errorf("chown corrosion schema: %w", err)
}
return nil return nil
} }
+8
View File
@@ -0,0 +1,8 @@
CREATE TABLE machines
(
id TEXT NOT NULL PRIMARY KEY,
name TEXT AS (json_extract(info, '$.name')),
info TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(info))
);
CREATE INDEX idx_machines_name ON machines (name);
+6
View File
@@ -0,0 +1,6 @@
package store
import _ "embed"
//go:embed schema.sql
var Schema string