Merge pull request #229 from vbatts/redhatrelease_detector

Redhatrelease detector
This commit is contained in:
Jimmy Zelinskie 2016-09-06 15:04:17 -04:00
parent 8d974c75ae
commit fd586bb39e
4 changed files with 39 additions and 3 deletions

View File

@ -291,6 +291,7 @@ func toFeatureVersions(criteria criteria) []database.FeatureVersion {
}
if osVersion >= firstConsideredRHEL {
// TODO(vbatts) this is where features need multiple labels ('centos' and 'rhel')
featureVersion.Feature.Namespace.Name = "centos" + ":" + strconv.Itoa(osVersion)
} else {
continue

View File

@ -21,6 +21,7 @@ import (
"sync"
"github.com/coreos/clair/database"
"github.com/coreos/pkg/capnslog"
)
// The NamespaceDetector interface defines a way to detect a Namespace from input data.
@ -34,6 +35,8 @@ type NamespaceDetector interface {
}
var (
nlog = capnslog.NewPackageLogger("github.com/coreos/clair", "worker/detectors")
namespaceDetectorsLock sync.Mutex
namespaceDetectors = make(map[string]NamespaceDetector)
)
@ -62,8 +65,9 @@ func RegisterNamespaceDetector(name string, f NamespaceDetector) {
// DetectNamespace finds the OS of the layer by using every registered NamespaceDetector.
func DetectNamespace(data map[string][]byte) *database.Namespace {
for _, detector := range namespaceDetectors {
for name, detector := range namespaceDetectors {
if namespace := detector.Detect(data); namespace != nil {
nlog.Debugf("detector: %q; namespace: %q\n", name, namespace.Name)
return namespace
}
}

View File

@ -24,6 +24,8 @@ import (
)
var (
//log = capnslog.NewPackageLogger("github.com/coreos/clair", "worker/detectors/namespace/osrelease")
osReleaseOSRegexp = regexp.MustCompile(`^ID=(.*)`)
osReleaseVersionRegexp = regexp.MustCompile(`^VERSION_ID=(.*)`)
)
@ -42,6 +44,12 @@ func init() {
func (detector *OsReleaseNamespaceDetector) Detect(data map[string][]byte) *database.Namespace {
var OS, version string
for _, filePath := range detector.getExcludeFiles() {
if _, hasFile := data[filePath]; hasFile {
return nil
}
}
for _, filePath := range detector.GetRequiredFiles() {
f, hasFile := data[filePath]
if !hasFile {
@ -74,3 +82,8 @@ func (detector *OsReleaseNamespaceDetector) Detect(data map[string][]byte) *data
func (detector *OsReleaseNamespaceDetector) GetRequiredFiles() []string {
return []string{"etc/os-release", "usr/lib/os-release"}
}
// getExcludeFiles returns the list of files that are ought to exclude this detector from Detect()
func (detector *OsReleaseNamespaceDetector) getExcludeFiles() []string {
return []string{"etc/redhat-release", "usr/lib/centos-release"}
}

View File

@ -20,9 +20,15 @@ import (
"github.com/coreos/clair/database"
"github.com/coreos/clair/worker/detectors"
"github.com/coreos/pkg/capnslog"
)
var redhatReleaseRegexp = regexp.MustCompile(`(?P<os>[^\s]*) (Linux release|release) (?P<version>[\d]+)`)
var (
log = capnslog.NewPackageLogger("github.com/coreos/clair", "worker/detectors/namespace/redhatrelease")
centosReleaseRegexp = regexp.MustCompile(`(?P<os>[^\s]*) (Linux release|release) (?P<version>[\d]+)`)
redhatReleaseRegexp = regexp.MustCompile(`(?P<os>Red Hat Enterprise Linux) (Client release|Server release|Workstation release) (?P<version>[\d]+)`)
)
// RedhatReleaseNamespaceDetector implements NamespaceDetector and detects the OS from the
// /etc/centos-release, /etc/redhat-release and /etc/system-release files.
@ -31,6 +37,7 @@ var redhatReleaseRegexp = regexp.MustCompile(`(?P<os>[^\s]*) (Linux release|rele
// eg. CentOS release 5.11 (Final)
// eg. CentOS release 6.6 (Final)
// eg. CentOS Linux release 7.1.1503 (Core)
// eg. Red Hat Enterprise Linux Server release 7.2 (Maipo)
type RedhatReleaseNamespaceDetector struct{}
func init() {
@ -44,10 +51,21 @@ func (detector *RedhatReleaseNamespaceDetector) Detect(data map[string][]byte) *
continue
}
r := redhatReleaseRegexp.FindStringSubmatch(string(f))
var r []string
// try for RHEL
r = redhatReleaseRegexp.FindStringSubmatch(string(f))
if len(r) == 4 {
// TODO(vbatts) this is a hack until https://github.com/coreos/clair/pull/193
return &database.Namespace{Name: "centos" + ":" + r[3]}
}
// then try centos first
r = centosReleaseRegexp.FindStringSubmatch(string(f))
if len(r) == 4 {
return &database.Namespace{Name: strings.ToLower(r[1]) + ":" + r[3]}
}
}
return nil