-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cluster status monitoring and improve error handling
Signed-off-by: Benevor <2647311844@qq.com>
- Loading branch information
Showing
52 changed files
with
2,576 additions
and
1,560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2023 Huawei Cloud Computing Technologies Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/openGemini/gemix/pkg/cluster/config" | ||
"github.com/openGemini/gemix/pkg/cluster/manager" | ||
"github.com/openGemini/gemix/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// clusterCmd represents the cluster command | ||
var ClusterCmd = &cobra.Command{ | ||
Use: "cluster", | ||
Short: "manage cluster", | ||
Long: `Manage openGemini cluster, including install, stop, uninstall, status, etc.`, | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
func getClusterOptions(cmd *cobra.Command) (manager.ClusterOptions, error) { | ||
var ops manager.ClusterOptions | ||
if version, _ := cmd.Flags().GetString("version"); version == "" { | ||
latestVer, err := util.GetLatestVerFromCurl() | ||
if err != nil { | ||
return ops, err | ||
} else { | ||
ops.Version = latestVer | ||
} | ||
} else { | ||
ops.Version = version | ||
} | ||
if user, _ := cmd.Flags().GetString("user"); user == "" { | ||
has, value := GetEnv(util.SshEnvUser) | ||
if has { | ||
ops.User = value | ||
} else { | ||
return ops, fmt.Errorf("the user is required") | ||
} | ||
} else { | ||
ops.User = user | ||
} | ||
password, _ := cmd.Flags().GetString("password") | ||
key, _ := cmd.Flags().GetString("key") | ||
if password == "" && key == "" { | ||
hasKey, key := GetEnv(util.SshEnvKey) | ||
if hasKey { | ||
ops.Key = key | ||
ops.SshType = config.SSH_KEY | ||
} else { | ||
hasPW, pw := GetEnv(util.SshEnvPW) | ||
if hasPW { | ||
ops.Password = pw | ||
ops.SshType = config.SSH_PW | ||
} else { | ||
return ops, fmt.Errorf("the password and key need at least one") | ||
} | ||
} | ||
|
||
} else if password != "" && key != "" { | ||
return ops, fmt.Errorf("the password and key need only one") | ||
} else { | ||
ops.Key = key | ||
ops.Password = password | ||
if key != "" { | ||
ops.SshType = config.SSH_KEY | ||
} else { | ||
ops.SshType = config.SSH_PW | ||
} | ||
} | ||
|
||
if yPath, _ := cmd.Flags().GetString("yaml"); yPath == "" { | ||
return ops, fmt.Errorf("the path of cluster configuration file must be specified") | ||
} else { | ||
ops.YamlPath = yPath | ||
} | ||
return ops, nil | ||
} | ||
|
||
func GetEnv(envVar string) (bool, string) { | ||
value := os.Getenv(envVar) | ||
if value == "" { | ||
return false, value | ||
} else { | ||
return true, value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2023 Huawei Cloud Computing Technologies Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/openGemini/gemix/pkg/cluster/manager" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// installCmd represents the install command | ||
var installCmd = &cobra.Command{ | ||
Use: "install", | ||
Short: "install cluster", | ||
Long: `Install an openGemini cluster based on configuration files and version numbers.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ops, err := getClusterOptions(cmd) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
err = InstallCluster(ops) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
}, | ||
} | ||
|
||
func InstallCluster(ops manager.ClusterOptions) error { | ||
installer := manager.NewGeminiInstaller(ops) | ||
defer installer.Close() | ||
|
||
if err := installer.PrepareForInstall(); err != nil { | ||
return err | ||
} | ||
if err := installer.Install(); err != nil { | ||
return err | ||
} | ||
fmt.Printf("Successfully installed the openGemini cluster with version : %s\n", ops.Version) | ||
return nil | ||
} | ||
|
||
func init() { | ||
ClusterCmd.AddCommand(installCmd) | ||
installCmd.Flags().StringP("version", "v", "", "component version") | ||
installCmd.Flags().StringP("yaml", "y", "", "The path to cluster topology yaml file") | ||
installCmd.Flags().StringP("user", "u", "", "The user name to login via SSH. The user must has root (or sudo) privilege.") | ||
installCmd.Flags().StringP("key", "k", "", "The path of the SSH identity file. If specified, public key authentication will be used.") | ||
installCmd.Flags().StringP("password", "p", "", "The password of target hosts. If specified, password authentication will be used.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2023 Huawei Cloud Computing Technologies Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/openGemini/gemix/pkg/cluster/manager" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// startCmd represents the start command | ||
var startCmd = &cobra.Command{ | ||
Use: "start", | ||
Short: "start cluster", | ||
Long: `Start an openGemini cluster based on configuration files and version numbers.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ops, err := getClusterOptions(cmd) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
err = StartCluster(ops) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Printf("\nCheck the status of openGemini cluster\n") | ||
err = PatrolCluster(ops) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
}, | ||
} | ||
|
||
func StartCluster(ops manager.ClusterOptions) error { | ||
starter := manager.NewGeminiStarter(ops) | ||
defer starter.Close() | ||
|
||
if err := starter.PrepareForStart(); err != nil { | ||
return err | ||
} | ||
if err := starter.Start(); err != nil { | ||
return err | ||
} | ||
fmt.Printf("Successfully started the openGemini cluster with version : %s\n", ops.Version) | ||
return nil | ||
} | ||
|
||
func init() { | ||
ClusterCmd.AddCommand(startCmd) | ||
startCmd.Flags().StringP("version", "v", "", "component version") | ||
startCmd.Flags().StringP("yaml", "y", "", "The path to cluster topology yaml file") | ||
startCmd.Flags().StringP("user", "u", "", "The user name to login via SSH. The user must has root (or sudo) privilege.") | ||
startCmd.Flags().StringP("key", "k", "", "The path of the SSH identity file. If specified, public key authentication will be used.") | ||
startCmd.Flags().StringP("password", "p", "", "The password of target hosts. If specified, password authentication will be used.") | ||
} |
Oops, something went wrong.