Skip to content

Commit

Permalink
examples: Add example to upload and unmount a floppy image
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Sep 11, 2023
1 parent 76a049a commit d4b5fbb
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/floppy-image/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
inventory is an example commmand that utilizes the 'v1' bmclib interface
methods to upload and mount, unmount a floppy image.
# mount image
$ go run examples/floppy-image/main.go \
-host 10.1.2.3 \
-user ADMIN \
-password hunter2 \
-image /tmp/floppy.img
# un-mount image
$ go run examples/floppy-image/main.go \
-host 10.1.2.3 \
-user ADMIN \
-password hunter2 \
-unmount
*/
package main
79 changes: 79 additions & 0 deletions examples/floppy-image/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"context"
"crypto/x509"
"flag"
"log"
"os"
"time"

bmclib "github.com/bmc-toolbox/bmclib/v2"
"github.com/bombsimon/logrusr/v2"
"github.com/sirupsen/logrus"
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()

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")
imagePath := flag.String("image", "", "The .img file to be uploaded")
unmountImage := flag.Bool("unmount", false, "Unmount floppy image")

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)

clientOpts := []bmclib.Option{bmclib.WithLogger(logger)}

if *withSecureTLS {
var pool *x509.CertPool
if *certPoolFile != "" {
pool = x509.NewCertPool()
data, err := os.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, *user, *pass, clientOpts...)
err := cl.Open(ctx)
if err != nil {
log.Fatal(err, "bmc login failed")
}

defer cl.Close(ctx)

if *unmountImage {
if err := cl.UnmountFloppyImage(ctx); err != nil {
log.Fatal(err)
}

return
}

// open file handle
fh, err := os.Open(*imagePath)
if err != nil {
l.Fatal(err)
}
defer fh.Close()

err = cl.UploadFloppyImage(ctx, fh)
if err != nil {
l.Fatal(err)
}

l.WithField("img", *imagePath).Info("image mounted successfully")
}

0 comments on commit d4b5fbb

Please sign in to comment.