huck 0.3.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca3bf22a8ff5affb95d7bb88e7d40aa58e8077ab
4
- data.tar.gz: ed30d1fedd87e7cc62b4942a7247323c57a77265
3
+ metadata.gz: 0b92d1a0367c6d0a896431dc7ccee1301c72367e
4
+ data.tar.gz: a30e14efb2a153c6ac90fdc9129dea7741bb2830
5
5
  SHA512:
6
- metadata.gz: fc300e2459538853941313c9352fed1ba7b661530dce80da41c9eb18a36991b1581918e4f206934603f979b5819d170a70dce7095fa8b8bc4bb8f5ab565091b7
7
- data.tar.gz: 80592af74c45e1eb246f20b1b99acfdedf363e8ef9adef532c3b918628523b2b4512805f9f0ca22c27950a2b50f9ec199e2ac06ac00f3867051b7d8233c80901
6
+ metadata.gz: 5a9a9adfd2acb585c57e370c91369aca27b594f65ce3335e5cdd43bada495d01d5dd042577d6ad932ccdc2f5064cea52f308455f9bf0c5b376a2853f7fe0ad45
7
+ data.tar.gz: dce955a2fcb58a4ddc38eb9150c5833ecde9f81ea4a228a05d64c7c1654576d2a35da42c06b0a484c07530a8584ed56f14e08da3239fcd2278eeb8b1b5478aec
@@ -28,10 +28,10 @@ module Huck
28
28
  g = Generators::FacterGenerator.new
29
29
  when 'ohai'
30
30
  g = Generators::OhaiGenerator.new
31
- when 'yaml'
32
- g = Generators::YamlGenerator.new
33
- when 'json'
34
- g = Generators::JsonGenerator.new
31
+ when 'file'
32
+ g = Generators::FileGenerator.new
33
+ when 'exec'
34
+ g = Generators::ExecGenerator.new
35
35
  else
36
36
  raise RuntimeError, "bad generator: #{name}"
37
37
  end
@@ -16,8 +16,9 @@ module Huck
16
16
  # A hash of host information
17
17
  #
18
18
  def generate
19
- {'hostname' => Socket.gethostname,
20
- 'platform' => RUBY_PLATFORM}
19
+ data = {'hostname' => Socket.gethostname,
20
+ 'platform' => RUBY_PLATFORM}
21
+ Huck::serialize data, :format => @config['format']
21
22
  end
22
23
 
23
24
  end
@@ -0,0 +1,32 @@
1
+ module Huck
2
+
3
+ module Generators
4
+
5
+ # A generator to execute arbitrary scripts and use their output
6
+ class ExecGenerator < Generator
7
+
8
+ # Includes all required modules
9
+ def initialize
10
+ Huck::must_load 'open3'
11
+ end
12
+
13
+ # Ensures that configuration is set properly before executing
14
+ def verify_config
15
+ if !@config.has_key? 'command'
16
+ raise RuntimeError, 'missing "exec" generator config: "command"'
17
+ end
18
+ end
19
+
20
+ # Generate data by running a command and collecting output
21
+ def generate
22
+ verify_config
23
+
24
+ Open3.popen2e @config['command'] do |_, output, thread|
25
+ thread.value # wait for process to complete
26
+ return output.read
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -16,7 +16,8 @@ module Huck
16
16
  # A hash of facts as returned by facter
17
17
  #
18
18
  def generate
19
- Facter.to_hash
19
+ data = Facter.to_hash
20
+ Huck::serialize data, :format => @config['format']
20
21
  end
21
22
 
22
23
  end
@@ -0,0 +1,27 @@
1
+ module Huck
2
+
3
+ module Generators
4
+
5
+ # File generator reads any file and submits the raw content
6
+ class FileGenerator < Generator
7
+
8
+ # Ensure that all JSON config items are properly set
9
+ def verify_config
10
+ if !@config.has_key? 'path'
11
+ raise RuntimeError, 'missing "file" generator config: "path"'
12
+ end
13
+ end
14
+
15
+ # Reads in a configured file and returns the data
16
+ #
17
+ # == Returns:
18
+ # The text contained within the specified file
19
+ #
20
+ def generate
21
+ verify_config
22
+ IO.read(@config['path'])
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -22,7 +22,9 @@ module Huck
22
22
  # Need to load the JSON output, since ohai emits a 'mash' object map
23
23
  # instead of normal hashes, making serialization ugly in some cases
24
24
  # like YAML where canonical objects (eg. !ruby/mash) is emitted.
25
- JSON.load ohai.json_pretty_print
25
+ data = JSON.load ohai.json_pretty_print
26
+
27
+ Huck::serialize data, :format => @config['format']
26
28
  end
27
29
 
