Skip to content

Commit

Permalink
optimize msg of foreground deletion and add ctx for download process
Browse files Browse the repository at this point in the history
Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 committed Aug 2, 2023
1 parent fc38790 commit 72744cd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
20 changes: 12 additions & 8 deletions pkg/cmd/gtctl/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ func NewCreateClusterCommand(l logger.Logger) *cobra.Command {
}

if err := deployGreptimeDBCluster(ctx, l, &options, spinner, clusterDeployer, clusterName); err != nil {
// Wait the cluster closing if deploy fails in bare-metal mode.
if options.BareMetal {
if err := waitChildProcess(ctx, clusterDeployer, false); err != nil {
if err := waitChildProcess(ctx, clusterDeployer, true); err != nil {
return err
}
}
Expand All @@ -131,7 +132,7 @@ func NewCreateClusterCommand(l logger.Logger) *cobra.Command {
}

if options.BareMetal {
if err := waitChildProcess(ctx, clusterDeployer, true); err != nil {
if err := waitChildProcess(ctx, clusterDeployer, false); err != nil {
return err
}
}
Expand Down Expand Up @@ -314,16 +315,19 @@ func printTips(l logger.Logger, clusterName string, options *createClusterCliOpt
l.V(0).Infof("\n%s 🔑", logger.Bold("Invest in Data, Harvest over Time."))
}

func waitChildProcess(ctx context.Context, deployer deployer.Interface, version bool) error {
func waitChildProcess(ctx context.Context, deployer deployer.Interface, close bool) error {
d, ok := deployer.(*baremetal.Deployer)
if ok {
if version {
v := d.Config().Cluster.Artifact.Version
if v == "" {
v = "unknown"
}
v := d.Config().Cluster.Artifact.Version
if len(v) == 0 {
v = "unknown"
}

if !close {
fmt.Printf("\x1b[32m%s\x1b[0m", fmt.Sprintf("The cluster(pid=%d, version=%s) is running in bare-metal mode now...\n", os.Getpid(), v))
fmt.Printf("\x1b[32m%s\x1b[0m", fmt.Sprintf("To view dashboard by accessing: %s\n", logger.Bold("http://localhost:4000/dashboard/")))
} else {
fmt.Printf("\x1b[32m%s\x1b[0m", fmt.Sprintf("The cluster(pid=%d, version=%s) run in bare-metal has been deleted now...\n", os.Getpid(), v))
}

// Wait for all the child processes to exit.
Expand Down
25 changes: 16 additions & 9 deletions pkg/deployer/baremetal/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"archive/zip"
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -96,7 +96,7 @@ func (am *ArtifactManager) BinaryPath(typ ArtifactType, artifact *config.Artifac
}

// PrepareArtifact will download the artifact from the given URL and uncompressed it.
func (am *ArtifactManager) PrepareArtifact(typ ArtifactType, artifact *config.Artifact) error {
func (am *ArtifactManager) PrepareArtifact(ctx context.Context, typ ArtifactType, artifact *config.Artifact) error {
// If you use the local artifact, we don't need to download it.
if artifact.Local != "" {
return nil
Expand All @@ -107,7 +107,7 @@ func (am *ArtifactManager) PrepareArtifact(typ ArtifactType, artifact *config.Ar
binDir = path.Join(am.dir, typ.String(), artifact.Version, "bin")
)

artifactFile, err := am.download(typ, artifact.Version, pkgDir)
artifactFile, err := am.download(ctx, typ, artifact.Version, pkgDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (am *ArtifactManager) installGreptime(artifactFile, binDir string) error {
return nil
}

func (am *ArtifactManager) download(typ ArtifactType, version, pkgDir string) (string, error) {
func (am *ArtifactManager) download(ctx context.Context, typ ArtifactType, version, pkgDir string) (string, error) {
var extension string
switch runtime.GOOS {
case GOOSDarwin:
Expand Down Expand Up @@ -224,7 +224,7 @@ func (am *ArtifactManager) download(typ ArtifactType, version, pkgDir string) (s

am.logger.V(3).Infof("Downloading artifact from '%s' to '%s'", downloadURL, artifactFile)

req, err := http.NewRequest(http.MethodGet, downloadURL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -326,15 +326,20 @@ func (am *ArtifactManager) unzip(file, dst string) error {
return err
}

dstFile.Close()
fileInArchive.Close()
if err := dstFile.Close(); err != nil {
return err
}

if err := fileInArchive.Close(); err != nil {
return err
}
}

return nil
}

func (am *ArtifactManager) untar(file, dst string) error {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return err
}
Expand Down Expand Up @@ -366,7 +371,9 @@ func (am *ArtifactManager) untar(file, dst string) error {
if _, err := io.Copy(outFile, tarReader); err != nil {
return err
}
outFile.Close()
if err := outFile.Close(); err != nil {
return err
}
case tar.TypeDir:
if err := os.Mkdir(dst+"/"+header.Name, 0755); err != nil {
return err
Expand Down
19 changes: 10 additions & 9 deletions pkg/deployer/baremetal/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ func NewDeployer(l logger.Logger, clusterName string, opts ...Option) (Interface
}
d.am = am

if err := d.createClusterDirs(clusterName); err != nil {
return nil, err
}
if len(clusterName) > 0 {
if err := d.createClusterDirs(clusterName); err != nil {
return nil, err
}

d.bm = component.NewGreptimeDBCluster(d.config.Cluster, d.dataDir, d.logsDir, d.pidsDir, &d.wg, d.logger)
d.bm = component.NewGreptimeDBCluster(d.config.Cluster, d.dataDir, d.logsDir, d.pidsDir, &d.wg, d.logger)
}

return d, nil
}
Expand Down Expand Up @@ -155,7 +157,7 @@ func (d *Deployer) ListGreptimeDBClusters(ctx context.Context, options *ListGrep
}

func (d *Deployer) CreateGreptimeDBCluster(ctx context.Context, clusterName string, options *CreateGreptimeDBClusterOptions) error {
if err := d.am.PrepareArtifact(GreptimeArtifactType, d.config.Cluster.Artifact); err != nil {
if err := d.am.PrepareArtifact(ctx, GreptimeArtifactType, d.config.Cluster.Artifact); err != nil {
return err
}

Expand Down Expand Up @@ -189,9 +191,8 @@ func (d *Deployer) DeleteGreptimeDBCluster(ctx context.Context, name string, opt

// deleteGreptimeDBClusterForeground delete the whole cluster if it runs in foreground.
func (d *Deployer) deleteGreptimeDBClusterForeground(ctx context.Context) error {
// It is really unnecessary to delete each components resources in the cluster
// since it is running in the foreground.
// So deleting the whole cluster resources will be fine.
// It is unnecessary to delete each component resources in cluster since it runs in the foreground.
// So deleting the whole cluster resources here would be fine.
if err := utils.DeleteDirIfExists(d.clusterDir); err != nil {
return err
}
Expand All @@ -200,7 +201,7 @@ func (d *Deployer) deleteGreptimeDBClusterForeground(ctx context.Context) error
}

func (d *Deployer) CreateEtcdCluster(ctx context.Context, clusterName string, options *CreateEtcdClusterOptions) error {
if err := d.am.PrepareArtifact(EtcdArtifactType, d.config.Etcd.Artifact); err != nil {
if err := d.am.PrepareArtifact(ctx, EtcdArtifactType, d.config.Etcd.Artifact); err != nil {
return err
}

Expand Down

0 comments on commit 72744cd

Please sign in to comment.