huck 0.1.0 → 0.2.0

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: 9e205a66c535c1083e677ee4c21216c82a7e1871
4
- data.tar.gz: 35f2101dd7b4bf04061a5551f10359d7c28ecfb5
3
+ metadata.gz: a73eb6afba2acf8dd3c721bb47af9255fbdb09aa
4
+ data.tar.gz: e5967e01e75d5adeecbe7d06863cfea33d1c6241
5
5
  SHA512:
6
- metadata.gz: 9290fec08bc1f3f37ebab767daaa3b6c216eb8c5599d7287c86dfcc92e883342a0773cab1da1f2a8bdeb43645bc3cfc158a68184d068557a3d2b537467aa7464
7
- data.tar.gz: 13fe443bd3e52143a0c9da1925ea104306335f3391a2ac96a52331b0f62a2e3da6c2b7f9c0090462f1469604bf7e447c9392d2ef27a24158b81dd4ce888243ff
6
+ metadata.gz: dbe365b4814a6050b0386e79a78dd2243ae062432484a92dcce2a92114e2fe663ebc1227513eba270835c407065f733771c0bff0854adbf2cf56394719074604
7
+ data.tar.gz: 99b9c4a04a7b5513795f6f4cd9f1d74a3e4a8887e5d3b792b89e0e53990decdfcf5fff50ac9a79ad5ccaccf245bf8866a4dc1352913b642dccfefb4cb30e6c36
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'huck'
4
+
5
+ begin
6
+ options = Hash.new
7
+ OptionParser.new do |opts|
8
+ opts.on '-c [path]' do |v|
9
+ options[:config] = v
10
+ end
11
+ end.parse!
12
+
13
+ case ARGV[0]
14
+ when 'run'
15
+ Huck.run :config => options[:config]
16
+ when 'serve'
17
+ Huck.serve :config => options[:config]
18
+ else
19
+ puts <<EOF
20
+ Usage: huck <run|serve>
21
+
22
+ Options:
23
+ -c The path to a configuration file
24
+ Defaults to ~/huck.conf
25
+ EOF
26
+ exit 1
27
+ end
28
+ rescue => e
29
+ puts "Error: #{e}"
30
+ exit 1
31
+ end
32
+
33
+ exit 0
@@ -6,11 +6,14 @@ require 'huck/util'
6
6
  require 'huck/generator'
7
7
  require 'huck/generators/facter'
8
8
  require 'huck/generators/ohai'
9
+ require 'huck/generators/yaml'
10
+ require 'huck/generators/json'
9
11
  require 'huck/sender'
10
12
  require 'huck/senders/sqs'
11
13
  require 'huck/receiver'
12
14
  require 'huck/receivers/sqs'
13
15
  require 'huck/handler'
16
+ require 'huck/handlers/echo'
14
17
  require 'huck/handlers/exec'
15
18
 
16
19
  module Huck
@@ -22,24 +25,23 @@ module Huck
22
25
  # The name of the generator to use (default=facter)
23
26
  #
24
27
  def self.run kwargs = {}
25
- config = Huck::config
28
+ conf_file = Huck::getarg kwargs, :config, nil
29
+ config = Huck::config :path => conf_file
30
+
26
31
  if config.has_key? 'generator'
27
32
  gen_name = config['generator']
28
33
  end
29
-
30
34
  gen_arg = Huck::getarg kwargs, :generator, nil
31
35
  gen_name = gen_arg if !gen_arg.nil?
32
-
33
- g = Generator::factory gen_name
36
+ g = Generator::factory :name => gen_name, :config => config
34
37
 
35
38
  if config.has_key? 'sender'
36
39
  send_name = config['sender']
37
40
  end
38
-
39
41
  send_arg = Huck::getarg kwargs, :sender, nil
40
42
  send_name = send_arg if !send_arg.nil?
43
+ s = Sender::factory :name => send_name, :config => config
41
44
 
42
- s = Sender::factory send_name
43
45
  s.send g.dump
44
46
  end
45
47
 
@@ -50,25 +52,29 @@ module Huck
50
52
  # The receiver to use (default=sqs)
51
53
  #
52
54
  def self.serve kwargs = {}
53
- config = Huck::config
55
+ conf_file = Huck::getarg kwargs, :config, nil
56
+ config = Huck::config :path => conf_file
57
+
54
58
  if config.has_key? 'handler'
55
59
  hand_name = config['handler']
56
60
  end
57
-
58
61
  hand_arg = Huck::getarg kwargs, :handler, nil
59
62
  hand_name = hand_arg if !hand_arg.nil?
60
- h = Handler::factory hand_name
63
+ h = Handler::factory :name => hand_name, :config => config
61
64
 
