From 121b0f0f458004be58314e89bafcaf0968ad017c Mon Sep 17 00:00:00 2001 From: Pau Capdevila Date: Wed, 15 Jan 2025 13:13:01 +0100 Subject: [PATCH] Read path to hhfab from ENV in reinstall script Signed-off-by: Pau Capdevila --- pkg/hhfab/vlabhelpers.go | 12 +++++++++++- pkg/hhfab/vlabhelpers_reinstall.exp | 30 ++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/pkg/hhfab/vlabhelpers.go b/pkg/hhfab/vlabhelpers.go index 4ee2d8c3d..23c624c7a 100644 --- a/pkg/hhfab/vlabhelpers.go +++ b/pkg/hhfab/vlabhelpers.go @@ -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) } @@ -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 { diff --git a/pkg/hhfab/vlabhelpers_reinstall.exp b/pkg/hhfab/vlabhelpers_reinstall.exp index 4ddc92ffa..16299d106 100755 --- a/pkg/hhfab/vlabhelpers_reinstall.exp +++ b/pkg/hhfab/vlabhelpers_reinstall.exp @@ -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} { @@ -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} { @@ -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: Z" {