Module: Huck

Defined in:
lib/huck.rb,
lib/huck/util.rb,
lib/huck/sender.rb,
lib/huck/handler.rb,
lib/huck/version.rb,
lib/huck/receiver.rb,
lib/huck/generator.rb,
lib/huck/senders/sqs.rb,
lib/huck/handlers/echo.rb,
lib/huck/handlers/exec.rb,
lib/huck/receivers/sqs.rb,
lib/huck/generators/exec.rb,
lib/huck/generators/file.rb,
lib/huck/generators/ohai.rb,
lib/huck/generators/basic.rb,
lib/huck/senders/rabbitmq.rb,
lib/huck/generators/facter.rb,
lib/huck/receivers/rabbitmq.rb

Defined Under Namespace

Modules: Generators, Handlers, Receivers, Senders Classes: Generator, Handler, Receiver, Sender

Constant Summary collapse

Version =
'0.3.3'

Class Method Summary collapse

Class Method Details

.config(kwargs = {}) ⇒ Object

Read configuration out of a file on the filesystem.

Parameters:

path

The path to the config file to read

Returns:

A hash containing the configuration



29
30
31
32
33
# File 'lib/huck/util.rb', line 29

def self.config kwargs = {}
  path = self.getarg kwargs, :path, nil
  path = File.join(Dir.home, 'huck.conf') if path.nil?
  YAML.load_file path
end

.getarg(args, arg, default) ⇒ Object

Retrieve the value of an “emulated” kwarg easily

Parameters:

args

A hash of kwargs

arg

The name of the arg desired

default

If arg does not exist in args, return this

Returns:

The value of the arg, or the default value



16
17
18
# File 'lib/huck/util.rb', line 16

def self.getarg args, arg, default
  return args.has_key?(arg) ? args[arg] : default
end

.must_load(name) ⇒ Object

Try to load a module, and raise a RuntimeError if it is not loadable. This is useful for trying to load certain provider code at runtime without dealing with LoadError.

Parameters:

name

The name of the module



61
62
63
64
65
# File 'lib/huck/util.rb', line 61

def self.must_load name
  if !self.try_load name
    raise RuntimeError, "unable to load #{name}"
  end
end

.parse_providers(data) ⇒ Object

Parses a list of provider data for generators or handlers. This allows each provider to have its own config, and if no config is required, to simply pass the name of the provider. It also allows multiples of the same provider type to be configured and run.

Parameters:

data

The configuration data to parse



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/huck/util.rb', line 99

def self.parse_providers data
  if !data.kind_of? Array
    raise RuntimeError, "expected array, got: #{data}"
  end
  data.each do |provider|
    config = Hash.new
    if provider.kind_of? Hash
      name = provider.keys[0]
      config = provider.values[0]
    elsif provider.kind_of? String
      name = provider
    else
      raise RuntimeError, "expected hash, got: #{gen}"
    end
    yield name, config
  end
end

.run(kwargs = {}) ⇒ Object

Main method to run Huck and dump info. If a block is given, the block will be used as the generator code. If no block is passed, then the configured generator will be invoked instead.

Parameters:

config_file

Configuration file path

config

Configuration hash to use in place of a config file



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/huck.rb', line 34

def self.run kwargs = {}
  config = Huck::getarg kwargs, :config, nil
  if config.nil?
    conf_file = Huck::getarg kwargs, :config_file, nil
    config = Huck::config :path => conf_file
  end

  # Prep the sender
  if config.has_key? 'sender'
    send_name = config['sender']
  end
  send_arg = Huck::getarg kwargs, :sender, nil
  send_name = send_arg if !send_arg.nil?
  s = Sender::factory :name => send_name, :config => config

  # Create an array of generators to execute
  gen_list = config.has_key?('generators') ? config['generators'] : ['basic']
  generators = Array.new
  Huck::parse_providers gen_list do |gen_name, gen_config|
    generators << Generator::factory(:name => gen_name, :config => gen_config)
  end

  # Execute the generators and send their output
  generators.each do |g|
    data = block_given? ? yield : g.generate
    if !data.kind_of? String
      raise RuntimeError, "generator produced non-string result: #{data.class}"
    end
    s.send data
  end
end

.serialize(data, kwargs = {}) ⇒ Object

Serialize data to a desired format.

Parameters:

format

The serialization format (json or yaml)

Returns:

A string of serialized text



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/huck/util.rb', line 76

def self.serialize data, kwargs = {}
  format = Huck::getarg kwargs, :format, nil
  format = 'json' if format.nil?

  case format
  when 'json'
    return JSON.dump data
  when 'yaml'
    return YAML.dump data
  else
    raise RuntimeError, "unknown format '#{format}'"
  end
end

.serve(kwargs = {}) ⇒ Object

Main method to receive messages from a Huck client. If a block is given, the block will be used as the handler code. Otherwise, the configured handler or default handler will be used.

Parameters:

config_file

Configuration file path

config

Configuration hash to use in place of a config file



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/huck.rb', line 76

def self.serve kwargs = {}
  config = Huck::getarg kwargs, :config, nil
  if config.nil?
    conf_file = Huck::getarg kwargs, :config_file, nil
    config = Huck::config :path => conf_file
  end

  # Create an array of handlers to run when messages arrive
  hand_list = config.has_key?('handlers') ? config['handlers'] : ['echo']
  handlers = Array.new
  Huck::parse_providers hand_list do |hand_name, hand_config|
    handlers << Handler::factory(:name => hand_name, :config => hand_config)
  end

  # Prep the receiver
  if config.has_key? 'receiver'
    recv_name = config['receiver']
  end
  recv_arg = Huck::getarg kwargs, :receiver, nil
  recv_name = recv_arg if !recv_arg.nil?
  r = Receiver::factory :name => recv_name, :config => config

  # Receive messages and pass them down to the handlers
  begin
    r.receive do |msg|
      begin
        if block_given?
          hname = '<block>'
          yield msg
          next # skip other handlers
        end

        handlers.each do |h|
          hname = h.class.to_s
          h.handle msg
        end
      rescue => e
        puts "Handler error (#{hname}): #{e.message}"
      end
    end
  rescue Interrupt, SystemExit
    return
  end
end

.try_load(name) ⇒ Object

Attempts to load a given module by name

Parameters:

name

The name of the module

Returns:

bool



44
45
46
47
48
49
50
51
# File 'lib/huck/util.rb', line 44

def self.try_load name
  begin
    require name
  rescue LoadError
    return false
  end
  true
end