From 3e9718f08c8d4ebeec9c58054b2e4459a4623057 Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Fri, 27 Sep 2024 20:10:05 +1000 Subject: [PATCH] update store schema when configuring corrosion, create machines table --- internal/machine/corrosion/config.go | 55 ++++++++++++---------------- internal/machine/machine.go | 16 ++++++-- internal/machine/store/schema.sql | 8 ++++ internal/machine/store/store.go | 6 +++ 4 files changed, 50 insertions(+), 35 deletions(-) create mode 100644 internal/machine/store/schema.sql create mode 100644 internal/machine/store/store.go diff --git a/internal/machine/corrosion/config.go b/internal/machine/corrosion/config.go index 1f709981..08282e12 100644 --- a/internal/machine/corrosion/config.go +++ b/internal/machine/corrosion/config.go @@ -54,7 +54,30 @@ func (c *Config) Write(path, owner string) error { if err := os.WriteFile(path, data.Bytes(), 0600); err != nil { 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 != "" { usr, err := user.Lookup(owner) if err != nil { @@ -74,35 +97,3 @@ func (c *Config) Write(path, owner string) error { } 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 -} diff --git a/internal/machine/machine.go b/internal/machine/machine.go index 87303554..1610bc78 100644 --- a/internal/machine/machine.go +++ b/internal/machine/machine.go @@ -21,6 +21,7 @@ import ( "uncloud/internal/machine/cluster" "uncloud/internal/machine/corrosion" "uncloud/internal/machine/network" + "uncloud/internal/machine/store" ) const ( @@ -256,7 +257,8 @@ func (m *Machine) configureCorrosion(dataDir string) error { if err := corrosion.MkDataDir(dataDir, corrosion.DefaultUser); err != nil { 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. var bootstrap []string @@ -270,7 +272,7 @@ func (m *Machine) configureCorrosion(dataDir string) error { cfg := corrosion.Config{ DB: corrosion.DBConfig{ Path: filepath.Join(dataDir, "store.db"), - SchemaPaths: []string{filepath.Join(dataDir, "schema")}, + SchemaPaths: []string{schemaPath}, }, Gossip: corrosion.GossipConfig{ 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"), }, } - 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) } + + 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 } diff --git a/internal/machine/store/schema.sql b/internal/machine/store/schema.sql new file mode 100644 index 00000000..3f8cfe74 --- /dev/null +++ b/internal/machine/store/schema.sql @@ -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); diff --git a/internal/machine/store/store.go b/internal/machine/store/store.go new file mode 100644 index 00000000..719cec69 --- /dev/null +++ b/internal/machine/store/store.go @@ -0,0 +1,6 @@ +package store + +import _ "embed" + +//go:embed schema.sql +var Schema string