Skip to content

Commit

Permalink
examples: Merge changes from master
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Apr 26, 2022
1 parent 5cf214e commit edb3e1c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 86 deletions.
62 changes: 0 additions & 62 deletions examples/v1/firmware/firmware.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"crypto/x509"
"flag"
"io/ioutil"
"log"
"os"
"strconv"
"time"

"github.com/bmc-toolbox/bmclib"
"github.com/bmc-toolbox/bmclib/devices"
"github.com/bombsimon/logrusr/v2"
"github.com/sirupsen/logrus"
)
Expand All @@ -26,6 +28,8 @@ func main() {
withSecureTLS := flag.Bool("secure-tls", false, "Enable secure TLS")
certPoolPath := flag.String("cert-pool", "", "Path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
firmwarePath := flag.String("firmware", "", "The firmware path to read")
firmwareVersion := flag.String("version", "", "The firmware version being installed")

flag.Parse()

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
Expand Down Expand Up @@ -62,11 +66,13 @@ func main() {

defer cl.Close(ctx)

v, err := cl.GetBMCVersion(ctx)
// collect inventory
inventory, err := cl.Inventory(ctx)
if err != nil {
l.Fatal(err, "unable to retrieve BMC version")
l.Fatal(err)
}
logger.Info("BMC version", v)

l.WithField("bmc-version", inventory.BMC.Firmware.Installed).Info()

// open file handle
fh, err := os.Open(*firmwarePath)
Expand All @@ -75,14 +81,17 @@ func main() {
}
defer fh.Close()

fi, err := fh.Stat()
// SlugBMC hardcoded here, this can be any of the existing component slugs from devices/constants.go
// assuming that the BMC provider implements the required component firmware update support
taskID, err := cl.FirmwareInstall(ctx, devices.SlugBMC, devices.FirmwareApplyOnReset, true, fh)
if err != nil {
l.Fatal(err)
l.Error(err)
}

err = cl.UpdateBMCFirmware(ctx, fh, fi.Size())
state, err := cl.FirmwareInstallStatus(ctx, taskID, devices.SlugBMC, *firmwareVersion)
if err != nil {
l.Fatal(err)
log.Fatal(err)
}
logger.WithValues("host", *host).Info("Updated BMC firmware")

l.WithField("state", state).Info("BMC firmware install state")
}
35 changes: 27 additions & 8 deletions examples/v1/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package main

import (
"context"
"crypto/x509"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"strconv"
"time"

"github.com/bmc-toolbox/bmclib"
Expand All @@ -16,21 +20,36 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()

// set BMC parameters here
host := ""
port := ""
user := ""
pass := ""
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")
port := flag.Int("port", 443, "BMC port to connect to")
withSecureTLS := flag.Bool("secure-tls", false, "Enable secure TLS")
certPoolFile := flag.String("cert-pool", "", "Path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
flag.Parse()

l := logrus.New()
l.Level = logrus.DebugLevel
logger := logrusr.New(l)

if host == "" || user == "" || pass == "" {
log.Fatal("required host/user/pass parameters not defined")
clientOpts := []bmclib.Option{bmclib.WithLogger(logger)}

if *withSecureTLS {
var pool *x509.CertPool
if *certPoolFile != "" {
pool = x509.NewCertPool()
data, err := ioutil.ReadFile(*certPoolFile)
if err != nil {
l.Fatal(err)
}
pool.AppendCertsFromPEM(data)
}
// a nil pool uses the system certs
clientOpts = append(clientOpts, bmclib.WithSecureTLS(pool))
}

cl := bmclib.NewClient(host, port, user, pass, bmclib.WithLogger(logger))
cl := bmclib.NewClient(*host, strconv.Itoa(*port), *user, *pass, clientOpts...)
cl.Registry.Drivers = cl.Registry.Using("redfish")

err := cl.Open(ctx)
if err != nil {
Expand Down
13 changes: 5 additions & 8 deletions examples/v1/status/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,18 @@ func main() {
}
defer cl.Close(ctx)

version, err := cl.GetBMCVersion(ctx)
inventory, err := cl.Inventory(ctx)
if err != nil {
l.WithError(err).Error()
l.Fatal(err)
}
l.WithField("bmc-version", version).Info()

l.WithField("bmc-version", inventory.BMC.Firmware.Installed).Info()

state, err := cl.GetPowerState(ctx)
if err != nil {
l.WithError(err).Error()
}
l.WithField("power-state", state).Info()

version, err = cl.GetBIOSVersion(ctx)
if err != nil {
l.WithError(err).Error()
}
l.WithField("bios-version", version).Info()
l.WithField("bios-version", inventory.BIOS.Firmware.Installed).Info()
}

0 comments on commit edb3e1c

Please sign in to comment.