Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FACT-2746) Added cloud resolver #2082

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/facter/facts/linux/cloud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Facts
module Linux
class Cloud
FACT_NAME = 'cloud'

def call_the_resolver
cloud_provider = Facter::Resolvers::Cloud.resolve(:cloud_provider)

Facter::ResolvedFact.new(FACT_NAME, cloud_provider)
end
end
end
end
39 changes: 39 additions & 0 deletions lib/facter/resolvers/cloud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Facter
module Resolvers
class Cloud < BaseResolver
# cloud_provider

@fact_list ||= {}

class << self
private

def post_resolve(fact_name)
@fact_list.fetch(fact_name) { detect_azure(fact_name) }
end

def detect_azure(fact_name)
search_dirs = %w[/var/lib/dhcp /var/lib/NetworkManager]
search_dirs.each do |path|
next unless File.directory?(path)

files = Dir.entries(path)
files.select! { |filename| filename =~ /^dhclient.*lease.*$/ }
files.each do |file|
path = File.join([path, file])
output = Util::FileHelper.safe_read(path)

if output.include?('option unknown-245') || output.include?('option 245')
@fact_list[:cloud_provider] = 'azure'
return @fact_list[fact_name]
end
end
end
nil
end
end
end
end
end
79 changes: 79 additions & 0 deletions spec/facter/resolvers/cloud_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

describe Facter::Resolvers::Cloud do
subject(:cloud_resolver) { Facter::Resolvers::Cloud }

let(:log_spy) { instance_spy(Facter::Log) }

before do
cloud_resolver.instance_variable_set(:@log, log_spy)
allow(File).to receive(:directory?)
allow(Dir).to receive(:entries)
end

after do
cloud_resolver.invalidate_cache
end

context 'when lease files are not found' do
before do
allow(File).to receive(:directory?).with('/var/lib/dhcp').and_return(true)
allow(Dir).to receive(:entries).with('/var/lib/dhcp').and_return('.')
end

it 'returns nil' do
expect(cloud_resolver.resolve(:cloud_provider)).to be_nil
end
end

context 'when lease file is found and contains option 245' do
let(:content) { load_fixture('dhclient_rhel_lease_8').read }

before do
allow(File).to receive(:directory?).with('/var/lib/dhcp').and_return(true)
allow(Facter::Util::FileHelper)
.to receive(:safe_read)
.with('/var/lib/dhcp/dhclient.rhel.lease.8')
.and_return(content)
allow(Dir).to receive(:entries).with('/var/lib/dhcp').and_return(['.', 'dhclient.rhel.lease.8'])
end

it 'returns azure' do
expect(cloud_resolver.resolve(:cloud_provider)).to eq('azure')
end
end

context 'when lease file is found without option 245' do
sebastian-miclea marked this conversation as resolved.
Show resolved Hide resolved
let(:content) { load_fixture('dhcp_lease').read }

before do
allow(File).to receive(:directory?).with('/var/lib/dhcp').and_return(true)
allow(Facter::Util::FileHelper)
.to receive(:safe_read)
.with('/var/lib/dhcp/dhclient.rhel.lease.8')
.and_return(content)
allow(Dir).to receive(:entries).with('/var/lib/dhcp').and_return(['.', 'dhclient.rhel.lease.8'])
end

it 'returns nil' do
expect(cloud_resolver.resolve(:cloud_provider)).to be_nil
end
end

context 'when lease file exists but not readable' do
let(:content) { '' }

before do
allow(File).to receive(:directory?).with('/var/lib/dhcp').and_return(true)
allow(Facter::Util::FileHelper)
.to receive(:safe_read)
.with('/var/lib/dhcp/dhclient.rhel.lease.8')
.and_return(content)
allow(Dir).to receive(:entries).with('/var/lib/dhcp').and_return(['.', 'dhclient.rhel.lease.8'])
end

it 'returns nil' do
expect(cloud_resolver.resolve(:cloud_provider)).to be_nil
end
end
end
39 changes: 39 additions & 0 deletions spec/fixtures/dhclient_rhel_lease_8
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
default-duid "\000\004N&+>\315WL\311\2756\\\036P\3448\017";
lease {
interface "eth0";
fixed-address 10.0.0.5;
server-name "AMS072031502021";
option subnet-mask 255.255.255.0;
option routers 10.0.0.1;
option dhcp-lease-time 4294967295;
option dhcp-message-type 5;
option domain-name-servers 168.63.129.16;
option dhcp-server-identifier 168.63.129.16;
option dhcp-renewal-time 4294967295;
option dhcp-rebinding-time 4294967295;
option unknown-245 a8:3f:81:10;
option rfc3442-classless-static-routes 0,10,0,0,1,32,168,63,129,16,10,0,0,1,32,169,254,169,254,10,0,0,1;
option domain-name "peq1diqrkypunc0cz32ezmxjye.ax.internal.cloudapp.net";
renew never;
rebind never;
expire never;
}
lease {
interface "eth0";
fixed-address 10.0.0.5;
server-name "AMS072031502021";
option subnet-mask 255.255.255.0;
option dhcp-lease-time 4294967295;
option routers 10.0.0.1;
option dhcp-message-type 5;
option dhcp-server-identifier 168.63.129.16;
option domain-name-servers 168.63.129.16;
option dhcp-renewal-time 4294967295;
option rfc3442-classless-static-routes 0,10,0,0,1,32,168,63,129,16,10,0,0,1,32,169,254,169,254,10,0,0,1;
option unknown-245 a8:3f:81:10;
option dhcp-rebinding-time 4294967295;
option domain-name "peq1diqrkypunc0cz32ezmxjye.ax.internal.cloudapp.net";
renew 0 2156/10/17 20:41:21;
rebind 0 2156/10/17 20:41:21;
expire 0 2156/10/17 20:41:21;
}