cmdb 2.6.0 → 2.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df72629dd0ba9fcb24a33607ca6216e9a70ff188
4
- data.tar.gz: 2daf8320e35c3cd58234d86b452b8574cc76e944
3
+ metadata.gz: 445daab93b31128021486ceabe032d190f05ce05
4
+ data.tar.gz: 195b0efcdbcc98d655f8799c60f3a5b736525a07
5
5
  SHA512:
6
- metadata.gz: 97c03113ce4aa1c50718edadd6c9649096a22916bf14a16721df59987f56745c8150131a5bbb63d6d6f2c3c9e4db59fff2568f2480e0e3488468400b4cc92253
7
- data.tar.gz: 38cc4d3c2701dfc66b2d3f27470851bd14128e5ec4b3548e85fa292daced4d609cb43f4d63746a73e449cd410e78f109ea27f8f822b99e73a0d4a45f4fa6fd7c
6
+ metadata.gz: e496a30d55cb46eb1bd08c621be5706732c70750a9474d6aca74856757702bd1369f5af4bb22e545b654c631dfb09987d46fa8a6fe1fb863b23a2bd5625c4dd8
7
+ data.tar.gz: ca6a65077171489c35740f42b8f43bff37a66aa9866629570b90d51a0eaef5655b8b7715edd872ad800f51e2dbd12b38b785f7e0c581a00c88d6ee9e8a2d0074
data/.rubocop.yml ADDED
@@ -0,0 +1,35 @@
1
+ # CODING CONVENTIONS
2
+ # - any cop that can be auto-corrected with `rubocop -a` is mandatory
3
+ # - other cops may be enabled if committer fixes all issues
4
+ AllCops:
5
+ Exclude:
6
+ - features/**/*
7
+ - spec/**/*
8
+ Style/Encoding:
9
+ Enabled: true
10
+ Style/FormatString:
11
+ Enabled: false
12
+ Style/GuardClause:
13
+ Enabled: false
14
+ Lint/AmbiguousRegexpLiteral:
15
+ Enabled: false
16
+ Metrics/AbcSize:
17
+ Enabled: false
18
+ Metrics/ClassLength:
19
+ Enabled: false
20
+ Metrics/LineLength:
21
+ Enabled: false
22
+ Metrics/MethodLength:
23
+ Enabled: false
24
+ Metrics/CyclomaticComplexity:
25
+ Enabled: false
26
+ Metrics/PerceivedComplexity:
27
+ Enabled: false
28
+ Style/ClassAndModuleChildren:
29
+ Enabled: false
30
+ Style/Documentation:
31
+ Enabled: false
32
+ Style/RaiseArgs:
33
+ Enabled: false
34
+ Style/Semicolon:
35
+ Enabled: false
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  source 'https://rubygems.org'
2
3
 
3
4
  gemspec
@@ -7,9 +8,13 @@ gem 'backticks'
7
8
  gem 'cucumber'
8
9
  gem 'rspec'
9
10
 
11
+ group :test do
12
+ gem 'rubocop'
13
+ gem 'webmock'
14
+ end
15
+
10
16
  group :development do
11
17
  gem 'yard'
12
18
  gem 'pry'
13
19
  gem 'pry-byebug'
14
20
  end
15
-
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cmdb (2.6.0)
4
+ cmdb (2.6.1)
5
5
  diplomat (~> 0.15)
6
6
  listen (~> 3.0)
7
7
  trollop (~> 2.0)
@@ -9,10 +9,14 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
+ addressable (2.4.0)
13
+ ast (2.2.0)
12
14
  backticks (0.4.0)
13
15
  builder (3.2.2)
14
16
  byebug (8.2.2)
15
17
  coderay (1.1.1)
18
+ crack (0.4.3)
19
+ safe_yaml (~> 1.0.0)
16
20
  cucumber (2.3.2)
17
21
  builder (>= 2.1.2)
18
22
  cucumber-core (~> 1.4.0)
@@ -32,6 +36,7 @@ GEM
32
36
  multipart-post (>= 1.2, < 3)
33
37
  ffi (1.9.10)
34
38
  gherkin (3.2.0)
