From 72744cdd21ef37861138e559201a6782096182e0 Mon Sep 17 00:00:00 2001 From: sh2 Date: Wed, 2 Aug 2023 17:32:53 +0800 Subject: [PATCH] optimize msg of foreground deletion and add ctx for download process Signed-off-by: sh2 --- pkg/cmd/gtctl/cluster/create/create.go | 20 ++++++++++++-------- pkg/deployer/baremetal/artifacts.go | 25 ++++++++++++++++--------- pkg/deployer/baremetal/deployer.go | 19 ++++++++++--------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/pkg/cmd/gtctl/cluster/create/create.go b/pkg/cmd/gtctl/cluster/create/create.go index 3078530b..55bc7c96 100644 --- a/pkg/cmd/gtctl/cluster/create/create.go +++ b/pkg/cmd/gtctl/cluster/create/create.go @@ -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 } } @@ -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 } } @@ -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. diff --git a/pkg/deployer/baremetal/artifacts.go b/pkg/deployer/baremetal/artifacts.go index eb70a466..39da6243 100644 --- a/pkg/deployer/baremetal/artifacts.go +++ b/pkg/deployer/baremetal/artifacts.go @@ -19,9 +19,9 @@ import ( "archive/zip" "bytes" "compress/gzip" + "context" "fmt" "io" - "io/ioutil" "net/http" "os" "path" @@ -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 @@ -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 } @@ -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: @@ -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 } @@ -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 } @@ -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 diff --git a/pkg/deployer/baremetal/deployer.go b/pkg/deployer/baremetal/deployer.go index 1583f24f..d8aa527c 100644 --- a/pkg/deployer/baremetal/deployer.go +++ b/pkg/deployer/baremetal/deployer.go @@ -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 } @@ -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 } @@ -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 } @@ -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 }