From bb18319bdcaf408e822456c0ab2285bb01ae3e0a Mon Sep 17 00:00:00 2001
From: cheliequan <liequanche@126.com>
Date: 星期三, 24 五月 2023 17:42:29 +0800
Subject: [PATCH] 更新util
---
src/util/util.go | 138 +++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 137 insertions(+), 1 deletions(-)
diff --git a/src/util/util.go b/src/util/util.go
index 05ef259..3da9a85 100644
--- a/src/util/util.go
+++ b/src/util/util.go
@@ -1,6 +1,15 @@
package util
-import "os"
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "net/http"
+ "os"
+ "time"
+
+ "golang.org/x/crypto/ssh"
+)
// homeDir 鑾峰彇褰撳墠鐢ㄦ埛鐨勫鐩綍璺緞
func homeDir() string {
@@ -9,3 +18,130 @@
}
return os.Getenv("USERPROFILE") // Windows 鐜涓嬭幏鍙栫敤鎴风洰褰�
}
+
+func sshExec(nodeIP, sshUsername, sshPassword, remoteSSHCommand string, sshPort int) (string, error) {
+ // SSH 杩炴帴閰嶇疆
+ config := &ssh.ClientConfig{
+ User: sshUsername,
+ Auth: []ssh.AuthMethod{
+ ssh.Password(sshPassword),
+ },
+ HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+ }
+
+ // 杩炴帴鍒拌繙绋嬫湇鍔″櫒
+ client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", nodeIP, sshPort), config)
+ if err != nil {
+ return "", fmt.Errorf("failed to connect to node %s: %v", nodeIP, err)
+ }
+ defer client.Close()
+
+ // 鍒涘缓浼氳瘽
+ session, err := client.NewSession()
+ if err != nil {
+ return "", fmt.Errorf("failed to create SSH session: %v", err)
+ }
+ defer session.Close()
+
+ // 鍒涘缓涓�涓叿鏈夎秴鏃剁殑涓婁笅鏂�
+ ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
+ defer cancel()
+
+ // 閫氳繃浼氳瘽鎵ц杩滅▼鍛戒护
+ outputChan := make(chan string)
+ errorChan := make(chan error)
+ go func() {
+ // 鍒涘缓涓�涓繛鎺ユ爣鍑嗚緭鍏ョ殑绠¢亾
+ stdinPipe, err := session.StdinPipe()
+ if err != nil {
+ errorChan <- fmt.Errorf("failed to create stdin pipe: %v", err)
+ return
+ }
+
+ // 鍚姩浼氳瘽
+ if err := session.Start(fmt.Sprintf("sudo -SE %s", remoteSSHCommand)); err != nil {
+ errorChan <- fmt.Errorf("failed to start command: %v err锛�%v", remoteSSHCommand, err)
+ return
+ }
+
+ // 灏嗗瘑鐮佸啓鍏ユ爣鍑嗚緭鍏ョ閬�
+ _, err = fmt.Fprintf(stdinPipe, "%s\n", sshPassword)
+ if err != nil {
+ errorChan <- fmt.Errorf("failed to write password to stdin: %v", err)
+ return
+ }
+
+ // 绛夊緟浼氳瘽缁撴潫
+ if err := session.Wait(); err != nil {
+ errorChan <- fmt.Errorf("command execution failed: %v err锛�%v", remoteSSHCommand, err)
+ return
+ }
+
+ outputChan <- ""
+ }()
+
+ // 绛夊緟缁撴灉鎴栬秴鏃�
+ select {
+ case <-ctx.Done():
+ // 鍏抽棴浼氳瘽浠ョ粓姝㈣繙绋嬪懡浠�
+ session.Close()
+ // 绛夊緟浼氳瘽鍏抽棴鐨� goroutine 缁撴潫
+ <-outputChan
+ return "", fmt.Errorf("SSH command execution timed out")
+ case err := <-errorChan:
+ return "", err
+ case <-outputChan:
+ fmt.Printf("Command: %v executed on the remote server: %s\n", remoteSSHCommand, nodeIP)
+ return "", nil
+ }
+}
+
+// 瀹夎Docker
+func installDocker(nodeIP, sshUsername, sshPassword string, sshPort int) error {
+ // 妫�鏌ocker鏄惁宸插畨瑁�
+ checkCommand := "which docker"
+ _, err := sshExec(nodeIP, sshUsername, sshPassword, checkCommand, sshPort)
+ if err == nil {
+ fmt.Println("Docker is already installed on the remote server.")
+ return nil
+ }
+
+ // 瀹夎Docker
+ installCommand := "sudo curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh"
+ _, err = sshExec(nodeIP, sshUsername, sshPassword, installCommand, sshPort)
+ if err != nil {
+ return fmt.Errorf("failed to install Docker on the remote server: %v", err)
+ }
+
+ fmt.Println("Docker has been installed on the remote server.")
+ return nil
+}
+
+// Create an HTTP client with insecure TLS configuration
+func createHTTPClient() *http.Client {
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ }
+ return &http.Client{Transport: transport}
+}
+
+// 瀹夎kubectl
+func installKubectl(nodeIP, sshUsername, sshPassword string, sshPort int) error {
+ // 妫�鏌ubectl鏄惁宸插畨瑁�
+ checkCommand := "which kubectl"
+ _, err := sshExec(nodeIP, sshUsername, sshPassword, checkCommand, sshPort)
+ if err == nil {
+ fmt.Println("kubectl is already installed on the remote server.")
+ return nil
+ }
+
+ // 瀹夎kubectl
+ installCommand := "sudo curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && sudo chmod +x kubectl && sudo mv kubectl /usr/local/bin/"
+ _, err = sshExec(nodeIP, sshUsername, sshPassword, installCommand, sshPort)
+ if err != nil {
+ return fmt.Errorf("failed to install kubectl on the remote server: %v", err)
+ }
+
+ fmt.Println("kubectl has been installed on the remote server.")
+ return nil
+}
--
Gitblit v1.8.0