39
+ hashdiff (0.3.0)
35
40
  json (1.8.3)
36
41
  listen (3.0.6)
37
42
  rb-fsevent (>= 0.9.3)
@@ -40,6 +45,9 @@ GEM
40
45
  multi_json (1.11.2)
41
46
  multi_test (0.1.2)
42
47
  multipart-post (2.0.0)
48
+ parser (2.3.0.6)
49
+ ast (~> 2.2)
50
+ powerpack (0.1.1)
43
51
  pry (0.10.3)
44
52
  coderay (~> 1.1.0)
45
53
  method_source (~> 0.8.1)
@@ -47,6 +55,7 @@ GEM
47
55
  pry-byebug (3.3.0)
48
56
  byebug (~> 8.0)
49
57
  pry (~> 0.10)
58
+ rainbow (2.1.0)
50
59
  rake (10.5.0)
51
60
  rb-fsevent (0.9.7)
52
61
  rb-inotify (0.9.7)
@@ -64,8 +73,21 @@ GEM
64
73
  diff-lcs (>= 1.2.0, < 2.0)
65
74
  rspec-support (~> 3.4.0)
66
75
  rspec-support (3.4.1)
76
+ rubocop (0.37.2)
77
+ parser (>= 2.3.0.4, < 3.0)
78
+ powerpack (~> 0.1)
79
+ rainbow (>= 1.99.1, < 3.0)
80
+ ruby-progressbar (~> 1.7)
81
+ unicode-display_width (~> 0.3)
82
+ ruby-progressbar (1.7.5)
83
+ safe_yaml (1.0.4)
67
84
  slop (3.6.0)
68
85
  trollop (2.1.2)
86
+ unicode-display_width (0.3.1)
87
+ webmock (1.24.1)
88
+ addressable (>= 2.3.6)
89
+ crack (>= 0.3.2)
90
+ hashdiff
69
91
  yard (0.8.7.6)
70
92
 
71
93
  PLATFORMS
@@ -80,6 +102,8 @@ DEPENDENCIES
80
102
  pry-byebug
81
103
  rake
82
104
  rspec
105
+ rubocop
106
+ webmock
83
107
  yard
84
108
 
85
109
  BUNDLED WITH
data/Rakefile CHANGED
@@ -2,16 +2,16 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'bundler/setup'
5
- require "bundler/gem_tasks"
5
+ require 'bundler/gem_tasks'
6
6
 
7
7
  require 'cucumber/rake/task'
8
- desc "Run functional tests"
8
+ desc 'Run functional tests'
9
9
  Cucumber::Rake::Task.new do |t|
10
- t.cucumber_opts = %w{--color --format pretty}
10
+ t.cucumber_opts = %w(--color --format pretty)
11
11
  end
12
12
 
13
13
  require 'rspec/core'
14
14
  require 'rspec/core/rake_task'
15
15
  RSpec::Core::RakeTask.new(:spec)
16
16
 
17
- task :default => [:spec, :cucumber]
17
+ task default: [:spec, :cucumber]
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
2
3
 
3
- require "bundler/setup"
4
- require "cmdb"
4
+ require 'bundler/setup'
5
+ require 'cmdb'
5
6
 
6
- require "pry"
7
+ require 'pry'
7
8
  Pry.start
data/cmdb.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ['RightScale']
10
10
  spec.email = ['rubygems@rightscale.com']
11
11
 
12
- spec.summary = %q{Command-line tool for configuration manegement databases}
13
- spec.description = %q{Reads CMDB variables from files, Consul, and elsewhere.}
12
+ spec.summary = 'Command-line tool for configuration manegement databases'
13
+ spec.description = 'Reads CMDB variables from files, Consul, and elsewhere.'
14
14
  spec.homepage = 'https://github.com/rightscale/cmdb'
15
15
  spec.license = 'MIT'
16
16
 
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.required_ruby_version = Gem::Requirement.new("~> 2.0")
22
+ spec.required_ruby_version = Gem::Requirement.new('~> 2.0')
23
23
 
24
24
  spec.add_dependency 'listen', '~> 3.0'
25
25
  spec.add_dependency 'diplomat', '~> 0.15'