62
65
  if config.has_key? 'receiver'
63
66
  recv_name = config['receiver']
64
67
  end
65
-
66
68
  recv_arg = Huck::getarg kwargs, :receiver, nil
67
69
  recv_name = recv_arg if !recv_arg.nil?
70
+ r = Receiver::factory :name => recv_name, :config => config
68
71
 
69
- r = Receiver::factory recv_name
70
- r.receive do |msg|
71
- h.handle msg
72
+ begin
73
+ r.receive do |msg|
74
+ h.handle msg
75
+ end
76
+ rescue Interrupt, SystemExit
77
+ return
72
78
  end
73
79
  end
74
80
 
@@ -2,6 +2,8 @@ module Huck
2
2
 
3
3
  class Generator
4
4
 
5
+ attr_accessor :config
6
+
5
7
  # This method will call the generation method, and return the data in the
6
8
  # desired format.
7
9
  def dump kwargs = {}
@@ -26,11 +28,16 @@ module Huck
26
28
  # == Parameters:
27
29
  # name::
28
30
  # The name of the generator, or empty/nil to guess
31
+ # config::
32
+ # A configuration hash
29
33
  #
30
34
  # == Returns:
31
35
  # A Huck::Generator instance
32
36
  #
33
- def self.factory name
37
+ def self.factory kwargs = {}
38
+ name = Huck::getarg kwargs, :name, nil
39
+ config = Huck::getarg kwargs, :config, nil
40
+
34
41
  if name.nil?
35
42
  if Huck::try_load 'facter'
36
43
  name = 'facter'
@@ -43,14 +50,19 @@ module Huck
43
50
 
44
51
  case name
45
52
  when 'facter'
46
- gen = Generators::FacterGenerator.new
53
+ g = Generators::FacterGenerator.new
47
54
  when 'ohai'
48
- gen = Generators::OhaiGenerator.new
55
+ g = Generators::OhaiGenerator.new
56
+ when 'yaml'
57
+ g = Generators::YamlGenerator.new
58
+ when 'json'
59
+ g = Generators::JsonGenerator.new
49
60
  else
50
61
  raise RuntimeError, "bad generator: #{name}"
51
62
  end
52
63
 
53
- return gen
64
+ g.config = config
65
+ return g
54
66
  end
55
67
 
56
68
  end
@@ -0,0 +1,34 @@
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
+ require '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
+ def generate
28
+ verify_config
29
+ JSON.load IO.read(@config['json']['file'])
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
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
+ require '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
+ def generate
28
+ verify_config
29
+ YAML.load_file @config['yaml']['file']
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -2,27 +2,36 @@ module Huck
2
2
 
3
3
  # Base handler class
4
4
  class Handler
5
+ attr_accessor :config
5
6
 
6
7
  # Given a handler's name (or no name), return a new handler instance
7
8
  #
8
9
  # == Parameters:
9
10
  # name::
10
11
  # The name of the handler, or nil to guess
12
+ # config::
13
+ # A configuration hash
11
14
  #
12
15
  # == Returns:
13
16
  # A Huck::Handler instance
14
17
  #
15
- def self.factory name
16
- name = 'exec' if name.nil?
18
+ def self.factory kwargs = {}
19
+ name = Huck::getarg kwargs, :name, nil
20
+ config = Huck::getarg kwargs, :config, nil
21
+
22
+ name = 'echo' if name.nil?
17
23
 
18
24
  case name
25
+ when 'echo'
26
+ h = Handlers::EchoHandler.new
19
27
  when 'exec'
20
- hand = Handlers::ExecHandler.new
28
+ h = Handlers::ExecHandler.new
21
29
  else
22
30
  raise RuntimeError, "bad handler: #{name}"
23
31
  end
24
32
 
25
- return hand
33
+ h.config = config
34
+ return h
26
35
  end
27
36
 
28
37
  end
@@ -0,0 +1,15 @@
1
+ module Huck
2
+
3
+ module Handlers
4
+
5
+ # The most basic handler to echo messages to stdout
6
+ class EchoHandler < Handler
7
+
8
+ # Handles a message by printing it
9
+ def handle msg
10
+ puts msg
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -6,21 +6,17 @@ module Huck
6
6
  # input on stdin.
7
7
  class ExecHandler < Handler
8
8
 
9
+ # Includes all required modules
9
10
  def initialize
10
11
  require 'open3'
11
12
  end
12
13
 
13
14
  # Ensures that configuration is set properly before executing
14
- #
15
- # == Parameters:
16
- # config::
17
- # A hash of configuration data to verify
18
- #
19
- def verify_config config
20
- if !config.has_key? 'exec'
15
+ def verify_config
16
+ if !@config.has_key? 'exec'
21
17
  raise RuntimeError, 'missing exec config'
