Skip to content

Commit

Permalink
code revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Adub17030MS committed Sep 27, 2024
1 parent 5592e8d commit a956a53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func CustomizeImageWithConfigFile(buildDir string, configFile string, imageFile
) error {
var err error

getVersionsOfToolDeps()
logVersionsOfToolDeps()

var config imagecustomizerapi.Config
err = imagecustomizerapi.UnmarshalYamlFile(configFile, &config)
Expand Down
42 changes: 22 additions & 20 deletions toolkit/tools/pkg/imagecustomizerlib/versionsOfToolDependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/microsoft/azurelinux/toolkit/tools/internal/logger"
)

func getVersionsOfToolDeps() {
func logVersionsOfToolDeps() {
// Map of version flags with corresponding packages
versionFlags := map[string][]string{
"--version": {
Expand All @@ -25,41 +25,48 @@ func getVersionsOfToolDeps() {
"-V": {
"mkfs.ext4", "mkfs.xfs", "e2fsck", "xfs_repair", "xfs_admin",
},
"none": {
"": {
"mkfs.vfat", "resize2fs", "tune2fs",
},
}

// Get distro and version
distro, version := getDistroAndVersion()
logger.Log.Debugf("Distro: %s, Version: %s\n", distro, version)
logger.Log.Debugf("Distro: %s, Version: %s", distro, version)

// Get versions of packages
logger.Log.Debugf("Tool Dependencies:")
for versionFlag, pkgList := range versionFlags {
for _, pkg := range pkgList {
version, err := getPackageVersion(pkg, versionFlag)
if err != nil {
logger.Log.Debugf("%s: not installed or error retrieving version\n", pkg)
logger.Log.Debugf("%s: not installed or error retrieving version", pkg)
} else {
logger.Log.Debugf("%s: %s", pkg, version)
}
}
}
}

// Function to get the distribution and version of host machine
// Function to get the distribution and version of the host machine
func getDistroAndVersion() (string, string) {
output, err := exec.Command("lsb_release", "-si").Output()
output, err := exec.Command("cat", "/etc/os-release").Output()
if err != nil {
output = []byte("Unknown Distro")
return "Unknown Distro", "Unknown Version"
}
distro := strings.TrimSpace(string(output))
output, err = exec.Command("lsb_release", "-sr").Output()
if err != nil {
output = []byte("Unknown Version")

lines := strings.Split(string(output), "\n")
distro := "Unknown Distro"
version := "Unknown Version"

for _, line := range lines {
if strings.HasPrefix(line, "NAME=") {
distro = strings.Trim(strings.TrimPrefix(line, "NAME="), "\"")
} else if strings.HasPrefix(line, "VERSION=") {
version = strings.Trim(strings.TrimPrefix(line, "VERSION="), "\"")
}
}
version := strings.TrimSpace(string(output))

return distro, version
}

Expand All @@ -68,17 +75,12 @@ func getPackageVersion(pkg string, versionFlagParameter string) (string, error)
var cmd *exec.Cmd
var pkgVersion string

// If the package does not have a parameter, we call the package alone and extract the version from the output
if versionFlagParameter == "none" {
cmd = exec.Command(pkg)
} else {
cmd = exec.Command(pkg, versionFlagParameter)
}

cmd = exec.Command(pkg, versionFlagParameter)
output, _ := cmd.CombinedOutput()
outputLines := strings.Split(string(output), "\n")

if versionFlagParameter == "none" {
// If the package does not have a version parameter, we need extract the version from the full output
if versionFlagParameter == "" {
// Regular expression to match various version formats including num.num.num, num.num, and alphanumeric versions
re := regexp.MustCompile(`\b\d+(\.\d+){1,3}(-\w+)?\b`)
for _, line := range outputLines {
Expand Down

0 comments on commit a956a53

Please sign in to comment.