data/exe/cmdb CHANGED
@@ -1,10 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
2
3
 
3
4
  require 'rubygems'
4
5
  require 'trollop'
6
+
5
7
  require 'cmdb'
8
+ require 'cmdb/commands'
6
9
 
7
- if gemspec = Gem.loaded_specs['cmdb']
10
+ if (gemspec = Gem.loaded_specs['cmdb'])
8
11
  gemspec_version = gemspec.version
9
12
  else
10
13
  require_relative '../lib/cmdb/version'
@@ -39,7 +42,7 @@ To get help on a command:
39
42
  stop_on commands.keys
40
43
  end
41
44
 
42
- opts = Trollop::with_standard_exception_handling p do
45
+ Trollop.with_standard_exception_handling p do
43
46
  raise Trollop::HelpNeeded if ARGV.empty?
44
47
  p.parse ARGV
45
48
  cmd = ARGV.shift
@@ -48,6 +51,6 @@ opts = Trollop::with_standard_exception_handling p do
48
51
  if klass
49
52
  klass.create.run
50
53
  else
51
- raise ArgumentError, "Unrecognized command #{cmd}"
54
+ raise(ArgumentError, "Unrecognized command #{cmd}")
52
55
  end
53
56
  end
data/fixtures/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  source 'https://rubygems.org'
2
3
 
3
4
  gem 'sinatra', '~> 1.3'
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  get '/widgets' do
2
3
  headers 'Content-Type' => 'application/json; charset=utf-8'
3
4
  [200, '[]']
data/fixtures/config.ru CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'rubygems'
2
3
  require 'bundler/setup'
3
4
  require 'sinatra'
@@ -24,6 +25,6 @@ if ENV['DIE_DIE_DIE']
24
25
  puts "I am dying as requested by ENV['DIE_DIE_DIE']"
25
26
  exit(42)
26
27
  else
27
- puts "I am up and running"
28
+ puts 'I am up and running'
28
29
  run Sinatra::Application
29
30
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'logger'
2
3
  require 'listen'
3
4
 
@@ -31,45 +32,46 @@ cmdb shim [options] -- <command_to_exec> [options_for_command]
31
32
  Where [options] are selected from:
32
33
  EOS
33
34
  opt :dir,
34
- "Directory to scan for key-replacement tokens in data files",
35
- :type => :string
35
+ 'Directory to scan for key-replacement tokens in data files',
36
+ type: :string
36
37
  opt :consul_url,
37
- "The URL for talking to consul",
38
- :type => :string
38
+ 'The URL for talking to consul',
39
+ type: :string
39
40
  opt :consul_prefix,
40
- "The prefix to use when getting keys from consul, can be specified more than once",
41
- :type => :string,
42
- :multi => true
41
+ 'The prefix to use when getting keys from consul, can be specified more than once',
42
+ type: :string,
43
+ multi: true
43
44
  opt :keys,
44
- "Override search path(s) for CMDB key files",
45
- :type => :strings
45
+ 'Override search path(s) for CMDB key files',
46
+ type: :strings
46
47
  opt :pretend,
47
- "Check for errors, but do not actually launch the app or rewrite files",
48
- :default => false
48
+ 'Check for errors, but do not actually launch the app or rewrite files',
49
+ default: false
49
50
  opt :quiet,
50
51
  "Don't print any output",
51
- :default => false
52
+ default: false
52
53
  opt :reload,
53
- "CMDB key that enables reload-on-edit",
54
- :type => :string
54
+ 'CMDB key that enables reload-on-edit',
55
+ type: :string
55
56
  opt :reload_signal,
56
- "Signal to send to app server when code is edited",
57
- :type => :string,
58
- :default => "HUP"
57
+ 'Signal to send to app server when code is edited',
58
+ type: :string,
59
+ default: 'HUP'
59
60
  opt :env,
60
61
  "Add CMDB keys to the app server's process environment",
61
- :default => false
62
+ default: false
62
63
  opt :user,
63
- "Switch to named user before executing app",
64
- :type => :string
64
+ 'Switch to named user before executing app',
65
+ type: :string
65
66
  opt :root,
