Skip to content

Commit

Permalink
WIP: Add TerminateSOL method
Browse files Browse the repository at this point in the history
  • Loading branch information
zevweiss committed Dec 13, 2023
1 parent b299d27 commit 4081957
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
70 changes: 70 additions & 0 deletions bmc/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package bmc

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
)

// SOLTerminator for terminating SOL sessions on a BMC.
type SOLTerminator interface {
TerminateSOL(ctx context.Context) (err error)
}

// terminatorProvider is an internal struct to correlate an implementation/provider and its name
type terminatorProvider struct {
name string
solTerminator SOLTerminator
}

// terminateSOL tries all implementations for a successful SOL termination
func terminateSOL(ctx context.Context, timeout time.Duration, b []terminatorProvider) (ok bool, metadata Metadata, err error) {
var metadataLocal Metadata

Check warning on line 25 in bmc/sol.go

View check run for this annotation

Codecov / codecov/patch

bmc/sol.go#L24-L25

Added lines #L24 - L25 were not covered by tests

for _, elem := range b {
if elem.solTerminator == nil {
continue

Check warning on line 29 in bmc/sol.go

View check run for this annotation

Codecov / codecov/patch

bmc/sol.go#L27-L29

Added lines #L27 - L29 were not covered by tests
}
select {
case <-ctx.Done():
err = multierror.Append(err, ctx.Err())

Check warning on line 33 in bmc/sol.go

View check run for this annotation

Codecov / codecov/patch

bmc/sol.go#L31-L33

Added lines #L31 - L33 were not covered by tests

return false, metadata, err
default:
metadataLocal.ProvidersAttempted = append(metadataLocal.ProvidersAttempted, elem.name)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
err := elem.solTerminator.TerminateSOL(ctx)
if err != nil {
err = multierror.Append(err, errors.WithMessagef(err, "provider: %v", elem.name))

Check failure on line 42 in bmc/sol.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
continue

Check warning on line 43 in bmc/sol.go

View check run for this annotation

Codecov / codecov/patch

bmc/sol.go#L35-L43

Added lines #L35 - L43 were not covered by tests
}
metadataLocal.SuccessfulProvider = elem.name
return true, metadataLocal, nil

Check warning on line 46 in bmc/sol.go

View check run for this annotation

Codecov / codecov/patch

bmc/sol.go#L45-L46

Added lines #L45 - L46 were not covered by tests
}
}
return false, metadataLocal, multierror.Append(err, errors.New("failed to terminate SOL session"))

Check warning on line 49 in bmc/sol.go

View check run for this annotation

Codecov / codecov/patch

bmc/sol.go#L49

Added line #L49 was not covered by tests
}

// TerminateSOLFromInterfaces identifies implementations of the SOLTerminator interface and passes them to the terminateSOL() wrapper method.
func TerminateSOLFromInterfaces(ctx context.Context, timeout time.Duration, generic []interface{}) (ok bool, metadata Metadata, err error) {
terminators := make([]terminatorProvider, 0)
for _, elem := range generic {
temp := terminatorProvider{name: getProviderName(elem)}
switch p := elem.(type) {
case SOLTerminator:
temp.solTerminator = p
terminators = append(terminators, temp)
default:
e := fmt.Sprintf("not an SOLTerminator implementation: %T", p)
err = multierror.Append(err, errors.New(e))

Check warning on line 63 in bmc/sol.go

View check run for this annotation

Codecov / codecov/patch

bmc/sol.go#L53-L63

Added lines #L53 - L63 were not covered by tests
}
}
if len(terminators) == 0 {
return false, metadata, multierror.Append(err, errors.New("no SOLTerminator implementations found"))

Check warning on line 67 in bmc/sol.go

View check run for this annotation

Codecov / codecov/patch

bmc/sol.go#L66-L67

Added lines #L66 - L67 were not covered by tests
}
return terminateSOL(ctx, timeout, terminators)

Check warning on line 69 in bmc/sol.go

View check run for this annotation

Codecov / codecov/patch

bmc/sol.go#L69

Added line #L69 was not covered by tests
}
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,13 @@ func (c *Client) ResetBMC(ctx context.Context, resetType string) (ok bool, err e
return ok, err
}

// TerminateSOL pass through library function to terminate active SOL sessions
func (c *Client) TerminateSOL(ctx context.Context) (ok bool, err error) {
ok, metadata, err := bmc.TerminateSOLFromInterfaces(ctx, c.perProviderTimeout(ctx), c.registry().GetDriverInterfaces())
c.setMetadata(metadata)
return ok, err

Check warning on line 533 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L530-L533

Added lines #L530 - L533 were not covered by tests
}

// Inventory pass through library function to collect hardware and firmware inventory
func (c *Client) Inventory(ctx context.Context) (device *common.Device, err error) {
ctx, span := c.traceprovider.Tracer(pkgName).Start(ctx, "Inventory")
Expand Down
10 changes: 10 additions & 0 deletions internal/ipmi/ipmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,13 @@ func (i *Ipmi) GetSystemEventLogRaw(ctx context.Context) (eventlog string, err e

return output, nil
}

func (i *Ipmi) TerminateSOL(ctx context.Context) (err error) {
out, err := i.run(ctx, []string{"sol", "deactivate"})
// Don't treat this as a failure (we just want to ensure there
// isn't an active SOL session left open)
if strings.TrimSpace(out) == "Info: SOL payload already de-activated" {
err = nil
}
return err
}
6 changes: 6 additions & 0 deletions providers/ipmitool/ipmitool.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
providers.FeatureClearSystemEventLog,
providers.FeatureGetSystemEventLog,
providers.FeatureGetSystemEventLogRaw,
providers.FeatureTerminateSOL,
}
)

Expand Down Expand Up @@ -149,6 +150,11 @@ func (c *Conn) BmcReset(ctx context.Context, resetType string) (ok bool, err err
return c.ipmitool.PowerResetBmc(ctx, resetType)
}

// TerminateSOL will terminate active SOL sessions
func (c *Conn) TerminateSOL(ctx context.Context) (err error) {
return c.ipmitool.TerminateSOL(ctx)

Check warning on line 155 in providers/ipmitool/ipmitool.go

View check run for this annotation

Codecov / codecov/patch

providers/ipmitool/ipmitool.go#L154-L155

Added lines #L154 - L155 were not covered by tests
}

// UserRead list all users
func (c *Conn) UserRead(ctx context.Context) (users []map[string]string, err error) {
return c.ipmitool.ReadUsers(ctx)
Expand Down
3 changes: 3 additions & 0 deletions providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ const (

// FeatureFirmwareUploadInitiateInstall identifies an implementation that uploads firmware _and_ initiates the install process.
FeatureFirmwareUploadInitiateInstall registrar.Feature = "uploadandinitiateinstall"

// FeatureTerminateSOL means an implementation that can terminate active SOL sessions
FeatureTerminateSOL registrar.Feature = "terminatesol"
)

0 comments on commit 4081957

Please sign in to comment.