From 8c2a5009265f642d1e0b17df446c9dcae868d24f Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Tue, 8 Oct 2024 11:27:54 -0600 Subject: [PATCH] Add example for virtual media: This helps users see how to use the virtual media mounting capabilities. Signed-off-by: Jacob Weinstock --- examples/virtualmedia/doc.go | 18 +++++++++++++ examples/virtualmedia/main.go | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 examples/virtualmedia/doc.go create mode 100644 examples/virtualmedia/main.go diff --git a/examples/virtualmedia/doc.go b/examples/virtualmedia/doc.go new file mode 100644 index 00000000..2dd0ef3a --- /dev/null +++ b/examples/virtualmedia/doc.go @@ -0,0 +1,18 @@ +/* +Virtual Media is an example command to mount and umount virtual media (ISO) on a BMC. + + # mount an ISO + $ go run examples/virtualmedia/main.go \ + -host 10.1.2.3 \ + -user root \ + -password calvin \ + -iso http://example.com/image.iso + + # unmount an ISO + $ go run examples/virtualmedia/main.go \ + -host 10.1.2.3 \ + -user root \ + -password calvin \ + -iso "" +*/ +package main diff --git a/examples/virtualmedia/main.go b/examples/virtualmedia/main.go new file mode 100644 index 00000000..6dd0ec14 --- /dev/null +++ b/examples/virtualmedia/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "context" + "flag" + "fmt" + "log/slog" + "os" + "time" + + "github.com/bmc-toolbox/bmclib/v2" + "github.com/go-logr/logr" +) + +func main() { + + ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) + defer cancel() + + user := flag.String("user", "", "BMC username, required") + pass := flag.String("password", "", "BMC password, required") + host := flag.String("host", "", "BMC hostname or IP address, required") + isoURL := flag.String("iso", "", "The HTTP URL to the ISO to be mounted, leave empty to unmount") + flag.Parse() + + if *user == "" || *pass == "" || *host == "" { + fmt.Fprintln(os.Stderr, "user, password, and host are required") + flag.PrintDefaults() + os.Exit(1) + } + + l := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})) + log := logr.FromSlogHandler(l.Handler()) + + cl := bmclib.NewClient(*host, *user, *pass, bmclib.WithLogger(log)) + if err := cl.Open(ctx); err != nil { + panic(err) + } + defer cl.Close(ctx) + + ok, err := cl.SetVirtualMedia(ctx, "CD", *isoURL) + if err != nil { + log.Info("debugging", "metadata", cl.GetMetadata()) + panic(err) + } + if !ok { + log.Info("debugging", "metadata", cl.GetMetadata()) + panic("failed virtual media operation") + } + log.Info("virtual media operation successful", "metadata", cl.GetMetadata()) +}