Class: Chef::Resource::HabitatService

Inherits:
Chef::Resource show all
Defined in:
lib/chef/resource/habitat_service.rb

Constant Summary

Constants inherited from Chef::Resource

FORBIDDEN_IVARS, HIDDEN_IVARS

Instance Attribute Summary

Attributes inherited from Chef::Resource

#allowed_actions, #cookbook_name, #declared_type, #default_guard_interpreter, #elapsed_time, #enclosing_provider, #executed_by_runner, #logger, #params, #recipe_name, #resource_initializing, #run_context, #source_line, #updated

Instance Method Summary collapse

Methods inherited from Chef::Resource

#action, action, #action=, action_class, #action_description, #after_created, allowed_actions, allowed_actions=, #as_json, #before_notifications, chef_version_for_provides, #compile_time, #cookbook_version, #current_value, #current_value_does_not_exist!, #custom_exception_message, custom_resource?, #customize_exception, declare_action_class, #declared_key, default_action, default_action=, default_description, #defined_at, #delayed_action, #delayed_notifications, deprecated, description, #events, examples, from_hash, from_json, #guard_interpreter, #identity, identity_attr, identity_property, #ignore_failure, #immediate_notifications, inherited, #initialize, #inspect, introduced, is_custom_resource!, json_create, load_current_value, #load_from, #lookup_provider_constant, #method_missing, #name, #node, #not_if, #notifies, #notifies_before, #notifies_delayed, #notifies_immediately, #only_if, preview_resource, #provider, #provider=, #provider_for_action, provides, provides?, #resolve_notification_references, resource_for_node, resource_matching_short_name, resource_name, #resource_name, resource_name=, #retries, #retry_delay, #run_action, #sensitive, #should_skip?, skip_docs, sorted_descendants, #source_line_file, #source_line_number, state_attrs, #state_for_resource_reporter, #subscribes, #suppress_up_to_date_messages?, #to_h, #to_json, #to_s, #to_text, #umask, unified_mode, #updated?, #updated_by_last_action, #updated_by_last_action?, use, #validate_action, #validate_resource_spec!, #value_to_text, #with_umask

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 Mixin::PowershellExec

#powershell_exec, #powershell_exec!

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

#ps_credential

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

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Methods included from Mixin::Deprecation

#deprecated_attr, #deprecated_attr_reader, #deprecated_attr_writer, #deprecated_ivar

Methods included from Mixin::Properties

#copy_properties_from, included, #property_description, #property_is_set?, #reset_property

Methods included from Mixin::ParamsValidate

#lazy, #set_or_return, #validate

Methods included from DSL::RebootPending

#reboot_pending?

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

Constructor Details

This class inherits a constructor from Chef::Resource

Dynamic Method Handling

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

Instance Method Details

#get_binding_mode(service_details) ⇒ Object



270
271
272
273
274
275
# File 'lib/chef/resource/habitat_service.rb', line 270

def get_binding_mode(service_details)
  service_details["binding_mode"].to_sym
rescue
  Chef::Log.debug("Binding mode for #{service_name} not found on Supervisor API")
  "strict"
end

#get_binds(service_details) ⇒ Object



263
264
265
266
267
268
# File 'lib/chef/resource/habitat_service.rb', line 263

def get_binds(service_details)
  service_details["binds"]
rescue
  Chef::Log.debug("Update Strategy for #{service_name} not found on Supervisor API")
  []
end

#get_builder_url(service_details) ⇒ Object



249
250
251
252
253
254
# File 'lib/chef/resource/habitat_service.rb', line 249

def get_builder_url(service_details)
  service_details["bldr_url"]
rescue
  Chef::Log.debug("Habitat Builder URL for #{service_name} not found on Supervisor API")
  "https://bldr.habitat.sh"
end

#get_channel(service_details) ⇒ Object



256
257
258
259
260
261
# File 'lib/chef/resource/habitat_service.rb', line 256

def get_channel(service_details)
  service_details["channel"].to_sym
rescue
  Chef::Log.debug("Channel for #{service_name} not found on Supervisor API")
  "stable"
end

#get_health_check_interval(service_details) ⇒ Object



291
292
293
294
295
296
# File 'lib/chef/resource/habitat_service.rb', line 291