66
- "Promote named subkey to the root when it is present in a namespace",
67
- :type => :string
67
+ 'Promote named subkey to the root when it is present in a namespace',
68
+ type: :string
68
69
  end
69
70
 
70
- self.new(ARGV, options)
71
+ new(ARGV, options)
71
72
  end
72
73
 
73
-
74
+ def run
75
+ end
74
76
  end
75
77
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'logger'
2
3
  require 'listen'
3
4
 
@@ -31,43 +32,43 @@ cmdb shim [options] -- <command_to_exec> [options_for_command]
31
32
  Where [options] are selected from:
32
33
  EOS
33
34
  opt :dir,
34
- "Directory to scan for key-replacement tokens in data files",
35
- :type => :string
35
+ 'Directory to scan for key-replacement tokens in data files',
36
+ type: :string
36
37
  opt :consul_url,
37
- "The URL for talking to consul",
38
- :type => :string
38
+ 'The URL for talking to consul',
39
+ type: :string
39
40
  opt :consul_prefix,
40
- "The prefix to use when getting keys from consul, can be specified more than once",
41
- :type => :string,
42
- :multi => true
41
+ 'The prefix to use when getting keys from consul, can be specified more than once',
42
+ type: :string,
43
+ multi: true
43
44
  opt :keys,
44
- "Override search path(s) for CMDB key files",
45
- :type => :strings
45
+ 'Override search path(s) for CMDB key files',
46
+ type: :strings
46
47
  opt :pretend,
47
- "Check for errors, but do not actually launch the app or rewrite files",
48
- :default => false
48
+ 'Check for errors, but do not actually launch the app or rewrite files',
49
+ default: false
49
50
  opt :quiet,
50
51
  "Don't print any output",
51
- :default => false
52
+ default: false
52
53
  opt :reload,
53
- "CMDB key that enables reload-on-edit",
54
- :type => :string
54
+ 'CMDB key that enables reload-on-edit',
55
+ type: :string
55
56
  opt :reload_signal,
56
- "Signal to send to app server when code is edited",
57
- :type => :string,
58
- :default => "HUP"
57
+ 'Signal to send to app server when code is edited',
58
+ type: :string,
59
+ default: 'HUP'
59
60
  opt :env,
60
61
  "Add CMDB keys to the app server's process environment",
61
- :default => false
62
+ default: false
62
63
  opt :user,
63
- "Switch to named user before executing app",
64
- :type => :string
64
+ 'Switch to named user before executing app',
65
+ type: :string
65
66
  opt :root,
66
- "Promote named subkey to the root when it is present in a namespace",
67
- :type => :string
67
+ 'Promote named subkey to the root when it is present in a namespace',
68
+ type: :string
68
69
  end
69
70
 
70
- self.new(ARGV, options)
71
+ new(ARGV, options)
71
72
  end
72
73
 
73
74
  # Irrevocably change the current user for this Unix process by calling the
@@ -90,7 +91,7 @@ Where [options] are selected from:
90
91
  # Create a Shim.
91
92
  # @param [Array] command collection of string to pass to Kernel#exec; 0th element is the command name
92
93
  # @options [String] :condif_dir
93
- def initialize(command, options={})
94
+ def initialize(command, options = {})
94
95
  @command = command
95
96
  @dir = options[:dir]
96
97
  @consul_url = options[:consul_url]
@@ -103,32 +104,26 @@ Where [options] are selected from:
103
104
  @user = options[:user]
104
105
  @root = options[:root]
105
106
 
106
- unless @keys.empty?
107
- CMDB::FileSource.base_directories = @keys
108
- end
109
- unless @consul_url.nil?
110
- CMDB::ConsulSource.url = @consul_url
111
- end
107
+ CMDB::FileSource.base_directories = @keys unless @keys.empty?
108
+ CMDB::ConsulSource.url = @consul_url unless @consul_url.nil?
112
109
  if !@consul_prefixes.nil? && !@consul_prefixes.empty?
113
110
  CMDB::ConsulSource.prefixes = @consul_prefixes
114
111
  end
115
112
 
116
- if options[:quiet]
117
- CMDB.log.level = Logger::FATAL
118
- end
113
+ CMDB.log.level = Logger::FATAL if options[:quiet]
119
114
  end
