Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use bmc tags from metal-lib #323

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/metal-api/internal/grpc/boot-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
v1 "github.com/metal-stack/metal-api/pkg/api/v1"
"github.com/metal-stack/metal-lib/bus"
"github.com/metal-stack/metal-lib/pkg/tag"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -417,9 +418,9 @@ func (b *BootService) AbortReinstall(ctx context.Context, req *v1.BootServiceAbo

func (b *BootService) setBootOrderDisk(m *metal.Machine) {
evt := metal.MachineEvent{
Type: metal.COMMAND,
Type: tag.MachineEventCommand,
Cmd: &metal.MachineExecCommand{
Command: metal.MachineDiskCmd,
Command: tag.MachineDiskCmd,
TargetMachineID: m.ID,
IPMI: &m.IPMI,
},
Expand Down
40 changes: 11 additions & 29 deletions cmd/metal-api/internal/metal/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/dustin/go-humanize"
mn "github.com/metal-stack/metal-lib/pkg/net"
"github.com/metal-stack/metal-lib/pkg/tag"
)

// A MState is an enum which indicates the state of a machine
Expand Down Expand Up @@ -368,42 +369,23 @@ func (m *Machine) HasMAC(mac string) bool {
return false
}

// A MachineCommand is an alias of a string
type MachineCommand string

// our supported machines commands.
const (
MachineOnCmd MachineCommand = "ON"
MachineOffCmd MachineCommand = "OFF"
MachineResetCmd MachineCommand = "RESET"
MachineCycleCmd MachineCommand = "CYCLE"
MachineBiosCmd MachineCommand = "BIOS"
MachineDiskCmd MachineCommand = "DISK"
MachinePxeCmd MachineCommand = "PXE"
MachineReinstallCmd MachineCommand = "REINSTALL"
ChassisIdentifyLEDOnCmd MachineCommand = "LED-ON"
ChassisIdentifyLEDOffCmd MachineCommand = "LED-OFF"
UpdateFirmwareCmd MachineCommand = "UPDATE-FIRMWARE"
)

// A MachineExecCommand can be sent via a MachineEvent to execute
// the command against the specific machine. The specified command
// should be executed against the given target machine. The parameters
// is an optional array of strings which are implementation specific
// and dependent of the command.
// should be executed against the given target machine.
// FIXME next 4 structs are also implemented in metal-bmc, modifications here must be reflected there as well
type MachineExecCommand struct {
TargetMachineID string `json:"target,omitempty"`
Command MachineCommand `json:"cmd,omitempty"`
IPMI *IPMI `json:"ipmi,omitempty"`
FirmwareUpdate *FirmwareUpdate `json:"firmwareupdate,omitempty"`
TargetMachineID string `json:"target,omitempty"`
Command tag.MachineCommand `json:"cmd,omitempty"`
IPMI *IPMI `json:"ipmi,omitempty"`
FirmwareUpdate *FirmwareUpdate `json:"firmwareupdate,omitempty"`
}

// MachineEvent is propagated when a machine is create/updated/deleted.
type MachineEvent struct {
Type EventType `json:"type,omitempty"`
OldMachineID string `json:"old,omitempty"`
NewMachineID string `json:"new,omitempty"`
Cmd *MachineExecCommand `json:"cmd,omitempty"`
Type tag.MachineEventType `json:"type,omitempty"`
OldMachineID string `json:"old,omitempty"` // FIXME maybe unused
NewMachineID string `json:"new,omitempty"`
Cmd *MachineExecCommand `json:"cmd,omitempty"`
}

// AllocationEvent is propagated when a machine is allocated.
Expand Down
11 changes: 0 additions & 11 deletions cmd/metal-api/internal/metal/metal.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,12 @@ var (
AdminAccess = AdminGroups
)

// EventType is the type for event types.
type EventType string

// NSQTopic .
type NSQTopic struct {
Name string
PartitionAgnostic bool
}

// Some enums.
const (
CREATE EventType = "create"
UPDATE EventType = "update"
DELETE EventType = "delete"
COMMAND EventType = "command"
)

var (
TopicMachine = NSQTopic{Name: "machine", PartitionAgnostic: true}
TopicAllocation = NSQTopic{Name: "allocation", PartitionAgnostic: false}
Expand Down
7 changes: 0 additions & 7 deletions cmd/metal-api/internal/metal/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ const (
SwitchReplace SwitchMode = "replace"
)

// SwitchEvent is propagated when a switch needs to update its configuration.
type SwitchEvent struct {
Type EventType `json:"type"`
Machine Machine `json:"machine"`
Switches []Switch `json:"switches"`
}

// SwitchSync contains information about the last synchronization of the state held in the metal-api to a switch.
type SwitchSync struct {
Time time.Time `rethinkdb:"time" json:"time"`
Expand Down
44 changes: 22 additions & 22 deletions cmd/metal-api/internal/service/machine-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1505,9 +1505,9 @@ func (r machineResource) freeMachine(request *restful.Request, response *restful

logger := r.logger(request)

err = publishMachineCmd(logger, m, r.Publisher, metal.ChassisIdentifyLEDOffCmd)
err = publishMachineCmd(logger, m, r.Publisher, tag.ChassisIdentifyLEDOffCmd)
if err != nil {
logger.Error("unable to publish machine command", zap.String("command", string(metal.ChassisIdentifyLEDOffCmd)), zap.String("machineID", m.ID), zap.Error(err))
logger.Error("unable to publish machine command", zap.String("command", string(tag.ChassisIdentifyLEDOffCmd)), zap.String("machineID", m.ID), zap.Error(err))
}

err = r.actor.freeMachine(r.Publisher, m, r.headscaleClient, logger)
Expand Down Expand Up @@ -1678,9 +1678,9 @@ func (r *machineResource) reinstallMachine(request *restful.Request, response *r
return
}

err = publishMachineCmd(logger, m, r.Publisher, metal.MachineReinstallCmd)
err = publishMachineCmd(logger, m, r.Publisher, tag.MachineReinstallCmd)
if err != nil {
logger.Error("unable to publish machine command", zap.String("command", string(metal.MachineReinstallCmd)), zap.String("machineID", m.ID), zap.Error(err))
logger.Error("unable to publish machine command", zap.String("command", string(tag.MachineReinstallCmd)), zap.String("machineID", m.ID), zap.Error(err))
}

r.send(request, response, http.StatusOK, resp)
Expand Down Expand Up @@ -1715,7 +1715,7 @@ func deleteVRFSwitches(ds *datastore.RethinkStore, m *metal.Machine, logger *zap

func publishDeleteEvent(publisher bus.Publisher, m *metal.Machine, logger *zap.Logger) error {
logger.Info("publish machine delete event", zap.String("machineID", m.ID))
deleteEvent := metal.MachineEvent{Type: metal.DELETE, OldMachineID: m.ID, Cmd: &metal.MachineExecCommand{TargetMachineID: m.ID, IPMI: &m.IPMI}}
deleteEvent := metal.MachineEvent{Type: tag.MachineEventDelete, OldMachineID: m.ID, Cmd: &metal.MachineExecCommand{TargetMachineID: m.ID, IPMI: &m.IPMI}}
err := publisher.Publish(metal.TopicMachine.GetFQN(m.PartitionID), deleteEvent)
if err != nil {
logger.Error("cannot publish delete event", zap.String("machineID", m.ID), zap.Error(err))
Expand Down Expand Up @@ -1846,39 +1846,39 @@ func ResurrectMachines(ds *datastore.RethinkStore, publisher bus.Publisher, ep *
}

func (r *machineResource) machineOn(request *restful.Request, response *restful.Response) {
r.machineCmd(metal.MachineOnCmd, request, response)
r.machineCmd(tag.MachineOnCmd, request, response)
}

func (r *machineResource) machineOff(request *restful.Request, response *restful.Response) {
r.machineCmd(metal.MachineOffCmd, request, response)
r.machineCmd(tag.MachineOffCmd, request, response)
}

func (r *machineResource) machineReset(request *restful.Request, response *restful.Response) {
r.machineCmd(metal.MachineResetCmd, request, response)
r.machineCmd(tag.MachineResetCmd, request, response)
}

func (r *machineResource) machineCycle(request *restful.Request, response *restful.Response) {
r.machineCmd(metal.MachineCycleCmd, request, response)
r.machineCmd(tag.MachineCycleCmd, request, response)
}

func (r *machineResource) machineBios(request *restful.Request, response *restful.Response) {
r.machineCmd(metal.MachineBiosCmd, request, response)
r.machineCmd(tag.MachineBiosCmd, request, response)
}

func (r *machineResource) machineDisk(request *restful.Request, response *restful.Response) {
r.machineCmd(metal.MachineDiskCmd, request, response)
r.machineCmd(tag.MachineDiskCmd, request, response)
}

func (r *machineResource) machinePxe(request *restful.Request, response *restful.Response) {
r.machineCmd(metal.MachinePxeCmd, request, response)
r.machineCmd(tag.MachinePxeCmd, request, response)
}

func (r *machineResource) chassisIdentifyLEDOn(request *restful.Request, response *restful.Response) {
r.machineCmd(metal.ChassisIdentifyLEDOnCmd, request, response)
r.machineCmd(tag.ChassisIdentifyLEDOnCmd, request, response)
}

func (r *machineResource) chassisIdentifyLEDOff(request *restful.Request, response *restful.Response) {
r.machineCmd(metal.ChassisIdentifyLEDOffCmd, request, response)
r.machineCmd(tag.ChassisIdentifyLEDOffCmd, request, response)
}

func (r *machineResource) updateFirmware(request *restful.Request, response *restful.Response) {
Expand Down Expand Up @@ -1943,9 +1943,9 @@ func (r *machineResource) updateFirmware(request *restful.Request, response *res
}

evt := metal.MachineEvent{
Type: metal.COMMAND,
Type: tag.MachineEventCommand,
Cmd: &metal.MachineExecCommand{
Command: metal.UpdateFirmwareCmd,
Command: tag.UpdateFirmwareCmd,
TargetMachineID: m.ID,
IPMI: &m.IPMI,
FirmwareUpdate: &metal.FirmwareUpdate{
Expand All @@ -1971,7 +1971,7 @@ func (r *machineResource) updateFirmware(request *restful.Request, response *res
r.send(request, response, http.StatusOK, resp)
}

func (r *machineResource) machineCmd(cmd metal.MachineCommand, request *restful.Request, response *restful.Response) {
func (r *machineResource) machineCmd(cmd tag.MachineCommand, request *restful.Request, response *restful.Response) {
logger := r.logger(request)

id := request.PathParameter("id")
Expand All @@ -1986,7 +1986,7 @@ func (r *machineResource) machineCmd(cmd metal.MachineCommand, request *restful.
old := *newMachine
needsUpdate := false
switch cmd { // nolint:exhaustive
case metal.MachineResetCmd, metal.MachineOffCmd, metal.MachineCycleCmd:
case tag.MachineResetCmd, tag.MachineOffCmd, tag.MachineCycleCmd:
ev := metal.ProvisioningEvent{
Time: time.Now(),
Event: metal.ProvisioningEventPlannedReboot,
Expand All @@ -1997,13 +1997,13 @@ func (r *machineResource) machineCmd(cmd metal.MachineCommand, request *restful.
r.sendError(request, response, defaultError(err))
return
}
case metal.ChassisIdentifyLEDOnCmd:
case tag.ChassisIdentifyLEDOnCmd:
newMachine.LEDState = metal.ChassisIdentifyLEDState{
Value: metal.LEDStateOn,
Description: description,
}
needsUpdate = true
case metal.ChassisIdentifyLEDOffCmd:
case tag.ChassisIdentifyLEDOffCmd:
newMachine.LEDState = metal.ChassisIdentifyLEDState{
Value: metal.LEDStateOff,
Description: description,
Expand Down Expand Up @@ -2034,9 +2034,9 @@ func (r *machineResource) machineCmd(cmd metal.MachineCommand, request *restful.
r.send(request, response, http.StatusOK, resp)
}

func publishMachineCmd(logger *zap.SugaredLogger, m *metal.Machine, publisher bus.Publisher, cmd metal.MachineCommand) error {
func publishMachineCmd(logger *zap.SugaredLogger, m *metal.Machine, publisher bus.Publisher, cmd tag.MachineCommand) error {
evt := metal.MachineEvent{
Type: metal.COMMAND,
Type: tag.MachineEventCommand,
Cmd: &metal.MachineExecCommand{
Command: cmd,
TargetMachineID: m.ID,
Expand Down
23 changes: 12 additions & 11 deletions cmd/metal-api/internal/service/machine-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
v1 "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/v1"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/testdata"
"github.com/metal-stack/metal-lib/bus"
"github.com/metal-stack/metal-lib/pkg/tag"
"github.com/metal-stack/security"
)

Expand Down Expand Up @@ -426,48 +427,48 @@ func TestOnMachine(t *testing.T) {
log := zaptest.NewLogger(t).Sugar()

tests := []struct {
cmd metal.MachineCommand
cmd tag.MachineCommand
endpoint string
param string
}{
{
cmd: metal.MachineOnCmd,
cmd: tag.MachineOnCmd,
endpoint: "on",
},
{
cmd: metal.MachineOffCmd,
cmd: tag.MachineOffCmd,
endpoint: "off",
},
{
cmd: metal.MachineResetCmd,
cmd: tag.MachineResetCmd,
endpoint: "reset",
},
{
cmd: metal.MachineCycleCmd,
cmd: tag.MachineCycleCmd,
endpoint: "cycle",
},
{
cmd: metal.MachineBiosCmd,
cmd: tag.MachineBiosCmd,
endpoint: "bios",
},
{
cmd: metal.MachineDiskCmd,
cmd: tag.MachineDiskCmd,
endpoint: "disk",
},
{
cmd: metal.MachinePxeCmd,
cmd: tag.MachinePxeCmd,
endpoint: "pxe",
},
{
cmd: metal.ChassisIdentifyLEDOnCmd,
cmd: tag.ChassisIdentifyLEDOnCmd,
endpoint: "chassis-identify-led-on",
},
{
cmd: metal.ChassisIdentifyLEDOnCmd,
cmd: tag.ChassisIdentifyLEDOnCmd,
endpoint: "chassis-identify-led-on?description=test",
},
{
cmd: metal.ChassisIdentifyLEDOffCmd,
cmd: tag.ChassisIdentifyLEDOffCmd,
endpoint: "chassis-identify-led-off?description=test",
},
}
Expand Down
Loading