Class: Truth::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/truth/client.rb

Overview

Client contains the business logic

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



5
6
7
# File 'lib/truth/client.rb', line 5

def initialize(config)
  self.config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/truth/client.rb', line 4

def config
  @config
end

Class Method Details

.get_command(channel) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/truth/client.rb', line 37

def self.get_command(channel)
  case channel
  when /add_roles$/       then :add_roles
  when /remove_roles$/    then :remove_roles
  when /add_info$/        then :add_info
  when /remove_info$/     then :remove_info
  when /set_environment$/ then :set_environment
  end
end

Instance Method Details

#add_info(message) ⇒ Object

add meta info to be used by your configuration management



66
67
68
69
# File 'lib/truth/client.rb', line 66

def add_info(message)
  key, value = message.split(',', 2)
  config.add_info key, value.strip
end

#add_roles(message) ⇒ Object

add an roles array to the roles for this node message is a comma-separated string



51
52
53
54
55
# File 'lib/truth/client.rb', line 51

def add_roles(message)
  roles = message.split(',').map{|x| x.strip}
  config.add_roles roles
  return config.current_roles
end

#execute_command(channel, message) ⇒ Object

this is the entry point for the client channel can look like: truth-req.hostname.* truth-req.all.*



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/truth/client.rb', line 13

def execute_command(channel, message)
  begin
    response = case Client.get_command(channel)
    when :add_roles
      add_roles(message)
    when :remove_roles
      remove_roles(message)
    when :add_info
      add_info(message)
    when :remove_info
      remove_info(message)
    when :set_environment
      set_environment(message)
    else
      puts "no command for #{channel} available"
      'NOT AVAILABLE'
    end
  rescue Exception => e
    puts "ERROR: #{e}"
    response = "ERROR"
  end
  response
end

#remove_info(message) ⇒ Object



71
72
73
# File 'lib/truth/client.rb', line 71

def remove_info(message)
  config.remove_info message.strip
end

#remove_roles(message) ⇒ Object

removes an roles array from the roles for this node. message is a comma-separated string



59
60
61
62
63
# File 'lib/truth/client.rb', line 59

def remove_roles(message)
  roles = message.split(',').map{|x| x.strip}
  config.remove_roles roles
  return config.current_roles
end

#set_environment(message) ⇒ Object

this can change the environment for this node



76
77
78
# File 'lib/truth/client.rb', line 76

def set_environment(message)

end