cmdb 3.0.0rc2 → 3.0.0rc3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/Rakefile +2 -2
- data/exe/cmdb +3 -3
- data/lib/cmdb.rb +10 -10
- data/lib/cmdb/source.rb +26 -18
- data/lib/cmdb/source/consul.rb +10 -8
- data/lib/cmdb/source/file.rb +17 -16
- data/lib/cmdb/source/memory.rb +2 -1
- data/lib/cmdb/source/network.rb +28 -17
- data/lib/cmdb/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c44ac22f3267a7961d7034b5a0b56fc34a7bd7a
|
4
|
+
data.tar.gz: dfd4eaded71592fe0f8be7aa5b2a552610972811
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7910184801caaa46c493b1eca7094bf9d5107741cb20c67f1fdaaa537b99652634c528b7d9864865ddd0cdb8e902cb0373fd829ca70fb595ff39c075f1fa3541
|
7
|
+
data.tar.gz: 5779dece8e9682d685a148a6c5315a10aae3b6be19d2505e02ed0beb923895a3b8c931068e97322874f9fbd63f8379bbc704c9a503de39fedb03003961692dae
|
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
@@ -23,8 +23,8 @@ task :sandbox do
|
|
23
23
|
compose = Docker::Compose::Session.new
|
24
24
|
compose.up 'consul', detached:true
|
25
25
|
mapper = Docker::Compose::Mapper.new(compose)
|
26
|
-
|
26
|
+
uri = mapper.map('consul://consul:8500/sandbox')
|
27
27
|
|
28
28
|
lib = File.expand_path('../lib', __FILE__)
|
29
|
-
exec "bin/shell --source=#{
|
29
|
+
exec "bin/shell --source=#{uri}"
|
30
30
|
end
|
data/exe/cmdb
CHANGED
@@ -90,15 +90,15 @@ rescue CMDB::BadKey => e
|
|
90
90
|
CMDB.log.fatal "CMDB: Bad Key: malformed CMDB key '#{e.key}'"
|
91
91
|
exit 1
|
92
92
|
rescue CMDB::BadValue => e
|
93
|
-
CMDB.log.fatal "CMDB: Bad Value: illegal value for CMDB key '#{e.key}' in source #{e.
|
93
|
+
CMDB.log.fatal "CMDB: Bad Value: illegal value for CMDB key '#{e.key}' in source #{e.uri}"
|
94
94
|
exit 2
|
95
95
|
rescue CMDB::BadData => e
|
96
|
-
CMDB.log.fatal "CMDB: Bad Data: malformed CMDB data in source #{e.
|
96
|
+
CMDB.log.fatal "CMDB: Bad Data: malformed CMDB data in source #{e.uri}"
|
97
97
|
exit 3
|
98
98
|
rescue CMDB::ValueConflict => e
|
99
99
|
CMDB.log.fatal "CMDB: Value Conflict: #{e.message}"
|
100
100
|
e.sources.each do |s|
|
101
|
-
CMDB.log.fatal " - #{s.
|
101
|
+
CMDB.log.fatal " - #{s.uri}"
|
102
102
|
end
|
103
103
|
exit 4
|
104
104
|
rescue CMDB::NameConflict => e
|
data/lib/cmdb.rb
CHANGED
@@ -41,17 +41,17 @@ module CMDB
|
|
41
41
|
|
42
42
|
# A value of an unsupported type was encountered in the CMDB or filesystem.
|
43
43
|
class BadValue < Error
|
44
|
-
# @return [URI]
|
45
|
-
attr_reader :
|
44
|
+
# @return [URI] source that contains the bad data
|
45
|
+
attr_reader :uri
|
46
46
|
|
47
47
|
# @return [String] the name of the key that contained the bad value
|
48
48
|
attr_reader :key
|
49
49
|
|
50
|
-
# @param [URI]
|
50
|
+
# @param [URI] source that contains the bad value
|
51
51
|
# @param [String] key CMDB key name under which the bad value was found
|
52
52
|
# @param [Object] value the bad value itself
|
53
|
-
def initialize(
|
54
|
-
@
|
53
|
+
def initialize(uri, key, value, desc=nil)
|
54
|
+
@uri = uri
|
55
55
|
@key = key
|
56
56
|
msg = "Unsupported #{value.class.name} value"
|
57
57
|
msg << ": #{desc}" if desc
|
@@ -61,13 +61,13 @@ module CMDB
|
|
61
61
|
|
62
62
|
# Malformed data was encountered in the CMDB or filesystem.
|
63
63
|
class BadData < Error
|
64
|
-
# @return [URI]
|
65
|
-
attr_reader :
|
64
|
+
# @return [URI] source that contains the bad data
|
65
|
+
attr_reader :uri
|
66
66
|
|
67
|
-
# @param [URI]
|
67
|
+
# @param [URI] source that contains the bad value
|
68
68
|
# @param [String] context brief description of where data was found e.g. 'CMDB data file' or 'input config file'
|
69
|
-
def initialize(
|
70
|
-
@
|
69
|
+
def initialize(uri, context = nil)
|
70
|
+
@uri = uri
|
71
71
|
super("Malformed data encountered #{(' in ' + context) if context}")
|
72
72
|
end
|
73
73
|
end
|
data/lib/cmdb/source.rb
CHANGED
@@ -1,24 +1,25 @@
|
|
1
1
|
module CMDB
|
2
2
|
class Source
|
3
|
-
#
|
4
|
-
|
5
|
-
|
3
|
+
# @return [URI] logical description of this source
|
4
|
+
attr_reader :uri
|
5
|
+
|
6
|
+
# The dot-notation prefix of all keys provided by this source. No two
|
7
|
+
# sources can share a prefix (other than nil) and no source's prefix can
|
8
|
+
# be a prefix of any other source's prefix.
|
6
9
|
#
|
7
|
-
# Some sources have no
|
10
|
+
# Some sources have no prefix, in which case this reader returns nil.
|
8
11
|
#
|
9
|
-
# @return [nil,String]
|
12
|
+
# @return [nil,String] unique dot-notation prefix of all this source's keys, if any
|
10
13
|
attr_reader :prefix
|
11
14
|
|
12
|
-
# @return [URI] a URL describing where this source's data comes from
|
13
|
-
attr_reader :url
|
14
|
-
|
15
15
|
# Construct a source given a URI that identifies both the type of
|
16
16
|
# source (consul, file or environment) and its location if applicable.
|
17
17
|
# Choose a suitable prefix for the source based on the URI contents.
|
18
18
|
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
19
|
+
# This method accepts a special URI notation that is specific to the cmdb
|
20
|
+
# gem; in this notation, the scheme of the URI specifies the type of source
|
21
|
+
# (consul, file, etc) and the other components describe how to locate the
|
22
|
+
# source.
|
22
23
|
#
|
23
24
|
# @param [String,URI] location of source
|
24
25
|
#
|
@@ -55,15 +56,11 @@ module CMDB
|
|
55
56
|
|
56
57
|
case uri.scheme
|
57
58
|
when 'consul'
|
58
|
-
|
59
|
-
curi.scheme = 'http'
|
60
|
-
curi.port ||= 8500
|
61
|
-
curi.path = ''
|
62
|
-
Source::Consul.new(URI.parse(curi.to_s), prefix)
|
59
|
+
Source::Consul.new(uri, prefix)
|
63
60
|
when 'file'
|
64
|
-
Source::File.new(uri
|
61
|
+
Source::File.new(uri, prefix)
|
65
62
|
when 'memory'
|
66
|
-
Source::Memory.new({},
|
63
|
+
Source::Memory.new({},prefix)
|
67
64
|
else
|
68
65
|
raise ArgumentError, "Unrecognized URL scheme '#{uri.scheme}'"
|
69
66
|
end
|
@@ -81,6 +78,17 @@ module CMDB
|
|
81
78
|
sources
|
82
79
|
end
|
83
80
|
|
81
|
+
# Construct a new Source.
|
82
|
+
#
|
83
|
+
# @param [String,URI] uri logical description of this source
|
84
|
+
# @param [String] prefix unique dot-notation prefix of all this source's keys, if any
|
85
|
+
# @raise [URI::InvalidURIError] if an invalid string is passed
|
86
|
+
def initialize(uri, prefix)
|
87
|
+
uri = URI.parse(uri) if uri.is_a?(String)
|
88
|
+
@uri = uri
|
89
|
+
@prefix = prefix
|
90
|
+
end
|
91
|
+
|
84
92
|
private
|
85
93
|
|
86
94
|
# Check whether a key's prefix is suitable for this source.
|
data/lib/cmdb/source/consul.rb
CHANGED
@@ -8,6 +8,10 @@ module CMDB
|
|
8
8
|
# Regular expression to match array values
|
9
9
|
ARRAY_VALUE = /^\[(.*)\]$/
|
10
10
|
|
11
|
+
def initialize(uri, prefix)
|
12
|
+
super(uri, 8500, prefix)
|
13
|
+
end
|
14
|
+
|
11
15
|
# Get a single key from consul. If the key is not found, return nil.
|
12
16
|
#
|
13
17
|
# @param [String] key dot-notation key
|
@@ -77,15 +81,13 @@ module CMDB
|
|
77
81
|
false
|
78
82
|
end
|
79
83
|
|
80
|
-
private
|
81
|
-
|
82
84
|
# Given a key's relative path, return its absolute REST path in the consul
|
83
|
-
# kv, including prefix
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
# kv, including the `/v1/kv/` prefix and any subkey path specified at
|
86
|
+
# initialize time.
|
87
|
+
#
|
88
|
+
# @param [String] subpath key path relative to base
|
89
|
+
def path_to(subpath)
|
90
|
+
::File.join('/v1/kv/', @uri.path, subpath)
|
89
91
|
end
|
90
92
|
end
|
91
93
|
end
|
data/lib/cmdb/source/file.rb
CHANGED
@@ -10,32 +10,33 @@ module CMDB
|
|
10
10
|
# source = Source::File.new('/tmp/my.yml') # contains a top-level stanza named "database"
|
11
11
|
# source['my']['database']['host'] # => 'db1-1.example.com'
|
12
12
|
class Source::File < Source
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# @param [
|
13
|
+
# Read a data file whose location is specified by a file:// URI.
|
14
|
+
#
|
15
|
+
# @param [URI] uri logical description of this source
|
16
|
+
# @param [String] prefix unique dot-notation prefix of all this source's keys, if any
|
16
17
|
# @raise [BadData] if the file's content is malformed
|
17
|
-
def initialize(
|
18
|
-
|
19
|
-
|
20
|
-
filename
|
21
|
-
|
22
|
-
|
23
|
-
raw_bytes = ::File.read(filename)
|
18
|
+
def initialize(uri, prefix)
|
19
|
+
super(uri, prefix)
|
20
|
+
path = @uri.path
|
21
|
+
filename = ::File.basename(path)
|
22
|
+
extension = ::File.extname(filename)
|
23
|
+
raw_bytes = ::File.read(path)
|
24
24
|
raw_data = nil
|
25
25
|
|
26
26
|
begin
|
27
|
-
case
|
27
|
+
case extension
|
28
28
|
when /jso?n?$/i
|
29
29
|
raw_data = JSON.load(raw_bytes)
|
30
30
|
when /ya?ml$/i
|
31
31
|
raw_data = YAML.load(raw_bytes)
|
32
32
|
else
|
33
|
-
raise BadData.new(
|
33
|
+
raise BadData.new(@uri, 'file with unknown extension; expected js(on) or y(a)ml')
|
34
34
|
end
|
35
35
|
rescue StandardError
|
36
|
-
raise BadData.new(
|
36
|
+
raise BadData.new(@uri, 'CMDB data file')
|
37
37
|
end
|
38
38
|
|
39
|
+
@data = {}
|
39
40
|
flatten(raw_data, @prefix, @data)
|
40
41
|
end
|
41
42
|
|
@@ -66,7 +67,7 @@ module CMDB
|
|
66
67
|
when Array
|
67
68
|
if value.any? { |e| e.is_a?(Hash) }
|
68
69
|
# mismatched arrays: not allowed
|
69
|
-
raise BadValue.new(
|
70
|
+
raise BadValue.new(uri, key, value, 'hashes not allowed inside arrays')
|
70
71
|
elsif value.all? { |e| e.is_a?(String) } ||
|
71
72
|
value.all? { |e| e.is_a?(Numeric) } ||
|
72
73
|
value.all? { |e| e == true } ||
|
@@ -74,13 +75,13 @@ module CMDB
|
|
74
75
|
output[key] = value
|
75
76
|
else
|
76
77
|
# mismatched arrays: not allowed
|
77
|
-
raise BadValue.new(
|
78
|
+
raise BadValue.new(uri, key, value, 'mismatched/unsupported element types')
|
78
79
|
end
|
79
80
|
when String, Numeric, TrueClass, FalseClass
|
80
81
|
output[key] = value
|
81
82
|
else
|
82
83
|
# nil and anything else: not allowed
|
83
|
-
raise BadValue.new(
|
84
|
+
raise BadValue.new(uri, key, value)
|
84
85
|
end
|
85
86
|
end
|
86
87
|
end
|
data/lib/cmdb/source/memory.rb
CHANGED
data/lib/cmdb/source/network.rb
CHANGED
@@ -1,10 +1,21 @@
|
|
1
1
|
module CMDB
|
2
|
+
# Abstract base class for sources that are backed by an HTTP k/v store.
|
3
|
+
# Contains reusable HTTP request logic.
|
2
4
|
class Source::Network < Source
|
3
|
-
# @
|
5
|
+
# @return [URI] HTTP base location of this source (minus path)
|
6
|
+
attr_reader :http_url
|
7
|
+
|
8
|
+
# Construct a new HTTP source. The logical URI is transformed into an
|
9
|
+
# HTTP base URL by preserving the hostname, overriding the port (unless
|
10
|
+
# the URI already has a specific port), replacing the scheme with http
|
11
|
+
# and eliminating the path.
|
12
|
+
#
|
13
|
+
# @param [String,URI] uri logical description of this source
|
14
|
+
# @param [Integer] port default HTTP port if not specified in URI
|
4
15
|
# @param [String] dot-notation prefix of all keys
|
5
|
-
def initialize(uri, prefix)
|
6
|
-
|
7
|
-
@
|
16
|
+
def initialize(uri, port, prefix)
|
17
|
+
super(uri, prefix)
|
18
|
+
@http_url = URI.parse("http://%s:%s" % [@uri.host, @uri.port || port])
|
8
19
|
end
|
9
20
|
|
10
21
|
private
|
@@ -16,12 +27,12 @@ module CMDB
|
|
16
27
|
# @param [String] path
|
17
28
|
# @return [Integer,String]
|
18
29
|
def http_get(path, query:nil, retries:3)
|
19
|
-
@http ||= Net::HTTP.start(@
|
20
|
-
|
21
|
-
|
22
|
-
|
30
|
+
@http ||= Net::HTTP.start(@http_url.host, @http_url.port)
|
31
|
+
url = @http_url.dup
|
32
|
+
url.path = path
|
33
|
+
url.query = query unless query.nil? || query.empty?
|
23
34
|
|
24
|
-
request = Net::HTTP::Get.new
|
35
|
+
request = Net::HTTP::Get.new url
|
25
36
|
response = @http.request request
|
26
37
|
|
27
38
|
case response.code.to_i
|
@@ -47,11 +58,11 @@ module CMDB
|
|
47
58
|
def http_put(path, entity)
|
48
59
|
entity = JSON.dump(entity) unless entity.is_a?(String)
|
49
60
|
|
50
|
-
@http ||= Net::HTTP.start(@
|
51
|
-
|
52
|
-
|
61
|
+
@http ||= Net::HTTP.start(@http_url.host, @http_url.port)
|
62
|
+
url = @http_url.dup
|
63
|
+
url.path = path
|
53
64
|
|
54
|
-
request = Net::HTTP::Put.new
|
65
|
+
request = Net::HTTP::Put.new url
|
55
66
|
request.body = entity
|
56
67
|
response = @http.request request
|
57
68
|
|
@@ -64,11 +75,11 @@ module CMDB
|
|
64
75
|
# @return [Integer] HTTP status code
|
65
76
|
# @param [String] path
|
66
77
|
def http_delete(path)
|
67
|
-
@http ||= Net::HTTP.start(@
|
68
|
-
|
69
|
-
|
78
|
+
@http ||= Net::HTTP.start(@http_url.host, @http_url.port)
|
79
|
+
url = @http_url.dup
|
80
|
+
url.path = path
|
70
81
|
|
71
|
-
request = Net::HTTP::Delete.new
|
82
|
+
request = Net::HTTP::Delete.new url
|
72
83
|
response = @http.request request
|
73
84
|
|
74
85
|
response.code.to_i
|
data/lib/cmdb/version.rb
CHANGED