| | |
| | | "net/http" |
| | | "strings" |
| | | |
| | | "basic.com/aps/aps_deploy.git/src/util" |
| | | "basic.com/aps/aps_deploy.git/src/rancher" |
| | | "basic.com/aps/aps_deploy.git/src/util" |
| | | ) |
| | | |
| | | type Cluster struct { |
| | |
| | | return "", fmt.Errorf("cluster '%s' not found", clusterName) |
| | | } |
| | | |
| | | func GetClusters(serverURL, bearerToken string) ([]string, error) { |
| | | |
| | | url := fmt.Sprintf("%s/v3/clusters", serverURL) |
| | | |
| | | req, err := http.NewRequest("GET", url, nil) |
| | | if err != nil { |
| | | return nil, fmt.Errorf("failed to get cluster list request: %v", err) |
| | | } |
| | | |
| | | req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerToken)) |
| | | |
| | | client := util.CreateHTTPClient() |
| | | |
| | | resp, err := client.Do(req) |
| | | if err != nil { |
| | | return nil, fmt.Errorf("failed to get cluster list: %v", err) |
| | | } |
| | | defer resp.Body.Close() |
| | | |
| | | if resp.StatusCode != http.StatusOK { |
| | | return nil, fmt.Errorf("failed to get cluster list: unexpected status code %d", resp.StatusCode) |
| | | } |
| | | |
| | | // Parse the API response |
| | | var clustersResponse ClustersResponse |
| | | err = json.NewDecoder(resp.Body).Decode(&clustersResponse) |
| | | if err != nil { |
| | | return nil, fmt.Errorf("failed to decode cluster list response: %v", err) |
| | | } |
| | | |
| | | var clusters = make([]string, 0) |
| | | // Print cluster names |
| | | for _, cluster := range clustersResponse.Data { |
| | | clusters = append(clusters, cluster.ID) |
| | | fmt.Printf("Cluster ID: %s, Name: %s\n", cluster.ID, cluster.Name) |
| | | } |
| | | |
| | | return clusters, nil |
| | | } |
| | | |
| | | type Node struct { |
| | | ClusterName string `json:"clusterName"` |
| | | Roles []string `json:"roles"` |
| | |
| | | SSHPassword string `json:"sshPassword"` |
| | | SSHPort int `json:"sshPort"` |
| | | } |
| | | |
| | | |
| | | type ClusterCreateRequest struct { |
| | | Name string `json:"name"` |
| | |
| | | |
| | | func CreateCluster(rancherClusterConfig rancher.RancherClusterConfig, clusterName string) error { |
| | | requestBody := createClusterData(clusterName) |
| | | fmt.Println(rancherClusterConfig.RancherURL) |
| | | fmt.Println(rancherClusterConfig.RancherURL) |
| | | |
| | | url := fmt.Sprintf("%s/v3/clusters", rancherClusterConfig.RancherURL) |
| | | req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody)) |
| | |
| | | ) |
| | | |
| | | func rancher_install_test(node rancher.Node) { |
| | | /* nodes := []Node{ |
| | | /* nodes := []Node{ |
| | | { |
| | | Roles: []string{"etcd", "controlplane", "worker"}, |
| | | IP: "192.168.20.189", |
| | |
| | | os.Exit(0) |
| | | } |
| | | |
| | | func cluster_test(clusterName string,rancherClusterConfig rancher.RancherClusterConfig) { |
| | | func cluster_test(clusterName string, rancherClusterConfig rancher.RancherClusterConfig) { |
| | | |
| | | // Create the cluster |
| | | // Rancher configuration |
| | |
| | | BearerToken: "token-nnrsc:w68zdt8s47fnpjd5xqdl5hhzpz4j2d56kt5nx49nsswcbpdzc28kh5", |
| | | }*/ |
| | | |
| | | fmt.Println(rancherClusterConfig) |
| | | fmt.Println(rancherClusterConfig.RancherURL) |
| | | fmt.Println(rancherClusterConfig.BearerToken) |
| | | fmt.Println(rancherClusterConfig) |
| | | fmt.Println(rancherClusterConfig.RancherURL) |
| | | fmt.Println(rancherClusterConfig.BearerToken) |
| | | // Deploy clusterId |
| | | clusterID, err := cluster.GetClusterID(rancherClusterConfig.RancherURL, rancherClusterConfig.BearerToken, clusterName) |
| | | if err != nil { |
| | |
| | | } |
| | | |
| | | if rancherInstallCmd.Parsed() { |
| | | // 初始化配置解析库 |
| | | viper.SetConfigName("config") |
| | | viper.SetConfigType("yaml") |
| | | viper.AddConfigPath("./config") |
| | | |
| | | // 读取配置文件 |
| | | err := viper.ReadInConfig() |
| | | if err != nil { |
| | | log.Fatalf("Failed to read config file: %v", err) |
| | | } |
| | | //// 初始化配置解析库 |
| | | //viper.SetConfigName("config") |
| | | //viper.SetConfigType("yaml") |
| | | //viper.AddConfigPath("./config") |
| | | // |
| | | //// 读取配置文件 |
| | | //err := viper.ReadInConfig() |
| | | //if err != nil { |
| | | // log.Fatalf("Failed to read config file: %v", err) |
| | | //} |
| | | |
| | | // 解析配置文件中的字段 |
| | | var rancherClusterConfig rancher.RancherClusterConfig |
| | | err = viper.Unmarshal(&rancherClusterConfig) |
| | | if err != nil { |
| | | log.Fatalf("Failed to unmarshal config: %v", err) |
| | | var rancherClusterConfig rancher.RancherClusterConfig |
| | | //err = viper.Unmarshal(&rancherClusterConfig) |
| | | //if err != nil { |
| | | // log.Fatalf("Failed to unmarshal config: %v", err) |
| | | //} |
| | | |
| | | rancherClusterConfig.Nodes = make([]rancher.Node, 3) |
| | | rancherClusterConfig.Nodes[0] = rancher.Node{ |
| | | Roles: []string{"worker"}, |
| | | IP: "192.168.20.189", |
| | | SSHUsername: "basic", |
| | | SSHPassword: "123", |
| | | SSHPort: 22, |
| | | } |
| | | |
| | | rancher_install_test(rancherClusterConfig.Nodes[0]) |
| | | os.Exit(1) |
| | | } |
| | |
| | | } |
| | | |
| | | // 解析配置文件中的字段 |
| | | var rancherClusterConfig rancher.RancherClusterConfig |
| | | var rancherClusterConfig rancher.RancherClusterConfig |
| | | err = viper.Unmarshal(&rancherClusterConfig) |
| | | if err != nil { |
| | | log.Fatalf("Failed to unmarshal config: %v", err) |
| | | } |
| | | clusterName := "kubernetus" |
| | | fmt.Println(rancherClusterConfig) |
| | | clusterName := "kubernetus" |
| | | fmt.Println(rancherClusterConfig) |
| | | cluster_test(clusterName, rancherClusterConfig) |
| | | } |
| | | |
| | |
| | | "basic.com/aps/aps_deploy.git/src/util" |
| | | ) |
| | | |
| | | |
| | | type Node struct { |
| | | Roles []string `json:"roles"` |
| | | IP string `json:"ip"` |
| | |
| | | SSHPort int `json:"sshPort"` |
| | | } |
| | | |
| | | |
| | | type RancherClusterConfig struct { |
| | | RancherURL string `json:"rancherURL"` |
| | | BearerToken string `json:"bearerToken"` |
| | | ClusterName string `mapstructure:"ClusterName"` |
| | | Nodes []Node `mapstructure:"Nodes"` |
| | | ClusterName string `mapstructure:"ClusterName"` |
| | | Nodes []Node `mapstructure:"Nodes"` |
| | | } |
| | | |
| | | |
| | | func isRancherInstalled(ip, username, password string, sshPort int) (bool, error) { |
| | | // 检查Rancher容器是否已运行 |