LanGrove 0.0.1 → 0.0.2

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.
Files changed (84) hide show
  1. data/.watchr +11 -3
  2. data/Rakefile +12 -5
  3. data/functional/.gitignore +1 -0
  4. data/{bin/datagram_example → functional/bin/datagram} +2 -0
  5. data/{config/environments/development.rb → functional/config/.gitignore} +0 -0
  6. data/{config → functional/config}/boot.rb +0 -0
  7. data/functional/config/daemons.yml +12 -0
  8. data/{config → functional/config}/environment.rb +0 -0
  9. data/{config/environments/production.rb → functional/config/environments/development.rb} +0 -0
  10. data/{config/environments/test.rb → functional/config/environments/production.rb} +0 -0
  11. data/{lib/jobs/Rakefile → functional/config/environments/test.rb} +0 -0
  12. data/functional/lib/daemon/datagram.rb +23 -0
  13. data/functional/lib/handler/socket_to_file.rb +36 -0
  14. data/functional/lib/protocol/socket_to_file.rb +55 -0
  15. data/{libexec → functional/libexec}/daemon.rb +14 -3
  16. data/functional/log/.gitignore +3 -0
  17. data/functional/tmp/README +1 -0
  18. data/lib/langrove.rb +1 -0
  19. data/lib/langrove/_base.rb +26 -0
  20. data/lib/langrove/adaptor/base.rb +3 -0
  21. data/lib/langrove/adaptor/datagram.rb +27 -0
  22. data/lib/langrove/adaptor_base.rb +89 -0
  23. data/lib/langrove/client/base.rb +2 -0
  24. data/lib/langrove/client/datagram.rb +28 -0
  25. data/lib/langrove/client_base.rb +114 -0
  26. data/lib/langrove/daemon/base.rb +2 -0
  27. data/lib/{daemon_base.rb → langrove/daemon_base.rb} +30 -23
  28. data/lib/langrove/ext.rb +7 -0
  29. data/lib/langrove/ext/class_loader.rb +146 -0
  30. data/lib/{ext → langrove/ext}/config_item.rb +4 -5
  31. data/lib/{ext → langrove/ext}/config_loader.rb +2 -2
  32. data/lib/langrove/ext/fake_logger.rb +8 -0
  33. data/lib/{ext → langrove/ext}/persistable.rb +2 -2
  34. data/lib/{ext → langrove/ext}/string.rb +0 -0
  35. data/lib/langrove/handler/base.rb +2 -0
  36. data/lib/{handler_base.rb → langrove/handler_base.rb} +25 -9
  37. data/lib/langrove/protocol/base.rb +2 -0
  38. data/lib/langrove/protocol/syslog.rb +32 -0
  39. data/lib/langrove/protocol_base.rb +32 -0
  40. data/lib/langrove/version.rb +3 -0
  41. data/spec/functional/daemon/datagram_spec.rb +115 -0
  42. data/spec/langrove/adaptor/datagram_spec.rb +6 -0
  43. data/spec/langrove/adaptor_base_spec.rb +48 -0
  44. data/spec/langrove/client/datagram_spec.rb +1 -0
  45. data/spec/langrove/client_base_spec.rb +5 -0
  46. data/spec/langrove/daemon_base_spec.rb +101 -0
  47. data/spec/langrove/ext/class_loader_spec.rb +83 -0
  48. data/spec/langrove/ext/config_item_spec.rb +81 -0
  49. data/spec/langrove/ext/config_loader_spec.rb +5 -0
  50. data/{tmp/TMP → spec/langrove/ext/fake_logger_spec.rb} +0 -0
  51. data/spec/{ext → langrove/ext}/persistable_spec.rb +8 -9
  52. data/spec/{ext → langrove/ext}/string_spec.rb +1 -1
  53. data/spec/langrove/handler_base_spec.rb +57 -0
  54. data/spec/langrove/protocol/syslog_spec.rb +45 -0
  55. data/spec/langrove/protocol_base_spec.rb +6 -0
  56. data/spec/todo_spec.rb +1 -2
  57. data/tmp/README +2 -0
  58. metadata +150 -46
  59. data/config/daemons.yml.tmpl +0 -78
  60. data/lib/adaptor/base.rb +0 -1
  61. data/lib/adaptor/datagram.rb +0 -20
  62. data/lib/adaptor/socket_handler.rb +0 -27
  63. data/lib/adaptor_base.rb +0 -39
  64. data/lib/client/base.rb +0 -1
  65. data/lib/client/puppet_state.rb +0 -74
  66. data/lib/client/radio_state.rb +0 -81
  67. data/lib/client_base.rb +0 -24
  68. data/lib/daemon/base.rb +0 -1
  69. data/lib/daemon/datagram_example.rb +0 -12
  70. data/lib/handler/base.rb +0 -1
  71. data/lib/handler/socket_to_file.rb +0 -30
  72. data/lib/jobs/jobs.rb +0 -1
  73. data/lib/jobs/updated_puppet_state.rb +0 -17
  74. data/lib/protocol_base.rb +0 -5
  75. data/spec/adaptor/datagram_spec.rb +0 -6
  76. data/spec/adaptor/socket_handler_spec.rb +0 -5
  77. data/spec/adaptor_base_spec.rb +0 -45
  78. data/spec/client_base_spec.rb +0 -5
  79. data/spec/daemon_base_spec.rb +0 -97
  80. data/spec/ext/config_item_spec.rb +0 -77
  81. data/spec/ext/config_loader_spec.rb +0 -5
  82. data/spec/functional/datagram_spec.rb +0 -122
  83. data/spec/handler_base_spec.rb +0 -71
  84. data/spec/protocol_base_spec.rb +0 -6
