Skip to content

Instantly share code, notes, and snippets.

@cheesesashimi
Created February 1, 2023 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheesesashimi/89184074cd2fe066232c512db4969015 to your computer and use it in GitHub Desktop.
Save cheesesashimi/89184074cd2fe066232c512db4969015 to your computer and use it in GitHub Desktop.
Verify /etc/os-release contents
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/ashcrow/osrelease"
"github.com/davecgh/go-spew/spew"
"github.com/openshift/machine-config-operator/pkg/daemon"
)
func getNodeRunningOS(etcPath, libPath string) (daemon.OperatingSystem, error) {
ret := daemon.OperatingSystem{}
or, err := osrelease.NewWithOverrides(etcPath, libPath)
if err != nil {
return ret, err
}
ret.ID = or.ID
ret.VariantID = or.VARIANT_ID
ret.VersionID = or.VERSION_ID
return ret, nil
}
func analyzeOSRelease(osReleaseContents string) error {
tmpDir := os.TempDir()
defer os.RemoveAll(tmpDir)
osReleasePath := filepath.Join(tmpDir, "etc-os-release")
libReleasePath := filepath.Join(tmpDir, "usr-lib-os-release")
if err := os.WriteFile(osReleasePath, []byte(osReleaseContents), 0644); err != nil {
return err
}
if err := os.WriteFile(libReleasePath, []byte(osReleaseContents), 0644); err != nil {
return err
}
os, err := getNodeRunningOS(osReleasePath, libReleasePath)
if err != nil {
return err
}
spew.Dump(os)
fmt.Printf("IsEL(): %v\n", os.IsEL())
fmt.Printf("IsEL9(): %v\n", os.IsEL9())
fmt.Printf("IsFCOS(): %v\n", os.IsFCOS())
fmt.Printf("IsSCOS(): %v\n", os.IsSCOS())
fmt.Printf("IsCoreOSVariant(): %v\n", os.IsCoreOSVariant())
fmt.Printf("IsLikeTraditionalRHEL7(): %v\n", os.IsLikeTraditionalRHEL7())
fmt.Printf("ToPrometheusLabel(): %s\n", os.ToPrometheusLabel())
return nil
}
func main() {
osReleaseContents := `NAME="Red Hat Enterprise Linux CoreOS"
ID="rhcos"
ID_LIKE="rhel fedora"
VERSION="412.86.202301311551-0"
VERSION_ID="4.12"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux CoreOS 412.86.202301311551-0 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8::coreos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://docs.openshift.com/container-platform/4.12/"
BUG_REPORT_URL="https://access.redhat.com/labs/rhir/"
REDHAT_BUGZILLA_PRODUCT="OpenShift Container Platform"
REDHAT_BUGZILLA_PRODUCT_VERSION="4.12"
REDHAT_SUPPORT_PRODUCT="OpenShift Container Platform"
REDHAT_SUPPORT_PRODUCT_VERSION="4.12"
OPENSHIFT_VERSION="4.12"
RHEL_VERSION="8.6"
OSTREE_VERSION="412.86.202301311551-0"`
if err := analyzeOSRelease(osReleaseContents); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment