Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a script to enroll multiple servers #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions script/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"bufio"
"context"
"encoding/json"
"fmt"
"os"

coapiv1 "github.com/metal-toolbox/conditionorc/pkg/api/v1/types"
"github.com/metal-toolbox/mctl/internal/app"
"github.com/metal-toolbox/mctl/pkg/model"
)

type ParsedHardware struct {
ID string
FacilityID string
IPMIAddress string
IPMIUsername string
IPMIPassword string
}

func main() {
// The provided text data
file, err := os.Open("test.txt")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()

apps := app.App{
Config: &model.Config{
Conditions: &model.ConfigOIDC{
Endpoint: "http://localhost:9001",
Disable: true,
ClientID: "",
IssuerEndpoint: "",
Scopes: []string{},
PkceCallbackURL: "",
},
},
Reauth: false,
}
client, err := app.NewConditionsClient(context.Background(), apps.Config.Conditions, apps.Reauth)
if err != nil {
fmt.Printf("failed to connect to conditionorc %v", err)
return
}

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
var hardwareMap map[string]interface{}

// Parse JSON string into the map
err := json.Unmarshal([]byte(line), &hardwareMap)
if err != nil {
fmt.Printf("Error parsing JSON: %w", err)
return
}

if hardwareMap["type"] != "server" {
continue
}

var enrollHardware = ParsedHardware{}
enrollHardware.ID = hardwareMap["id"].(string)

if facilityMap, ok := hardwareMap["facility"].(map[string]interface{}); ok {
enrollHardware.FacilityID = facilityMap["id"].(string)
}

if dataMap, ok := hardwareMap["data"].(map[string]interface{}); ok {
if ipmiMap, ok := dataMap["ipmi"].(map[string]interface{}); ok {
enrollHardware.IPMIAddress = ipmiMap["address"].(string)
enrollHardware.IPMIUsername = ipmiMap["username"].(string)
enrollHardware.IPMIPassword = ipmiMap["password"].(string)
}
}
fmt.Println()

params, err := json.Marshal(coapiv1.AddServerParams{
Facility: enrollHardware.FacilityID,
IP: enrollHardware.IPMIAddress,
Username: enrollHardware.IPMIUsername,
Password: enrollHardware.IPMIPassword,
})
if err != nil {
fmt.Printf("Error marshal %v", err)
}

conditionCreate := coapiv1.ConditionCreate{
Parameters: params,
}
fmt.Printf("conditionCreate %v\n", conditionCreate)

_, err = client.ServerEnroll(context.Background(), enrollHardware.ID, conditionCreate)
if err != nil {
fmt.Printf("failed to enroll server %v\n", err)
return
}
}
}