Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Merge b20eba7 into 36c50a4
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-miclea committed Sep 6, 2019
2 parents 36c50a4 + b20eba7 commit 70f1fc5
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 67 deletions.
15 changes: 15 additions & 0 deletions lib/facts/fedora/os/architecture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Facter
module Fedora
class OsArchitecture
FACT_NAME = 'os.architecture'

def call_the_resolver
fact_value = UnameResolver.resolve(:machine)

Fact.new(FACT_NAME, fact_value)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# frozen_string_literal: true

module Facter
module Scientific
module Fedora
class OsFamily
FACT_NAME = 'os.family'

def call_the_resolver
fact_value = UnameResolver.resolve(:family)

Fact.new(FACT_NAME, fact_value)
Fact.new(FACT_NAME, 'RedHat')
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

module Facter
module Scientific
class OsName
FACT_NAME = 'os.name'
module Fedora
class OsHardware
FACT_NAME = 'os.hardware'

def call_the_resolver
fact_value = UnameResolver.resolve(:machine)
Expand Down
15 changes: 15 additions & 0 deletions lib/facts/fedora/os/name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Facter
module Fedora
class OsName
FACT_NAME = 'os.name'

def call_the_resolver
fact_value = OsReleaseResolver.resolve('NAME')

Fact.new(FACT_NAME, fact_value)
end
end
end
end
22 changes: 22 additions & 0 deletions lib/facts/fedora/os/release.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Facter
module Fedora
class OsRelease
FACT_NAME = 'os.release'

def call_the_resolver
version = OsReleaseResolver.resolve('VERSION_ID')

Fact.new(FACT_NAME, build_fact_list(version))
end

def build_fact_list(version)
{
full: version,
major: version
}
end
end
end
end
21 changes: 21 additions & 0 deletions lib/facts/fedora/os/selinux.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Facter
module Fedora
class OsSelinux
FACT_NAME = 'os.selinux'

def call_the_resolver
selinux = SELinuxResolver.resolve(:enabled)

Fact.new(FACT_NAME, build_fact_list(selinux))
end

def build_fact_list(selinux)
{
enabled: selinux
}
end
end
end
end
25 changes: 0 additions & 25 deletions lib/facts/scientific/os/lsb.rb

This file was deleted.

21 changes: 0 additions & 21 deletions lib/facts/scientific/os/release.rb

This file was deleted.

9 changes: 7 additions & 2 deletions lib/resolvers/os_release_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ def resolve(fact_name)

def read_os_release_file(fact_name)
output, _status = Open3.capture2('cat /etc/os-release')
release_info = output.delete('\"').split("\n").map { |e| e.split('=') }
pairs = []

output.each_line do |line|
pairs << line.strip.delete('"').split('=', 2)
end

@@fact_list = Hash[*pairs.flatten]

@@fact_list = Hash[*release_info.flatten]
@@fact_list[:slug] = @@fact_list['ID'].downcase

@@fact_list[fact_name]
Expand Down
38 changes: 38 additions & 0 deletions lib/resolvers/selinux_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

class SELinuxResolver < BaseResolver
# :name
# :version
# :codename

class << self
@@semaphore = Mutex.new
@@fact_list ||= {}

def resolve(fact_name)
@@semaphore.synchronize do
result ||= @@fact_list[fact_name]
result || read_lsb_release_file(fact_name)
end
end

def read_lsb_release_file(fact_name)
output, _s = Open3.capture2('cat /proc/self/mounts')
@@fact_list[:enabled] = false

output.each_line do |line|
next unless line.match(/selinuxfs/)

@@fact_list[:enabled] = true
@@fact_list[:mountpoint] = line
break
end

@@fact_list[fact_name]
end

def invalidate_cache
@@fact_list = {}
end
end
end
13 changes: 1 addition & 12 deletions spec/facter/resolvers/os_release_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@
before do
allow(Open3).to receive(:capture2)
.with('cat /etc/os-release')
.and_return('NAME="Ubuntu"
VERSION="18.04.1 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.1 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic')
.and_return(load_fixture('os_release').read)
end

it 'returns os NAME' do
Expand Down
24 changes: 24 additions & 0 deletions spec/facter/resolvers/selinux_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

describe 'SELinuxResolver' do
after(:each) do
SELinuxResolver.invalidate_cache
end
it 'returns false when selinux is not enabled' do
allow(Open3).to receive(:capture2)
.with('cat /proc/self/mounts')
.and_return(load_fixture('proc_self_mounts').read)
result = SELinuxResolver.resolve(:enabled)

expect(result).to be_falsey
end

it 'returns true when selinux is enabled' do
allow(Open3).to receive(:capture2)
.with('cat /proc/self/mounts')
.and_return(load_fixture('proc_self_mounts_selinux').read)
result = SELinuxResolver.resolve(:enabled)

expect(result).to be_truthy
end
end
12 changes: 12 additions & 0 deletions spec/fixtures/os_release
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NAME="Ubuntu"
VERSION="18.04.1 LTS (Bionic Beaver)"
ID=
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.1 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
6 changes: 6 additions & 0 deletions spec/fixtures/proc_self_mounts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
udev /dev devtmpfs rw,nosuid,relatime,size=3023708k,nr_inodes=755927,mode=755 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,noexec,relatime,size=610440k,mode=755 0 0
/dev/mapper/localhost--vg-root / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0'
6 changes: 6 additions & 0 deletions spec/fixtures/proc_self_mounts_selinux
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
udev /dev devtmpfs rw,nosuid,relatime,size=3023708k,nr_inodes=755927,mode=755 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
/dev/mapper/localhost--vg-root / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

require 'open3'
require 'thor'
require 'fileutils'

require_relative '../lib/resolvers/base_resolver'
require_relative '../spec/helpers/kernel_mock'
Expand Down Expand Up @@ -42,3 +43,7 @@
c.syntax = :expect
end
end

def load_fixture(filename)
File.open(File.join('spec', 'fixtures', filename))
end

0 comments on commit 70f1fc5

Please sign in to comment.