120
115
 
121
116
  # Run the shim.
122
117
  #
123
118
  # @raise [SystemExit] if something goes wrong
124
119
  def run
125
- @cmdb = CMDB::Interface.new(:root=>@root)
120
+ @cmdb = CMDB::Interface.new(root: @root)
126
121
 
127
122
  rewrote = rewrite_files
128
123
  populated = populate_environment
129
124
 
130
- if (!rewrote && !populated && !@pretend && @command.empty?)
131
- CMDB.log.warn "CMDB: nothing to do; please specify --dir, --env, or a command to run"
125
+ if !rewrote && !populated && !@pretend && @command.empty?
126
+ CMDB.log.warn 'CMDB: nothing to do; please specify --dir, --env, or a command to run'
132
127
  exit 7
133
128
  end
134
129
 
@@ -170,7 +165,7 @@ Where [options] are selected from:
170
165
 
171
166
  CMDB.log.info 'Starting rewrite of configuration...'
172
167
 
173
- rewriter = CMDB::Rewriter.new(@dir)
168
+ rewriter = CMDB::Rewriter.new(@dir)
174
169
 
175
170
  total = rewriter.rewrite(@cmdb)
176
171
 
@@ -178,7 +173,7 @@ Where [options] are selected from:
178
173
  missing = rewriter.missing_keys.map { |k| " #{k}" }.join("\n")
179
174
  CMDB.log.error "Cannot rewrite configuration; #{rewriter.missing_keys.size} missing keys:\n#{missing}"
180
175
 
181
- exit -rewriter.missing_keys.size
176
+ exit(-rewriter.missing_keys.size)
182
177
  end
183
178
 
184
179
  report_rewrite(total)
@@ -236,7 +231,7 @@ Where [options] are selected from:
236
231
  exec_app
237
232
  end
238
233
 
239
- CMDB.log.info("App (pid %d) has been forked; watching %s" % [pid, ::Dir.pwd])
234
+ CMDB.log.info('App (pid %d) has been forked; watching %s' % [pid, ::Dir.pwd])
240
235
 
241
236
  t0 = Time.at(0)
242
237
 
@@ -250,39 +245,39 @@ Where [options] are selected from:
250
245
  dt = Time.now - t0
251
246
  if dt > 15
252
247
  Process.kill(@signal, pid)
253
- CMDB.log.info "Sent SIG%s to app (pid %d) because (modified,created,deleted)=(%d,%d,%d)" %
248
+ CMDB.log.info 'Sent SIG%s to app (pid %d) because (modified,created,deleted)=(%d,%d,%d)' %
254
249
  [@signal, pid, modified.size, added.size, removed.size]
255
250
  t0 = Time.now
256
251
  else
257
- CMDB.log.error "Skipped SIG%s to app (pid %d) due to timeout (%d)" %
252
+ CMDB.log.error 'Skipped SIG%s to app (pid %d) due to timeout (%d)' %
258
253
  [@signal, pid, dt]
259
254
  end
260
255
  rescue
261
- CMDB.log.error "Skipped SIG%s to app (pid %d) due to %s" %
262
- [@signal, pid, $!.to_s]
256
+ CMDB.log.error 'Skipped SIG%s to app (pid %d) due to %s' %
257
+ [@signal, pid, $ERROR_INFO.to_s]
263
258
  end
264
259
  end
265
260
 
266
261
  listener.start
267
262
 
268
- wpid, wstatus = nil, nil
263
+ wpid = nil
264
+ wstatus = nil
269
265
 
270
266
  loop do
271
267
  begin
272
268
  wpid, wstatus = Process.wait2(-1, Process::WNOHANG)
273
- if wpid == pid && wstatus.exited?
274
- break
275
- elsif wpid
276
- CMDB.log.info("Descendant (pid %d) has waited with %s" % [wpid, wstatus.inspect])
269
+ if wpid
270
+ break if wstatus.exited?
271
+ CMDB.log.info('Descendant (pid %d) has waited with %s' % [wpid, wstatus.inspect])
277
272
  end
278
273
  rescue
