-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: Add example to upload and unmount a floppy image
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |