Skip to content

Commit

Permalink
implement bare-metal delete api
Browse files Browse the repository at this point in the history
Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 committed Nov 1, 2023
1 parent 9b3a6a8 commit f208f62
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 83 deletions.
49 changes: 35 additions & 14 deletions pkg/cluster/baremetal/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,53 @@ package baremetal

import (
"context"
"fmt"
"os"
"syscall"

opt "github.com/GreptimeTeam/gtctl/pkg/cluster"
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
)

func (c *Cluster) Delete(ctx context.Context, options *opt.DeleteOptions) error {
// TODO(sh2): check whether the cluster is still running
if err := c.delete(ctx, options); err != nil {
cluster, err := c.get(ctx, &opt.GetOptions{Name: options.Name})
if err != nil {
return err
}

return nil
}

func (c *Cluster) delete(ctx context.Context, options *opt.DeleteOptions) error {
if err := c.cc.Frontend.Delete(ctx, options); err != nil {
return err
running, ferr, serr := c.isClusterRunning(cluster.ForegroundPid)
if ferr != nil {
return fmt.Errorf("error checking whether cluster '%s' is running: %v", options.Name, ferr)
}
if err := c.cc.Datanode.Delete(ctx, options); err != nil {
return err
}
if err := c.cc.MetaSrv.Delete(ctx, options); err != nil {
return err
if running || serr == nil {
return fmt.Errorf("cluster '%s' is running, please stop it before deleting", options.Name)
}
if err := c.cc.Etcd.Delete(ctx, options); err != nil {

csd := c.mm.GetClusterScopeDirs()
c.logger.V(0).Infof("Deleting cluster configurations and runtime directories in %s", csd.BaseDir)
if err = c.delete(ctx, csd.BaseDir); err != nil {
return err
}
c.logger.V(0).Info("Deleted!")

return nil
}

func (c *Cluster) delete(_ context.Context, baseDir string) error {
return fileutils.DeleteDirIfExists(baseDir)
}

// isClusterRunning checks the current status of cluster by sending signal to process.
func (c *Cluster) isClusterRunning(pid int) (runs bool, f error, s error) {
p, f := os.FindProcess(pid)
if f != nil {
return false, f, nil
}

s = p.Signal(syscall.Signal(0))
if s != nil {
return false, nil, s
}

return true, nil, nil
}
15 changes: 0 additions & 15 deletions pkg/components/datanode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

greptimev1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"

opt "github.com/GreptimeTeam/gtctl/pkg/cluster"
"github.com/GreptimeTeam/gtctl/pkg/config"
"github.com/GreptimeTeam/gtctl/pkg/logger"
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
Expand Down Expand Up @@ -176,20 +175,6 @@ func (d *datanode) IsRunning(_ context.Context) bool {
return true
}

func (d *datanode) Delete(ctx context.Context, options *opt.DeleteOptions) error {
for _, dir := range d.dataHomeDirs {
if err := fileutils.DeleteDirIfExists(dir); err != nil {
return err
}
}

if err := d.delete(ctx, options); err != nil {
return err
}

return nil
}

func generateDatanodeAddr(addr string, nodeID int) string {
// Already checked in validation.
host, port, _ := net.SplitHostPort(addr)
Expand Down
8 changes: 0 additions & 8 deletions pkg/components/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"path"
"sync"

opt "github.com/GreptimeTeam/gtctl/pkg/cluster"
"github.com/GreptimeTeam/gtctl/pkg/logger"
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
)
Expand Down Expand Up @@ -82,10 +81,3 @@ func (e *etcd) IsRunning(_ context.Context) bool {
// Have not implemented the healthy checker now.
return false
}

func (e *etcd) Delete(ctx context.Context, options *opt.DeleteOptions) error {
if err := e.delete(ctx, options); err != nil {
return err
}
return nil
}
9 changes: 0 additions & 9 deletions pkg/components/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"

opt "github.com/GreptimeTeam/gtctl/pkg/cluster"
"github.com/GreptimeTeam/gtctl/pkg/config"
"github.com/GreptimeTeam/gtctl/pkg/logger"
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
Expand Down Expand Up @@ -108,11 +107,3 @@ func (f *frontend) IsRunning(_ context.Context) bool {
// Have not implemented the healthy checker now.
return false
}

func (f *frontend) Delete(ctx context.Context, options *opt.DeleteOptions) error {
if err := f.delete(ctx, options); err != nil {
return err
}

return nil
}
8 changes: 0 additions & 8 deletions pkg/components/metasrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"sync"
"time"

opt "github.com/GreptimeTeam/gtctl/pkg/cluster"
"github.com/GreptimeTeam/gtctl/pkg/config"
"github.com/GreptimeTeam/gtctl/pkg/logger"
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
Expand Down Expand Up @@ -160,13 +159,6 @@ func (m *metaSrv) IsRunning(_ context.Context) bool {
return true
}

func (m *metaSrv) Delete(ctx context.Context, options *opt.DeleteOptions) error {
if err := m.delete(ctx, options); err != nil {
return err
}
return nil
}

func generateMetaSrvAddr(addr string, nodeID int) string {
host, port, _ := net.SplitHostPort(addr)
portInt, _ := strconv.Atoi(port)
Expand Down
31 changes: 2 additions & 29 deletions pkg/components/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,26 @@ package components

import (
"context"

opt "github.com/GreptimeTeam/gtctl/pkg/cluster"
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
)

const (
DefaultLogLevel = "info"
)

// WorkingDirs include all the dirs used in bare-metal mode.
// WorkingDirs include all the directories used in bare-metal mode.
type WorkingDirs struct {
DataDir string `yaml:"dataDir"`
LogsDir string `yaml:"logsDir"`
PidsDir string `yaml:"pidsDir"`
}

// allocatedDirs include all the directories that created during bare-metal mode.
type allocatedDirs struct {
dataDirs []string
logsDirs []string
pidsDirs []string
}

func (ad *allocatedDirs) delete(_ context.Context, _ *opt.DeleteOptions) error {
for _, dir := range ad.logsDirs {
if err := fileutils.DeleteDirIfExists(dir); err != nil {
return err
}
}

for _, dir := range ad.dataDirs {
if err := fileutils.DeleteDirIfExists(dir); err != nil {
return err
}
}

for _, dir := range ad.pidsDirs {
if err := fileutils.DeleteDirIfExists(dir); err != nil {
return err
}
}

return nil
}

// ClusterComponent is the basic component of running GreptimeDB Cluster in bare-metal mode.
type ClusterComponent interface {
// Start starts cluster component by executing binary.
Expand All @@ -71,9 +47,6 @@ type ClusterComponent interface {
// IsRunning returns the status of current cluster component.
IsRunning(ctx context.Context) bool

// Delete deletes resources that allocated in the system for current component.
Delete(ctx context.Context, options *opt.DeleteOptions) error

// Name return the name of component.
Name() string
}

0 comments on commit f208f62

Please sign in to comment.