22
18
  end
23
- if !config['exec'].has_key? 'command'
19
+ if !@config['exec'].has_key? 'command'
24
20
  raise RuntimeError, 'missing exec config: command'
25
21
  end
26
22
  end
@@ -28,10 +24,9 @@ module Huck
28
24
  # Handle an individual message by running an executable, passing in the
29
25
  # gathered data via stdin.
30
26
  def handle msg
31
- config = Huck::config
32
- verify_config config
27
+ verify_config
33
28
 
34
- Open3.popen2 config['exec']['command'] do |stdin, stdout, thread|
29
+ Open3.popen2 @config['exec']['command'] do |stdin, stdout, thread|
35
30
  stdin.print msg
36
31
  end
37
32
  end
@@ -2,17 +2,23 @@ module Huck
2
2
 
3
3
  # Base receiver class
4
4
  class Receiver
5
+ attr_accessor :config
5
6
 
6
7
  # Given a receiver's name (or no name), return a new receiver instance
7
8
  #
8
9
  # == Parameters:
9
10
  # name::
10
11
  # The name of the receiver, or nil to guess
12
+ # config::
13
+ # A configuration hash
11
14
  #
12
15
  # == Returns:
13
16
  # A Huck::Receiver instance
14
17
  #
15
- def self.factory name
18
+ def self.factory kwargs = {}
19
+ name = Huck::getarg kwargs, :name, nil
20
+ config = Huck::getarg kwargs, :config, nil
21
+
16
22
  if name.nil?
17
23
  if Huck::try_load 'aws-sdk'
18
24
  name = 'sqs'
@@ -23,12 +29,13 @@ module Huck
23
29
 
24
30
  case name
25
31
  when 'sqs'
26
- recv = Receivers::SQSReceiver.new
32
+ r = Receivers::SQSReceiver.new
27
33
  else
28
34
  raise RuntimeError, "bad receiver: #{name}"
29
35
  end
30
36
 
31
- return recv
37
+ r.config = config
38
+ return r
32
39
  end
33
40
 
34
41
  end
@@ -12,18 +12,13 @@ module Huck
12
12
 
13
13
  # Ensures that configuration is set properly before trying to use the
14
14
  # connection data to talk to AWS
15
- #
16
- # == Parameters:
17
- # config::
18
- # A hash of configuration data to verify
19
- #
20
- def verify_config config
21
- if !config.has_key? 'sqs'
15
+ def verify_config
16
+ if !@config.has_key? 'sqs'
22
17
  raise RuntimeError, 'missing sqs config'
23
18
  end
24
19
  ['access_key_id', 'secret_access_key', 'region',
25
20
  'queue_name'].each do |key|
26
- if !config['sqs'].has_key? key
21
+ if !@config['sqs'].has_key? key
27
22
  raise RuntimeError, "missing sqs config: #{key}"
28
23
  end
29
24
  end
@@ -32,16 +27,15 @@ module Huck
32
27
  # A long-running poller process which reads messages out of the remote
33
28
  # queue and yields them to higher-order logic.
34
29
  def receive
35
- config = Huck::config
36
- verify_config config
30
+ verify_config
37
31
 
38
32
  sqs = AWS::SQS.new(
39
- :access_key_id => config['sqs']['access_key_id'],
40
- :secret_access_key => config['sqs']['secret_access_key'],
41
- :region => config['sqs']['region']
33
+ :access_key_id => @config['sqs']['access_key_id'],
34
+ :secret_access_key => @config['sqs']['secret_access_key'],
35
+ :region => @config['sqs']['region']
42
36
  )
43
37
 
44
- queue = sqs.queues.create config['sqs']['queue_name']
38
+ queue = sqs.queues.create @config['sqs']['queue_name']
45
39
  queue.poll do |msg|
46
40
  yield msg.body
47
41
  end
@@ -2,17 +2,23 @@ module Huck
2
2
 
3
3
  # The base sender class
4
4
  class Sender
5
+ attr_accessor :config
5
6
 
6
7
  # Given a sender's name (or no name), return a new sender instance
7
8
  #
8
9
  # == Parameters:
9
10
  # name::
10
11
  # The name of the sender, or nil to guess
12
+ # config::
13
+ # A configuration hash
11
14
  #
12
15
  # == Returns:
13
16
  # A Huck::Sender instance
14
17
  #
15
- def self.factory name
18
+ def self.factory kwargs = {}
19
+ name = Huck::getarg kwargs, :name, nil
20
+ config = Huck::getarg kwargs, :config, nil
21
+
16
22
  if name.nil?
