Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
Add set-config parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mangelajo committed Oct 10, 2023
1 parent af3f572 commit 75b351d
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
73 changes: 73 additions & 0 deletions cmd/power.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright © 2023 Miguel Angel Ajo Pelayo <[email protected]
*/
package cmd

import (
"fmt"

"github.com/fatih/color"
"github.com/redhat-et/jumpstarter/pkg/harness"
"github.com/spf13/cobra"
)

// powerCmd represents the listDevices command
var powerCmd = &cobra.Command{
Use: "power",
Short: "Powers control for devices",

Run: func(cmd *cobra.Command, args []string) {
if err := cobra.MinimumNArgs(2)(cmd, args); err != nil {
handleErrorAsFatal(err)
}

action := args[0]
device_id := args[1]

driver := cmd.Flag("driver").Value.String()
console, _ := cmd.Flags().GetBool("console")
reset, _ := cmd.Flags().GetBool("reset")
attach_storage, _ := cmd.Flags().GetBool("attach-storage")

device, err := harness.FindDevice(driver, device_id)
handleErrorAsFatal(err)

color.Set(COLOR_CMD_INFO)
fmt.Printf("🔌 Power action %s on %s ... ", action, device_id)
color.Unset()

err = device.Power(action)
handleErrorAsFatal(err)

color.Set(COLOR_CMD_INFO)
fmt.Println("done")
color.Unset()

if attach_storage {
color.Set(COLOR_CMD_INFO)
fmt.Printf("💾 Attaching storage for %s ... ", device_id)
color.Unset()
err = device.AttachStorage(true)
handleErrorAsFatal(err)
color.Set(COLOR_CMD_INFO)
fmt.Println("done")
color.Unset()
}

if reset {
resetDevice(device, args[0])
}

if console {
serialConsole(device)
}
},
}

func init() {
rootCmd.AddCommand(powerCmd)
powerCmd.Flags().StringP("driver", "d", "", "Only list devices for the specified driver")
powerCmd.Flags().BoolP("console", "c", false, "Open console terminal after powering on")
powerCmd.Flags().BoolP("reset", "r", false, "Reset device after power up")
powerCmd.Flags().BoolP("attach-storage", "a", false, "Attach storage before powering on")
}
48 changes: 48 additions & 0 deletions cmd/setConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright © 2023 Miguel Angel Ajo Pelayo <[email protected]
*/
package cmd

import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/redhat-et/jumpstarter/pkg/harness"
"github.com/spf13/cobra"
)

// powerCmd represents the listDevices command
var setConfig = &cobra.Command{
Use: "set-config",
Short: "Changes a device config parameter",

Run: func(cmd *cobra.Command, args []string) {
if err := cobra.MinimumNArgs(3)(cmd, args); err != nil {
handleErrorAsFatal(err)
}

device_id := args[0]
k := strings.ToLower(args[1])
v := args[2]

driver := cmd.Flag("driver").Value.String()
device, err := harness.FindDevice(driver, device_id)
handleErrorAsFatal(err)
color.Set(COLOR_CMD_INFO)
fmt.Printf("✍ Changing device setting for %s > %s=%s ... ", device_id, k, v)
color.Unset()

err = device.SetConfig(k, v)
handleErrorAsFatal(err)

color.Set(COLOR_CMD_INFO)
fmt.Println("done")
color.Unset()
},
}

func init() {
rootCmd.AddCommand(setConfig)
setConfig.Flags().StringP("driver", "d", "", "Only list devices for the specified driver")
}
17 changes: 17 additions & 0 deletions pkg/drivers/jumpstarter-board/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ func (d *JumpstarterDevice) readConfig() error {
return nil
}

func (d *JumpstarterDevice) SetConfig(k, v string) error {
k = strings.ToLower(k)
if err := d.ensureSerial(); err != nil {
return fmt.Errorf("SetConfig(%v, %v): %w", k, v, err)
}

if err := d.exitConsole(); err != nil {
return fmt.Errorf("SetConfig(%v, %v): %w", k, v, err)
}

if err := d.sendAndExpect("set-config "+k+" "+v, "Set "+k+" to "+v); err != nil {
return fmt.Errorf("SetConfig(%v, %v) %w", k, v, err)
}

return nil
}

func (d *JumpstarterDevice) SetName(name string) error {
if err := d.ensureSerial(); err != nil {
return fmt.Errorf("SetName(%v): %w", name, err)
Expand Down
1 change: 1 addition & 0 deletions pkg/harness/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Device interface {
SetName(name string) error // set the name of the device, should be stored in config or flashed to device
SetUsbConsole(name string) error // set the substring of an out of band console name for this device
SetTags(tags []string) error
SetConfig(k, v string) error
Serial() (string, error)
SetDiskImage(path string, offset uint64) error
AttachStorage(connect bool) error
Expand Down

0 comments on commit 75b351d

Please sign in to comment.