28
30
  end
@@ -13,11 +13,8 @@ module Huck
13
13
 
14
14
  # Ensures that configuration is set properly before executing
15
15
  def verify_config
16
- if !@config.has_key? 'exec'
17
- raise RuntimeError, 'missing exec config'
18
- end
19
- if !@config['exec'].has_key? 'command'
20
- raise RuntimeError, 'missing exec config: command'
16
+ if !@config.has_key? 'command'
17
+ raise RuntimeError, 'missing "exec" generator config: "command"'
21
18
  end
22
19
  end
23
20
 
@@ -31,7 +28,7 @@ module Huck
31
28
  def handle msg
32
29
  verify_config
33
30
 
34
- Open3.popen2 @config['exec']['command'] do |stdin, stdout, thread|
31
+ Open3.popen2 @config['command'] do |stdin, _, _|
35
32
  stdin.print msg
36
33
  end
37
34
  end
data/lib/huck/util.rb CHANGED
@@ -74,7 +74,9 @@ module Huck
74
74
  # A string of serialized text
75
75
  #
76
76
  def self.serialize data, kwargs = {}
77
- format = Huck::getarg kwargs, :format, 'json'
77
+ format = Huck::getarg kwargs, :format, nil
78
+ format = 'json' if format.nil?
79
+
78
80
  case format
79
81
  when 'json'
80
82
  return JSON.dump data
@@ -85,4 +87,31 @@ module Huck
85
87
  end
86
88
  end
87
89
 
90
+ # Parses a list of provider data for generators or handlers. This allows
91
+ # each provider to have its own config, and if no config is required, to
92
+ # simply pass the name of the provider. It also allows multiples of the
93
+ # same provider type to be configured and run.
94
+ #
95
+ # == Parameters:
96
+ # data::
97
+ # The configuration data to parse
98
+ #
99
+ def self.parse_providers data
100
+ if !data.kind_of? Array
101
+ raise RuntimeError, "expected array, got: #{data}"
102
+ end
103
+ data.each do |provider|
104
+ config = Hash.new
105
+ if provider.kind_of? Hash
106
+ name = provider.keys[0]
107
+ config = provider.values[0]
108
+ elsif provider.kind_of? String
109
+ name = provider
110
+ else
111
+ raise RuntimeError, "expected hash, got: #{gen}"
112
+ end
113
+ yield name, config
114
+ end
115
+ end
116
+
88
117
  end
data/lib/huck/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Huck
2
- Version = '0.3.0'
2
+ Version = '0.3.2'
3
3
  end
data/lib/huck.rb CHANGED
@@ -7,8 +7,8 @@ require 'huck/generator'
7
7
  require 'huck/generators/basic'
8
8
  require 'huck/generators/facter'
9
9
  require 'huck/generators/ohai'
10
- require 'huck/generators/yaml'
11
- require 'huck/generators/json'
10
+ require 'huck/generators/file'
11
+ require 'huck/generators/exec'
12
12
  require 'huck/sender'
13
13
  require 'huck/senders/sqs'
14
14
  require 'huck/senders/rabbitmq'
@@ -26,12 +26,10 @@ module Huck
26
26
  # generator will be invoked instead.
27
27
  #
28
28
  # == Parameters:
29
- # config::
29
+ # config_file::
30
30
  # Configuration file path
31
- # generator::
32
- # The name of the generator to use
33
- # sender::
34
- # The name of the sender to use
31
+ # config::
32
+ # Configuration hash to use in place of a config file
35
33
  #
36
34
  def self.run kwargs = {}
37
35
  config = Huck::getarg kwargs, :config, nil
@@ -40,13 +38,7 @@ module Huck
40
38
  config = Huck::config :path => conf_file
41
39
  end
42
40
 
43
- if config.has_key? 'generator'
44
- gen_name = config['generator']
45
- end
46
- gen_arg = Huck::getarg kwargs, :generator, nil
47
- gen_name = gen_arg if !gen_arg.nil?
48
- g = Generator::factory :name => gen_name, :config => config
49
-
41
+ # Prep the sender
50
42
  if config.has_key? 'sender'
51
43
  send_name = config['sender']
52
44
  end
@@ -54,8 +46,18 @@ module Huck
54
46
  send_name = send_arg if !send_arg.nil?
55
47
  s = Sender::factory :name => send_name, :config => config
56
48
 
57
- data = block_given? ? yield : g.generate
58
- s.send Huck::serialize data
49
+ # Create an array of generators to execute
50
+ gen_list = config.has_key?('generators') ? config['generators'] : ['basic']
51
+ generators = Array.new
52
+ Huck::parse_providers gen_list do |gen_name, gen_config|
53
+ generators << Generator::factory(:name => gen_name, :config => gen_config)
54
+ end
55
+
56
+ # Execute the generators and send their output
57
+ generators.each do |g|
58
+ data = block_given? ? yield : g.generate
59
+ s.send data
60
+ end
59
61
  end
