class PoiseApplication::Resources::Application::Resource
An `application` resource to manage application deployment.
@since 5.0.0 @provides application @action deploy @action start @action stop @action restart @action reload @example
application '/srv/myapp' do git '...' poise_service 'myapp' do command '/srv/myapp/main' end end
Public Class Methods
Run the DSL rewire when the resource object is created. @api private
# File lib/poise_application/resources/application.rb, line 78 def initialize(*args) super _rewire_dsl! if node end
Public Instance Methods
Application-specific state values used as a way to communicate between subresources.
@return [Mash] @example
if new_resource.parent && new_resource.parent.app_state['gemfile_path']
# File lib/poise_application/resources/application.rb, line 89 def app_state @app_state ||= Mash.new(environment: environment) end
Override Container#register_subresource to add our action_on_update.
@api private
# File lib/poise_application/resources/application.rb, line 96 def register_subresource(resource) super.tap do |added| if added && action_on_update Chef::Log.debug("[#{self}] Registering #{action_on_update_immediately ? 'immediate ' : ''}#{action_on_update} notification from #{resource}") resource.notifies action_on_update.to_sym, self, (action_on_update_immediately ? :immediately : :delayed) end end end
Private Instance Methods
Find all cookbooks that might contain LWRPs matching our name scheme.
@return [Array<String>]
# File lib/poise_application/resources/application.rb, line 134 def _rewire_cookbooks # Run context might be unset during test setup. if run_context run_context.cookbook_collection.keys.select {|cookbook_name| cookbook_name.start_with?('application_') } else [] end end
Build new DSL methods to implement the foo -> application_foo behavior.
@return [void]
# File lib/poise_application/resources/application.rb, line 167 def _rewire_dsl! # Generate stub methods for all the rewiring. _rewire_map.each do |new_name, old_name| # This is defined as a singleton method on self so it looks like # the DSL but is scoped to just this context. define_singleton_method(new_name) do |name=nil, *args, &block| # Store the caller to correct the source_line. created_at = caller[0] public_send(old_name, name, *args) do # Set the declared type to be the native name. self.declared_type = self.class.resource_name # Fix the source location. For Chef 12.4 we could do this with the # declared_at parameter on the initial send. self.source_line = created_at # Run the original block. instance_exec(&block) if block end end end end
Build the mapping of new_name => old_name for each resource to rewire.
@return [Hash<String, String>]
# File lib/poise_application/resources/application.rb, line 146 def _rewire_map application_cookbooks = _rewire_cookbooks _rewire_resources.inject({}) do |memo, name| # Grab the resource class to check if it is an LWRP. klass = Chef::Resource.resource_for_node(name.to_sym, node) # Find the part to trim. Check for LWRP first, then just application_. trim = if klass < Chef::Resource::LWRPBase application_cookbooks.find {|cookbook_name| name.start_with?(cookbook_name) && name != cookbook_name } || 'application' else # Non-LWRPs are assumed to have a better name. 'application' end # Map trimmed to untrimmed. memo[name[trim.length+1..-1]] = name memo end end
Find all resources that need to be rewired. This is anything with a name starting with application_.
@return [Array<String>]
# File lib/poise_application/resources/application.rb, line 111 def _rewire_resources if defined?(Chef::DSL::Resources) # Chef >= 12.4. Chef::DSL::Resources.instance_methods else # Chef < 12.4 >= 12.0. Chef::Resource.descendants.map do |klass| klass.node_map.instance_variable_get(:@map).keys + if klass.dsl_name.include?('::') # Probably not valid. # :nocov: [] # :nocov: else # Needed for things that don't call provides(). [klass.dsl_name] end end.flatten end.map {|name| name.to_s }.select {|name| name.start_with?('application_') }.uniq end