def get_health_check_interval(service_details)
  service_details["health_check_interval"]["secs"]
rescue
  Chef::Log.debug("Health Check Interval for #{service_name} not found on Supervisor API")
  30
end

#get_service_details(svc_name) ⇒ Object

This method is defined here otherwise it isn’t usable in the ‘load_current_value` method.

It performs a check with TCPSocket to ensure that the HTTP API is available first. If it cannot connect, it assumes that the service is not running. It then attempts to reach the ‘/services` path of the API to get a list of services. If this fails for some reason, then it assumes the service is not running.

Finally, it walks the services returned by the API to look for the service we’re configuring. If it is “Up”, then we know the service is running and fully operational according to Habitat. This is wrapped in a begin/rescue block because if the service isn’t present and ‘sup_for_service_name` will be nil and we will get a NoMethodError.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/chef/resource/habitat_service.rb', line 181

def get_service_details(svc_name)
  http_uri = "http://#{remote_sup_http}"

  begin
    TCPSocket.new(URI(http_uri).host, URI(http_uri).port).close
  rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
    Chef::Log.debug("Could not connect to #{http_uri} to retrieve status for #{service_name}")
    return false
  end

  begin
    headers = {}
    headers["Authorization"] = "Bearer #{gateway_auth_token}" if property_is_set?(:gateway_auth_token)
    svcs = Chef::HTTP::SimpleJSON.new(http_uri).get("/services", headers)
  rescue
    Chef::Log.debug("Could not connect to #{http_uri}/services to retrieve status for #{service_name}")
    return false
  end

  origin, name, _version, _release = svc_name.split("/")
  svcs.find do |s|
    s["pkg"]["origin"] == origin && s["pkg"]["name"] == name
  end
end

#get_service_group(service_details) ⇒ Object



277
278
279
280
281
282
# File 'lib/chef/resource/habitat_service.rb', line 277

def get_service_group(service_details)
  service_details["service_group"].split(".").last
rescue
  Chef::Log.debug("Service Group for #{service_name} not found on Supervisor API")
  "default"
end

#get_shutdown_timeout(service_details) ⇒ Object



284
285
286
287
288
289
# File 'lib/chef/resource/habitat_service.rb', line 284

def get_shutdown_timeout(service_details)
  service_details["pkg"]["shutdown_timeout"]
rescue
  Chef::Log.debug("Shutdown Timeout for #{service_name} not found on Supervisor API")
  8
end

#get_spec_identifier(service_details) ⇒ Object



221
222
223
224
225
226
# File 'lib/chef/resource/habitat_service.rb', line 221

def get_spec_identifier(service_details)
  service_details["spec_ident"]["spec_identifier"]
rescue
  Chef::Log.debug("#{service_name} not found on the Habitat supervisor")
  nil
end

#get_topology(service_details) ⇒ Object



242
243
244
245
246
247
# File 'lib/chef/resource/habitat_service.rb', line 242

def get_topology(service_details)
  service_details["topology"].to_sym
rescue
  Chef::Log.debug("Topology for #{service_name} not found on Supervisor API")
  "standalone"
end

#get_update_condition(service_details) ⇒ Object



235
236
237
238
239
240
# File 'lib/chef/resource/habitat_service.rb', line 235

def get_update_condition(service_details)
  service_details["update_condition"].to_sym
rescue
  Chef::Log.debug("Update condition #{service_name} not found on Supervisor API")
  "latest"
end

#get_update_strategy(service_details) ⇒ Object



228
229
230
231
232
233
# File 'lib/chef/resource/habitat_service.rb', line 228

def get_update_strategy(service_details)
  service_details["update_strategy"].to_sym
rescue
  Chef::Log.debug("Update Strategy for #{service_name} not found on Supervisor API")
  "none"
end

#service_loaded?(service_details) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
218
219
# File 'lib/chef/resource/habitat_service.rb', line 213

def service_loaded?(service_details)
  if service_details
    true
  else
    false
  end
end

#service_up?(service_details) ⇒ Boolean

Returns:

  • (Boolean)


206
207
208
209
210
211
# File 'lib/chef/resource/habitat_service.rb', line 206

def service_up?(service_details)
  service_details["process"]["state"] == "up"
rescue
  Chef::Log.debug("#{service_name} not found on the Habitat supervisor")
  false
end