mirror of
https://github.com/kairos-io/openamt.git
synced 2025-09-03 13:54:52 +00:00
converted from provider plugin to bundle binary
Signed-off-by: Jacob Payne <jacob@spectrocloud.com>
This commit is contained in:
114
cmd/main.go
114
cmd/main.go
@@ -1,11 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/kairos-io/kairos-sdk/bus"
|
||||
"github.com/mudler/go-pluggable"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io"
|
||||
"os"
|
||||
"provider-amt/pkg/amtrpc"
|
||||
"reflect"
|
||||
@@ -13,109 +12,46 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
StateActive = "active"
|
||||
StateError = "error"
|
||||
StateUnavailable = "unavailable"
|
||||
StateSkipped = "skipped"
|
||||
)
|
||||
|
||||
type AMT struct {
|
||||
DnsSuffixOverride string `json:"dns_suffix_override,omitempty" flag:"-d"`
|
||||
Hostname string `json:"hostname,omitempty" flag:"-h"`
|
||||
LMSAddress string `json:"lms_address,omitempty" flag:"-lmsaddress"`
|
||||
LMSPort string `json:"lms_port,omitempty" flag:"-lmsport"`
|
||||
ProxyAddress string `json:"proxy_address,omitempty" flag:"-p"`
|
||||
Password string `json:"password,omitempty" flag:"-password"`
|
||||
Profile string `json:"profile,omitempty" flag:"-profile"`
|
||||
ServerAddress string `json:"server_address,omitempty" flag:"-u"`
|
||||
Timeout string `json:"timeout,omitempty" flag:"-t"`
|
||||
Extra map[string]string `json:"extra,omitempty"`
|
||||
}
|
||||
|
||||
type Configuration struct {
|
||||
AMT *AMT `yaml:"amt,omitempty" json:"amt,omitempty"`
|
||||
DnsSuffixOverride string `json:"dns_suffix_override,omitempty" yaml:"dns_suffix_override,omitempty" flag:"-d"`
|
||||
Hostname string `json:"hostname,omitempty" yaml:"hostname,omitempty" flag:"-h"`
|
||||
LMSAddress string `json:"lms_address,omitempty" yaml:"lms_address,omitempty" flag:"-lmsaddress"`
|
||||
LMSPort string `json:"lms_port,omitempty" yaml:"lms_port,omitempty" flag:"-lmsport"`
|
||||
ProxyAddress string `json:"proxy_address,omitempty" yaml:"proxy_address,omitempty" flag:"-p"`
|
||||
Password string `json:"password,omitempty" yaml:"password,omitempty" flag:"-password"`
|
||||
Profile string `json:"profile,omitempty" yaml:"profile,omitempty" flag:"-profile"`
|
||||
ServerAddress string `json:"server_address,omitempty" yaml:"server_address,omitempty" flag:"-u"`
|
||||
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty" flag:"-t"`
|
||||
Extra map[string]string `json:"extra,omitempty" yaml:"extra,omitempty"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := pluggable.NewPluginFactory(
|
||||
pluggable.FactoryPlugin{
|
||||
EventType: bus.EventInstall,
|
||||
PluginHandler: func(event *pluggable.Event) pluggable.EventResponse {
|
||||
return activateAMT(amtrpc.AMTRPC{}, event)
|
||||
},
|
||||
},
|
||||
).Run(pluggable.EventType(os.Args[1]), os.Stdin, os.Stdout)
|
||||
var config AMT
|
||||
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
if err := yaml.NewDecoder(io.TeeReader(os.Stdin, os.Stderr)).Decode(&config); err != nil {
|
||||
logrus.Fatal("failed to parse configuration: ", err)
|
||||
}
|
||||
|
||||
if err := activateAMT(amtrpc.AMTRPC{}, &config); err != nil {
|
||||
logrus.Fatal()
|
||||
}
|
||||
}
|
||||
|
||||
func getConfiguration(event *pluggable.Event) (*AMT, error) {
|
||||
var payload bus.EventPayload
|
||||
var config Configuration
|
||||
|
||||
// parse the event to get the configuration
|
||||
if err := json.Unmarshal([]byte(event.Data), &payload); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse event payload %s", err.Error())
|
||||
}
|
||||
|
||||
// parse the configuration to get the amt configuration
|
||||
if err := json.Unmarshal([]byte(payload.Config), &config); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse configuration %s", err.Error())
|
||||
}
|
||||
|
||||
return config.AMT, nil
|
||||
}
|
||||
|
||||
func activateAMT(rpc amtrpc.Interface, event *pluggable.Event) pluggable.EventResponse {
|
||||
config, err := getConfiguration(event)
|
||||
if err != nil {
|
||||
return pluggable.EventResponse{
|
||||
State: StateError,
|
||||
Data: event.Data,
|
||||
Error: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
// if no amt configuration is given we do nothing
|
||||
if config == nil {
|
||||
return pluggable.EventResponse{
|
||||
State: StateSkipped,
|
||||
Data: event.Data,
|
||||
}
|
||||
}
|
||||
|
||||
func activateAMT(rpc amtrpc.Interface, config *AMT) error {
|
||||
if status := rpc.CheckAccess(); status != utils.Success {
|
||||
if status == utils.AmtNotDetected {
|
||||
return pluggable.EventResponse{
|
||||
State: StateUnavailable,
|
||||
Data: event.Data,
|
||||
Logs: "no intel AMT device detected",
|
||||
}
|
||||
logrus.Info("amt device could not be detected, skipping configuration")
|
||||
return nil
|
||||
}
|
||||
|
||||
return pluggable.EventResponse{
|
||||
State: StateError,
|
||||
Data: event.Data,
|
||||
Error: fmt.Sprintf("failed to access AMT device with status code: %d", status),
|
||||
}
|
||||
return fmt.Errorf("failed to access AMT device with status code: %d", status)
|
||||
}
|
||||
|
||||
if response, status := rpc.Exec(toCLIFlags("activate", config, config.Extra)); status != utils.Success {
|
||||
return pluggable.EventResponse{
|
||||
State: StateError,
|
||||
Data: event.Data,
|
||||
Error: fmt.Sprintf("failed to activate AMT device with status code: %d", status),
|
||||
Logs: response,
|
||||
}
|
||||
return fmt.Errorf("failed to access AMT device with status code: %d %s", status, response)
|
||||
}
|
||||
|
||||
return pluggable.EventResponse{
|
||||
State: StateActive,
|
||||
Data: event.Data,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const flagTag = "flag"
|
||||
|
Reference in New Issue
Block a user