control agent can now uboot commands
This commit is contained in:
89
clitools/pkg/cmd/internal/fwprintenv.go
Normal file
89
clitools/pkg/cmd/internal/fwprintenv.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"example.com/monok8s/pkg/system"
|
||||
)
|
||||
|
||||
func newInternalFWPrintEnvCmd() *cobra.Command {
|
||||
var key string
|
||||
var configPath string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "fw-printenv",
|
||||
Short: "Run fw_printenv",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
|
||||
key = strings.TrimSpace(key)
|
||||
configPath = strings.TrimSpace(configPath)
|
||||
|
||||
if configPath == "" {
|
||||
configPath = defaultFWEnvConfigPath
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configPath); err != nil {
|
||||
return fmt.Errorf("stat fw env config %q: %w", configPath, err)
|
||||
}
|
||||
|
||||
runner := system.NewRunner(system.RunnerConfig{
|
||||
DefaultTimeout: 15 * time.Second,
|
||||
StreamOutput: false,
|
||||
Logger: &system.StdLogger{},
|
||||
})
|
||||
|
||||
runArgs := []string{"-c", configPath}
|
||||
if key != "" {
|
||||
runArgs = append(runArgs, key)
|
||||
}
|
||||
|
||||
res, err := runner.RunWithOptions(
|
||||
ctx,
|
||||
"/fw_printenv",
|
||||
runArgs,
|
||||
system.RunOptions{
|
||||
Quiet: true,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if res != nil {
|
||||
klog.ErrorS(err, "fw_printenv failed",
|
||||
"key", key,
|
||||
"stdout", strings.TrimSpace(res.Stdout),
|
||||
"stderr", strings.TrimSpace(res.Stderr),
|
||||
)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
stdout := strings.TrimSpace(res.Stdout)
|
||||
stderr := strings.TrimSpace(res.Stderr)
|
||||
|
||||
if stdout != "" {
|
||||
fmt.Println(stdout)
|
||||
}
|
||||
if stderr != "" {
|
||||
klog.InfoS("fw_printenv stderr", "output", stderr)
|
||||
}
|
||||
|
||||
klog.InfoS("fw_printenv succeeded",
|
||||
"key", key,
|
||||
"configPath", configPath,
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVar(&key, "key", "", "U-Boot environment variable name to print")
|
||||
cmd.Flags().StringVar(&configPath, "config", defaultFWEnvConfigPath, "Path to fw_env.config")
|
||||
|
||||
return cmd
|
||||
}
|
||||
115
clitools/pkg/cmd/internal/fwsetenv.go
Normal file
115
clitools/pkg/cmd/internal/fwsetenv.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"example.com/monok8s/pkg/system"
|
||||
)
|
||||
|
||||
const defaultFWEnvConfigPath = "/host/etc/fw_env.config"
|
||||
|
||||
func newInternalFWSetEnvCmd() *cobra.Command {
|
||||
var key string
|
||||
var value string
|
||||
var configPath string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "fw-setenv",
|
||||
Short: "Run fw_setenv",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
|
||||
key = strings.TrimSpace(key)
|
||||
value = strings.TrimSpace(value)
|
||||
configPath = strings.TrimSpace(configPath)
|
||||
|
||||
if key == "" {
|
||||
return fmt.Errorf("--key is required")
|
||||
}
|
||||
if value == "" {
|
||||
return fmt.Errorf("--value is required")
|
||||
}
|
||||
if configPath == "" {
|
||||
configPath = defaultFWEnvConfigPath
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configPath); err != nil {
|
||||
return fmt.Errorf("stat fw env config %q: %w", configPath, err)
|
||||
}
|
||||
|
||||
runner := system.NewRunner(system.RunnerConfig{
|
||||
DefaultTimeout: 15 * time.Second,
|
||||
StreamOutput: false,
|
||||
Logger: &system.StdLogger{},
|
||||
})
|
||||
|
||||
// Preflight first so failure is clearer than blindly writing.
|
||||
preflightRes, err := runner.RunWithOptions(
|
||||
ctx,
|
||||
"/fw_printenv",
|
||||
[]string{"-c", configPath},
|
||||
system.RunOptions{
|
||||
Quiet: true,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if preflightRes != nil {
|
||||
klog.ErrorS(err, "fw_printenv preflight failed",
|
||||
"stdout", strings.TrimSpace(preflightRes.Stdout),
|
||||
"stderr", strings.TrimSpace(preflightRes.Stderr),
|
||||
)
|
||||
}
|
||||
return fmt.Errorf("fw_printenv preflight: %w", err)
|
||||
}
|
||||
|
||||
res, err := runner.RunWithOptions(
|
||||
ctx,
|
||||
"/fw_setenv",
|
||||
[]string{
|
||||
"-c", configPath,
|
||||
key, value,
|
||||
},
|
||||
system.RunOptions{
|
||||
Quiet: true,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if res != nil {
|
||||
klog.ErrorS(err, "fw_setenv failed",
|
||||
"key", key,
|
||||
"value", value,
|
||||
"stdout", strings.TrimSpace(res.Stdout),
|
||||
"stderr", strings.TrimSpace(res.Stderr),
|
||||
)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if strings.TrimSpace(res.Stdout) != "" {
|
||||
klog.InfoS("fw_setenv stdout", "output", strings.TrimSpace(res.Stdout))
|
||||
}
|
||||
if strings.TrimSpace(res.Stderr) != "" {
|
||||
klog.InfoS("fw_setenv stderr", "output", strings.TrimSpace(res.Stderr))
|
||||
}
|
||||
|
||||
klog.InfoS("fw_setenv succeeded",
|
||||
"key", key,
|
||||
"value", value,
|
||||
"configPath", configPath,
|
||||
)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVar(&key, "key", "", "U-Boot environment variable name")
|
||||
cmd.Flags().StringVar(&value, "value", "", "U-Boot environment variable value")
|
||||
cmd.Flags().StringVar(&configPath, "config", defaultFWEnvConfigPath, "Path to fw_env.config")
|
||||
|
||||
return cmd
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"example.com/monok8s/pkg/bootstrap"
|
||||
"example.com/monok8s/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewCmdInternal() *cobra.Command {
|
||||
@@ -33,6 +33,9 @@ func NewCmdInternal() *cobra.Command {
|
||||
return nil
|
||||
},
|
||||
})
|
||||
cmd.AddCommand(newInternalFWSetEnvCmd())
|
||||
cmd.AddCommand(newInternalFWPrintEnvCmd())
|
||||
|
||||
cmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "path to MonoKSConfig yaml")
|
||||
return cmd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user