LanGrove 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.watchr +11 -3
- data/Rakefile +12 -5
- data/functional/.gitignore +1 -0
- data/{bin/datagram_example → functional/bin/datagram} +2 -0
- data/{config/environments/development.rb → functional/config/.gitignore} +0 -0
- data/{config → functional/config}/boot.rb +0 -0
- data/functional/config/daemons.yml +12 -0
- data/{config → functional/config}/environment.rb +0 -0
- data/{config/environments/production.rb → functional/config/environments/development.rb} +0 -0
- data/{config/environments/test.rb → functional/config/environments/production.rb} +0 -0
- data/{lib/jobs/Rakefile → functional/config/environments/test.rb} +0 -0
- data/functional/lib/daemon/datagram.rb +23 -0
- data/functional/lib/handler/socket_to_file.rb +36 -0
- data/functional/lib/protocol/socket_to_file.rb +55 -0
- data/{libexec → functional/libexec}/daemon.rb +14 -3
- data/functional/log/.gitignore +3 -0
- data/functional/tmp/README +1 -0
- data/lib/langrove.rb +1 -0
- data/lib/langrove/_base.rb +26 -0
- data/lib/langrove/adaptor/base.rb +3 -0
- data/lib/langrove/adaptor/datagram.rb +27 -0
- data/lib/langrove/adaptor_base.rb +89 -0
- data/lib/langrove/client/base.rb +2 -0
- data/lib/langrove/client/datagram.rb +28 -0
- data/lib/langrove/client_base.rb +114 -0
- data/lib/langrove/daemon/base.rb +2 -0
- data/lib/{daemon_base.rb → langrove/daemon_base.rb} +30 -23
- data/lib/langrove/ext.rb +7 -0
- data/lib/langrove/ext/class_loader.rb +146 -0
- data/lib/{ext → langrove/ext}/config_item.rb +4 -5
- data/lib/{ext → langrove/ext}/config_loader.rb +2 -2
- data/lib/langrove/ext/fake_logger.rb +8 -0
- data/lib/{ext → langrove/ext}/persistable.rb +2 -2
- data/lib/{ext → langrove/ext}/string.rb +0 -0
- data/lib/langrove/handler/base.rb +2 -0
- data/lib/{handler_base.rb → langrove/handler_base.rb} +25 -9
- data/lib/langrove/protocol/base.rb +2 -0
- data/lib/langrove/protocol/syslog.rb +32 -0
- data/lib/langrove/protocol_base.rb +32 -0
- data/lib/langrove/version.rb +3 -0
- data/spec/functional/daemon/datagram_spec.rb +115 -0
- data/spec/langrove/adaptor/datagram_spec.rb +6 -0
- data/spec/langrove/adaptor_base_spec.rb +48 -0
- data/spec/langrove/client/datagram_spec.rb +1 -0
- data/spec/langrove/client_base_spec.rb +5 -0
- data/spec/langrove/daemon_base_spec.rb +101 -0
- data/spec/langrove/ext/class_loader_spec.rb +83 -0
- data/spec/langrove/ext/config_item_spec.rb +81 -0
- data/spec/langrove/ext/config_loader_spec.rb +5 -0
- data/{tmp/TMP → spec/langrove/ext/fake_logger_spec.rb} +0 -0
- data/spec/{ext → langrove/ext}/persistable_spec.rb +8 -9
- data/spec/{ext → langrove/ext}/string_spec.rb +1 -1
- data/spec/langrove/handler_base_spec.rb +57 -0
- data/spec/langrove/protocol/syslog_spec.rb +45 -0
- data/spec/langrove/protocol_base_spec.rb +6 -0
- data/spec/todo_spec.rb +1 -2
- data/tmp/README +2 -0
- metadata +150 -46
- data/config/daemons.yml.tmpl +0 -78
- data/lib/adaptor/base.rb +0 -1
- data/lib/adaptor/datagram.rb +0 -20
- data/lib/adaptor/socket_handler.rb +0 -27
- data/lib/adaptor_base.rb +0 -39
- data/lib/client/base.rb +0 -1
- data/lib/client/puppet_state.rb +0 -74
- data/lib/client/radio_state.rb +0 -81
- data/lib/client_base.rb +0 -24
- data/lib/daemon/base.rb +0 -1
- data/lib/daemon/datagram_example.rb +0 -12
- data/lib/handler/base.rb +0 -1
- data/lib/handler/socket_to_file.rb +0 -30
- data/lib/jobs/jobs.rb +0 -1
- data/lib/jobs/updated_puppet_state.rb +0 -17
- data/lib/protocol_base.rb +0 -5
- data/spec/adaptor/datagram_spec.rb +0 -6
- data/spec/adaptor/socket_handler_spec.rb +0 -5
- data/spec/adaptor_base_spec.rb +0 -45
- data/spec/client_base_spec.rb +0 -5
- data/spec/daemon_base_spec.rb +0 -97
- data/spec/ext/config_item_spec.rb +0 -77
- data/spec/ext/config_loader_spec.rb +0 -5
- data/spec/functional/datagram_spec.rb +0 -122
- data/spec/handler_base_spec.rb +0 -71
- data/spec/protocol_base_spec.rb +0 -6
@@ -1,20 +1,17 @@
|
|
1
1
|
require 'eventmachine'
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'langrove'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
module Daemon class Base
|
5
|
+
module LanGrove::Daemon class Base
|
8
6
|
|
9
7
|
def listen
|
10
8
|
|
11
9
|
#
|
12
|
-
# Handler
|
13
|
-
#
|
14
|
-
#
|
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 =
|
107
|
+
@my_config = @config[ :daemons ][ @daemon_name ]
|
111
108
|
|
112
|
-
@server = @my_config[
|
109
|
+
@server = @my_config[ :server ] if @my_config.has_key? :server
|
113
110
|
|
114
|
-
adaptor = @my_config[
|
111
|
+
adaptor = @my_config[ :adaptor ][ :connection ]
|
115
112
|
|
116
|
-
handler = @my_config[
|
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
|
144
|
-
|
145
|
-
@adaptor =
|
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
|
162
|
-
|
163
|
-
@handler =
|
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
|
data/lib/langrove/ext.rb
ADDED
@@ -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
|
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
|
File without changes
|
@@ -1,7 +1,6 @@
|
|
1
|
-
require '
|
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?
|
79
|
+
if @my_config.has_key? :protocol then
|
69
80
|
|
70
|
-
protocol = @my_config[
|
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 "
|
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,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
|