From 9026d1b25cb27cc8ec3e1d438a18c011b8b48773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=86=E6=B5=81=E8=80=8C=E4=B8=8A?= <62740231+DokiDoki1103@users.noreply.github.com> Date: Wed, 24 Jul 2024 16:44:00 +0800 Subject: [PATCH] feat: rek2 env check,tls config (#81) * feat: support check k8s,rke2 env * feat: skip tls error * feat: skip tls error --- api/cloud-adaptor/v1/cluster.go | 3 +- internal/adaptor/rke2/rke2.go | 7 ++- internal/handler/cluster.go | 87 +++++++++++++++++++++++++++++++-- internal/model/cloud.go | 2 +- pkg/util/util.go | 23 ++++++++- 5 files changed, 113 insertions(+), 9 deletions(-) diff --git a/api/cloud-adaptor/v1/cluster.go b/api/cloud-adaptor/v1/cluster.go index 73d0bea..27b188e 100644 --- a/api/cloud-adaptor/v1/cluster.go +++ b/api/cloud-adaptor/v1/cluster.go @@ -75,7 +75,8 @@ type CheckSSHReq struct { } type CheckSSHRes struct { - Status bool `json:"status"` + Status bool `json:"status"` + Msg string `json:"msg"` } // CreateRke2ClusterRequest 创建rke2 集群请求体 diff --git a/internal/adaptor/rke2/rke2.go b/internal/adaptor/rke2/rke2.go index 2c006c3..71ded3d 100644 --- a/internal/adaptor/rke2/rke2.go +++ b/internal/adaptor/rke2/rke2.go @@ -191,10 +191,15 @@ disable: tls-san: - goodrain.rke2` + var registriesConfig = `configs: + "goodrain.me": + tls: + insecure_skip_verify: true` + if cluster == nil { staticConfig += "\nserver: https://goodrain.rke2:9345" } - err = session.Run(fmt.Sprintf("mkdir -p /etc/rancher/rke2/config.yaml.d/; echo \"%s\" > /etc/rancher/rke2/config.yaml; cd /etc/rancher/rke2/config.yaml.d; echo \"%s\" > static.yaml", rke2Server.ConfigFile, staticConfig)) + err = session.Run(fmt.Sprintf("mkdir -p /etc/rancher/rke2/config.yaml.d/; echo \"%s\" > /etc/rancher/rke2/config.yaml; echo \"%s\" > /etc/rancher/rke2/registries.yaml; cd /etc/rancher/rke2/config.yaml.d; echo \"%s\" > static.yaml", rke2Server.ConfigFile, registriesConfig, staticConfig)) if err != nil { logrus.Errorf("Failed to execute saveConfig command: %s", err) return err diff --git a/internal/handler/cluster.go b/internal/handler/cluster.go index 9f1d548..959f0b2 100644 --- a/internal/handler/cluster.go +++ b/internal/handler/cluster.go @@ -21,6 +21,7 @@ package handler import ( "encoding/json" "fmt" + cryptossh "golang.org/x/crypto/ssh" "goodrain.com/cloud-adaptor/internal/adaptor/rke2" "goodrain.com/cloud-adaptor/internal/datastore" "goodrain.com/cloud-adaptor/internal/model" @@ -541,6 +542,33 @@ func (e *ClusterHandler) CheckSSH(ctx *gin.Context) { ginutil.JSON(ctx, res) } +func execCommand(conn *cryptossh.Client, command string) error { + session, err := conn.NewSession() + if err != nil { + logrus.Errorf("Failed to create session: %s", err) + return err + } + defer session.Close() + err = session.Run(command) + return err +} + +// checkPort checks if a specific port is in use on the remote server +func checkPort(conn *cryptossh.Client, port int) (bool, error) { + command := fmt.Sprintf("netstat -tuln | grep ':%d '", port) + session, err := conn.NewSession() + if err != nil { + return false, fmt.Errorf("failed to create session: %v", err) + } + defer session.Close() + + output, err := session.CombinedOutput(command) + if err != nil && !strings.Contains(string(output), fmt.Sprintf(":%d", port)) { + return false, nil // Port is not in use + } + return true, nil // Port is in use +} + // CheckSSHPassword 检查账号密码是否正确 func (e *ClusterHandler) CheckSSHPassword(ctx *gin.Context) { var node model.RKE2Nodes @@ -549,11 +577,62 @@ func (e *ClusterHandler) CheckSSHPassword(ctx *gin.Context) { ginutil.JSON(ctx, nil, bcode.BadRequest) return } - _, err = rke2.InitConn(&node) - var res = v1.CheckSSHRes{ - Status: err == nil, + conn, err := rke2.InitConn(&node) + if err != nil { + ginutil.JSON(ctx, v1.CheckSSHRes{ + Status: false, + Msg: "用户名或者密码错误", + }) } - ginutil.JSON(ctx, res) + defer conn.Close() + + err = execCommand(conn, "curl") + if err != nil { + ginutil.JSON(ctx, v1.CheckSSHRes{ + Status: false, + Msg: "curl 命令未找到", + }) + return + } + + err = execCommand(conn, "wget") + if err != nil { + ginutil.JSON(ctx, v1.CheckSSHRes{ + Status: false, + Msg: "netstat 命令未找到", + }) + return + } + + err = execCommand(conn, "netstat") + if err != nil { + ginutil.JSON(ctx, v1.CheckSSHRes{ + Status: false, + Msg: "netstat 命令未找到", + }) + return + } + + use6443, err := checkPort(conn, 6443) + if err != nil { + ginutil.JSON(ctx, v1.CheckSSHRes{ + Status: false, + Msg: "检查端口命令失败", + }) + return + } + if use6443 { + ginutil.JSON(ctx, v1.CheckSSHRes{ + Status: false, + Msg: "6443 端口已经被占用", + }) + return + } + + ginutil.JSON(ctx, v1.CheckSSHRes{ + Status: true, + Msg: "通过所有检测", + }) } // RKE2DeleteCluster 安装rainbond diff --git a/internal/model/cloud.go b/internal/model/cloud.go index 1cb534a..3cd8384 100644 --- a/internal/model/cloud.go +++ b/internal/model/cloud.go @@ -95,7 +95,7 @@ type RKE2Nodes struct { NodeName string `gorm:"column:node_name" json:"node_name"` Role string `gorm:"column:role" json:"role"` Host string `gorm:"column:host" json:"host"` - Port uint `gorm:"column:port" json:"port"` + Port int `gorm:"column:port" json:"port"` User string `gorm:"column:user" json:"user"` Pass string `gorm:"column:pass" json:"pass"` ClusterID string `gorm:"column:cluster_id" json:"cluster_id"` diff --git a/pkg/util/util.go b/pkg/util/util.go index a1c06ed..ea378f5 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -22,6 +22,8 @@ import ( "math/rand" "net" "net/url" + "os/exec" + "strconv" "strings" "time" ) @@ -32,7 +34,7 @@ func init() { r = rand.New(rand.NewSource(time.Now().Unix())) } -//RandString create rand string +// RandString create rand string func RandString(len int) string { bytes := make([]byte, len) for i := 0; i < len; i++ { @@ -42,7 +44,7 @@ func RandString(len int) string { return string(bytes) } -//GetIPByURL get ip by url +// GetIPByURL get ip by url func GetIPByURL(u string) string { url, _ := url.Parse(u) if url != nil { @@ -57,3 +59,20 @@ func GetIPByURL(u string) string { } return "" } + +// CheckCommandExists checks if a command exists in the system +func CheckCommandExists(command string) bool { + _, err := exec.LookPath(command) + return err == nil +} + +// CheckPortInUse checks if a port is in use on 127.0.0.1 +func CheckPortInUse(port int) bool { + address := net.JoinHostPort("127.0.0.1", strconv.Itoa(port)) + conn, err := net.Listen("tcp", address) + if err != nil { + return true // Port is in use + } + conn.Close() + return false // Port is not in use +}