facter 2.0.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of facter might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Gemfile +14 -0
- data/ext/project_data.yaml +9 -6
- data/lib/facter/core/execution/base.rb +3 -3
- data/lib/facter/core/suitable.rb +1 -5
- data/lib/facter/dhcp_servers.rb +39 -0
- data/lib/facter/ec2.rb +33 -26
- data/lib/facter/ec2/rest.rb +130 -0
- data/lib/facter/fqdn.rb +2 -0
- data/lib/facter/gce.rb +16 -0
- data/lib/facter/gce/metadata.rb +87 -0
- data/lib/facter/kernelmajversion.rb +8 -0
- data/lib/facter/kernelrelease.rb +8 -0
- data/lib/facter/memory.rb +21 -15
- data/lib/facter/netmask.rb +1 -1
- data/lib/facter/operatingsystem.rb +20 -0
- data/lib/facter/operatingsystemmajrelease.rb +2 -1
- data/lib/facter/operatingsystemrelease.rb +19 -0
- data/lib/facter/osfamily.rb +3 -1
- data/lib/facter/partitions.rb +35 -0
- data/lib/facter/physicalprocessorcount.rb +9 -0
- data/lib/facter/processor.rb +25 -25
- data/lib/facter/util/config.rb +3 -1
- data/lib/facter/util/dhcp_servers.rb +43 -0
- data/lib/facter/util/ec2.rb +5 -0
- data/lib/facter/util/formatter.rb +2 -1
- data/lib/facter/util/ip.rb +1 -1
- data/lib/facter/util/loader.rb +10 -1
- data/lib/facter/util/manufacturer.rb +15 -16
- data/lib/facter/util/memory.rb +12 -12
- data/lib/facter/util/netmask.rb +1 -1
- data/lib/facter/util/operatingsystem.rb +21 -0
- data/lib/facter/util/partitions.rb +41 -0
- data/lib/facter/util/partitions/linux.rb +65 -0
- data/lib/facter/util/posix.rb +16 -0
- data/lib/facter/util/processor.rb +8 -10
- data/lib/facter/util/resolution.rb +4 -0
- data/lib/facter/util/values.rb +29 -0
- data/lib/facter/util/virtual.rb +32 -3
- data/lib/facter/util/windows_root.rb +2 -32
- data/lib/facter/version.rb +1 -1
- data/lib/facter/virtual.rb +53 -12
- data/spec/fixtures/ifconfig/openbsd_bridge_rules +11 -0
- data/spec/fixtures/unit/dhcp_servers/nmcli_devices +4 -0
- data/spec/fixtures/unit/dhcp_servers/nmcli_devices_disconnected +4 -0
- data/spec/fixtures/unit/dhcp_servers/nmcli_eth0_dhcp +36 -0
- data/spec/fixtures/unit/dhcp_servers/nmcli_eth0_static +24 -0
- data/spec/fixtures/unit/dhcp_servers/nmcli_wlan0_dhcp +49 -0
- data/spec/fixtures/unit/dhcp_servers/nmcli_wlan0_static +37 -0
- data/spec/fixtures/unit/dhcp_servers/route +3 -0
- data/spec/fixtures/unit/dhcp_servers/route_nogw +1 -0
- data/spec/fixtures/unit/ec2/rest/meta-data/root +20 -0
- data/spec/fixtures/unit/gce/metadata/metadata.json +69 -0
- data/spec/fixtures/unit/kernelrelease/openbsd-5.3 +2 -0
- data/spec/fixtures/unit/kernelrelease/openbsd-5.3-current +3 -0
- data/spec/fixtures/unit/memory/aix-svmon +9 -0
- data/spec/fixtures/unit/memory/aix-swap_l +2 -0
- data/spec/fixtures/unit/memory/darwin-swapinfo-multiple +3 -0
- data/spec/fixtures/unit/memory/darwin-swapinfo-single +2 -0
- data/spec/fixtures/unit/memory/darwin-vm_stat +13 -0
- data/spec/fixtures/unit/memory/dragonfly-vmstat +3 -0
- data/spec/fixtures/unit/memory/freebsd-vmstat +3 -0
- data/spec/fixtures/unit/memory/linux-proc_meminfo +10 -0
- data/spec/fixtures/unit/memory/openbsd-vmstat +3 -0
- data/spec/fixtures/unit/memory/smartos_zone_swap_l-single +2 -0
- data/spec/fixtures/unit/memory/solaris-prtconf +4 -0
- data/spec/fixtures/unit/memory/solaris-swap_l-multiple +3 -0
- data/spec/fixtures/unit/memory/solaris-swap_l-single +2 -0
- data/spec/fixtures/unit/memory/solaris-vmstat +3 -0
- data/spec/fixtures/unit/netmask/ifconfig_aix_7.txt +3 -0
- data/spec/fixtures/unit/util/dhcp_servers/route +3 -0
- data/spec/fixtures/unit/util/dhcp_servers/route_nogw +1 -0
- data/spec/fixtures/unit/util/manufacturer/smartos_smbios +533 -0
- data/spec/fixtures/unit/util/operatingsystem/cumuluslinux.txt +8 -0
- data/spec/fixtures/unit/util/operatingsystem/redhat-7.txt +12 -0
- data/spec/fixtures/unit/util/operatingsystem/sabayon.txt +7 -0
- data/spec/fixtures/unit/util/operatingsystem/wheezy.txt +9 -0
- data/spec/fixtures/unit/util/partitions/partitions/mount +9 -0
- data/spec/fixtures/virtual/proc_1_cgroup/in_a_container +9 -0
- data/spec/fixtures/virtual/proc_1_cgroup/in_a_docker_container +8 -0
- data/spec/fixtures/virtual/proc_1_cgroup/not_in_a_container +9 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/core/execution/base_spec.rb +3 -5
- data/spec/unit/core/execution/posix_spec.rb +2 -2
- data/spec/unit/core/suitable_spec.rb +10 -0
- data/spec/unit/dhcp_servers_spec.rb +152 -0
- data/spec/unit/ec2/rest_spec.rb +145 -0
- data/spec/unit/ec2_spec.rb +87 -147
- data/spec/unit/fqdn_spec.rb +16 -0
- data/spec/unit/gce/metadata_spec.rb +49 -0
- data/spec/unit/gce_spec.rb +34 -0
- data/spec/unit/interfaces_spec.rb +9 -9
- data/spec/unit/kernelmajversion_spec.rb +14 -9
- data/spec/unit/kernelrelease_spec.rb +16 -0
- data/spec/unit/macaddress_spec.rb +12 -0
- data/spec/unit/memory_spec.rb +53 -122
- data/spec/unit/netmask_spec.rb +11 -0
- data/spec/unit/operatingsystem_spec.rb +19 -0
- data/spec/unit/operatingsystemmajrelease_spec.rb +1 -1
- data/spec/unit/operatingsystemrelease_spec.rb +8 -0
- data/spec/unit/osfamily_spec.rb +62 -0
- data/spec/unit/partitions_spec.rb +48 -0
- data/spec/unit/physicalprocessorcount_spec.rb +9 -0
- data/spec/unit/processor_spec.rb +15 -7
- data/spec/unit/util/config_spec.rb +13 -0
- data/spec/unit/util/dhcp_servers_spec.rb +63 -0
- data/spec/unit/util/ec2_spec.rb +4 -0
- data/spec/unit/util/formatter_spec.rb +50 -0
- data/spec/unit/util/loader_spec.rb +4 -4
- data/spec/unit/util/macosx_spec.rb +3 -2
- data/spec/unit/util/manufacturer_spec.rb +44 -0
- data/spec/unit/util/operatingsystem_spec.rb +92 -0
- data/spec/unit/util/partitions/partitions_spec.rb +67 -0
- data/spec/unit/util/partitions_spec.rb +19 -0
- data/spec/unit/util/posix_spec.rb +11 -0
- data/spec/unit/util/values_spec.rb +40 -0
- data/spec/unit/util/virtual_spec.rb +72 -2
- data/spec/unit/virtual_spec.rb +67 -18
- metadata +116 -4
- data/spec/fixtures/unit/util/loader/nosuchfact.rb +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cb4862f2eb3f0d8775b6210c51019de8a50ffde
|
4
|
+
data.tar.gz: 0a8d5a86e615e7a24843b067b13fa77bac24e0a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 122587b08ed62e98a961dccedacc7cb3d74e95658b3f9211a4d97ea624ddf2057af2cf8191c4511af1adeee61138b266c3789f7e8f99ce86ab075f020c4763a4
|
7
|
+
data.tar.gz: ea7461f93e554632beee3cf605f948c09104d7bf9221e210e7dd94592e80290956a7692d9f23065d225a4a2e9ff07b90e6e1067af2612e25d520c1ffbf49b3eb
|
data/Gemfile
CHANGED
@@ -20,6 +20,7 @@ require 'yaml'
|
|
20
20
|
data = YAML.load_file(File.join(File.dirname(__FILE__), 'ext', 'project_data.yaml'))
|
21
21
|
bundle_platforms = data['bundle_platforms']
|
22
22
|
data['gem_platform_dependencies'].each_pair do |gem_platform, info|
|
23
|
+
next if gem_platform =~ /mingw/
|
23
24
|
if bundle_deps = info['gem_runtime_dependencies']
|
24
25
|
bundle_platform = bundle_platforms[gem_platform] or raise "Missing bundle_platform"
|
25
26
|
platform(bundle_platform.intern) do
|
@@ -30,6 +31,19 @@ data['gem_platform_dependencies'].each_pair do |gem_platform, info|
|
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
34
|
+
platform(:mingw_19) do
|
35
|
+
gem 'win32console', '~> 1.3.2', :require => false
|
36
|
+
end
|
37
|
+
|
38
|
+
mingw = [:mingw]
|
39
|
+
mingw << :x64_mingw if Bundler::Dsl::VALID_PLATFORMS.include?(:x64_mingw)
|
40
|
+
|
41
|
+
platform(*mingw) do
|
42
|
+
gem 'ffi', '~> 1.9.3', :require => false
|
43
|
+
gem 'win32-dir', '~> 0.4.8', :require => false
|
44
|
+
gem 'win32-security', '~> 0.2.5', :require => false
|
45
|
+
end
|
46
|
+
|
33
47
|
gem 'facter', ">= 1.0.0", :path => File.expand_path("..", __FILE__)
|
34
48
|
|
35
49
|
if File.exists? "#{__FILE__}.local"
|
data/ext/project_data.yaml
CHANGED
@@ -19,15 +19,18 @@ gem_platform_dependencies:
|
|
19
19
|
CFPropertyList: '~> 2.2.6'
|
20
20
|
x86-mingw32:
|
21
21
|
gem_runtime_dependencies:
|
22
|
-
ffi: '1.9.
|
23
|
-
|
24
|
-
win32-
|
25
|
-
win32-dir: '~> 0.4.3'
|
26
|
-
windows-api: '~> 0.4.2'
|
27
|
-
windows-pr: '~> 1.2.2'
|
22
|
+
ffi: '~> 1.9.3'
|
23
|
+
win32-dir: '~> 0.4.8'
|
24
|
+
win32-security: '~> 0.2.5'
|
28
25
|
win32console: '~> 1.3.2'
|
26
|
+
x64-mingw32:
|
27
|
+
gem_runtime_dependencies:
|
28
|
+
ffi: '~> 1.9.3'
|
29
|
+
win32-dir: '~> 0.4.8'
|
30
|
+
win32-security: '~> 0.2.5'
|
29
31
|
bundle_platforms:
|
30
32
|
universal-darwin: ruby
|
31
33
|
x86-mingw32: mingw
|
34
|
+
x64-mingw32: x64_mingw
|
32
35
|
pre_tasks:
|
33
36
|
'package:apple': 'cfpropertylist'
|
@@ -31,9 +31,9 @@ class Facter::Core::Execution::Base
|
|
31
31
|
|
32
32
|
on_fail = options.fetch(:on_fail, :raise)
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
with_env
|
34
|
+
# Set LC_ALL and LANG to force i18n to C for the duration of this exec; this ensures that any code that parses the
|
35
|
+
# output of the command can expect it to be in a consistent / predictable format / locale
|
36
|
+
with_env 'LC_ALL' => 'C', 'LANG' => 'C' do
|
37
37
|
|
38
38
|
expanded_command = expand_command(command)
|
39
39
|
|
data/lib/facter/core/suitable.rb
CHANGED
@@ -108,10 +108,6 @@ module Facter::Core::Suitable
|
|
108
108
|
#
|
109
109
|
# @api private
|
110
110
|
def suitable?
|
111
|
-
|
112
|
-
@suitable = ! @confines.detect { |confine| ! confine.true? }
|
113
|
-
end
|
114
|
-
|
115
|
-
return @suitable
|
111
|
+
@confines.all? { |confine| confine.true? }
|
116
112
|
end
|
117
113
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# Fact: dhcp_servers
|
2
|
+
#
|
3
|
+
# Purpose:
|
4
|
+
# Return the DHCP server addresses for all interfaces as a hash.
|
5
|
+
# If the interface that is the default gateway is dhcp assigned, there
|
6
|
+
# will also be a 'system' entry in the hash.
|
7
|
+
#
|
8
|
+
# Resolution:
|
9
|
+
# Parses the output of nmcli to find the DHCP server for the interface if available
|
10
|
+
#
|
11
|
+
# Caveats:
|
12
|
+
# Requires nmcli to be available and the interface must use network-manager.
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'facter'
|
16
|
+
require 'facter/util/dhcp_servers'
|
17
|
+
|
18
|
+
|
19
|
+
Facter.add(:dhcp_servers) do
|
20
|
+
confine :kernel => :linux
|
21
|
+
confine do
|
22
|
+
Facter::Core::Execution.which('nmcli')
|
23
|
+
end
|
24
|
+
|
25
|
+
setcode do
|
26
|
+
gwdev = Facter::Util::DHCPServers.gateway_device
|
27
|
+
devices = Facter::Util::DHCPServers.devices
|
28
|
+
|
29
|
+
dhcp_servers = {}
|
30
|
+
devices.each do |device|
|
31
|
+
if server = Facter::Util::DHCPServers.device_dhcp_server(device)
|
32
|
+
dhcp_servers['system'] = server if device == gwdev
|
33
|
+
dhcp_servers[device] = server
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
dhcp_servers.keys.length > 0 ? dhcp_servers : nil
|
38
|
+
end
|
39
|
+
end
|
data/lib/facter/ec2.rb
CHANGED
@@ -1,37 +1,44 @@
|
|
1
|
-
require 'facter/
|
2
|
-
require 'open-uri'
|
1
|
+
require 'facter/ec2/rest'
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
3
|
+
Facter.define_fact(:ec2_metadata) do
|
4
|
+
define_resolution(:rest) do
|
5
|
+
confine do
|
6
|
+
Facter.value(:virtual).match /^xen/
|
7
|
+
end
|
8
|
+
|
9
|
+
@querier = Facter::EC2::Metadata.new
|
10
|
+
confine do
|
11
|
+
@querier.reachable?
|
12
|
+
end
|
13
|
+
|
14
|
+
setcode do
|
15
|
+
@querier.fetch
|
15
16
|
end
|
16
17
|
end
|
17
|
-
rescue => details
|
18
|
-
Facter.warn "Could not retrieve ec2 metadata: #{details.message}"
|
19
18
|
end
|
20
19
|
|
21
|
-
|
22
|
-
|
20
|
+
Facter.define_fact(:ec2_userdata) do
|
21
|
+
define_resolution(:rest) do
|
22
|
+
confine do
|
23
|
+
Facter.value(:virtual).match /^xen/
|
24
|
+
end
|
25
|
+
|
26
|
+
@querier = Facter::EC2::Userdata.new
|
27
|
+
confine do
|
28
|
+
@querier.reachable?
|
29
|
+
end
|
30
|
+
|
23
31
|
setcode do
|
24
|
-
|
25
|
-
userdata.split
|
26
|
-
end
|
32
|
+
@querier.fetch
|
27
33
|
end
|
28
34
|
end
|
29
35
|
end
|
30
36
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
+
# The flattened version of the EC2 facts are deprecated and will be removed in
|
38
|
+
# a future release of Facter.
|
39
|
+
if (ec2_metadata = Facter.value(:ec2_metadata))
|
40
|
+
ec2_facts = Facter::Util::Values.flatten_structure("ec2", ec2_metadata)
|
41
|
+
ec2_facts.each_pair do |factname, factvalue|
|
42
|
+
Facter.add(factname, :value => factvalue)
|
43
|
+
end
|
37
44
|
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'timeout'
|
2
|
+
require 'open-uri'
|
3
|
+
|
4
|
+
module Facter
|
5
|
+
module EC2
|
6
|
+
CONNECTION_ERRORS = [
|
7
|
+
Errno::EHOSTDOWN,
|
8
|
+
Errno::EHOSTUNREACH,
|
9
|
+
Errno::ENETUNREACH,
|
10
|
+
Errno::ECONNABORTED,
|
11
|
+
Errno::ECONNREFUSED,
|
12
|
+
Errno::ECONNRESET,
|
13
|
+
Errno::ETIMEDOUT,
|
14
|
+
]
|
15
|
+
|
16
|
+
class Base
|
17
|
+
def reachable?(retry_limit = 3)
|
18
|
+
timeout = 0.2
|
19
|
+
able_to_connect = false
|
20
|
+
attempts = 0
|
21
|
+
|
22
|
+
begin
|
23
|
+
Timeout.timeout(timeout) do
|
24
|
+
open(@baseurl).read
|
25
|
+
end
|
26
|
+
able_to_connect = true
|
27
|
+
rescue OpenURI::HTTPError => e
|
28
|
+
if e.message.match /404 Not Found/i
|
29
|
+
able_to_connect = false
|
30
|
+
else
|
31
|
+
attempts = attempts + 1
|
32
|
+
retry if attempts < retry_limit
|
33
|
+
end
|
34
|
+
rescue Timeout::Error
|
35
|
+
attempts = attempts + 1
|
36
|
+
retry if attempts < retry_limit
|
37
|
+
rescue *CONNECTION_ERRORS
|
38
|
+
attempts = attempts + 1
|
39
|
+
retry if attempts < retry_limit
|
40
|
+
end
|
41
|
+
|
42
|
+
able_to_connect
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Metadata < Base
|
47
|
+
|
48
|
+
DEFAULT_URI = "http://169.254.169.254/latest/meta-data/"
|
49
|
+
|
50
|
+
def initialize(uri = DEFAULT_URI)
|
51
|
+
@baseurl = uri
|
52
|
+
end
|
53
|
+
|
54
|
+
def fetch(path = '')
|
55
|
+
results = {}
|
56
|
+
|
57
|
+
keys = fetch_endpoint(path)
|
58
|
+
keys.each do |key|
|
59
|
+
if key.match(%r[/$])
|
60
|
+
# If a metadata key is suffixed with '/' then it's a general metadata
|
61
|
+
# resource, so we have to recursively look up all the keys in the given
|
62
|
+
# collection.
|
63
|
+
name = key[0..-2]
|
64
|
+
results[name] = fetch("#{path}#{key}")
|
65
|
+
else
|
66
|
+
# This is a simple key/value pair, we can just query the given endpoint
|
67
|
+
# and store the results.
|
68
|
+
ret = fetch_endpoint("#{path}#{key}")
|
69
|
+
results[key] = ret.size > 1 ? ret : ret.first
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
results
|
74
|
+
end
|
75
|
+
|
76
|
+
# @param path [String] The path relative to the object base url
|
77
|
+
#
|
78
|
+
# @return [Array, NilClass]
|
79
|
+
def fetch_endpoint(path)
|
80
|
+
uri = @baseurl + path
|
81
|
+
body = open(uri).read
|
82
|
+
parse_results(body)
|
83
|
+
rescue OpenURI::HTTPError => e
|
84
|
+
if e.message.match /404 Not Found/i
|
85
|
+
return nil
|
86
|
+
else
|
87
|
+
Facter.log_exception(e, "Failed to fetch ec2 uri #{uri}: #{e.message}")
|
88
|
+
return nil
|
89
|
+
end
|
90
|
+
rescue *CONNECTION_ERRORS => e
|
91
|
+
Facter.log_exception(e, "Failed to fetch ec2 uri #{uri}: #{e.message}")
|
92
|
+
return nil
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def parse_results(body)
|
98
|
+
lines = body.split("\n")
|
99
|
+
lines.map do |line|
|
100
|
+
if (match = line.match(/^(\d+)=.*$/))
|
101
|
+
# Metadata arrays are formatted like '<index>=<associated key>/', so
|
102
|
+
# we need to extract the index from that output.
|
103
|
+
"#{match[1]}/"
|
104
|
+
else
|
105
|
+
line
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
class Userdata < Base
|
112
|
+
DEFAULT_URI = "http://169.254.169.254/latest/user-data/"
|
113
|
+
|
114
|
+
def initialize(uri = DEFAULT_URI)
|
115
|
+
@baseurl = uri
|
116
|
+
end
|
117
|
+
|
118
|
+
def fetch
|
119
|
+
open(@baseurl).read
|
120
|
+
rescue OpenURI::HTTPError => e
|
121
|
+
if e.message.match /404 Not Found/i
|
122
|
+
return nil
|
123
|
+
else
|
124
|
+
Facter.log_exception(e, "Failed to fetch ec2 uri #{uri}: #{e.message}")
|
125
|
+
return nil
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
data/lib/facter/fqdn.rb
CHANGED
data/lib/facter/gce.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'facter/gce/metadata'
|
2
|
+
|
3
|
+
Facter.define_fact(:gce) do
|
4
|
+
define_resolution(:rest) do
|
5
|
+
confine :virtual => 'gce'
|
6
|
+
|
7
|
+
confine do
|
8
|
+
Facter.json?
|
9
|
+
end
|
10
|
+
|
11
|
+
setcode do
|
12
|
+
querier = Facter::GCE::Metadata.new
|
13
|
+
querier.fetch
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
module Facter
|
4
|
+
module GCE
|
5
|
+
|
6
|
+
# @api private
|
7
|
+
class Metadata
|
8
|
+
CONNECTION_ERRORS = [
|
9
|
+
OpenURI::HTTPError,
|
10
|
+
Errno::EHOSTDOWN,
|
11
|
+
Errno::EHOSTUNREACH,
|
12
|
+
Errno::ENETUNREACH,
|
13
|
+
Errno::ECONNABORTED,
|
14
|
+
Errno::ECONNREFUSED,
|
15
|
+
Errno::ECONNRESET,
|
16
|
+
Errno::ETIMEDOUT,
|
17
|
+
Timeout::Error,
|
18
|
+
]
|
19
|
+
|
20
|
+
METADATA_URL = "http://metadata/computeMetadata/v1beta1/?recursive=true&alt=json"
|
21
|
+
|
22
|
+
def initialize(url = METADATA_URL)
|
23
|
+
@url = url
|
24
|
+
end
|
25
|
+
|
26
|
+
def fetch
|
27
|
+
with_metadata_server do |body|
|
28
|
+
# This will only be reached if the confine associated with this class
|
29
|
+
# was true which means that JSON was required, but it's a bit
|
30
|
+
# questionable that we're relying on JSON being loaded as a side
|
31
|
+
# effect of that.
|
32
|
+
hash = ::JSON.parse(body)
|
33
|
+
transform_metadata!(hash)
|
34
|
+
hash
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def with_metadata_server
|
41
|
+
retry_limit = 3
|
42
|
+
timeout = 0.05
|
43
|
+
body = nil
|
44
|
+
attempts = 0
|
45
|
+
|
46
|
+
begin
|
47
|
+
Timeout.timeout(timeout) do
|
48
|
+
body = open(@url).read
|
49
|
+
end
|
50
|
+
rescue *CONNECTION_ERRORS => e
|
51
|
+
attempts = attempts + 1
|
52
|
+
if attempts < retry_limit
|
53
|
+
retry
|
54
|
+
else
|
55
|
+
Facter.log_exception(e, "Unable to fetch metadata from #{@url}: #{e.message}")
|
56
|
+
return nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
if body
|
61
|
+
yield body
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# @return [void]
|
66
|
+
def transform_metadata!(data)
|
67
|
+
case data
|
68
|
+
when Hash
|
69
|
+
data.keys.each do |key|
|
70
|
+
value = data[key]
|
71
|
+
if ["image", "machineType", "zone", "network"].include? key
|
72
|
+
data[key] = value.split('/').last
|
73
|
+
elsif key == "sshKeys"
|
74
|
+
data['sshKeys'] = value.split("\n")
|
75
|
+
end
|
76
|
+
transform_metadata!(value)
|
77
|
+
end
|
78
|
+
when Array
|
79
|
+
data.each do |value|
|
80
|
+
transform_metadata!(value)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
nil
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|