17
23
  if Huck::try_load 'aws-sdk'
18
24
  name = 'sqs'
@@ -23,12 +29,13 @@ module Huck
23
29
 
24
30
  case name
25
31
  when 'sqs'
26
- send = Senders::SQSSender.new
32
+ s = Senders::SQSSender.new
27
33
  else
28
34
  raise RuntimeError, "bad sender: #{name}"
29
35
  end
30
36
 
31
- return send
37
+ s.config = config
38
+ return s
32
39
  end
33
40
 
34
41
  end
@@ -12,18 +12,13 @@ module Huck
12
12
 
13
13
  # Ensures that configuration is set properly before trying to use the
14
14
  # connection data to talk to AWS
15
- #
16
- # == Parameters:
17
- # config::
18
- # A hash of configuration data to verify
19
- #
20
- def verify_config config
21
- if !config.has_key? 'sqs'
15
+ def verify_config
16
+ if !@config.has_key? 'sqs'
22
17
  raise RuntimeError, 'missing sqs sender config'
23
18
  end
24
19
  ['access_key_id', 'secret_access_key', 'region',
25
20
  'queue_name'].each do |key|
26
- if !config['sqs'].has_key? key
21
+ if !@config['sqs'].has_key? key
27
22
  raise RuntimeError, "missing sqs sender config: #{key}"
28
23
  end
29
24
  end
@@ -36,16 +31,15 @@ module Huck
36
31
  # The arbitrary text data to send
37
32
  #
38
33
  def send msg
39
- config = Huck::config
40
- verify_config config
34
+ verify_config
41
35
 
42
36
  sqs = AWS::SQS.new(
43
- :access_key_id => config['sqs']['access_key_id'],
44
- :secret_access_key => config['sqs']['secret_access_key'],
45
- :region => config['sqs']['region']
37
+ :access_key_id => @config['sqs']['access_key_id'],
38
+ :secret_access_key => @config['sqs']['secret_access_key'],
39
+ :region => @config['sqs']['region']
46
40
  )
47
41
 
48
- queue = sqs.queues.create config['sqs']['queue_name']
42
+ queue = sqs.queues.create @config['sqs']['queue_name']
49
43
  queue.send_message msg
50
44
  end
51
45
 
@@ -27,7 +27,8 @@ module Huck
27
27
  # A hash containing the configuration
28
28
  #
29
29
  def self.config kwargs = {}
30
- path = self.getarg kwargs, 'path', '/etc/huck.conf'
30
+ path = self.getarg kwargs, :path, nil
31
+ path = File.join(Dir.home, 'huck.conf') if path.nil?
31
32
  YAML.load_file path
32
33
  end
33
34
 
@@ -1,3 +1,3 @@
1
1
  module Huck
2
- Version = '0.1.0'
2
+ Version = '0.2.0'
3
3
  end
@@ -0,0 +1,9 @@
1
+ require 'huck'
2
+ require 'aws-sdk'
3
+ #AWS::Stub!
4
+
5
+ describe 'Running Huck Client' do
6
+ it 'should run the client without errors' do
7
+ Huck.run
8
+ end
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Uber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-14 00:00:00.000000000 Z
11
+ date: 2014-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -52,28 +52,17 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: coveralls
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
55
  description: Open-ended information sharing framework
70
56
  email: ru@ryanuber.com
71
- executables: []
57
+ executables:
58
+ - huck
72
59
  extensions: []
73
60
  extra_rdoc_files: []
74
61
  files:
75
62
  - lib/huck/generators/ohai.rb
76
63
  - lib/huck/generators/facter.rb
64
+ - lib/huck/generators/yaml.rb
65
+ - lib/huck/generators/json.rb
77
66
  - lib/huck/senders/sqs.rb
78
67
  - lib/huck/receiver.rb
79
68
  - lib/huck/sender.rb
@@ -81,9 +70,12 @@ files:
81
70
  - lib/huck/generator.rb
82
71
  - lib/huck/receivers/sqs.rb
83
72
  - lib/huck/handlers/exec.rb
73
+ - lib/huck/handlers/echo.rb
84
74
  - lib/huck/version.rb
85
75
  - lib/huck/handler.rb
86
76
  - lib/huck.rb
77
+ - bin/huck
78
+ - spec/huck/huck_spec.rb
87
79
  homepage: https://github.com/ryanuber/huck
88
80
  licenses:
89
81
  - MIT
@@ -108,4 +100,5 @@ rubygems_version: 2.1.11
108
100
  signing_key:
109
101
  specification_version: 4
110
102
  summary: Information sharing framework
111
- test_files: []
103
+ test_files:
104
+ - spec/huck/huck_spec.rb