2023-03-20 16:28:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/sirupsen/logrus"
|
2023-04-14 20:44:54 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"io"
|
2023-04-17 21:07:56 +00:00
|
|
|
"openamt/pkg/amtrpc"
|
2023-03-20 16:28:45 +00:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"rpc/pkg/utils"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AMT struct {
|
2023-04-14 20:44:54 +00:00
|
|
|
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"`
|
2023-03-20 16:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-04-14 20:44:54 +00:00
|
|
|
var config AMT
|
2023-03-20 16:28:45 +00:00
|
|
|
|
2023-04-14 20:44:54 +00:00
|
|
|
if err := yaml.NewDecoder(io.TeeReader(os.Stdin, os.Stderr)).Decode(&config); err != nil {
|
|
|
|
logrus.Fatal("failed to parse configuration: ", err)
|
2023-03-20 16:28:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 20:44:54 +00:00
|
|
|
if err := activateAMT(amtrpc.AMTRPC{}, &config); err != nil {
|
|
|
|
logrus.Fatal()
|
2023-03-20 16:28:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 20:44:54 +00:00
|
|
|
func activateAMT(rpc amtrpc.Interface, config *AMT) error {
|
2023-03-20 16:28:45 +00:00
|
|
|
if status := rpc.CheckAccess(); status != utils.Success {
|
|
|
|
if status == utils.AmtNotDetected {
|
2023-04-14 20:44:54 +00:00
|
|
|
logrus.Info("amt device could not be detected, skipping configuration")
|
|
|
|
return nil
|
2023-03-20 16:28:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 20:44:54 +00:00
|
|
|
return fmt.Errorf("failed to access AMT device with status code: %d", status)
|
2023-03-20 16:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if response, status := rpc.Exec(toCLIFlags("activate", config, config.Extra)); status != utils.Success {
|
2023-04-14 20:44:54 +00:00
|
|
|
return fmt.Errorf("failed to access AMT device with status code: %d %s", status, response)
|
2023-03-20 16:28:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 20:44:54 +00:00
|
|
|
return nil
|
2023-03-20 16:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const flagTag = "flag"
|
|
|
|
|
|
|
|
func toCLIFlags(command string, config any, extra map[string]string) string {
|
|
|
|
t := reflect.TypeOf(config).Elem()
|
|
|
|
val := reflect.ValueOf(config).Elem()
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
flag := t.Field(i).Tag.Get(flagTag)
|
|
|
|
fval := val.Field(i).String()
|
|
|
|
|
|
|
|
if flag == "" || val.Field(i).IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
command = strings.Join([]string{command, flag, fval}, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
if extra != nil {
|
|
|
|
for k, v := range extra {
|
|
|
|
command = strings.Join([]string{command, k, v}, " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return command
|
|
|
|
}
|