Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add runtime manager for bare-metal cluster #169

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("", clusterName)
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.GetClusterScopeDir()
_, 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
Loading