Class: Etoro::Utility::MCollective::Service

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

Instance Attribute Summary

Attributes inherited from RPC

#current_host, #post_execute, #pre_execute

Instance Method Summary collapse

Methods inherited from RPC

#class_filter, #fact_filter, #identity_filter, #initialize, #logging, #run, #sequential=, #sequential?

Constructor Details

This class inherits a constructor from Etoro::Utility::MCollective::RPC

Instance Method Details

#check_run_statusObject



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
68
69
70
71
72
73
74
75
76
77
# File 'lib/etoro/utility/mcollective/service.rb', line 43

def check_run_status
  @logger.info("Checking Service #{@config[:service_name]} for completion")

  begin
    timeout(@config[:timeout]) do
      until @service_status == {}
        @service_status.each do |host, status|
          run = rpcclient(self.class.name.downcase.split('::').last, :chomp => true)
          run.identity_filter host
          run.progress = false
          results = run.send 'status', service: @config[:service_name]
          results.each do |result|
            state = ''
            case @config[:action]
              when 'start'
                state = 'running'
              when 'stop'
                state = 'stopped'
            end
            @logger.info("#{host} - Service: current state: #{result[:data][:status]} desired_state: #{state}")
            if result[:data][:status] == state
              @logger.info("#{host} - Service: #{@config[:service_name]} #{@config[:action]} action  #{result[:data][:status]} - completed")
              @service_status.delete(host)
            else
              @logger.info("#{host} - Service #{@config[:service_name]} #{@config[:action]} action not complete - #{result[:data][:status]}")
            end
          end
        end
        sleep @config[:wait_between_checks]
      end
    end
  rescue Timeout::Error
    @logger.error("Service run timed out. Took more than #{@config[:timeout]} seconds")
  end
end

#executeObject



19
20
21
22
23
24
# File 'lib/etoro/utility/mcollective/service.rb', line 19

def execute
  @logger.info("Sending service #{@config[:service_name]} #{@config[:action]} action")
  @mc.send @config[:action], service: @config[:service_name]
  sleep @config[:wait_for_status]
  self.check_run_status
end

#hostsObject



35
36
37
38
39
40
41
# File 'lib/etoro/utility/mcollective/service.rb', line 35

def hosts
  hosts = []
  @service_status.each_key do |host|
    @hosts.push host
  end
  hosts
end

#statusObject



26
27
28
29
30
31
32
33
# File 'lib/etoro/utility/mcollective/service.rb', line 26

def status
  @service_status = {}
  @logger.info("Getting service information")
  results = @mc.send 'status', service: @config[:service_name]
  results.each do |result|
    @service_status[result[:sender]] = result[:data][:status]
  end
end

#validate(config) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/etoro/utility/mcollective/service.rb', line 6

def validate(config)
  super
  unless config.has_key?(:service_name)
    raise RuntimeError, "config[:service_name] must be defined"
  end
  unless config.has_key?(:action)
    raise RuntimeError, "config[:action] must be defined"
  end
  unless ['start','stop'].include? config[:action]
    raise RuntimeError, "Service action must be either start or stop"
  end
end