Module: OmfCommon
- Defined in:
- lib/omf_common/message.rb,
lib/omf_common.rb,
lib/omf_common/key.rb,
lib/omf_common/auth.rb,
lib/omf_common/comm.rb,
lib/omf_common/measure.rb,
lib/omf_common/version.rb,
lib/omf_common/eventloop.rb,
lib/omf_common/comm/topic.rb,
lib/omf_common/eventloop/em.rb,
lib/omf_common/comm/xmpp/topic.rb,
lib/omf_common/default_logging.rb,
lib/omf_common/comm/amqp/amqp_mp.rb,
lib/omf_common/comm/xmpp/xmpp_mp.rb,
lib/omf_common/eventloop/local_evl.rb,
lib/omf_common/message/xml/message.rb,
lib/omf_common/comm/amqp/amqp_topic.rb,
lib/omf_common/comm/local/local_topic.rb,
lib/omf_common/comm/xmpp/communicator.rb,
lib/omf_common/message/json/json_message.rb,
lib/omf_common/message/xml/relaxng_schema.rb,
lib/omf_common/comm/amqp/amqp_communicator.rb,
lib/omf_common/comm/amqp/amqp_file_transfer.rb,
lib/omf_common/comm/local/local_communicator.rb
Overview
require ‘omf_common/comm/monkey_patches’
Defined Under Namespace
Modules: Auth, Command, DSL, DefaultLogging Classes: Comm, Eventloop, Key, MPMessage, Measure, Message, RelaxNGSchema
Constant Summary collapse
- DEFAULTS =
{ development: { eventloop: { type: 'em' }, logging: { level: { default: 'debug' }, appenders: { stdout: { date_pattern: '%H:%M:%S', pattern: '%d %5l %c{2}: %m\n', color_scheme: 'none' } } } }, production: { eventloop: { type: :em }, logging: { level: { default: 'info' }, appenders: { file: { log_dir: '/var/log', #log_file: 'foo.log', date_pattern: '%F %T %z', pattern: '[%d] %-5l %c: %m\n' } } } }, daemon: { daemonize: { dir_mode: :script, dir: '/tmp', backtrace: true, log_dir: '/var/log', log_output: true }, eventloop: { type: :em }, logging: { level: { default: 'info' }, appenders: { file: { log_dir: '/var/log', #log_file: 'foo.log', date_pattern: '%F %T %z', pattern: '[%d] %-5l %c: %m\n' } } } }, local: { communication: { type: :local, }, eventloop: { type: :local}, logging: { level: { default: 'debug' }, appenders: { stdout: { date_pattern: '%H:%M:%S', pattern: '%d %5l %c{2}: %m\n', color_scheme: 'none' } } } }, test_daemon: { daemonize: { dir_mode: :script, dir: '/tmp', backtrace: true, log_dir: '/tmp', log_output: true }, eventloop: { type: :em }, logging: { level: { default: 'debug' }, appenders: { file: { log_dir: '/tmp', #log_file: 'foo.log', date_pattern: '%F %T %z', pattern: '[%d] %-5l %c: %m\n' } } } } }
- PROTOCOL_VERSION =
"6.0"
- VERSION =
version_of('omf_common')
Class Method Summary collapse
-
._init_logging(opts = {}) ⇒ Object
DO NOT CALL DIRECTLY.
- ._rec_merge(this_hash, other_hash) ⇒ Object
-
._rec_sym_keys(hash) ⇒ Object
Recursively Symbolize keys of hash.
-
.comm ⇒ Object
Return the communication driver instance.
-
.eventloop ⇒ Object
(also: el)
Return the communication driver instance.
-
.init(op_mode, opts = {}, &block) ⇒ Object
Initialize the OMF runtime.
- .load_credentials(opts) ⇒ Object
-
.load_logging_config(file_path) ⇒ Object
Load a config file compatible with Logging gem.
-
.load_yaml(file_name, opts = {}) ⇒ Object
Load a YAML file and return it as hash.
- .version_of(name) ⇒ Object
Class Method Details
._init_logging(opts = {}) ⇒ Object
DO NOT CALL DIRECTLY
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/omf_common.rb', line 250 def self._init_logging(opts = {}) logger = Logging.logger.root if appenders = opts[:appenders] logger.clear_appenders appenders.each do |type, topts| case type.to_sym when :stdout $stdout.sync = true logger.add_appenders( Logging.appenders.stdout('custom', :layout => Logging.layouts.pattern(topts) )) when :file dir_name = topts.delete(:log_dir) || DEF_LOG_DIR file_name = topts.delete(:log_file) || "#{File.basename($0, File.extname($0))}.log" path = File.join(dir_name, file_name) logger.add_appenders( Logging.appenders.file(path, :layout => Logging.layouts.pattern(topts) )) else raise "Unknown logging appender type '#{type}'" end end end if level = opts[:level] if level.is_a? Hash # package level settings level.each do |name, lvl| if name.to_s == 'default' logger.level = lvl.to_sym else Logging.logger[name.to_s].level = lvl.to_sym end end else logger.level = level.to_sym end end end |
._rec_merge(this_hash, other_hash) ⇒ Object
292 293 294 295 296 297 298 299 300 |
# File 'lib/omf_common.rb', line 292 def self._rec_merge(this_hash, other_hash) # if the dominant side is not a hash we stop recursing and pick the primitive value return other_hash unless other_hash.is_a? Hash r = {} this_hash.merge(other_hash) do |key, oldval, newval| r[key] = oldval.is_a?(Hash) ? _rec_merge(oldval, newval) : newval end end |
._rec_sym_keys(hash) ⇒ Object
Recursively Symbolize keys of hash
304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/omf_common.rb', line 304 def self._rec_sym_keys(hash) h = {} hash.each do |k, v| if v.is_a? Hash v = _rec_sym_keys(v) elsif v.is_a? Array v = v.map {|e| e.is_a?(Hash) ? _rec_sym_keys(e) : e } end h[k.to_sym] = v end h end |
.comm ⇒ Object
Return the communication driver instance
186 187 188 |
# File 'lib/omf_common.rb', line 186 def self.comm() Comm.instance end |
.eventloop ⇒ Object Also known as: el
Return the communication driver instance
192 193 194 |
# File 'lib/omf_common.rb', line 192 def self.eventloop() Eventloop.instance end |
.init(op_mode, opts = {}, &block) ⇒ Object
Initialize the OMF runtime. Options are:
:communication
:type
... specific opts
:eventloop
:type {:em|:local...}
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/omf_common.rb', line 146 def self.init(op_mode, opts = {}, &block) opts = _rec_sym_keys(opts) if op_mode && defs = DEFAULTS[op_mode.to_sym] opts = _rec_merge(defs, opts) end if dopts = opts.delete(:daemonize) dopts[:app_name] ||= "#{File.basename($0, File.extname($0))}_daemon" require 'daemons' Daemons.run_proc(dopts[:app_name], dopts) do init(nil, opts, &block) end return end if lopts = opts[:logging] _init_logging(lopts) unless lopts.empty? end unless copts = opts[:communication] raise "Missing :communication description" end if aopts = opts[:auth] require 'omf_common/auth/credential_store' OmfCommon::Auth::CredentialStore.init(aopts) end # Initialise event loop eopts = opts[:eventloop] Eventloop.init(eopts) # start eventloop immediately if we received a run block eventloop.run do Comm.init(copts) block.call(eventloop) if block end end |
.load_credentials(opts) ⇒ Object
334 335 336 337 338 339 340 |
# File 'lib/omf_common.rb', line 334 def self.load_credentials(opts) unless opts.nil? OmfCommon::Auth::CertificateStore.instance.register_default_certs(File.(opts[:root_cert_dir])) cert_and_priv_key = File.read(File.(opts[:entity_cert])) << "\n" << File.read(File.(opts[:entity_key])) OmfCommon::Auth::Certificate.create_from_pem(cert_and_priv_key) end end |
.load_logging_config(file_path) ⇒ Object
Load a config file compatible with Logging gem
320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/omf_common.rb', line 320 def self.load_logging_config(file_path) unless file_path.nil? l_cfg_mime_type = File.extname(file_path) case l_cfg_mime_type when /rb/ load file_path when /yml|yaml/ Logging::Config::YamlConfigurator.load(file_path) else warn "Invalid config file format for logging, please use Ruby or Yaml." end end end |
.load_yaml(file_name, opts = {}) ⇒ Object
Load a YAML file and return it as hash.
options:
:symbolize_keys FLAG: Symbolize keys if set
:path:
:same - Look in the same directory as '$0'
:remove_root ROOT_NAME: Remove the root node. Throw exception if not ROOT_NAME
:wait_for_readable SECS: Wait until the yaml file becomes readable. Check every SECS
:erb_process flag: Run the content of the loaded file through ERB first before YAML parsing
:erb_safe_level level: If safe_level is set to a non-nil value, ERB code will be run in a
separate thread with $SAFE set to the provided level.
:erb_binding binding: Optional binding given to ERB#result
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/omf_common.rb', line 213 def self.load_yaml(file_name, opts = {}) if path_opt = opts[:path] case path_opt when :same file_name = File.join(File.dirname($0), file_name) else raise "Unknown value '#{path_opt}' for 'path' option" end end if readable_check = opts[:wait_for_readable] while not File.readable?(file_name) puts "WAIT #{file_name}" sleep readable_check # wait until file shows up end end str = File.read(file_name) if opts[:erb_process] require 'erb' str = ERB.new(str, opts[:erb_safe_level]).result(opts[:erb_binding] || binding) end yh = YAML.load(str) if opts[:symbolize_keys] yh = _rec_sym_keys(yh) end if root = opts[:remove_root] if yh.length != 1 && yh.key?(root) raise "Expected root '#{root}', but found '#{yh.keys.inspect}" end yh = yh.delete(root) end yh end |
.version_of(name) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/omf_common/version.rb', line 9 def self.version_of(name) git_tag = `git describe --tags 2> /dev/null`.chomp git_root = `git rev-parse --show-toplevel 2> /dev/null`.chomp gem_v = Gem.loaded_specs[name].version.to_s rescue '0.0.0' # Not in a development environment or git not present if git_root != File.absolute_path("#{File.dirname(__FILE__)}/../../../") || git_tag.empty? gem_v else git_tag.gsub(/-/, '.').gsub(/^v/, '') end end |