legion-settings 1.1.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bac4b40b52e35751f9b72eb5bb806c7a83702f887f0e817ca11505d72f32a008
4
+ data.tar.gz: e9e01f6af6b63cfb2860c64d6f8a80d80c70a9647f94a7c6365091a716dadca9
5
+ SHA512:
6
+ metadata.gz: 5dbafcc1e2994fe5bc1f3a915765388b47f8cabf7575a63a92130c149be6aa0fe2d767f586e110878ac0595260ad77293720dc7eb8b1c906e90c37d482b1395d
7
+ data.tar.gz: a3fc8d52a94daf8e430489299435eb2ba5c974d14fd99cd92ec8577561325ae8a1aa10322baa1c7d1e052fcbaad9386b784aa6c09ba5b758e2813a8bf95abadf
@@ -0,0 +1,83 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@0.2.1
4
+ sonarcloud: sonarsource/sonarcloud@1.0.1
5
+
6
+ jobs:
7
+ "rubocop":
8
+ docker:
9
+ - image: circleci/ruby:2.7-node
10
+ steps:
11
+ - checkout
12
+ - ruby/load-cache
13
+ - ruby/install-deps
14
+ - run:
15
+ name: Run Rubocop
16
+ command: bundle exec rubocop
17
+ - ruby/save-cache
18
+ "ruby-two-five":
19
+ docker:
20
+ - image: circleci/ruby:2.5
21
+ steps:
22
+ - checkout
23
+ - ruby/load-cache
24
+ - run:
25
+ name: update bundler
26
+ command: gem update bundler
27
+ - ruby/install-deps
28
+ - ruby/run-tests
29
+ - ruby/save-cache
30
+ "ruby-two-six":
31
+ docker:
32
+ - image: circleci/ruby:2.6
33
+ steps:
34
+ - checkout
35
+ - ruby/load-cache
36
+ - run:
37
+ name: update bundler
38
+ command: gem update bundler
39
+ - ruby/install-deps
40
+ - ruby/run-tests
41
+ - ruby/save-cache
42
+ "ruby-two-seven":
43
+ docker:
44
+ - image: circleci/ruby:2.7
45
+ - image: memcached:1.5-alpine
46
+ steps:
47
+ - checkout
48
+ - ruby/load-cache
49
+ - ruby/install-deps
50
+ - ruby/run-tests
51
+ - ruby/save-cache
52
+ "sonarcloud":
53
+ docker:
54
+ - image: circleci/ruby:2.7
55
+ steps:
56
+ - checkout
57
+ - ruby/load-cache
58
+ - ruby/install-deps
59
+ - ruby/run-tests
60
+ - run:
61
+ name: Run Rubocop
62
+ command: bundle exec rubocop --format=json --out=rubocop-result.json
63
+ - sonarcloud/scan
64
+ - ruby/save-cache
65
+
66
+ workflows:
67
+ version: 2
68
+ rubocop-rspec:
69
+ jobs:
70
+ - rubocop
71
+ - ruby-two-five:
72
+ requires:
73
+ - rubocop
74
+ - ruby-two-six:
75
+ requires:
76
+ - ruby-two-five
77
+ - ruby-two-seven:
78
+ requires:
79
+ - ruby-two-five
80
+ - sonarcloud:
81
+ requires:
82
+ - ruby-two-seven
83
+ - ruby-two-six
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /rspec/
10
+ /test-results/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
@@ -0,0 +1,24 @@
1
+ Layout/LineLength:
2
+ Max: 120
3
+ Metrics/MethodLength:
4
+ Max: 30
5
+ Metrics/ClassLength:
6
+ Max: 1500
7
+ Metrics/BlockLength:
8
+ Max: 50
9
+ Layout/SpaceAroundEqualsInParameterDefault:
10
+ EnforcedStyle: space
11
+ Style/SymbolArray:
12
+ Enabled: true
13
+ Layout/HashAlignment:
14
+ EnforcedHashRocketStyle: table
15
+ EnforcedColonStyle: table
16
+ Style/Documentation:
17
+ Enabled: false
18
+ AllCops:
19
+ TargetRubyVersion: 2.5
20
+ NewCops: enable
21
+ Style/FrozenStringLiteralComment:
22
+ Enabled: false
23
+ Naming/FileName:
24
+ Enabled: false
@@ -0,0 +1,9 @@
1
+ # Legion::Settings Changelog
2
+
3
+ ## v0.2.0
4
+ * Adding merge settings method
5
+ * Updating default settings
6
+
7
+ ## v0.1.1
8
+ * Updating default hash to include legion:{extensions:{}}
9
+ * Fixing rubocop errors
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ legion-settings (1.1.1)
5
+ legion-json
6
+ legion-logging
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.1)
12
+ diff-lcs (1.4.4)
13
+ docile (1.3.2)
14
+ legion-json (1.1.0)
15
+ multi_json
16
+ legion-logging (1.1.0)
17
+ rainbow (~> 3)
18
+ multi_json (1.15.0)
19
+ parallel (1.19.2)
20
+ parser (2.7.1.4)
21
+ ast (~> 2.4.1)
22
+ rainbow (3.0.0)
23
+ rake (13.0.1)
24
+ regexp_parser (1.7.1)
25
+ rexml (3.2.4)
26
+ rspec (3.9.0)
27
+ rspec-core (~> 3.9.0)
28
+ rspec-expectations (~> 3.9.0)
29
+ rspec-mocks (~> 3.9.0)
30
+ rspec-core (3.9.2)
31
+ rspec-support (~> 3.9.3)
32
+ rspec-expectations (3.9.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.9.0)
35
+ rspec-mocks (3.9.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-support (3.9.3)
39
+ rspec_junit_formatter (0.4.1)
40
+ rspec-core (>= 2, < 4, != 2.12.0)
41
+ rubocop (0.88.0)
42
+ parallel (~> 1.10)
43
+ parser (>= 2.7.1.1)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ regexp_parser (>= 1.7)
46
+ rexml
47
+ rubocop-ast (>= 0.1.0, < 1.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 1.4.0, < 2.0)
50
+ rubocop-ast (0.2.0)
51
+ parser (>= 2.7.0.1)
52
+ ruby-progressbar (1.10.1)
53
+ simplecov (0.18.5)
54
+ docile (~> 1.1)
55
+ simplecov-html (~> 0.11)
56
+ simplecov-html (0.12.2)
57
+ unicode-display_width (1.7.0)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ bundler
64
+ legion-settings!
65
+ rake
66
+ rspec
67
+ rspec_junit_formatter
68
+ rubocop
69
+ simplecov
70
+
71
+ BUNDLED WITH
72
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Esity
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ # Legion::Settings
2
+
3
+ Legion::Settings is part of the Legion Framework
4
+
5
+ ###Badges
6
+ ####CICD
7
+ | | License | CircleCI | CodeCov | CodeBeat |
8
+ |---------|---------|----------|---------|----------|
9
+ | Master |[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)|[![CircleCI](https://circleci.com/bb/legion-io/legion-settings/tree/master.svg?style=svg)](https://circleci.com/bb/legion-io/legion-settings/tree/master)|[![codecov](https://codecov.io/bb/legion-io/legion-settings/branch/master/graph/badge.svg)](https://codecov.io/bb/legion-io/legion-settings)|[![codebeat badge](https://codebeat.co/badges/e86dbe3e-b463-4f3c-91a8-c85492fd3833)](https://codebeat.co/projects/bitbucket-org-legion-io-legion-settings-master)|
10
+ | Develop |[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)|[![CircleCI](https://circleci.com/bb/legion-io/legion-settings/tree/develop.svg?style=svg)](https://circleci.com/bb/legion-io/legion-settings/tree/develop)|[![codecov](https://codecov.io/bb/legion-io/legion-settings/branch/develop/graph/badge.svg)](https://codecov.io/bb/legion-io/legion-settings)|[![codebeat badge](https://codebeat.co/badges/e86dbe3e-b463-4f3c-91a8-c85492fd3833)](https://codebeat.co/projects/bitbucket-org-legion-io-legion-settings-develop)|
11
+
12
+ #####RubyGems
13
+ ![](https://ruby-gem-downloads-badge.herokuapp.com/legion-settings)
14
+ [![Gem Version](https://badge.fury.io/rb/legion-settings.svg)](https://badge.fury.io/rb/legion-settings)
15
+
16
+ #####Bitbucket
17
+ ![](https://img.shields.io/bitbucket/issues-raw/legion-io/legion-settings.svg)
18
+ ![](https://img.shields.io/bitbucket/pr-raw/legion-io/legion-settings.svg)
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'legion-settings'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install legion-settings
34
+
35
+ ## Usage
36
+
37
+ This library is utilized by Legion to load config files
38
+
39
+ ## Gem
40
+
41
+ This gem can be viewed and download from [RubyGems - Legion-Settings](https://rubygems.org/gems/legion-settings)
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec install`.
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://bitbucket.org/legion-io/legion-settings/issues This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,39 @@
1
+ image: ruby:2.4.0
2
+
3
+ pipelines:
4
+ branches:
5
+ master:
6
+ - step:
7
+ caches:
8
+ - bundler
9
+ script:
10
+ - gem install bundle rubocop
11
+ - bundle install
12
+ - rubocop
13
+ - rake
14
+ - step:
15
+ name: Deploy to test gem server
16
+ deployment: test
17
+ script:
18
+ - gem install bundle geminabox
19
+ - bundle install
20
+ - gem build legion-settings.gemspec
21
+ - gem inabox legion-settings*.gem -g https://gems.whonodes.org/ -o
22
+ - step:
23
+ name: Deploy to RubyGems
24
+ deployment: production
25
+ trigger: manual
26
+ script:
27
+ - gem build legion-settings.gemspec
28
+ default:
29
+ - step:
30
+ caches:
31
+ - bundler
32
+ script:
33
+ - gem install bundle rubocop
34
+ - bundle install
35
+ - rubocop
36
+ - rake
37
+ definitions:
38
+ caches:
39
+ bundler: vendor/bundle
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'legion/settings/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'legion-settings'
7
+ spec.version = Legion::Settings::VERSION
8
+ spec.authors = ['Esity']
9
+ spec.email = ['matthewdiverson@gmail.com']
10
+
11
+ spec.summary = 'Used to load the json settings files'
12
+ spec.description = "Based on Sensu's implementation of settings"
13
+ spec.homepage = 'https://bitbucket.org/whonodes/legion-settings'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'rspec_junit_formatter'
26
+ spec.add_development_dependency 'rubocop'
27
+ spec.add_development_dependency 'simplecov'
28
+ spec.add_dependency 'legion-json'
29
+ spec.add_dependency 'legion-logging'
30
+ end
@@ -0,0 +1,48 @@
1
+ require 'legion/json'
2
+ require 'legion/settings/version'
3
+ require 'legion/json/parse_error'
4
+ require 'legion/settings/loader'
5
+
6
+ module Legion
7
+ module Settings
8
+ class << self
9
+ attr_accessor :loader
10
+
11
+ def load(options = {})
12
+ @loader = Legion::Settings::Loader.new
13
+ @loader.load_env
14
+ @loader.load_file(options[:config_file]) if options[:config_file]
15
+ @loader.load_directory(options[:config_dir]) if options[:config_dir]
16
+ options[:config_dirs]&.each do |directory|
17
+ @loader.load_directory(directory)
18
+ end
19
+ @loader
20
+ end
21
+
22
+ def get(options = {})
23
+ @loader || @loader = load(options)
24
+ end
25
+
26
+ def [](key)
27
+ Legion::Logging.info('Legion::Settings was not loading, auto loading now!') if @loader.nil?
28
+ @loader = load if @loader.nil?
29
+ @loader[key]
30
+ rescue NoMethodError, TypeError
31
+ Legion::Logging.fatal 'rescue inside [](key)'
32
+ nil
33
+ end
34
+
35
+ def set_prop(key, value)
36
+ @loader = load if @loader.nil?
37
+ @loader[key] = value
38
+ end
39
+
40
+ def merge_settings(key, hash)
41
+ @loader = load if @loader.nil?
42
+ thing = {}
43
+ thing[key.to_sym] = hash
44
+ @loader.load_module_settings(thing)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module Legion
2
+ module Settings
3
+ CATEGORIES = [:interations].freeze
4
+ end
5
+ end
@@ -0,0 +1,285 @@
1
+ require 'socket'
2
+
3
+ module Legion
4
+ module Settings
5
+ class Loader
6
+ class Error < RuntimeError; end
7
+ attr_reader :warnings, :errors, :loaded_files, :settings
8
+
9
+ def initialize
10
+ @warnings = []
11
+ @errors = []
12
+ @settings = default_settings
13
+ @indifferent_access = false
14
+ @loaded_files = []
15
+ self.class.create_category_methods
16
+ end
17
+
18
+ def client_defaults
19
+ {
20
+ hostname: system_hostname,
21
+ address: system_address,
22
+ name: "#{::Socket.gethostname.gsub('.', '_')}.#{::Process.pid}",
23
+ ready: false
24
+ }
25
+ end
26
+
27
+ def default_settings
28
+ {
29
+ client: client_defaults,
30
+ cluster: { public_keys: {} },
31
+ crypt: {
32
+ cluster_secret: nil,
33
+ cluster_secret_timeout: 5,
34
+ vault: { connected: false }
35
+ },
36
+ cache: { enabled: true, connected: false, driver: 'dalli' },
37
+ extensions: {},
38
+ default_extension_settings: {
39
+ logger: { level: 'info', trace: false, extended: false }
40
+ },
41
+ logging: {
42
+ level: 'info',
43
+ location: 'stdout',
44
+ trace: true,
45
+ backtrace_logging: true
46
+ },
47
+ transport: { connected: false },
48
+ data: { connected: false }
49
+ }
50
+ end
51
+
52
+ def self.create_category_methods
53
+ # CATEGORIES.each do |category|
54
+ # define_method(category) do
55
+ # setting_category(category)
56
+ # end
57
+ # method_name = category.to_s.chop + "_exists?"
58
+ # define_method(method_name.to_sym) do |name|
59
+ # definition_exists?(category, name)
60
+ # end
61
+ # end
62
+ end
63
+
64
+ def to_hash
65
+ unless @indifferent_access
66
+ indifferent_access!
67
+ @hexdigest = nil
68
+ end
69
+ @settings
70
+ end
71
+
72
+ def [](key)
73
+ to_hash[key]
74
+ end
75
+
76
+ def hexdigest
77
+ if @hexdigest && @indifferent_access
78
+ @hexdigest
79
+ else
80
+ hash = case legion_service_name
81
+ when 'client', 'rspec'
82
+ to_hash
83
+ else
84
+ to_hash.reject do |key, _value|
85
+ key.to_s == 'client'
86
+ end
87
+ end
88
+ @hexdigest = Digest::SHA256.hexdigest(hash.to_s)
89
+ end
90
+ end
91
+
92
+ def load_env
93
+ load_api_env
94
+ end
95
+
96
+ def load_module_settings(config)
97
+ @settings = deep_merge(config, @settings)
98
+ end
99
+
100
+ def load_module_default(config)
101
+ merged = deep_merge(@settings, config)
102
+ deep_diff(@settings, merged) unless @loaded_files.empty?
103
+ @settings = merged
104
+ end
105
+
106
+ def load_file(file) # rubocop:disable Metrics/AbcSize
107
+ Legion::Logging.debug("Trying to load file #{file}")
108
+ if File.file?(file) && File.readable?(file)
109
+ begin
110
+ contents = read_config_file(file)
111
+ config = contents.empty? ? {} : Legion::JSON.load(contents)
112
+ merged = deep_merge(@settings, config)
113
+ deep_diff(@settings, merged) unless @loaded_files.empty?
114
+ @settings = merged
115
+ # @indifferent_access = false
116
+ @loaded_files << file
117
+ rescue Legion::JSON::ParseError => e
118
+ Legion::Logging.error('config file must be valid json')
119
+ Legion::Logging.debug("file:#{file}, error: #{e}")
120
+ end
121
+ else
122
+ Legion::Logging.warn("Config file does not exist or is not readable file:#{file}")
123
+ end
124
+ end
125
+
126
+ def load_directory(directory)
127
+ path = directory.gsub(/\\(?=\S)/, '/')
128
+ if File.readable?(path) && File.executable?(path)
129
+ Dir.glob(File.join(path, '**{,/*/**}/*.json')).uniq.each do |file|
130
+ load_file(file)
131
+ end
132
+ else
133
+ load_error('insufficient permissions for loading', directory: directory)
134
+ end
135
+ end
136
+
137
+ def load_client_overrides
138
+ @settings[:client][:subscriptions] ||= []
139
+ if @settings[:client][:subscriptions].is_a?(Array)
140
+ @settings[:client][:subscriptions] << "client:#{@settings[:client][:name]}"
141
+ @settings[:client][:subscriptions].uniq!
142
+ @indifferent_access = false
143
+ else
144
+ Legion::Logging.warn('unable to apply legion client overrides, reason: client subscriptions is not an array')
145
+ end
146
+ end
147
+
148
+ def load_overrides!
149
+ load_client_overrides if %w[client rspec].include?(legion_service_name)
150
+ end
151
+
152
+ def set_env!
153
+ ENV['LEGION_LOADED_TEMPFILE'] = create_loaded_tempfile!
154
+ end
155
+
156
+ def validate
157
+ validator = Validator.new
158
+ @errors += validator.run(@settings, legion_service_name)
159
+ end
160
+
161
+ private
162
+
163
+ def setting_category(category)
164
+ @settings[category].map do |name, details|
165
+ details.merge(name: name.to_s)
166
+ end
167
+ end
168
+
169
+ def definition_exists?(category, name)
170
+ @settings[category].key?(name.to_sym)
171
+ end
172
+
173
+ def indifferent_hash
174
+ Hash.new do |hash, key|
175
+ hash[key.to_sym] if key.is_a?(String)
176
+ end
177
+ end
178
+
179
+ def with_indifferent_access(hash)
180
+ hash = indifferent_hash.merge(hash)
181
+ hash.each do |key, value|
182
+ hash[key] = with_indifferent_access(value) if value.is_a?(Hash)
183
+ end
184
+ end
185
+
186
+ def indifferent_access!
187
+ @settings = with_indifferent_access(@settings)
188
+ @indifferent_access = true
189
+ end
190
+
191
+ def load_api_env
192
+ return unless ENV['LEGION_API_PORT']
193
+
194
+ @settings[:api] ||= {}
195
+ @settings[:api][:port] = ENV['LEGION_API_PORT'].to_i
196
+ Legion::Logging.warn("using api port environment variable, api: #{@settings[:api]}")
197
+ @indifferent_access = false
198
+ end
199
+
200
+ def read_config_file(file)
201
+ contents = IO.read(file)
202
+ if contents.respond_to?(:force_encoding)
203
+ encoding = ::Encoding::ASCII_8BIT
204
+ contents = contents.force_encoding(encoding)
205
+ contents.sub!("\xEF\xBB\xBF".force_encoding(encoding), '')
206
+ else
207
+ contents.sub!(/^\357\273\277/, '')
208
+ end
209
+ contents.strip
210
+ end
211
+
212
+ def deep_merge(hash_one, hash_two)
213
+ merged = hash_one.dup
214
+ hash_two.each do |key, value|
215
+ merged[key] = if hash_one[key].is_a?(Hash) && value.is_a?(Hash)
216
+ deep_merge(hash_one[key], value)
217
+ elsif hash_one[key].is_a?(Array) && value.is_a?(Array)
218
+ hash_one[key].concat(value).uniq
219
+ else
220
+ value
221
+ end
222
+ end
223
+ merged
224
+ end
225
+
226
+ # rubocop:disable Metrics/AbcSize
227
+ def deep_diff(hash_one, hash_two)
228
+ keys = hash_one.keys.concat(hash_two.keys).uniq
229
+ keys.each_with_object({}) do |key, diff|
230
+ next if hash_one[key] == hash_two[key]
231
+
232
+ diff[key] = if hash_one[key].is_a?(Hash) && hash_two[key].is_a?(Hash)
233
+ deep_diff(hash_one[key], hash_two[key])
234
+ else
235
+ [hash_one[key], hash_two[key]]
236
+ end
237
+ end
238
+ end
239
+ # rubocop:enable Metrics/AbcSize
240
+
241
+ def create_loaded_tempfile!
242
+ dir = ENV['LEGION_LOADED_TEMPFILE_DIR'] || Dir.tmpdir
243
+ file_name = "legion_#{legion_service_name}_loaded_files"
244
+ path = File.join(dir, file_name)
245
+ File.open(path, 'w') do |file|
246
+ file.write(@loaded_files.join(':'))
247
+ end
248
+ path
249
+ end
250
+
251
+ def legion_service_name
252
+ File.basename($PROGRAM_NAME).split('-').last
253
+ end
254
+
255
+ def system_hostname
256
+ Socket.gethostname
257
+ rescue StandardError
258
+ 'unknown'
259
+ end
260
+
261
+ def system_address
262
+ Socket.ip_address_list.find do |address|
263
+ address.ipv4? && !address.ipv4_loopback?
264
+ end.ip_address
265
+ rescue StandardError
266
+ 'unknown'
267
+ end
268
+
269
+ def warning(message, data = {})
270
+ @warnings << {
271
+ message: message
272
+ }.merge(data)
273
+ Legion::Logging.warn(message)
274
+ end
275
+
276
+ def load_error(message, data = {})
277
+ @errors << {
278
+ message: message
279
+ }.merge(data)
280
+ Legion::Logging.error(message)
281
+ raise(Error, message)
282
+ end
283
+ end
284
+ end
285
+ end
@@ -0,0 +1,92 @@
1
+ module Legion
2
+ module Settings
3
+ module Rules
4
+ def must_be_a_hash(value)
5
+ value.is_a?(Hash)
6
+ end
7
+ alias is_a_hash? must_be_a_hash
8
+
9
+ def must_be_a_hash_if_set(value)
10
+ value.nil? ? true : must_be_a_hash(value)
11
+ end
12
+
13
+ def must_be_an_array(value)
14
+ value.is_a?(Array)
15
+ end
16
+ alias is_an_array? must_be_an_array
17
+
18
+ def must_be_an_array_if_set(value)
19
+ value.nil? ? true : must_be_an_array(value)
20
+ end
21
+
22
+ def must_be_a_string(value)
23
+ value.is_a?(String)
24
+ end
25
+ alias is_a_string? must_be_a_string
26
+
27
+ def must_be_a_string_if_set(value)
28
+ value.nil? ? true : must_be_a_string(value)
29
+ end
30
+
31
+ def must_be_an_integer(value)
32
+ value.is_a?(Integer)
33
+ end
34
+ alias is_an_integer? must_be_an_integer
35
+
36
+ def must_be_an_integer_if_set(value)
37
+ value.nil? ? true : must_be_an_integer(value)
38
+ end
39
+
40
+ def must_be_a_numeric(value)
41
+ value.is_a?(Numeric)
42
+ end
43
+
44
+ def must_be_a_numeric_if_set(value)
45
+ value.nil? ? true : must_be_a_numeric(value)
46
+ end
47
+
48
+ def must_match_regex(regex, value)
49
+ (value =~ regex).zero?
50
+ end
51
+
52
+ def must_be_boolean(value)
53
+ !value.nil?
54
+ end
55
+
56
+ def must_be_boolean_if_set(value)
57
+ value.nil? ? true : must_be_boolean(value)
58
+ end
59
+
60
+ def items_must_be_strings(value, regex = nil)
61
+ value.all? do |item|
62
+ item.is_a?(String) && !item.empty? &&
63
+ (regex.nil? || item =~ regex)
64
+ end
65
+ end
66
+
67
+ def either_are_set?(*values)
68
+ values.any? do |value|
69
+ !value.nil?
70
+ end
71
+ end
72
+
73
+ def must_be_time(*values)
74
+ values.all? do |value|
75
+ Time.parse(value)
76
+ rescue StandardError
77
+ false
78
+ end
79
+ end
80
+
81
+ def must_be_either(allowed, *values)
82
+ values.flatten.all? do |value|
83
+ allowed.include?(value)
84
+ end
85
+ end
86
+
87
+ def must_be_either_if_set(allowed, *values)
88
+ values[0].nil? ? true : must_be_either(allowed, values)
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,63 @@
1
+ require 'legion/settings/rules'
2
+ require 'legion/settings/validators'
3
+ require 'legion/settings/constants'
4
+
5
+ module Legion
6
+ module Settings
7
+ class Validator
8
+ include Rules
9
+ include Validators
10
+
11
+ attr_reader :failures
12
+
13
+ def initialize
14
+ @failures = []
15
+ end
16
+
17
+ def run(settings, service = nil)
18
+ validate_legion(settings[:legion])
19
+ validate_transport(settings[:transport])
20
+ validate_categories(settings)
21
+ case service
22
+ when 'client'
23
+ validate_client(settings[:client])
24
+ when 'api'
25
+ validate_api(settings[:api])
26
+ when 'rspec'
27
+ validate_client(settings[:client])
28
+ validate_api(settings[:api])
29
+ end
30
+ @failures
31
+ end
32
+
33
+ def reset!
34
+ failure_count = @failures.size
35
+ @failures = []
36
+ failure_count
37
+ end
38
+ alias reset reset!
39
+
40
+ private
41
+
42
+ def validate_categories(settings)
43
+ CATEGORIES.each do |category|
44
+ if is_a_hash?(settings[category])
45
+ validate_method = ('validate_' + category.to_s.chop).to_sym
46
+ settings[category].each do |name, details|
47
+ send(validate_method, details.merge(name: name.to_s))
48
+ end
49
+ else
50
+ invalid(settings[category], "#{category} must be a hash")
51
+ end
52
+ end
53
+ end
54
+
55
+ def invalid(object, message)
56
+ @failures << {
57
+ object: object,
58
+ message: message
59
+ }
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,9 @@
1
+ require 'legion/settings/validators/legion'
2
+
3
+ module Legion
4
+ module Settings
5
+ module Validators
6
+ include Legion
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ module Legion
2
+ module Settings
3
+ module Validators
4
+ module Legion
5
+ def validate_legion_spawn(legion)
6
+ spawn = legion[:spawn]
7
+ if is_a_hash?(spawn)
8
+ if is_an_integer?(spawn[:limit])
9
+ (spawn[:limit]).positive? ||
10
+ invalid(legion, 'legion spawn limit must be greater than 0')
11
+ else
12
+ invalid(legion, 'legion spawn limit must be an integer')
13
+ end
14
+ else
15
+ invalid(legion, 'legion spawn must be a hash')
16
+ end
17
+ end
18
+
19
+ def validate_legion(legion)
20
+ if is_a_hash?(legion)
21
+ validate_legion_spawn(legion)
22
+ else
23
+ invalid(legion, 'legion must be a hash')
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module Legion
2
+ module Settings
3
+ VERSION = '1.1.1'.freeze
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ sonar.projectKey=legion-io_legion-settings
2
+ sonar.organization=legion-io
3
+ sonar.projectName=Legion::Settings
4
+ sonar.sources=.
5
+ sonar.exclusions=vendor/**
6
+ sonar.coverage.exclusions=spec/**
7
+ sonar.ruby.coverage.reportPath=coverage/.resultset.json
8
+ sonar.ruby.file.suffixes=rb,ruby
9
+ sonar.ruby.coverage.framework=RSpec
10
+ sonar.ruby.rubocopConfig=.rubocop.yml
11
+ sonar.ruby.rubocop.reportPath=rubocop-result.json
12
+ sonar.ruby.rubocop.filePath=.
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: legion-settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Esity
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec_junit_formatter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: legion-json
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: legion-logging
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Based on Sensu's implementation of settings
126
+ email:
127
+ - matthewdiverson@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".circleci/config.yml"
133
+ - ".gitignore"
134
+ - ".rubocop.yml"
135
+ - CHANGELOG.md
136
+ - Gemfile
137
+ - Gemfile.lock
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bitbucket-pipelines.yml
142
+ - legion-settings.gemspec
143
+ - lib/legion/settings.rb
144
+ - lib/legion/settings/constants.rb
145
+ - lib/legion/settings/loader.rb
146
+ - lib/legion/settings/rules.rb
147
+ - lib/legion/settings/validator.rb
148
+ - lib/legion/settings/validators.rb
149
+ - lib/legion/settings/validators/legion.rb
150
+ - lib/legion/settings/version.rb
151
+ - sonar-project.properties
152
+ homepage: https://bitbucket.org/whonodes/legion-settings
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubygems_version: 3.1.2
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Used to load the json settings files
175
+ test_files: []