@@ -0,0 +1,2 @@
1
+ module LanGrove::Daemon; end
2
+ require 'langrove/daemon_base'
@@ -1,20 +1,17 @@
1
1
  require 'eventmachine'
2
2
 
3
- require 'ext/string'
3
+ require 'langrove'
4
4
 
5
- class DaemonConfigException < Exception; end
6
-
7
- module Daemon class Base
5
+ module LanGrove::Daemon class Base
8
6
 
9
7
  def listen
10
8
 
11
9
  #
12
- # Handler, the CollectionOfClients,
13
- # is passed through the adaptor to
14
- # be assigned to the attaching sockets.
10
+ # Handler - The CollectionOfClients, is passed
11
+ # through the adaptor to be assigned
12
+ # to the attaching Clients.
15
13
  #
16
-
17
- @adaptor.listen( @handler )
14
+ @adaptor.listen( @handler, @handler.protocol )
18
15
 
19
16
  end
20
17
 
@@ -101,29 +98,29 @@ module Daemon class Base
101
98
  #
102
99
  # you shall not pass
103
100
  #
104
- raise DaemonConfigException.new( "Requires a logger" )
101
+ raise LanGrove::DaemonConfigException.new( "Requires a logger" )
105
102
 
106
103
  end
107
104
 
108
105
  begin
109
106
 
110
- @my_config = config_hash['daemons'][daemon_name]
107
+ @my_config = @config[ :daemons ][ @daemon_name ]
111
108
 
112
- @server = @my_config['listen'] if @my_config.has_key? 'listen'
109
+ @server = @my_config[ :server ] if @my_config.has_key? :server
113
110
 
114
- adaptor = @my_config['adaptor']['connection']
111
+ adaptor = @my_config[ :adaptor ][ :connection ]
115
112
 
116
- handler = @my_config['handler']['collection']
113
+ handler = @my_config[ :handler ][ :collection ]
117
114
 
118
115
  rescue
119
116
 
120
- error = "Missing config item for daemon: #{daemon_name}"
117
+ error = "Missing config item for daemon: #{@daemon_name}"
121
118
 
122
119
  @logger.error "EXIT: #{error}"
123
120
 
