Class: TemplateConfigurator::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/template_configurator/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Service



36
37
38
# File 'lib/template_configurator/service.rb', line 36

def initialize options
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



35
36
37
# File 'lib/template_configurator/service.rb', line 35

def options
  @options
end

Instance Method Details

#command(action) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/template_configurator/service.rb', line 40

def command action
  args = []
  args << @options[:command]
  args << @options[:name]
  args << action
  TemplateConfigurator.log.debug("command args: #{args.inspect}")
  Shellwords.join(args)
end

#conditional_reloadObject



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
# File 'lib/template_configurator/service.rb', line 81

def conditional_reload
  # Attempt to reload service if it's running, otherwise start it.
  @options[:retries].times do 
    begin
      status_output = self.status
      TemplateConfigurator.log.debug("#{@options[:name]} is running")
      # If the configuration has changed, reload config
      begin 
        reload_output = self.reload
        TemplateConfigurator.log.debug("Reload command succeeded")
        return reload_output
      rescue ServiceException => e
        TemplateConfigurator.log.error(e.message)
        TemplateConfigurator.log.error(e.output) unless e.output.nil?
      end
    rescue ServiceException => e
      # service is not running
      TemplateConfigurator.log.error(e.message)
      TemplateConfigurator.log.error(e.output) unless e.output.nil?
      begin
        start_output = self.start
        TemplateConfigurator.log.debug("Start command succeeded")
        return start_output
      rescue ServiceException => e
        TemplateConfigurator.log.error(e.message)
        TemplateConfigurator.log.error(e.output) unless e.output.nil?
      end
    end
    sleep(@options[:retry_delay])
  end
  # Everything else failed. Try a restart
  return self.restart
end

#execute(command) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/template_configurator/service.rb', line 49

def execute command
  TemplateConfigurator.log.debug("command: #{command}")
  begin
    output = %x{#{command}}
    exit_code = $?.exitstatus
    raise ServiceException.new("execution failed; #{command} exited with status #{exit_code}", exit_code, output) unless exit_code == 0
  rescue Errno::ENOENT => e
    raise ServiceException.new(e.message, 1)
  end
  return output
end

#reloadObject



69
70
71
# File 'lib/template_configurator/service.rb', line 69

def reload
  execute command(@options[:reload])
end

#restartObject



65
66
67
# File 'lib/template_configurator/service.rb', line 65

def restart
  execute command(@options[:restart])
end

#startObject



73
74
75
# File 'lib/template_configurator/service.rb', line 73

def start
  execute command(@options[:start])
end

#statusObject



61
62
63
# File 'lib/template_configurator/service.rb', line 61

def status
  execute command(@options[:status])
end

#stopObject



77
78
79
# File 'lib/template_configurator/service.rb', line 77

def stop
  execute command(@options[:stop])
end