Skip to content

Commit

Permalink
feat: rek2 env check,tls config (#81)
Browse files Browse the repository at this point in the history
* feat: support check k8s,rke2 env

* feat: skip tls error

* feat: skip tls error
  • Loading branch information
DokiDoki1103 authored Jul 24, 2024
1 parent 807b91c commit 9026d1b
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 9 deletions.
3 changes: 2 additions & 1 deletion api/cloud-adaptor/v1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ type CheckSSHReq struct {
}

type CheckSSHRes struct {
Status bool `json:"status"`
Status bool `json:"status"`
Msg string `json:"msg"`
}

// CreateRke2ClusterRequest 创建rke2 集群请求体
Expand Down
7 changes: 6 additions & 1 deletion internal/adaptor/rke2/rke2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 83 additions & 4 deletions internal/handler/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/model/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
23 changes: 21 additions & 2 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"math/rand"
"net"
"net/url"
"os/exec"
"strconv"
"strings"
"time"
)
Expand All @@ -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++ {
Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit 9026d1b

Please sign in to comment.