279
- CMDB.log.error "Skipped wait2 to app (pid %d) due to %s" %
280
- [pid, $!.to_s]
274
+ CMDB.log.error 'Skipped wait2 to app (pid %d) due to %s' %
275
+ [pid, $ERROR_INFO.to_s]
281
276
  end
282
277
  sleep(1)
283
278
  end
284
279
 
285
- CMDB.log.info("App (pid %d) has exited with %s" % [wpid, wstatus.inspect])
280
+ CMDB.log.info('App (pid %d) has exited with %s' % [wpid, wstatus.inspect])
286
281
  listener.stop
287
282
  exit(wstatus.exitstatus || 43)
288
283
  end
data/lib/cmdb/commands.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module CMDB
2
3
  module Commands
3
4
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'diplomat'
2
3
 
3
4
  module CMDB
@@ -5,31 +6,33 @@ module CMDB
5
6
  # Regular expression to match array values
6
7
  ARRAY_VALUE = /^\[(.*)\]$/
7
8
 
8
- # The url to communicate with consul
9
- @@url = nil
10
- # The prefixes to use when getting keys from consul
11
- @@prefixes = nil
9
+ ### Class variables
12
10
 
13
- def self.url=(url)
14
- @@url = url
11
+ class << self
12
+ attr_writer :url
15
13
  end
16
14
 
17
- def self.url
18
- @@url
15
+ class << self
16
+ attr_reader :url
19
17
  end
20
18
 
21
- def self.prefixes=(prefixes)
22
- @@prefixes = prefixes
19
+ class << self
20
+ attr_writer :prefixes
23
21
  end
24
22
 
25
- def self.prefixes
26
- @@prefixes
23
+ class << self
24
+ attr_reader :prefixes
27
25
  end
28
26
 
27
+ ### Instance variables
28
+
29
+ # The url to communicate with consul
30
+ @url = nil
31
+
29
32
  # Initialize the configuration for consul source
30
33
  def initialize(prefix)
31
34
  Diplomat.configure do |config|
32
- config.url = @@url
35
+ config.url = self.class.url
33
36
  end
34
37
  @prefix = prefix
35
38
  end
@@ -38,12 +41,14 @@ module CMDB
38
41
  def get(key)
39
42
  value = Diplomat::Kv.get(dot_to_slash(key))
40
43
  process_value(value)
44
+ rescue TypeError
45
+ puts 'hi'
41
46
  rescue Diplomat::KeyNotFound
42
47
  nil
43
48
  end
44
49
 
45
50
  # Not implemented for consul source
46
- def each_pair(&block)
51
+ def each_pair(&_block)
47
52
  prefix = @prefix || ''
48
53
  all = Diplomat::Kv.get(prefix, recurse: true)
49
54
  all.each do |item|
@@ -51,21 +56,23 @@ module CMDB
51
56
  dotted_key = item[:key].split('/').join('.')
52
57
  key = dotted_prefix == '' ? dotted_key : dotted_key.split("#{dotted_prefix}.").last
53
58
  value = process_value(item[:value])
54
- puts "Key: #{key}, Value: #{value}"
55
- block.call(dotted_key.split("#{dotted_prefix}.").last, process_value(item[:value]))
59
+ yield(key, value)
56
60
  end
57
61
  rescue Diplomat::KeyNotFound
62
+ CMDB.log.warn exc.message
58
63
  end
59
64
 
60
65
  private
61
66
 
67
+ # Lazily parse a value, which may be valid JSON or may be a bare string.
68
+ # TODO: concat a regexp to match JSONable things
62
69
  def process_value(val)
63
- return JSON.load(val)
64
- rescue Exception
65
- return val
70
+ JSON.load(val)
71
+ rescue JSON::ParserError
72
+ val
66
73
  end
67
74
 
68
- # Converts the dotted notation to a slashed notation. If a @@prefix is set, it applies the prefix.
75
+ # Converts the dotted notation to a slashed notation. If a @prefix is set, it applies the prefix.
69
76
  # @example
70
77
  # "common.proxy.endpoints" => common/proxy/endpoints (or) shard403/common/proxy/endpoints
71
78
  def dot_to_slash(key)
