Skip to content

Commit

Permalink
refactor: add runtime manager for bare-metal cluster (#169)
Browse files Browse the repository at this point in the history
* add runtime manager for bare-metal cluster

Signed-off-by: sh2 <shawnhxh@outlook.com>

* fix linter

Signed-off-by: sh2 <shawnhxh@outlook.com>

* refactor runtime manager into metadata manager

Signed-off-by: sh2 <shawnhxh@outlook.com>

* fix linter

Signed-off-by: sh2 <shawnhxh@outlook.com>

* fix comments

Signed-off-by: sh2 <shawnhxh@outlook.com>

---------

Signed-off-by: sh2 <shawnhxh@outlook.com>
  • Loading branch information
shawnh2 authored Oct 26, 2023
1 parent 2817da1 commit 5e2fb15
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 305 deletions.
71 changes: 10 additions & 61 deletions pkg/cluster/baremetal/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ package baremetal

import (
"context"
"fmt"
"os"
"os/signal"
"path"
"sync"
"syscall"

Expand All @@ -28,107 +25,59 @@ import (
"github.com/GreptimeTeam/gtctl/pkg/deployer/baremetal/component"
"github.com/GreptimeTeam/gtctl/pkg/logger"
"github.com/GreptimeTeam/gtctl/pkg/metadata"
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
)

type Cluster struct {
logger logger.Logger
config *Config
wg sync.WaitGroup
bm *component.BareMetalCluster
ctx context.Context

createNoDirs bool
workingDirs component.WorkingDirs
clusterDir string
baseDir string
clusterConfigPath string
createNoDirs bool
enableCache bool

am artifacts.Manager
mm metadata.Manager

enableCache bool
bm *component.BareMetalCluster
}

type Option func(cluster *Cluster)

func NewCluster(l logger.Logger, clusterName string, opts ...Option) (cluster.Operations, error) {
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

c := &Cluster{
logger: l,
config: DefaultConfig(),
ctx: ctx,
}

mm, err := metadata.New("")
if err != nil {
return nil, err
}
c.mm = mm

for _, opt := range opts {
if opt != nil {
opt(c)
}
}

if err = ValidateConfig(c.config); err != nil {
if err := ValidateConfig(c.config); err != nil {
return nil, err
}

if len(c.baseDir) == 0 {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
c.baseDir = path.Join(homeDir, GtctlDir)
}

if err = fileutils.EnsureDir(c.baseDir); err != nil {
// Configure Metadata Manager
mm, err := metadata.New("")
if err != nil {
return nil, err
}
c.mm = mm

// Configure Artifact Manager.
am, err := artifacts.NewManager(l)
if err != nil {
return nil, err
}
c.am = am

c.initClusterDirsAndPath(clusterName)

// TODO(sh2): implement it in the following PR
// if !cluster.createNoDirs {}

return c, nil
}

func (c *Cluster) initClusterDirsAndPath(clusterName string) {
// Dirs
var (
// ${HOME}/${GtctlDir}/${ClusterName}
clusterDir = path.Join(c.baseDir, clusterName)

// ${HOME}/${GtctlDir}/${ClusterName}/logs
logsDir = path.Join(clusterDir, LogsDir)

// ${HOME}/${GtctlDir}/${ClusterName}/data
dataDir = path.Join(clusterDir, DataDir)

// ${HOME}/${GtctlDir}/${ClusterName}/pids
pidsDir = path.Join(clusterDir, PidsDir)
)

// Path
var (
// ${HOME}/${GtctlDir}/${ClusterName}/${ClusterName}.yaml
clusterConfigPath = path.Join(clusterDir, fmt.Sprintf("%s.yaml", clusterName))
)

c.clusterDir = clusterDir
c.workingDirs = component.WorkingDirs{
LogsDir: logsDir,
DataDir: dataDir,
PidsDir: pidsDir,
}
c.clusterConfigPath = clusterConfigPath
}
7 changes: 4 additions & 3 deletions pkg/cluster/baremetal/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ func (c *Cluster) Get(ctx context.Context, options *opt.GetOptions) error {
}

func (c *Cluster) get(_ context.Context, options *opt.GetOptions) (*ClusterMetadata, error) {
_, err := os.Stat(c.clusterDir)
csd := c.mm.GetClusterScopeDirs()
_, err := os.Stat(csd.BaseDir)
if os.IsNotExist(err) {
return nil, fmt.Errorf("cluster %s is not exist", options.Name)
}
if err != nil {
return nil, err
}

ok, err := fileutils.IsFileExists(c.clusterConfigPath)
ok, err := fileutils.IsFileExists(csd.ConfigPath)
if err != nil {
return nil, err
}
Expand All @@ -59,7 +60,7 @@ func (c *Cluster) get(_ context.Context, options *opt.GetOptions) (*ClusterMetad
}

var cluster ClusterMetadata
in, err := os.ReadFile(c.clusterConfigPath)
in, err := os.ReadFile(csd.ConfigPath)
if err != nil {
return nil, err
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/deployer/baremetal/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ import (
)

const (
// GtctlDir is the root directory that contains states of cluster info.
GtctlDir = ".gtctl"
LogsDir = "logs"
DataDir = "data"
PidsDir = "pids"
PidsDir = "pids"

DataHomeDir = "home"
DataWalDir = "wal"

DefaultLogLevel = "info"
)

// MetaConfig stores metadata of a GreptimeDB cluster with Config nested.
type MetaConfig struct {
// RuntimeConfig stores runtime metadata of a GreptimeDB cluster.
type RuntimeConfig struct {
*Config

CreationDate time.Time `yaml:"creationDate"`
Expand Down
Loading

0 comments on commit 5e2fb15

Please sign in to comment.