-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
98 lines (78 loc) · 2.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"strings"
"time"
bmclib "github.com/bmc-toolbox/bmclib/v2"
"github.com/bmc-toolbox/bmclib/v2/providers"
logrusr "github.com/bombsimon/logrusr/v2"
"github.com/sirupsen/logrus"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
// Command line option flag parsing
user := flag.String("user", "", "Username to login with")
pass := flag.String("password", "", "Username to login with")
host := flag.String("host", "", "BMC hostname to connect to")
mode := flag.String("mode", "get", "Mode [get,set,reset]")
dfile := flag.String("file", "", "Read data from file")
flag.Parse()
// Logger configuration
l := logrus.New()
l.Level = logrus.DebugLevel
logger := logrusr.New(l)
// bmclib client abstraction
clientOpts := []bmclib.Option{bmclib.WithLogger(logger)}
client := bmclib.NewClient(*host, *user, *pass, clientOpts...)
client.Registry.Drivers = client.Registry.Supports(providers.FeatureGetBiosConfiguration, providers.FeatureSetBiosConfiguration, providers.FeatureResetBiosConfiguration)
err := client.Open(ctx)
if err != nil {
l.Fatal(err, "bmc login failed")
}
defer client.Close(ctx)
// Operating mode selection
switch strings.ToLower(*mode) {
case "get":
// retrieve bios configuration
biosConfig, err := client.GetBiosConfiguration(ctx)
if err != nil {
l.Fatal(err)
}
fmt.Printf("biosConfig: %+v\n", biosConfig)
case "set":
exampleConfig := make(map[string]string)
if *dfile != "" {
jsonFile, err := os.Open(*dfile)
if err != nil {
l.Fatal(err)
}
defer jsonFile.Close()
jsonData, _ := io.ReadAll(jsonFile)
err = json.Unmarshal(jsonData, &exampleConfig)
if err != nil {
l.Fatal(err)
}
} else {
exampleConfig["TpmSecurity"] = "Off"
}
fmt.Println("Attempting to set BIOS configuration:")
fmt.Printf("exampleConfig: %+v\n", exampleConfig)
err := client.SetBiosConfiguration(ctx, exampleConfig)
if err != nil {
l.Error(err)
}
case "reset":
err := client.ResetBiosConfiguration(ctx)
if err != nil {
l.Error(err)
}
default:
l.Fatal("Unknown mode: " + *mode)
}
}