Class: Etoro::Utility::MCollective::RPC

Inherits:
Object
  • Object
show all
Defined in:
lib/etoro/utility/mcollective/rpc.rb

Direct Known Subclasses

HAProxy, Puppet, Service

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ RPC

Returns a new instance of RPC.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
65
66
67
# File 'lib/etoro/utility/mcollective/rpc.rb', line 15

def initialize (config={})

  default_config = {
      timeout: 600,
      log_file: 'deployment.log',
      log_level: Logger::INFO,
      sequential: false,
      wait_for_status: 30,
      wait_between_checks: 5
  }
  @config = default_config.merge(config)

  self.validate(config)

  @mc = rpcclient(self.class.name.downcase.split('::').last, :chomp => true)
  @mc.progress = false

  if @config.has_key?(:limit_targets)
    @mc.limit_targets = @config[:limit_targets]
    @mc.limit_method = :random
  end

  @mc.compound_filter @config[:compound_filter] if @config.has_key?(:compound_filter)

  log_descriptor = ""
  if (@config.has_key?(:fact_filter) && @config[:fact_filter].has_key?(:service) &&  @config[:fact_filter].has_key?(:application)) || (@config.has_key?(:application) && @config.has_key?(:service))
    if @config.has_key?(:fact_filter)
      service=@config[:fact_filter][:service]
      application=@config[:fact_filter][:application]
    else
      service=@config[:service]
      application=@config[:application]
    end
    log_descriptor="service=#{service}, application=#{application},"
  end

  self.logging(@config[:log_file],@config[:log_level],log_descriptor)
  @logger.info("Setting timeout to #{@config[:timeout]}")
  @logger.info("Setting log file to #{@config[:log_file]}")
  @logger.info("Setting log level to #{@config[:log_level]}")

  @config.each do |key,value|
    case key
      when :fact_filter
        self.fact_filter(value)
      when :identity_filter
        self.identity_filter(value)
      when :class_filter
        self.identity_filter(value)
    end
  end

end

Instance Attribute Details

#current_hostObject (readonly)

Returns the value of attribute current_host.



12
13
14
# File 'lib/etoro/utility/mcollective/rpc.rb', line 12

def current_host
  @current_host
end

#executeObject (readonly)

Returns the value of attribute execute.



13
14
15
# File 'lib/etoro/utility/mcollective/rpc.rb', line 13

def execute
  @execute
end

#hostsObject (readonly)

Returns the value of attribute hosts.



13
14
15
# File 'lib/etoro/utility/mcollective/rpc.rb', line 13

def hosts
  @hosts
end

#post_execute(&block) ⇒ Object (readonly)

Returns the value of attribute post_execute.



13
14
15
# File 'lib/etoro/utility/mcollective/rpc.rb', line 13

def post_execute
  @post_execute
end

#pre_execute(&block) ⇒ Object (readonly)

Returns the value of attribute pre_execute.



13
14
15
# File 'lib/etoro/utility/mcollective/rpc.rb', line 13

def pre_execute
  @pre_execute
end

Instance Method Details

#class_filter(class_array) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/etoro/utility/mcollective/rpc.rb', line 149

def class_filter(class_array)
  unless class_array.is_a?(Array)
    @logger.error('Class filter needs to be of type array')
  end
  class_array.each do |value|
    @logger.info("Settting class to  #{value}")
    @mc.class_filter value
  end
end

#fact_filter(fact_hash) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/etoro/utility/mcollective/rpc.rb', line 129

def fact_filter(fact_hash)
  unless fact_hash.is_a?(Hash)
    @logger.error('Fact filter needs to be of type hash')
  end
  fact_hash.each do |key,value|
    @logger.info("Setting Fact #{key} to value #{value}")
    @mc.fact_filter key, value
  end
end

#identity_filter(identity_array) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/etoro/utility/mcollective/rpc.rb', line 139

def identity_filter(identity_array)
  unless identity_array.is_a?(Array)
    @logger.error('Identity filter needs to be of type array')
  end
  identity_array.each do |value|
    @logger.info("Setting Identity to host #{value}")
    @mc.identity_filter value
  end
end

#logging(log_file, log_level, description = '') ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/etoro/utility/mcollective/rpc.rb', line 159

def logging(log_file, log_level, description='')
  log = File.open(log_file,'a')
  @logger = Logger.new Etoro::Utility::MCollective::MultiDelegator.delegate(:write, :close).to(STDOUT, log)
  @logger.datetime_format = ('%Y-%m-%d_%H:%M:%S')
  @logger.formatter = proc do |severity, datetime, progname, msg|
    "#{datetime},level=#{severity},type=Deployment,#{description} message=#{msg}\n"
  end
  @logger.level = log_level
end

#runObject



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
120
121
122
123
124
125
126
127
# File 'lib/etoro/utility/mcollective/rpc.rb', line 94

def run
  # Check
  status
  @logger.info("#{self.class.name.split('::').last} run begin")

  if self.sequential? then
    hosts = self.hosts

    if hosts.length == 0
      @logger.error("No Hosts Defined")
      raise "No Hosts Defined"
    end

    @logger.info("Running deployment sequentially")

    hosts.each do |host|
      @current_host = host

      instance_eval &@config[:pre_execute] if @config.has_key?(:pre_execute)
      @mc.reset_filter
      @mc.identity_filter @current_host
      self.status
      self.execute
      instance_eval &@config[:post_execute] if @config.has_key?(:post_execute)
    end
  else
    instance_eval &@config[:pre_execute] if @config.has_key?(:pre_execute)
    self.execute
    instance_eval &@config[:post_execute] if @config.has_key?(:post_execute)
  end

  @logger.info("#{self.class.name.split('::').last} run complete")

end

#sequential=(value) ⇒ Object



78
79
80
# File 'lib/etoro/utility/mcollective/rpc.rb', line 78

def sequential=(value)
  @config[:sequential] = value
end

#sequential?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/etoro/utility/mcollective/rpc.rb', line 82

def sequential?
  return @config[:sequential] ? true : false
end

#validate(config) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/etoro/utility/mcollective/rpc.rb', line 69

def validate(config)
  unless config.detect{|k| k.to_s =~ /:(fact|identity|class|compound)_filter/} then
    raise RuntimeError, "Filters need to be provided"
  end
  if self.class.name == 'Etoro::MCollective::RPC'
    raise RuntimeError, "Cannot instantiate this class directly - abstract"
  end
end