Class: Chef::Provider::Service::Upstart
- Inherits:
-
Simple
- Object
- Chef::Provider
- Chef::Provider::Service
- Simple
- Chef::Provider::Service::Upstart
- Defined in:
- lib/chef/provider/service/upstart.rb
Constant Summary collapse
- UPSTART_STATE_FORMAT =
%r{\S+ \(?(start|stop)?\)? ?[/ ](\w+)}.freeze
Instance Attribute Summary collapse
-
#upstart_service_running ⇒ Object
to maintain a local state of service across restart’s internal calls.
Attributes inherited from Simple
Attributes inherited from Chef::Provider
#action, #after_resource, #current_resource, #logger, #new_resource, #run_context
Class Method Summary collapse
-
.supports?(resource, action) ⇒ Boolean
Returns true if the configs for the service name has upstart variable.
Instance Method Summary collapse
- #define_resource_requirements ⇒ Object
- #disable_service ⇒ Object
- #enable_service ⇒ Object
-
#initialize(new_resource, run_context) ⇒ Upstart
constructor
Upstart does more than start or stop a service, creating multiple ‘states’ [1] that a service can be in.
- #load_current_resource ⇒ Object
- #reload_service ⇒ Object
- #restart_service ⇒ Object
- #start_service ⇒ Object
- #stop_service ⇒ Object
- #upstart_goal_state ⇒ Object
Methods inherited from Simple
Methods inherited from Chef::Provider::Service
#enableable?, #load_new_resource_state, #mask_service, #shared_resource_requirements, #supports, #unmask_service, #user_services_requirements
Methods included from Chef::Platform::ServiceHelpers
#config_for_service, #service_resource_providers
Methods inherited from Chef::Provider
action, action_description, action_descriptions, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #description, #events, include_resource_dsl?, include_resource_dsl_module, #introduced, #load_after_resource, #node, #process_resource_requirements, provides, provides?, #recipe_name, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, use, use_inline_resources, #validate_required_properties!, #whyrun_mode?, #whyrun_supported?
Methods included from Mixin::Provides
#provided_as, #provides, #provides?
Methods included from Mixin::DescendantsTracker
#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited
Methods included from Mixin::LazyModuleInclude
#descendants, #include, #included
Methods included from Mixin::PowershellOut
#powershell_out, #powershell_out!
Methods included from Mixin::WindowsArchitectureHelper
#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory
Methods included from DSL::Secret
#default_secret_config, #default_secret_service, #secret, #with_secret_config, #with_secret_service
Methods included from DSL::RenderHelpers
#render_json, #render_toml, #render_yaml
Methods included from DSL::ReaderHelpers
#parse_file, #parse_json, #parse_toml, #parse_yaml
Methods included from DSL::Powershell
Methods included from DSL::RegistryHelper
#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?
Methods included from DSL::ChefVault
#chef_vault, #chef_vault_item, #chef_vault_item_for_environment
Methods included from DSL::DataQuery
#data_bag, #data_bag_item, #search, #tagged?
Methods included from EncryptedDataBagItem::CheckEncrypted
Methods included from DSL::PlatformIntrospection
#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family
Methods included from DSL::Recipe
#exec, #have_resource_class_for?, #resource_class_for
Methods included from DSL::Definitions
add_definition, #evaluate_resource_definition, #has_resource_definition?
Methods included from DSL::Resources
add_resource_dsl, remove_resource_dsl
Methods included from DSL::Cheffish
Methods included from DSL::RebootPending
Methods included from DSL::IncludeRecipe
Methods included from Mixin::NotifyingBlock
#notifying_block, #subcontext_block
Methods included from DSL::DeclareResource
#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #resources, #with_run_context
Methods included from DSL::Compliance
#include_input, #include_profile, #include_waiver
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 with a 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 occurred.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/chef/provider/service/upstart.rb', line 47 def initialize(new_resource, run_context) # TODO: re-evaluate if this is needed after integrating cookbook fix raise ArgumentError, "run_context cannot be nil" unless run_context super run_context.node # dup so we can mutate @job @job = @new_resource.service_name.dup if @new_resource.parameters @new_resource.parameters.each do |key, value| @job << " #{key}=#{value}" end end @upstart_job_dir = "/etc/init" @upstart_conf_suffix = ".conf" @command_success = true # new_resource.status_command= false, means upstart used @config_file_found = true @upstart_command_success = true end |
Instance Attribute Details
#upstart_service_running ⇒ Object
to maintain a local state of service across restart’s internal calls
29 30 31 |
# File 'lib/chef/provider/service/upstart.rb', line 29 def upstart_service_running @upstart_service_running end |
Class Method Details
.supports?(resource, action) ⇒ Boolean
Returns true if the configs for the service name has upstart variable
34 35 36 |
# File 'lib/chef/provider/service/upstart.rb', line 34 def self.supports?(resource, action) service_script_exist?(:upstart, resource.service_name) end |
Instance Method Details
#define_resource_requirements ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chef/provider/service/upstart.rb', line 71 def define_resource_requirements # Do not call super, only call shared requirements shared_resource_requirements requirements.assert(:all_actions) do |a| unless @command_success whyrun_msg = if @new_resource.status_command "Provided status command #{@new_resource.status_command} failed." else "Could not determine upstart state for service" end end a.assertion { @command_success } # no failure here, just document the assumptions made. a.whyrun "#{whyrun_msg} Assuming service installed and not running." end requirements.assert(:all_actions) do |a| a.assertion { @config_file_found } # no failure here, just document the assumptions made. a.whyrun "Could not find #{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}. Assuming service is disabled." end end |
#disable_service ⇒ Object
225 226 227 228 229 230 |
# File 'lib/chef/provider/service/upstart.rb', line 225 def disable_service logger.trace("#{@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
218 219 220 221 222 223 |
# File 'lib/chef/provider/service/upstart.rb', line 218 def enable_service logger.trace("#{@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
94 95 96 97 98 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 |
# File 'lib/chef/provider/service/upstart.rb', line 94 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 logger.trace("#{@new_resource} you have specified a status command, running..") begin if shell_out!(@new_resource.status_command).exitstatus == 0 @upstart_service_running = true end rescue @command_success = false @upstart_service_running = false nil end else begin if upstart_goal_state == "start" @upstart_service_running = true else @upstart_service_running = false end rescue Chef::Exceptions::Exec @command_success = false @upstart_service_running = false nil end end # Get enabled/disabled state by reading job configuration file if ::File.exist?("#{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}") logger.trace("#{@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/ logger.trace("#{@new_resource} enabled: #{line.chomp}") @current_resource.enabled true break when /^#start on/ logger.trace("#{@new_resource} disabled: #{line.chomp}") @current_resource.enabled false break end end end else @config_file_found = false logger.trace("#{@new_resource} did not find #{@upstart_job_dir}/#{@new_resource.service_name}#{@upstart_conf_suffix}") @current_resource.enabled false end @current_resource.running @upstart_service_running @current_resource end |
#reload_service ⇒ Object
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/chef/provider/service/upstart.rb', line 205 def reload_service if @new_resource.reload_command super else # upstart >= 0.6.3-4 supports reload (HUP) shell_out!("/sbin/reload #{@job}", default_env: false) end @upstart_service_running = true end |
#restart_service ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/chef/provider/service/upstart.rb', line 185 def restart_service if @new_resource.restart_command super # Upstart always provides restart functionality so we don't need to mimic it with stop/sleep/start. # Older versions of upstart would fail on restart if the service was currently stopped, check for that. LP:430883 # But for safe working of latest upstart job config being loaded, 'restart' can't be used as per link # http://upstart.ubuntu.com/cookbook/#restart (it doesn't uses latest jon config from disk but retains old) else if @upstart_service_running stop_service sleep 1 start_service else start_service end end @upstart_service_running = true end |
#start_service ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/chef/provider/service/upstart.rb', line 153 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 @upstart_service_running logger.trace("#{@new_resource} already running, not starting") else if @new_resource.start_command super else shell_out!("/sbin/start #{@job}", default_env: false) end end @upstart_service_running = true end |
#stop_service ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/chef/provider/service/upstart.rb', line 169 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 @upstart_service_running logger.trace("#{@new_resource} not running, not stopping") else if @new_resource.stop_command super else shell_out!("/sbin/stop #{@job}", default_env: false) end end @upstart_service_running = false end |
#upstart_goal_state ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/chef/provider/service/upstart.rb', line 232 def upstart_goal_state command = "/sbin/status #{@job}" so = shell_out(command) so.stdout.each_line do |line| # service goal/state # OR # service (instance) goal/state # OR # service (goal) state line =~ UPSTART_STATE_FORMAT data = Regexp.last_match return data[1] end end |