Class: Chef::Provider::Service::Upstart

Inherits:
Simple show all
Defined in:
lib/chef/provider/service/upstart.rb

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

Methods inherited from Chef::Provider::Service

#action_disable, #action_enable, #action_reload, #action_restart, #action_start, #action_stop

Methods included from Mixin::Command

chdir_or_tmpdir, handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider

#action_nothing, build_from_file

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

#initialize(node, new_resource, collection = nil, definitions = nil, cookbook_loader = nil) ⇒ Upstart

Upstart does more than start or stop a service, creating multiple ‘states’ [1] that a service can be in. In chef, when we ask a service to start, we expect it to have started before performing the next step since we have top down dependencies. Which is to say we may follow witha resource next that requires that service to be running. According to [2] we can trust that sending a ‘goal’ such as start will not return until that ‘goal’ is reached, or some error has occured.

1

upstart.ubuntu.com/wiki/JobStates

2

www.netsplit.com/2008/04/27/upstart-05-events/



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/provider/service/upstart.rb', line 38

def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
  super(node, new_resource, collection, definitions, cookbook_loader)

  platform, version = Chef::Platform.find_platform_and_version(node)
  case platform
  when "ubuntu"
    case version
    when /8.04/,/8.10/,/9.04/
      @upstart_job_dir = "/etc/event.d"
      @upstart_conf_suffix = ""
    else
      @upstart_job_dir = "/etc/init"
      @upstart_conf_suffix = ".conf"
    end
  else
    @upstart_job_dir = "/etc/init"
    @upstart_conf_suffix = ".conf"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Method Details

#disable_serviceObject



168
169
170
171
172
173
# File 'lib/chef/provider/service/upstart.rb', line 168

def disable_service
  Chef::Log.warn("#{@new_resource}: upstart lacks inherent support for disabling services, editing job config file")
  conf = Chef::Util::FileEdit.new("#{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}")
  conf.search_file_replace(/^start on/, "#start on")
  conf.write_file
end

#enable_serviceObject



161
162
163
164
165
166
# File 'lib/chef/provider/service/upstart.rb', line 161

def enable_service
  Chef::Log.warn("#{@new_resource}: upstart lacks inherent support for enabling services, editing job config file")
  conf = Chef::Util::FileEdit.new("#{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}")
  conf.search_file_replace(/^#start on/, "start on")
  conf.write_file
end

#load_current_resourceObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
# File 'lib/chef/provider/service/upstart.rb', line 58

def load_current_resource
  @current_resource = Chef::Resource::Service.new(@new_resource.name)
  @current_resource.service_name(@new_resource.service_name)

  # Get running/stopped state
  # We do not support searching for a service via ps when using upstart since status is a native
  # upstart function. We will however support status_command in case someone wants to do something special.
  if @new_resource.status_command
    Chef::Log.debug("#{@new_resource} you have specified a status command, running..")

    begin
      if run_command_with_systems_locale(:command => @new_resource.status_command) == 0
        @current_resource.running true
      end
    rescue Chef::Exceptions::Exec
      @current_resource.running false
      nil
    end
  else
    begin
      if upstart_state == "running"
        @current_resource.running true
      else
        @current_resource.running false
      end
    rescue Chef::Exceptions::Exec
      @current_resource.running false
      nil
    end
  end

  # Get enabled/disabled state by reading job configuration file
  if ::File.exists?("#{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}")
    Chef::Log.debug("#{@new_resource}: found #{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}")
    ::File.open("#{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}",'r') do |file|
      while line = file.gets
        case line
        when /^start on/
          Chef::Log.debug("#{@new_resource}: enabled: #{line.chomp}")
          @current_resource.enabled true
          break
        when /^#start on/
          Chef::Log.debug("#{@new_resource}: disabled: #{line.chomp}")
          @current_resource.enabled false
          break
        end
      end
    end
  else
    Chef::Log.debug("#{@new_resource}: did not find #{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}")
    @current_resource.enabled false
  end

  @current_resource
end

#reload_serviceObject



150
151
152
153
154
155
156
157
# File 'lib/chef/provider/service/upstart.rb', line 150

def reload_service
  if @new_resource.reload_command
    super
  else
    # upstart >= 0.6.3-4 supports reload (HUP)
    run_command_with_systems_locale(:command => "/sbin/reload #{@new_resource.service_name}")
  end
end

#restart_serviceObject



142
143
144
145
146
147
148
# File 'lib/chef/provider/service/upstart.rb', line 142

def restart_service
  if @new_resource.restart_command
    super
  else
    run_command_with_systems_locale(:command => "/sbin/restart #{@new_resource.service_name}")
  end
end

#start_serviceObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef/provider/service/upstart.rb', line 114

def start_service
  # Calling start on a service that is already started will return 1
  # Our 'goal' when we call start is to ensure the service is started
  if @current_resource.running
    Chef::Log.debug("#{@new_resource}: Already running, not starting")
  else
    if @new_resource.start_command
      super
    else
      run_command_with_systems_locale(:command => "/sbin/start #{@new_resource.service_name}")
    end
  end
end

#stop_serviceObject



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chef/provider/service/upstart.rb', line 128

def stop_service
  # Calling stop on a service that is already stopped will return 1
  # Our 'goal' when we call stop is to ensure the service is stopped
  unless @current_resource.running
    Chef::Log.debug("#{@new_resource}: Not running, not stopping")
  else
    if @new_resource.stop_command
      super
    else
      run_command_with_systems_locale(:command => "/sbin/stop #{@new_resource.service_name}")
    end
  end
end

#upstart_stateObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/chef/provider/service/upstart.rb', line 175

def upstart_state
  command = "/sbin/status #{@new_resource.service_name}"
  status = popen4(command) do |pid, stdin, stdout, stderr|
    stdout.each do |line|
      # rsyslog stop/waiting
      # service goal/state
      # OR
      # rsyslog (stop) waiting
      # service (goal) state
      line =~ /\w+ \(?(\w+)\)?[\/ ](\w+)/
      data = Regexp.last_match
      return data[2]
    end
  end
end