60
62
 
61
63
  # Main method to receive messages from a Huck client. If a block is given, the
@@ -63,8 +65,10 @@ module Huck
63
65
  # or default handler will be used.
64
66
  #
65
67
  # == Parameters:
66
- # receiver::
67
- # The receiver to use (default=sqs)
68
+ # config_file::
69
+ # Configuration file path
70
+ # config::
71
+ # Configuration hash to use in place of a config file
68
72
  #
69
73
  def self.serve kwargs = {}
70
74
  config = Huck::getarg kwargs, :config, nil
@@ -73,13 +77,14 @@ module Huck
73
77
  config = Huck::config :path => conf_file
74
78
  end
75
79
 
76
- if config.has_key? 'handler'
77
- hand_name = config['handler']
80
+ # Create an array of handlers to run when messages arrive
81
+ hand_list = config.has_key?('handlers') ? config['handlers'] : ['echo']
82
+ handlers = Array.new
83
+ Huck::parse_providers hand_list do |hand_name, hand_config|
84
+ handlers << Handler::factory(:name => hand_name, :config => hand_config)
78
85
  end
79
- hand_arg = Huck::getarg kwargs, :handler, nil
80
- hand_name = hand_arg if !hand_arg.nil?
81
- h = Handler::factory :name => hand_name, :config => config
82
86
 
87
+ # Prep the receiver
83
88
  if config.has_key? 'receiver'
84
89
  recv_name = config['receiver']
85
90
  end
@@ -87,9 +92,10 @@ module Huck
87
92
  recv_name = recv_arg if !recv_arg.nil?
88
93
  r = Receiver::factory :name => recv_name, :config => config
89
94
 
95
+ # Receive messages and pass them down to the handlers
90
96
  begin
91
97
  r.receive do |msg|
92
- block_given? ? yield(msg) : h.handle(msg)
98
+ block_given? ? yield(msg) : handlers.each {|h| h.handle msg}
93
99
  end
94
100
  rescue Interrupt, SystemExit
95
101
  return
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Uber
@@ -62,8 +62,8 @@ files:
62
62
  - lib/huck/generators/ohai.rb
63
63
  - lib/huck/generators/facter.rb
64
64
  - lib/huck/generators/basic.rb
65
- - lib/huck/generators/yaml.rb
66
- - lib/huck/generators/json.rb
65
+ - lib/huck/generators/exec.rb
66
+ - lib/huck/generators/file.rb
67
67
  - lib/huck/senders/sqs.rb
68
68
  - lib/huck/senders/rabbitmq.rb
69
69
  - lib/huck/receiver.rb
@@ -1,35 +0,0 @@
1
- module Huck
2
-
3
- module Generators
4
-
5
- # JSON generator reads json files for data
6
- class JsonGenerator < Generator
7
-
8
- # Load required modules for json generator
9
- def initialize
10
- Huck::must_load 'json'
11
- end
12
-
13
- # Ensure that all JSON config items are properly set
14
- def verify_config
15
- if !@config.has_key? 'json'
16
- raise RuntimeError, 'missing json config'
17
- end
18
- if !@config['json'].has_key? 'file'
19
- raise RuntimeError, 'missing json config: file'
20
- end
21
- end
22
-
23
- # Reads in a configured JSON file and returns the data
24
- #
25
- # == Returns:
26
- # A hash of facts read from the JSON file
27
- #
28
- def generate
29
- verify_config
30
- JSON.load IO.read(@config['json']['file'])
31
- end
32
-
33
- end
34
- end
35
- end
@@ -1,35 +0,0 @@
1
- module Huck
2
-
3
- module Generators
4
-
5
- # YAML generator reads yaml files for data
6
- class YamlGenerator < Generator
7
-
8
- # Load required modules for yaml generator
9
- def initialize
10
- Huck::must_load 'yaml'
11
- end
12
-
13
- # Ensure that all YAML config items are properly set
14
- def verify_config
15
- if !@config.has_key? 'yaml'
16
- raise RuntimeError, 'missing yaml config'
17
- end
18
- if !@config['yaml'].has_key? 'file'
19
- raise RuntimeError, 'missing yaml config: file'
20
- end
21
- end
22
-
23
- # Reads in a configured YAML file and returns the data
24
- #
25
- # == Returns:
26
- # A hash of facts read from the YAML file
27
- #
28
- def generate
29
- verify_config
30
- YAML.load_file @config['yaml']['file']
31
- end
32
-
33
- end
34
- end
35
- end