@@ -74,4 +81,3 @@ module CMDB
74
81
  end
75
82
  end
76
83
  end
77
-
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'uri'
2
3
 
3
4
  module CMDB
@@ -12,29 +13,30 @@ module CMDB
12
13
  # @return [URI] a file:// URL describing where this source's data comes from
13
14
  attr_reader :prefix, :url
14
15
 
15
- @@base_directories = ['/var/lib/cmdb', File.expand_path('~/.cmdb')]
16
+ @base_directories = ['/var/lib/cmdb', File.expand_path('~/.cmdb')]
16
17
 
17
18
  # List of directories that will be searched (in order) for YML files at load time.
18
19
  # @return [Array] collection of String
19
- def self.base_directories
20
- @@base_directories
20
+ class << self
21
+ attr_reader :base_directories
21
22
  end
22
23
 
23
24
  # @param [Array] bd collection of String absolute paths to search for YML files
24
- def self.base_directories=(bd)
25
- @@base_directories = bd
25
+ class << self
26
+ attr_writer :base_directories
26
27
  end
27
28
 
28
29
  # Construct a new FileSource from an input YML file.
29
30
  # @param [String,Pathname] filename path to a YAML file
31
+ # @param [String] root optional subpath in data to "mount"
32
+ # @param [String] prefix optional prefix of
30
33
  # @raise [BadData] if the file's content is malformed
31
- def initialize(filename, root=nil)
34
+ def initialize(filename, root = nil, prefix = File.basename(filename, '.*'))
35
+ @data = {}
36
+ @prefix = prefix
32
37
  filename = File.expand_path(filename)
33
38
  @url = URI.parse("file://#{filename}")
34
- @prefix = File.basename(filename, ".*")
35
39
  @extension = File.extname(filename)
36
- @data = {}
37
-
38
40
  raw_bytes = File.read(filename)
39
41
  raw_data = nil
40
42
 
@@ -45,10 +47,10 @@ module CMDB
45
47
  when /ya?ml$/i
46
48
  raw_data = YAML.load(raw_bytes)
47
49
  else
48
- raise BadData.new(self.url, 'file with unknown extension; expected js(on) or y(a)ml')
50
+ raise BadData.new(url, 'file with unknown extension; expected js(on) or y(a)ml')
49
51
  end
50
- rescue Exception
51
- raise BadData.new(self.url, 'CMDB data file')
52
+ rescue StandardError
53
+ raise BadData.new(url, 'CMDB data file')
52
54
  end
53
55
 
54
56
  raw_data = raw_data[root] if !root.nil? && raw_data.key?(root)
@@ -67,9 +69,9 @@ module CMDB
67
69
  # @yield every key/value in the source
68
70
  # @yieldparam [String] key
69
71
  # @yieldparam [Object] value
70
- def each_pair(&block)
72
+ def each_pair(&_block)
71
73
  # Strip the prefix in the key and call the block
72
- @data.each_pair { |k, v| block.call(k.split("#{@prefix}.").last, v) }
74
+ @data.each_pair { |k, v| yield(k.split("#{@prefix}.").last, v) }
73
75
  end
74
76
 
75
77
  private
@@ -82,19 +84,19 @@ module CMDB
82
84
  flatten(value, key, output)
83
85
  when Array
84
86
  if value.all? { |e| e.is_a?(String) } ||
85
- value.all? { |e| e.is_a?(Numeric) } ||
86
- value.all? { |e| e == true } ||
87
- value.all? { |e| e == false }
87
+ value.all? { |e| e.is_a?(Numeric) } ||
88
+ value.all? { |e| e == true } ||
89
+ value.all? { |e| e == false }
88
90
  output[key] = value
89
91
  else
90
92
  # mismatched arrays: not allowed
91
- raise BadValue.new(self.url, key, value)
93
+ raise BadValue.new(url, key, value)
92
94
  end
93
95
  when String, Numeric, TrueClass, FalseClass
94
96
  output[key] = value
95
97
  else
96
98
  # nil and anything else: not allowed
97
- raise BadValue.new(self.url, key, value)
99
+ raise BadValue.new(url, key, value)
98
100
  end