124
- raise DaemonConfigException.new(
121
+ raise LanGrove::DaemonConfigException.new(
125
122
 
126
- "Missing config item for daemon: #{daemon_name}"
123
+ "Missing config item for daemon: #{@daemon_name}"
127
124
 
128
125
  )
129
126
 
@@ -140,9 +137,14 @@ module Daemon class Base
140
137
  # collection: CollectionOfClients
141
138
  #
142
139
 
143
- @logger.info "Initialize connection: Adaptor::#{adaptor}.new( @my_config['adaptor'], logger )"
144
- eval "require 'adaptor/#{adaptor.underscore}'"
145
- @adaptor = Adaptor.const_get(adaptor).new( @my_config['adaptor'], logger )
140
+ @logger.info "Initialize instance: Adaptor::#{adaptor}.new( @my_config[ :adaptor ], logger )"
141
+
142
+ @adaptor = LanGrove::ClassLoader.create( {
143
+
144
+ :module => 'Adaptor',
145
+ :class => adaptor
146
+
147
+ } ).new( @my_config[ :adaptor ], logger )
146
148
 
147
149
 
148
150
  #
@@ -158,9 +160,14 @@ module Daemon class Base
158
160
  # CollectionOfClients ---> is the handler passed into listen()
159
161
  #
160
162
 
161
- @logger.info "Initialize collection: #{handler}.new( config_hash, daemon_name, logger )"
162
- eval "require 'handler/#{handler.underscore}'"
163
- @handler = Handler.const_get(handler).new( config_hash, daemon_name, logger )
163
+ @logger.info "Initialize instance: Handler::#{handler}.new( config_hash, '#{daemon_name}', logger )"
164
+
165
+ @handler = LanGrove::ClassLoader.create( {
166
+
167
+ :module => 'Handler',
168
+ :class => handler
169
+
170
+ } ).new( @config, @daemon_name, @logger )
164
171
 
165
172
 
166
173
  end
@@ -0,0 +1,7 @@
1
+ module Langrove; end
2
+ require 'langrove/ext/class_loader'
3
+ require 'langrove/ext/config_item'
4
+ require 'langrove/ext/config_loader'
5
+ require 'langrove/ext/fake_logger'
6
+ require 'langrove/ext/persistable'
7
+ require 'langrove/ext/string'
@@ -0,0 +1,146 @@
1
+ require 'langrove/ext/string'
2
+ module LanGrove
3
+
4
+ class ClassLoaderException < Exception; end
5
+
6
+ class ClassLoader
7
+
8
+ def self.create( class_config, logger = nil )
9
+
10
+ logger.info( "#{self} is loading Class with #{class_config.inspect}" ) unless logger.nil?
11
+
12
+ #
13
+ # When <config> = {
14
+ #
15
+ # :module => 'ModuleName'
16
+ # :class => 'ClassName'
17
+ #
18
+ # }
19
+ #
20
+ # Then this will return the constantized
21
+ # definition instance of ClassName as
22
+ # loaded from the F1RST found .rb file
23
+ # according to:
24
+ #
25
+ # - lib/module_name/class_name.rb
26
+ # - langrove/module_name/class_name.rb
27
+ #
28
+ # Which then facilitates the following
29
+ # construct:
30
+ #
31
+ # planet = ClassLoader.create(
32
+ #
33
+ # :module => 'Planet',
34
+ # :class => 'Mercury'
35
+ #
36
+ # ).new( *initializer_parameters )
37
+ #
38
+
39
+ raise ClassLoaderException.new(
40
+
41
+ "class_config requires :module"
42
+
43
+ ) unless class_config.has_key? :module
44
+
45
+ raise ClassLoaderException.new(
46
+
47
+ "class_config requires :class"
48
+
49
+ ) unless class_config.has_key? :class
50
+
51
+
52
+ module_name = class_config[:module]
53
+ class_name = class_config[:class]
54
+
55
+
56
+ #
57
+ # SIGNIFICANT DECISION
58
+ #
59
+ # - Late binding to the extent of also calling
60
+ # to require the actual class.rb file could
61
+ # potentially be avoided by
62
+ #
63
+ # << using this layer in the abstraction >>
64
+ #
65
+ # to do the necessary requiring for the specific
66
+ # daemon being spawned.
67
+ #
68
+ # - Obviously there are downsides to eval...
69
+ #
70
+ # - But there are upsides to having this layer
71
+ # totally aliteral - it leaves the window open
72
+ # to the later posibility of collecting the class
73
+ # definition itself from across the network.
74
+ #
75
+ #
76
+ # Which was the cental purpose behind daemons
77
+ # by configuration in the first place.
78
+ #
79
+ # Taking latebinding to a whole new level...
80
+ #
81
+
82
+
83
+ #
84
+ # First try local implementation root
85
+ #
86
+
87
+ exception = nil
88
+
89
+ location = nil
90
+
91
+ begin
92
+
93
+ location = "#{module_name.underscore}/#{class_name.underscore}"
94
+
95
+ eval "require '#{location}'"
96
+
97
+ return Object.const_get( module_name ).const_get( class_name )
98
+
99
+ rescue LoadError => e
100
+
101
+ exception = e
102
+
103
+ logger.error "Missing class definition in lib/#{location}.rb" unless logger.nil?
104
+
105
+ #rescue Exception => e
106
+ #
107
+ # #
108
+ # # Incase of more... (discovery phase)
109
+ # #
110
+ #
111
+
112
+
113
+ end
114
+
115
+ #
116
+ # Fall back to langrove gem lib
117
+ #
118
+
119
+ begin
120
+
121
+ location = "langrove/#{module_name.underscore}/#{class_name.underscore}"
122
+
123
+ eval "require '#{location}'"
124
+
125
+ return LanGrove.const_get( module_name ).const_get( class_name )
126
+
127
+ rescue Exception => e
128
+
129
+ #
130
+ # Raise from the original exception to
131
+ # inform the local implementation
132
+ # it is missing a module/class .rb
133
+ # "no such file 'module/class"
134
+ #
135
+ # And not confuse the issue by raising
136
+ # "no such file 'langrove/module/class"
137
+ #
138
+ raise ClassLoaderException.new "#{exception.message}.rb"
139
+
140
+ end
141
+
142
+ end
143
+
144
+ end
145
+
146
+ end
@@ -1,8 +1,7 @@
1
- class ConfigException < Exception
2
-
3
- end
4
1
 
5
- class ConfigItem
2
+ module LanGrove class ConfigException < Exception; end; end
3
+
4
+ module LanGrove class ConfigItem
6
5
 
7
6
  def self.get(config_hash, key_array, mandatory = true)
8
7
 
@@ -32,4 +31,4 @@ class ConfigItem
32
31
 
33
32
  end
34
33
 
35
- end
34
+ end; end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'yaml'
4
4
 
5
- class ConfigLoader
5
+ module LanGrove class ConfigLoader
6
6
 
7
7
  def self.yaml( file )
8
8
 
@@ -13,4 +13,4 @@ class ConfigLoader
13
13
 
14
14
  end
15
15
 
16
- end
16
+ end; end
@@ -0,0 +1,8 @@
1
+ module LanGrove class FakeLogger
2
+ def initialize( parameter = :noisey )
3
+ @parameter = parameter
4
+ end
5
+ def method_missing( symbol, *args, &block )
6
+ puts "#{symbol}: #{args}" unless @parameter == :silent
7
+ end
8
+ end; end
@@ -3,7 +3,7 @@ require 'yaml'
3
3
 
4
4
  # require 'updated_puppet_state'
5
5
 
6
- class Persistable
6
+ module LanGrove class Persistable
7
7
 
8
8
  def initialize( notifies = [] )
9
9
 
@@ -100,4 +100,4 @@ class Persistable
100
100
 
101
101
  end
102
102
  end
103
- end
103
+ end; end
File without changes
@@ -0,0 +1,2 @@
1
+ module LanGrove::Handler; end
2
+ require 'langrove/handler_base'
@@ -1,7 +1,6 @@
1
- require 'protocol/base'
2
- require 'ext/string'
1
+ require 'langrove'
3
2
 
4
- module Handler class Base
3
+ module LanGrove::Handler class Base
5
4
 
6
5
  #
7
6
  # One handler exists in the daemon with
@@ -11,8 +10,19 @@ module Handler class Base
11
10
  #
12
11
  # In this:
13
12
  #
13
+ # but, NOT YET
14
+ #
14
15
  attr_writer :clients
15
16
 
17
+ #
18
+ # The protocol defininition per the config is
19
+ # loaded by this Handler, but not instanciated.
20
+ #
21
+ # This definition is then exposed,
22
+ #
23
+ # By this:
24
+ #
25
+ attr_reader :protocol
16
26
 
17
27
  def daemon_start
18
28
 
@@ -59,15 +69,16 @@ module Handler class Base
59
69
  def initialize( config_hash, daemon_name, logger )
60
70
 
61
71
  @config = config_hash
62
- @my_config = @config['daemons'][daemon_name]['handler']
63
72
  @daemon_name = daemon_name
64
73
  @logger = logger
65
74
 
75
+ @my_config = @config[ :daemons ][ @daemon_name ][ :handler ]
76
+
66
77
  protocol = 'Base'
67
78
 
68
- if @my_config.has_key? 'protocol' then
79
+ if @my_config.has_key? :protocol then
69
80
 
70
- protocol = @my_config['protocol']
81
+ protocol = @my_config[ :protocol ]
71
82
 
72
83
  end
73
84
 
@@ -116,10 +127,15 @@ module Handler class Base
116
127
  # adapter in use.
117
128
  #
118
129
 
119
- @logger.info "Initialize protocol: Protocol::#{protocol}.new( logger )"
120
- eval "require 'protocol/#{protocol.underscore}'"
121
- @protocol = Protocol.const_get(protocol).new( logger )
130
+ @logger.info "Load definition: Protocol::#{protocol}"
122
131
 
132
+ @protocol = LanGrove::ClassLoader.create( {
133
+
134
+ :module => 'Protocol',
135
+ :class => protocol
136
+
137
+ } )
138
+
123
139
  end
124
140
 
125
141
  end; end
@@ -0,0 +1,2 @@
1
+ module LanGrove::Protocol; end
2
+ require 'langrove/protocol_base'
@@ -0,0 +1,32 @@
1
+ require 'date'
2
+ require 'langrove'
3
+
4
+ module LanGrove::Protocol class Syslog
5
+
6
+ def decode( message )
7
+
8
+ #
9
+ # eg:
10
+ #
11
+ # <NN>Apr 4 11:00:06 fw.school1950.gp-online.net puppet[29206]: Autoloaded file blahblahblah
12
+ #
13
+
14
+ pass = message.split('>')[1].split()
15
+ time = DateTime.parse(pass[0..2].join(' '))
16
+ hostname = pass[3]
17
+ message = pass[4..-1].join(' ').split(': ',2)
18
+
19
+ hostname.gsub!( /vlan[\d]*\./, '')
20
+
21
+ @decoded = {
22
+ :timestamp => time, # DateTime object
23
+ :hostname => hostname, # without prepended vlanN
24
+ :tag => message[0], # includes[PID]
25
+ :event => message[1] # everything after the colon after the tag
26
+ }
27
+
28
+ return @decoded
29
+
30
+ end
31
+
32
+ end; end
@@ -0,0 +1,32 @@
1
+ require 'langrove'
2
+
3
+ module LanGrove::Protocol
4
+
5
+ class Base
6
+
7
+ def initialize( place_mark_protocol_config, logger )
8
+
9
+ @logger = logger
10
+
11
+ end
12
+
13
+ def decode( data )
14
+
15
+ #
16
+ # OVERRIDE
17
+ #
18
+ # To decode the data ahead of passing it
19
+ # into the <Handler>.receive() function.
20
+ #
21
+
22
+ {
23
+
24
+ :data => data
25
+
26
+ }
27
+
28
+ end
29
+
30
+ end
31
+
32
+ end