bmc-daemon-lib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 54cb8d5ebed46ef42910b486dd3cb4bb6cf7267d
4
+ data.tar.gz: 783a704b84e1737b60f495dbda942a4bb5e21d2b
5
+ SHA512:
6
+ metadata.gz: f939c454cdb4a2b0e95b024448f58aebe910638b4cf093a6de7897c369c4b06f0537c17a9356967e9522734ca23c98344f6d53d9c4d09b518193c792606164f9
7
+ data.tar.gz: 3bc34da2d06b8573eb8f45ba8a0c3e6d1ef42257352e8cf2cd8fe4cdf7d8f96602e8f46284a105e1a6eaf8b97e88537a0ff4f8a8cbfefbcc5b0964d287caab17
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bmc-daemon-lib (0.1.1)
5
+ chamber (~> 2.9)
6
+ newrelic_rpm
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.3.0)
12
+ chamber (2.9.0)
13
+ hashie (~> 3.3)
14
+ thor (~> 0.19.1)
15
+ diff-lcs (1.2.5)
16
+ hashie (3.4.4)
17
+ newrelic_rpm (3.16.0.318)
18
+ parser (2.3.1.2)
19
+ ast (~> 2.2)
20
+ powerpack (0.1.1)
21
+ rainbow (2.1.0)
22
+ rake (11.2.2)
23
+ rspec (3.5.0)
24
+ rspec-core (~> 3.5.0)
25
+ rspec-expectations (~> 3.5.0)
26
+ rspec-mocks (~> 3.5.0)
27
+ rspec-core (3.5.1)
28
+ rspec-support (~> 3.5.0)
29
+ rspec-expectations (3.5.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.5.0)
32
+ rspec-mocks (3.5.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.5.0)
35
+ rspec-support (3.5.0)
36
+ rubocop (0.41.2)
37
+ parser (>= 2.3.1.1, < 3.0)
38
+ powerpack (~> 0.1)
39
+ rainbow (>= 1.99.1, < 3.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (~> 1.0, >= 1.0.1)
42
+ ruby-progressbar (1.8.1)
43
+ thor (0.19.1)
44
+ unicode-display_width (1.1.0)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bmc-daemon-lib!
51
+ bundler (~> 1.6)
52
+ rake
53
+ rspec
54
+ rubocop
55
+
56
+ BUNDLED WITH
57
+ 1.11.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Bruno Medici
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # bmc-daemon-lib
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ require "bundler/gem_tasks"
3
+ require "rubygems"
4
+
5
+ require "rspec/core/rake_task"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ # Run specs by default
9
+ desc "Run all tests"
10
+
11
+ require "rubocop/rake_task"
12
+ RuboCop::RakeTask.new(:rubocop) do |task|
13
+ task.fail_on_error = false
14
+ end
15
+
16
+ task default: [:spec, :rubocop]
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ # Project version
4
+ spec.version = "0.1.1"
5
+
6
+ # Project description
7
+ spec.name = "bmc-daemon-lib"
8
+ spec.authors = ["Bruno MEDICI"]
9
+ spec.email = "bmc-daemon-lib@bmconseil.com"
10
+ spec.description = "Shared utilities to build a daemon: logger, configuration, helpers"
11
+ spec.summary = spec.description
12
+ spec.homepage = "http://github.com/bmedici/bmc-daemon-lib"
13
+ spec.licenses = ["MIT"]
14
+ spec.date = Time.now.strftime("%Y-%m-%d")
15
+
16
+ # List files and executables
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = ">= 2.2"
21
+
22
+ # Development dependencies
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "rubocop"
27
+
28
+ # Runtime dependencies
29
+ spec.add_runtime_dependency "chamber", "~> 2.9"
30
+ spec.add_runtime_dependency "newrelic_rpm"
31
+ end
@@ -0,0 +1,201 @@
1
+ # FIXME: files named with hyphens will not be found by Chamber for now
2
+ require "chamber"
3
+
4
+ module BmcDaemonLib
5
+ class ConfigMissingParameter < StandardError; end
6
+ class ConfigOtherError < StandardError; end
7
+ class ConfigParseError < StandardError; end
8
+ class ConfigMultipleGemspec < StandardError; end
9
+ class ConfigMissingGemspec < StandardError; end
10
+
11
+ class Conf
12
+ extend Chamber
13
+ PIDFILE_DIR = "/tmp/"
14
+
15
+ class << self
16
+ attr_accessor :app_env
17
+ attr_reader :app_root
18
+ attr_reader :app_libs
19
+ attr_reader :app_name
20
+ attr_reader :app_ver
21
+ attr_reader :app_started
22
+ attr_reader :app_spec
23
+ attr_reader :files
24
+ attr_reader :host
25
+ end
26
+
27
+ def self.init app_root
28
+ # Permanent flags
29
+ @initialized = true
30
+ @app_started = Time.now
31
+
32
+ # Default values
33
+ @files ||= []
34
+ @app_name ||= "app_name"
35
+ @app_env ||= "production"
36
+ @host ||= `hostname`.to_s.chomp.split(".").first
37
+
38
+ # Store and clean app_root
39
+ @app_root = File.expand_path(app_root)
40
+
41
+ # Try to find any gemspec file
42
+ matches = Dir["#{@app_root}/*.gemspec"]
43
+ fail ConfigMissingGemspec, "gemspec file not found: #{gemspec_path}" if matches.size < 1
44
+ fail ConfigMultipleGemspec, "gemspec file not found: #{gemspec_path}" if matches.size > 1
45
+
46
+ # Load Gemspec (just the only match)
47
+ @spec = Gem::Specification::load(matches.first)
48
+ @app_name = @spec.name
49
+ @app_ver = @spec.version
50
+ fail ConfigMissingParameter, "gemspec: missing name" unless @app_name
51
+ fail ConfigMissingParameter, "gemspec: missing version" unless @app_ver
52
+
53
+ # Now we know app_name, initalize app_libs
54
+ @app_libs = File.expand_path("lib/#{@app_name}/", @app_root)
55
+
56
+ # Add other config files
57
+ #add_default_config
58
+ add_config generate(:config_defaults)
59
+ add_config generate(:config_etc)
60
+
61
+ # Return something
62
+ return @app_name
63
+ end
64
+
65
+ def self.prepare args = {}
66
+ ensure_init
67
+
68
+ # Add extra config file and load them all
69
+ add_config args[:config]
70
+ reload!
71
+
72
+ # Set Rack env
73
+ ENV["RACK_ENV"] = @app_env.to_s
74
+
75
+ # Set up encodings
76
+ Encoding.default_internal = "utf-8"
77
+ Encoding.default_external = "utf-8"
78
+
79
+ # Init New Relic
80
+ newrelic_logfile = File.expand_path(Conf[:logs][:newrelic].to_s, Conf[:logs][:path].to_s)
81
+ prepare_newrelic self[:newrelic], newrelic_logfile
82
+
83
+ # Try to access any key to force parsing of the files
84
+ self[:dummy]
85
+
86
+ rescue Psych::SyntaxError => e
87
+ fail ConfigParseError, e.message
88
+ rescue StandardError => e
89
+ fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}"
90
+ end
91
+
92
+ # Reload files
93
+ def self.reload!
94
+ ensure_init
95
+ load_files
96
+ end
97
+
98
+ def self.dump
99
+ ensure_init
100
+ to_hash.to_yaml(indent: 4, useheader: true, useversion: false )
101
+ end
102
+
103
+ # Direct access to any depth
104
+ def self.at *path
105
+ ensure_init
106
+ path.reduce(Conf) { |m, key| m && m[key.to_s] }
107
+ end
108
+
109
+ def self.newrelic_enabled?
110
+ ensure_init
111
+ self[:newrelic] && self[:newrelic][:licence]
112
+ end
113
+
114
+ # Defaults generators
115
+ def self.generate what
116
+ ensure_init
117
+ return case what
118
+
119
+ when :user_agent
120
+ "#{@app_name}/#{@app_ver}" if @app_name && @app_ver
121
+
122
+ when :config_defaults
123
+ "#{@app_root}/defaults.yml" if @app_root
124
+
125
+ when :config_etc
126
+ "/etc/#{@app_name}.yml" if @app_name
127
+
128
+ when :process_name
129
+ parts = [@app_name, @app_env]
130
+ parts << self[:port] if self[:port]
131
+ parts.join('-')
132
+
133
+ when :pidfile
134
+ process_name = self.generate(:process_name)
135
+ File.expand_path "#{process_name}.pid", PIDFILE_DIR
136
+
137
+ when :config_message
138
+ config_defaults = self.generate(:config_defaults)
139
+ config_etc = self.generate(:config_etc)
140
+
141
+ "A default configuration is available (#{config_defaults}) and can be copied to the default location (#{config_etc}): \n sudo cp #{config_defaults} #{config_etc}"
142
+
143
+ end
144
+ end
145
+
146
+
147
+ protected
148
+
149
+ def self.load_files
150
+ load files: @files, namespaces: { environment: @app_env }
151
+ end
152
+
153
+ def self.add_config path
154
+ @files << File.expand_path(path) if path && File.readable?(path)
155
+ end
156
+
157
+ def self.prepare_newrelic section, logfile
158
+ # Disable NewRelic if no config present
159
+ unless self.newrelic_enabled?
160
+ ENV["NEWRELIC_AGENT_ENABLED"] = "false"
161
+ return
162
+ end
163
+
164
+ # Enable GC profiler
165
+ GC::Profiler.enable
166
+
167
+ # Enable module
168
+ ENV["NEWRELIC_AGENT_ENABLED"] = "true"
169
+ ENV["NEW_RELIC_MONITOR_MODE"] = "true"
170
+
171
+ # License
172
+ ENV["NEW_RELIC_LICENSE_KEY"] = section[:licence].to_s
173
+
174
+ # Build NewRelic app_name if not provided as-is
175
+ if section[:app_name]
176
+ ENV["NEW_RELIC_APP_NAME"] = section[:app_name].to_s
177
+ else
178
+ stack = []
179
+ stack << (section[:prefix] || @app_name)
180
+ stack << section[:platform] if section[:platform]
181
+ stack << @app_env
182
+ text = stack.join('-')
183
+ ENV["NEW_RELIC_APP_NAME"] = "#{text}-#{host};#{text}"
184
+ end
185
+
186
+ # Logfile
187
+ ENV["NEW_RELIC_LOG"] = logfile.to_s if logfile
188
+ end
189
+
190
+ private
191
+
192
+ def self.ensure_init
193
+ # Skip is already done
194
+ return if @initialized
195
+
196
+ # Go through init if not already done
197
+ self.init
198
+ end
199
+
200
+ end
201
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "bmc-daemon-lib/logger_formatter"
2
+ require_relative "bmc-daemon-lib/logger_helper"
3
+ require_relative "bmc-daemon-lib/conf"
4
+ require_relative "bmc-daemon-lib/worker_base"
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bmc-daemon-lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Bruno MEDICI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-12 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: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: rubocop
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: chamber
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.9'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: newrelic_rpm
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: 'Shared utilities to build a daemon: logger, configuration, helpers'
98
+ email: bmc-daemon-lib@bmconseil.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - LICENSE
107
+ - README.md
108
+ - Rakefile
109
+ - bmc-daemon-lib.gemspec
110
+ - lib/bmc-daemon-lib.rb
111
+ - lib/bmc-daemon-lib/conf.rb
112
+ homepage: http://github.com/bmedici/bmc-daemon-lib
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '2.2'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.6.6
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: 'Shared utilities to build a daemon: logger, configuration, helpers'
136
+ test_files: []