99
101
  end
100
102
  end
@@ -1,10 +1,11 @@
1
+ # encoding: utf-8
1
2
  require 'json'
2
3
 
3
4
  module CMDB
4
5
  class Interface
5
6
  # Create a new instance of the CMDB interface.
6
7
  # @option settings [String] root name of subkey to consider as root
7
- def initialize(settings={})
8
+ def initialize(settings = {})
8
9
  @root = settings[:root] if settings
9
10
 
10
11
  namespaces = {}
@@ -14,7 +15,7 @@ module CMDB
14
15
 
15
16
  @sources = []
16
17
  # Load from consul source first if one is available.
17
- if !ConsulSource.url.nil?
18
+ unless ConsulSource.url.nil?
18
19
  if ConsulSource.prefixes.nil? || ConsulSource.prefixes.empty?
19
20
  @sources << ConsulSource.new('')
20
21
  else
@@ -109,7 +110,7 @@ module CMDB
109
110
 
110
111
  # Also consult working dir in development environments
111
112
  if CMDB.development?
112
- local_dir = File.join(Dir.pwd, '.cmdb')
113
+ local_dir = File.join(Dir.pwd, '.cmdb')
113
114
  directories += [local_dir]
114
115
  end
115
116
 
@@ -136,18 +137,15 @@ module CMDB
136
137
  overlapping.each do |ns, sources|
137
138
  exc = ValueConflict.new(ns, sources)
138
139
 
139
- if CMDB.development?
140
- CMDB.log.warn exc.message
141
- else
142
- raise exc
143
- end
140
+ CMDB.log.warn exc.message
141
+ raise exc unless CMDB.development?
144
142
  end
145
143
  end
146
144
 
147
145
  # Make an environment variable out of a key name
148
146
  def key_to_env(key)
149
147
  env_name = key
150
- env_name.gsub!(/[^A-Za-z0-9_]+/,'_')
148
+ env_name.gsub!(/[^A-Za-z0-9_]+/, '_')
151
149
  env_name.upcase!
152
150
  env_name
153
151
  end
data/lib/cmdb/rewriter.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'yaml'
2
3
  require 'json'
3
4
 
@@ -30,13 +31,13 @@ module CMDB
30
31
  total = 0
31
32
  @rewriters.each { |rw| total += rw.rewrite(cmdb) }
32
33
 
33
- @missing_keys = @rewriters.map { |rw| rw.missing_keys}.flatten.uniq.sort
34
+ @missing_keys = @rewriters.map(&:missing_keys).flatten.uniq.sort
34
35
 
35
36
  total
36
37
  end
37
38
 
38
39
  def save
39
- @rewriters.each { |rw| rw.save }
40
+ @rewriters.each(&:save)
40
41
  true
41
42
  end
42
43
 
data/lib/cmdb/version.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module CMDB
2
- VERSION = '2.6.0'
3
+ VERSION = '2.6.1'.freeze
3
4
  end
data/lib/cmdb.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'set'
2
3
  require 'singleton'
3
4
 
@@ -62,7 +63,7 @@ module CMDB
62
63
 
63
64
  # @param [URI] url filesystem or network location of the bad value
64
65
  # @param [String] context brief description of where data was found e.g. 'CMDB data file' or 'input config file'
65
- def initialize(url, context=nil)
66
+ def initialize(url, context = nil)
66
67
  @url = url
67
68
  super("Malformed data encountered #{(' in ' + context) if context}")
68
69
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.0
4
+ version: 2.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - RightScale
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-29 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: listen
@@ -76,6 +76,7 @@ extra_rdoc_files: []
76
76
  files:
77
77
  - ".gitignore"
78
78
  - ".rspec"
79
+ - ".rubocop.yml"
79
80
  - ".travis.yml"
80
81
  - CHANGELOG.md
81
82
  - Gemfile
@@ -120,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
121
  version: '0'
121
122
  requirements: []
122
123
  rubyforge_project:
123
- rubygems_version: 2.4.5
124
+ rubygems_version: 2.4.5.1
124
125
  signing_key:
125
126
  specification_version: 4
126
127
  summary: Command-line tool for configuration manegement databases