Skip to content

Commit

Permalink
new cli
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Nov 15, 2024
1 parent 1f4d720 commit f91b6d9
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 125 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ func main() {

Sample Lab Machines
172.19.100.107
User: root
Default PW: 9XW9FR9PN3FF

172.19.100.108
User: root
Default PW: K4P4NVAK9KVK

TODO:
Expand Down
25 changes: 25 additions & 0 deletions cli/board.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"github.com/urfave/cli/v2"
)

var boardCmd = &cli.Command{
Name: "board",
Usage: "gather machine board details",
Flags: flags,
Action: func(ctx *cli.Context) error {
c, err := getHalConnection(log)
if err != nil {
return err
}
board := c.Board()
log.Infow("board", "bandtype", bandtype, "host", host, "result", board.String(), "bios", board.BIOS, "powermetric", board.PowerMetric, "powersupplies", board.PowerSupplies, "ledstate", board.IndicatorLED)
bmc, err := outBandBMCConnection.BMC()
if err != nil {
return err
}
log.Infow("bmc", "result", bmc)
return nil
},
}
20 changes: 20 additions & 0 deletions cli/led.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/urfave/cli/v2"
)

var ledCmd = &cli.Command{
Name: "led",
Usage: "gather machine led state",
Flags: flags,
Action: func(ctx *cli.Context) error {
// c, err := getHalConnection(log)
// if err != nil {
// return err
// }
// ledstate := c.IdentifyLEDState()
// log.Infow("board", "bandtype", bandtype, "host", host, "result", board.String(), "bios", board.BIOS, "powermetric", board.PowerMetric, "powersupplies", board.PowerSupplies)
return nil
},
}
301 changes: 178 additions & 123 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -1,154 +1,209 @@
package main

import (
"errors"
"flag"
"fmt"
"os"
"time"

"github.com/metal-stack/go-hal"

"github.com/metal-stack/go-hal/connect"
"github.com/metal-stack/go-hal/pkg/api"
"github.com/metal-stack/go-hal/pkg/logger"
"github.com/urfave/cli/v2"
)

var (
band = flag.String("bandtype", "outband", "inband/outband")
user = flag.String("user", "ADMIN", "bmc username")
password = flag.String("password", "ADMIN", "bmc password")
host = flag.String("host", "localhost", "bmc host")
port = flag.Int("port", 623, "bmc port")

errHelp = errors.New("usage: -bandtype inband|outband")
)

