Class: TemplateConfigurator::Service
- Inherits:
-
Object
- Object
- TemplateConfigurator::Service
- Defined in:
- lib/template_configurator/service.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #command(action) ⇒ Object
- #conditional_reload ⇒ Object
- #execute(command) ⇒ Object
-
#initialize(options) ⇒ Service
constructor
A new instance of Service.
- #reload ⇒ Object
- #restart ⇒ Object
- #start ⇒ Object
- #status ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options) ⇒ Service
36 37 38 |
# File 'lib/template_configurator/service.rb', line 36 def initialize = end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
35 36 37 |
# File 'lib/template_configurator/service.rb', line 35 def 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 << [:command] args << [:name] args << action TemplateConfigurator.log.debug("command args: #{args.inspect}") Shellwords.join(args) end |
#conditional_reload ⇒ Object
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. [: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.) TemplateConfigurator.log.error(e.output) unless e.output.nil? end rescue ServiceException => e # service is not running TemplateConfigurator.log.error(e.) 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.) TemplateConfigurator.log.error(e.output) unless e.output.nil? end end sleep([: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., 1) end return output end |
#reload ⇒ Object
69 70 71 |
# File 'lib/template_configurator/service.rb', line 69 def reload execute command([:reload]) end |
#restart ⇒ Object
65 66 67 |
# File 'lib/template_configurator/service.rb', line 65 def restart execute command([:restart]) end |
#start ⇒ Object
73 74 75 |
# File 'lib/template_configurator/service.rb', line 73 def start execute command([:start]) end |
#status ⇒ Object
61 62 63 |
# File 'lib/template_configurator/service.rb', line 61 def status execute command([:status]) end |
#stop ⇒ Object
77 78 79 |
# File 'lib/template_configurator/service.rb', line 77 def stop execute command([:stop]) end |