Skip to content

Commit

Permalink
Merge pull request #31 from bringauto/BAF-1032/lsb_release_bugfix
Browse files Browse the repository at this point in the history
Baf 1032/lsb release bugfix
  • Loading branch information
koudis authored Jan 13, 2025
2 parents 9845e1d + ce969e8 commit a872449
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
19 changes: 2 additions & 17 deletions modules/bringauto_package/PlatformString.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package bringauto_package
import (
"bringauto/modules/bringauto_docker"
"bringauto/modules/bringauto_prerequisites"
"bringauto/modules/bringauto_ssh"
"bringauto/modules/bringauto_process"
"bringauto/modules/bringauto_ssh"
"fmt"
"regexp"
"strings"
Expand All @@ -25,10 +25,6 @@ const (
ModeAnyMachine = "any_machine"
// ModeAuto compute platform string automatically by lsb_release and uname
ModeAuto = "auto"
// NumberOfTriesForFakeCommands try to call fake lsb_release multiple times if fails.
// For same reason the fake lsb_release fail 1 of 3 (max). It's not clear why.
// We choose 5 as a good compromise: 3 + 1 + 1
NumberOfTriesForFakeCommands = 5
)

// PlatformString represents standard platform string
Expand Down Expand Up @@ -165,18 +161,7 @@ func runShellCommandOverSSH(credentials bringauto_ssh.SSHCredentials, command st
Command: command,
}

// If the fake lsb_release run in the Docker container
// it fails in 2/3. So we try to run fake lsb_release and fake uname multiple times.
i := 0
var commandStdOut string
for {
commandStdOut, err = commandSsh.RunCommandOverSSH(credentials)
if err != nil && i < NumberOfTriesForFakeCommands {
i++
continue
}
break
}
commandStdOut, err := commandSsh.RunCommandOverSSH(credentials)
if err != nil {
panic(fmt.Errorf("cannot run command '%s', error: %s", command, err))
}
Expand Down
21 changes: 11 additions & 10 deletions tools/lsb_release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"regexp"
)

Expand Down Expand Up @@ -77,28 +76,30 @@ func (data *DataStruct) ReadFromFile(filePath string, validate bool) {
}
defer file.Close()

parseStruct := map[string]func(string){
"^Distributor ID:\t([^\t]+)$": func(s string) { data.DistributorID = s },
"^Release:\t([^\t]+)$": func(s string) { data.ReleaseNumber = s },
type KeyValue struct {
Key string
Callback func(string)
}
parseStruct := []KeyValue{
{"^Distributor ID:\t([^\t]+)$", func(s string) { data.DistributorID = s }},
{"^Release:\t([^\t]+)$", func(s string) { data.ReleaseNumber = s }},
}

scanner := bufio.NewScanner(file)
keys := reflect.ValueOf(parseStruct).MapKeys()

handled := 0
for scanner.Scan() {
line := scanner.Text()
for _, key := range keys {
keyString := key.String()
data := parseLine(line, keyString)
for _, keyValue := range parseStruct {
data := parseLine(line, keyValue.Key)
if data != "" {
parseStruct[keyString](data)
keyValue.Callback(data)
handled++
break
}
}
}
if validate && len(keys) != handled {
if validate && len(parseStruct) != handled {
log.Panicf("Not all needed values were extracted!")
}
}
Expand Down

0 comments on commit a872449

Please sign in to comment.