Class: Chef::Provider::Service::Upstart
- Inherits:
-
Simple
show all
- Defined in:
- lib/chef/provider/service/upstart.rb
Constant Summary
collapse
- UPSTART_STATE_FORMAT =
/\w+ \(?(\w+)\)?[\/ ](\w+)/
Mixin::ShellOut::DEPRECATED_OPTIONS
Instance Attribute Summary
Attributes inherited from Simple
#status_load_success
#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Simple
#shared_resource_requirements, #whyrun_supported?
#action_disable, #action_enable, #action_reload, #action_restart, #action_start, #action_stop, #load_new_resource_state, #shared_resource_requirements, #whyrun_supported?
#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #run_command_with_systems_locale
#popen4
#popen4
#action_nothing, #cleanup_after_converge, #converge_by, #events, #node, node_map, #process_resource_requirements, provides, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, #whyrun_mode?, #whyrun_supported?
#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited
#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!
Constructor Details
#initialize(new_resource, run_context) ⇒ 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/
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/chef/provider/service/upstart.rb', line 49
def initialize(new_resource, run_context)
raise ArgumentError, "run_context cannot be nil" unless run_context
super
run_context.node
@job = @new_resource.service_name.dup
if @new_resource.parameters
@new_resource.parameters.each do |key, value|
@job << " #{key}=#{value}"
end
end
platform, version = Chef::Platform.find_platform_and_version(run_context.node)
if platform == "ubuntu" && (8.04..9.04).include?(version.to_f)
@upstart_job_dir = "/etc/event.d"
@upstart_conf_suffix = ""
else
@upstart_job_dir = "/etc/init"
@upstart_conf_suffix = ".conf"
end
@command_success = true @config_file_found = true
@upstart_command_success = true
end
|
Class Method Details
.provides?(node, resource) ⇒ Boolean
.supports?(resource, action) ⇒ Boolean
Instance Method Details
#define_resource_requirements ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/chef/provider/service/upstart.rb', line 79
def define_resource_requirements
shared_resource_requirements
requirements.assert(:all_actions) do |a|
if !@command_success
whyrun_msg = @new_resource.status_command ? "Provided status command #{@new_resource.status_command} failed." :
"Could not determine upstart state for service"
end
a.assertion { @command_success }
a.whyrun "#{whyrun_msg} Assuming service installed and not running."
end
requirements.assert(:all_actions) do |a|
a.assertion { @config_file_found }
a.whyrun "Could not find #{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}. Assuming service is disabled."
end
end
|
#disable_service ⇒ Object
217
218
219
220
221
222
|
# File 'lib/chef/provider/service/upstart.rb', line 217
def disable_service
Chef::Log.debug("#{@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_service ⇒ Object
210
211
212
213
214
215
|
# File 'lib/chef/provider/service/upstart.rb', line 210
def enable_service
Chef::Log.debug("#{@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_resource ⇒ Object
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/chef/provider/service/upstart.rb', line 99
def load_current_resource
@current_resource = Chef::Resource::Service.new(@new_resource.name)
@current_resource.service_name(@new_resource.service_name)
if @new_resource.status_command
Chef::Log.debug("#{@new_resource} you have specified a status command, running..")
begin
if shell_out!(@new_resource.status_command) == 0
@current_resource.running true
end
rescue
@command_success = false
@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
@command_success = false
@current_resource.running false
nil
end
end
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
@config_file_found = false
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_service ⇒ Object
199
200
201
202
203
204
205
206
|
# File 'lib/chef/provider/service/upstart.rb', line 199
def reload_service
if @new_resource.reload_command
super
else
shell_out_with_systems_locale!("/sbin/reload #{@job}")
end
end
|
#restart_service ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/chef/provider/service/upstart.rb', line 185
def restart_service
if @new_resource.restart_command
super
else
if @current_resource.running
shell_out_with_systems_locale!("/sbin/restart #{@job}")
else
start_service
end
end
end
|
#start_service ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/chef/provider/service/upstart.rb', line 157
def start_service
if @current_resource.running
Chef::Log.debug("#{@new_resource} already running, not starting")
else
if @new_resource.start_command
super
else
shell_out_with_systems_locale!("/sbin/start #{@job}")
end
end
end
|
#stop_service ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/chef/provider/service/upstart.rb', line 171
def stop_service
unless @current_resource.running
Chef::Log.debug("#{@new_resource} not running, not stopping")
else
if @new_resource.stop_command
super
else
shell_out_with_systems_locale!("/sbin/stop #{@job}")
end
end
end
|
#upstart_state ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/chef/provider/service/upstart.rb', line 224
def upstart_state
command = "/sbin/status #{@job}"
status = popen4(command) do |pid, stdin, stdout, stderr|
stdout.each_line do |line|
line =~ UPSTART_STATE_FORMAT
data = Regexp.last_match
return data[2]
end
end
end
|