func main() {
flag.Parse()

log := logger.New()
switch *band {
case "inband":
inband(log)
case "outband":
outband(log)
default:
fmt.Printf("%s\n", errHelp)
os.Exit(1)
log logger.Logger

bandtype string
user string
password string
host string
port int

bandtypeFlag = &cli.StringFlag{
Name: "bandtype",
Value: "outband",
Usage: "inband/outband",
Destination: &bandtype,
}
}

func inband(log logger.Logger) {
ib, err := connect.InBand(log)
if err != nil {
panic(err)
userFlag = &cli.StringFlag{
Name: "user",
Value: "ADMIN",
Usage: "bmc user",
Destination: &user,
}
uuid, err := ib.UUID()
if err != nil {
panic(err)
passwordFlag = &cli.StringFlag{
Name: "password",
Value: "",
Usage: "bmc password",
Destination: &password,
}
fmt.Printf("UUID:%s\n", uuid)
}

func outband(log logger.Logger) {
ob, err := connect.OutBand(*host, *port, *user, *password, log)
if err != nil {
panic(err)
hostFlag = &cli.StringFlag{
Name: "host",
Value: "localhost",
Usage: "bmc host",
Destination: &host,
}

uu := make(map[string]string)
ee := make(map[string]error)

b := ob.Board()
fmt.Printf("Board:\n%#v\n", b)
fmt.Printf("Power:\n%#v\n", b.PowerMetric)
fmt.Printf("PowerSupplies:\n%#v\n", b.PowerSupplies)

bmc, err := ob.BMCConnection().BMC()
if err != nil {
ee["BMCConnection.BMC"] = err
portFlag = &cli.IntFlag{
Name: "port",
Value: 623,
Usage: "bmc port",
Destination: &port,
}
fmt.Printf("BMC:\n%#v\n", bmc)

_, err = ob.UUID()
if err != nil {
ee["UUID"] = err
flags = []cli.Flag{
bandtypeFlag,
hostFlag,
portFlag,
userFlag,
passwordFlag,
}
outBandBMCConnection api.OutBandBMCConnection
)

ps, err := ob.PowerState()
if err != nil {
ee["PowerState"] = err
}
if ps == hal.PowerUnknownState {
uu["PowerState"] = "unexpected power state: PowerUnknownState"
}
err = ob.PowerOff()
if err != nil {
fmt.Printf("error during power off: %v\n", err)
func main() {
log = logger.New()

app := &cli.App{
Name: "hal",
Usage: "try bmc commands",
Commands: []*cli.Command{
uuidCmd,
boardCmd,
ledCmd,
powerCmd,
},
}

board := ob.Board()
fmt.Println("LED: " + board.IndicatorLED)

err = ob.PowerCycle()
if err != nil {
ee["PowerCycle"] = err
if err := app.Run(os.Args); err != nil {
panic(err)
}

board = ob.Board()
fmt.Println("LED: " + board.IndicatorLED)

if false {
err = ob.PowerOff()
if err != nil {
fmt.Printf("error during power off: %v\n", err)
}

time.Sleep(10 * time.Second)
if outBandBMCConnection != nil {
outBandBMCConnection.Close()
}

err = ob.PowerOn()
if err != nil {
fmt.Printf("error during power on: %v\n", err)
}
os.Exit(1)
}

// ipmitool sel
err = ob.IdentifyLEDState(hal.IdentifyLEDStateOff)
if err != nil {
ee["IdentifyLEDState"] = err
}
err = ob.IdentifyLEDOn()
func getHalConnection(log logger.Logger) (hal.Hal, error) {
switch bandtype {
case "inband":
ib, err := connect.InBand(log)
if err != nil {
ee["IdentifyLEDOn"] = err
return nil, err
}
err = ob.IdentifyLEDOff()
return ib, nil
case "outband":
ob, err := connect.OutBand(host, port, user, password, log)
if err != nil {
ee["IdentifyLEDOff"] = err
}

//_, err = ob.UpdateBIOS()
//if err != nil {
// ee["UpdateBIOS"] = err
//}
//
//_, err = ob.UpdateBMC()
//if err != nil {
// ee["UpdateBMC"] = err
//}
}

if len(uu) > 0 {
fmt.Println("Unexpected things:")
for m, u := range uu {
fmt.Printf("%s: %s\n", m, u)
return nil, err
}
}

if len(ee) > 0 {
fmt.Println("Failed checks:")
for m, err := range ee {
fmt.Printf("%s: %s\n", m, err.Error())
}
} else {
fmt.Println("Check succeeded")
outBandBMCConnection = ob.BMCConnection()
return ob, nil
default:
return nil, fmt.Errorf("unknown bandtype %s", bandtype)
}
}

// func outband(log logger.Logger) {
// ob, err := connect.OutBand(*host, *port, *user, *password, log)
// if err != nil {
// panic(err)
// }

// uu := make(map[string]string)
// ee := make(map[string]error)

// b := ob.Board()
// fmt.Printf("Board:\n%#v\n", b)
// fmt.Printf("Power:\n%#v\n", b.PowerMetric)
// fmt.Printf("PowerSupplies:\n%#v\n", b.PowerSupplies)

// bmc, err := ob.BMCConnection().BMC()
// if err != nil {
// ee["BMCConnection.BMC"] = err
// }
// fmt.Printf("BMC:\n%#v\n", bmc)

// _, err = ob.UUID()
// if err != nil {
// ee["UUID"] = err
// }

// ps, err := ob.PowerState()
// if err != nil {
// ee["PowerState"] = err
// }
// if ps == hal.PowerUnknownState {
// uu["PowerState"] = "unexpected power state: PowerUnknownState"
// }
// err = ob.PowerOff()
// if err != nil {
// fmt.Printf("error during power off: %v\n", err)
// }

// board := ob.Board()
// fmt.Println("LED: " + board.IndicatorLED)

// err = ob.PowerCycle()
// if err != nil {
// ee["PowerCycle"] = err
// }

// board = ob.Board()
// fmt.Println("LED: " + board.IndicatorLED)

// if false {
// err = ob.PowerOff()
// if err != nil {
// fmt.Printf("error during power off: %v\n", err)
// }

// time.Sleep(10 * time.Second)

// err = ob.PowerOn()
// if err != nil {
// fmt.Printf("error during power on: %v\n", err)
// }

// // ipmitool sel
// err = ob.IdentifyLEDState(hal.IdentifyLEDStateOff)
// if err != nil {
// ee["IdentifyLEDState"] = err
// }
// err = ob.IdentifyLEDOn()
// if err != nil {
// ee["IdentifyLEDOn"] = err
// }
// err = ob.IdentifyLEDOff()
// if err != nil {
// ee["IdentifyLEDOff"] = err
// }

// //_, err = ob.UpdateBIOS()
// //if err != nil {
// // ee["UpdateBIOS"] = err
// //}
// //
// //_, err = ob.UpdateBMC()
// //if err != nil {
// // ee["UpdateBMC"] = err
// //}
// }

// if len(uu) > 0 {
// fmt.Println("Unexpected things:")
// for m, u := range uu {
// fmt.Printf("%s: %s\n", m, u)
// }
// }

// if len(ee) > 0 {
// fmt.Println("Failed checks:")
// for m, err := range ee {
// fmt.Printf("%s: %s\n", m, err.Error())
// }
// } else {
// fmt.Println("Check succeeded")
// }
// }
Loading

0 comments on commit f91b6d9

Please sign in to comment.