From ab70148fc0213a4523d976099a18e438cb2ed067 Mon Sep 17 00:00:00 2001
From: cheliequan <liequanche@126.com>
Date: 星期三, 24 五月 2023 19:55:49 +0800
Subject: [PATCH] 更新代码

---
 src/main/main.go |  153 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 150 insertions(+), 3 deletions(-)

diff --git a/src/main/main.go b/src/main/main.go
index a04a495..a6f27a7 100644
--- a/src/main/main.go
+++ b/src/main/main.go
@@ -8,11 +8,13 @@
 	"path/filepath"
 	"time"
 
-	"../util"
+	"basic.com/aps/aps_deploy.git/src/cluster"
+	"basic.com/aps/aps_deploy.git/src/rancher"
+	"basic.com/aps/aps_deploy.git/src/util"
 	"k8s.io/client-go/kubernetes"
 	"k8s.io/client-go/tools/clientcmd"
 
-	"../k8s"
+	"basic.com/aps/aps_deploy.git/src/k8s"
 )
 
 var (
@@ -22,10 +24,143 @@
 	namespaces          = []string{"guangsheng", "geruimi", "tongsheng"}
 )
 
+type Node struct {
+	ClusterName string   `json:"clusterName"`
+	Roles       []string `json:"roles"`
+	IP          string   `json:"ip"`
+	SSHUsername string   `json:"sshUsername"`
+	SSHPassword string   `json:"sshPassword"`
+	SSHPort     int      `json:"sshPort"`
+}
+
+func rancher_install_test() {
+	nodes := []Node{
+		{
+			Roles:       []string{"etcd", "controlplane", "worker"},
+			IP:          "192.168.20.189",
+			SSHUsername: "basic",
+			SSHPassword: "123",
+			SSHPort:     22,
+		},
+		{
+			Roles:       []string{"worker"},
+			IP:          "192.168.20.10",
+			SSHUsername: "basic",
+			SSHPassword: "123",
+			SSHPort:     22,
+		},
+		{
+			Roles:       []string{"worker"},
+			IP:          "192.168.20.115",
+			SSHUsername: "basic",
+			SSHPassword: "alien123",
+			SSHPort:     22,
+		},
+		// Add more nodes here if needed
+	}
+
+	//install rancher on master node
+	err := rancher.InstallDockerAndRancher(nodes[0].IP, nodes[0].SSHUsername, nodes[0].SSHPassword, nodes[0].SSHPort)
+	if err != nil {
+		log.Fatalf("Failed to install Rancher: %v", err)
+	}
+
+	os.Exit(0)
+}
+
+func cluster_test() {
+	clusterName := "kubernetus"
+	nodes := []Node{
+		{
+			ClusterName: clusterName,
+			Roles:       []string{"etcd", "controlplane", "worker"},
+			IP:          "192.168.20.189",
+			SSHUsername: "basic",
+			SSHPassword: "123",
+			SSHPort:     22,
+		},
+		{
+			ClusterName: clusterName,
+			Roles:       []string{"worker"},
+			IP:          "192.168.20.10",
+			SSHUsername: "basic",
+			SSHPassword: "123",
+			SSHPort:     22,
+		},
+		{
+			ClusterName: clusterName,
+			Roles:       []string{"worker"},
+			IP:          "192.168.20.115",
+			SSHUsername: "basic",
+			SSHPassword: "alien123",
+			SSHPort:     22,
+		},
+		// Add more nodes here if needed
+	}
+
+	// Create the cluster
+	// Rancher configuration
+	/*rancherConfig := RancherConfig{
+		RancherURL:  "https://192.168.20.119:8443",
+		BearerToken: "token-nnrsc:w68zdt8s47fnpjd5xqdl5hhzpz4j2d56kt5nx49nsswcbpdzc28kh5",
+	}*/
+
+	rancherConfig := rancher.RancherConfig{
+		RancherURL:  "https://192.168.20.189:8443",
+		BearerToken: "token-t4cdf:h7zhmbvbzdvd9mmjw8zmt8rh4m7rl5gtqpqljlhl9tlr2z26j9lf4l",
+	}
+
+	//	Deploy clusterId
+	clusterID, err := cluster.GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName)
+	if err != nil {
+		log.Fatal(err)
+		err = cluster.CreateCluster(rancherConfig, clusterName)
+		if err != nil {
+			log.Fatalf("Failed to create cluster: %v", err)
+		}
+		fmt.Printf("Cluster created: %s\n", clusterName)
+		clusterID, err = cluster.GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName)
+		if err != nil {
+			log.Fatal(err)
+		}
+	}
+	fmt.Println(clusterID)
+
+	//	Deploy nodeCommand
+	nodeCommand, err := cluster.GetNodeCommand(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterID)
+	if err != nil {
+		log.Fatal(err)
+	}
+	fmt.Println(nodeCommand)
+
+	for _, node := range nodes {
+		//Deploy Docker on each node
+		err = util.InstallDocker(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort)
+		if err != nil {
+			log.Fatal(err)
+		}
+
+		// Deploy Kubectl on each node
+		err = util.InstallKubectl(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort)
+		if err != nil {
+			log.Fatal(err)
+		}
+
+		// Deploy Kubernetes roles on each node
+		err = cluster.Deployk8sRolesOnNode(node.IP, node.SSHUsername, node.SSHPassword, nodeCommand, node.SSHPort, node.Roles)
+		if err != nil {
+			log.Fatal(err)
+		}
+	}
+	os.Exit(0)
+}
+
 func main() {
 	createCmd := flag.NewFlagSet("create", flag.ExitOnError)
 	deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
 	testCmd := flag.NewFlagSet("test", flag.ExitOnError)
+	clusterCmd := flag.NewFlagSet("cluster", flag.ExitOnError)
+	rancherInstallCmd := flag.NewFlagSet("rancher_install", flag.ExitOnError)
 
 	createNamespace := createCmd.String("ns", "", "Namespace name")
 	createDeployment := createCmd.String("deployment", "", "Deployment name")
@@ -36,7 +171,7 @@
 	deleteService := deleteCmd.String("service", "", "Service name")
 
 	if len(os.Args) < 2 {
-		fmt.Println("create/delete/test command is required")
+		fmt.Println("create/delete/test/cluster/rancher_install command is required")
 		os.Exit(1)
 	}
 
@@ -47,6 +182,10 @@
 		deleteCmd.Parse(os.Args[2:])
 	case "test":
 		testCmd.Parse(os.Args[2:])
+	case "cluster":
+		clusterCmd.Parse(os.Args[2:])
+	case "rancher_install":
+		rancherInstallCmd.Parse(os.Args[2:])
 	default:
 		flag.PrintDefaults()
 		os.Exit(1)
@@ -144,4 +283,12 @@
 		fmt.Println("Resources deleted.")
 	}
 
+	if clusterCmd.Parsed() {
+		cluster_test()
+	}
+
+	if rancherInstallCmd.Parsed() {
+		rancher_install_test()
+	}
+
 }

--
Gitblit v1.8.0