Skip to content

Commit

Permalink
Read path to hhfab from ENV in reinstall script
Browse files Browse the repository at this point in the history
Signed-off-by: Pau Capdevila <[email protected]>
  • Loading branch information
pau-hedgehog committed Jan 15, 2025
1 parent f986545 commit 121b0f0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
12 changes: 11 additions & 1 deletion pkg/hhfab/vlabhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ func (c *Config) prepareReinstallScript() (func(), string, error) {
cleanup := func() { os.RemoveAll(dir) }

path := filepath.Join(dir, "vlabhelpers_reinstall.exp")
if err := os.WriteFile(path, []byte(reinstallScript), 0o755); err != nil { //nolint:gosec
slog.Info("Creating helper script", "path", path)
if err := os.WriteFile(path, []byte(reinstallScript), 0o700); err != nil { //nolint:gosec
return cleanup, "", fmt.Errorf("failed to write reinstall script: %w", err)
}

Expand Down Expand Up @@ -292,6 +293,15 @@ func (c *Config) VLABSwitchReinstall(ctx context.Context, opts SwitchReinstallOp
}
}

self, err := os.Executable()
if err != nil {
return fmt.Errorf("getting executable path: %w", err)
}

if err := os.Setenv("HHFAB_BIN", self); err != nil {
return fmt.Errorf("setting HHFAB_BIN env variable: %w", err)
}

cleanup, script, err := c.prepareReinstallScript()
if err != nil {
if cleanup != nil {
Expand Down
30 changes: 21 additions & 9 deletions pkg/hhfab/vlabhelpers_reinstall.exp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set timeout -1

# Initialize variables
set WAIT_READY 0
set remaining_args {}

# Parse arguments
for {set i 0} {$i < [llength $argv]} {incr i} {
Expand Down Expand Up @@ -70,6 +71,7 @@ set ERROR_GRUB 3
set ERROR_INSTALL 4
set ERROR_TIMEOUT 5
set ERROR_EOF 6
set ERROR_HHFAB 7

# Define a procedure for GRUB menu handling
proc handle_grub_menu {SW_NAME KEY_HOME KEY_DOWN ONIE_HIGHLIGHT} {
Expand Down Expand Up @@ -102,16 +104,26 @@ proc handle_grub_menu {SW_NAME KEY_HOME KEY_DOWN ONIE_HIGHLIGHT} {
}
}

# connect to the serial console of the switch
# using hhfab now for simplicity but might want to do it directly instead?
puts "connecting to serial of $SW_NAME via hhfab serial..."
# check if hhfab is in the local folder or in the path
set HHFAB "NO"
if {[file exists "./hhfab"]} {
set HHFAB "./hhfab"
} else {
set HHFAB [exec which hhfab]
# Check if HHFAB_BIN is set in the environment
set HHFAB [exec echo $env(HHFAB_BIN)]

# Fallback hhfab if it's in the local folder or in PATH
if {$HHFAB eq ""} {
if {[file exists "./hhfab"]} {
set HHFAB "./hhfab"
} else {
set HHFAB [exec which hhfab]
}
}

# Verify that HHFAB contains a valid path
if {!([file exists $HHFAB] && [file executable $HHFAB])} {
log_message "ERR" $SW_NAME "HHFAB does not contain a valid path or is not executable."
exit $ERROR_HHFAB
}

# connect to the serial console of the switch with hhfab
puts "connecting to serial of $SW_NAME via hhfab serial..."
spawn $HHFAB vlab serial -n $SW_NAME
expect {
-ex "Type the hot key to suspend the connection: <CTRL>Z" {
Expand Down

0 comments on commit 121b0f0

Please sign in to comment.