default.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #
  2. # Author:: Noah Kantrowitz <noah@opscode.com>
  3. # Cookbook Name:: application
  4. # Resource:: default
  5. #
  6. # Copyright:: 2011-2012, Opscode, Inc <legal@opscode.com>
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. require 'weakref'
  21. include Chef::Mixin::RecipeDefinitionDSLCore
  22. def initialize(*args)
  23. super
  24. @action = :deploy
  25. @sub_resources = []
  26. end
  27. actions :deploy, :force_deploy
  28. attribute :name, :kind_of => String, :name_attribute => true
  29. attribute :environment_name, :kind_of => String, :default => (node.chef_environment =~ /_default/ ? "production" : node.chef_environment)
  30. attribute :path, :kind_of => String
  31. attribute :owner, :kind_of => String
  32. attribute :group, :kind_of => String
  33. attribute :strategy, :kind_of => [String, Symbol], :default => :deploy_revision
  34. attribute :scm_provider, :kind_of => [Class, String, Symbol]
  35. attribute :revision, :kind_of => String
  36. attribute :repository, :kind_of => String
  37. attribute :enable_submodules, :kind_of => [TrueClass, FalseClass], :default => false
  38. attribute :environment, :kind_of => Hash, :default => {}
  39. attribute :deploy_key, :kind_of => [String, NilClass], :default => nil
  40. attribute :shallow_clone, :kind_of => [TrueClass, FalseClass], :default => true
  41. attribute :force, :kind_of => [TrueClass, FalseClass], :default => false
  42. attribute :rollback_on_error, :kind_of => [TrueClass, FalseClass], :default => true
  43. attribute :purge_before_symlink, :kind_of => Array, :default => []
  44. attribute :create_dirs_before_symlink, :kind_of => Array, :default => []
  45. attribute :symlinks, :kind_of => Hash, :default => {}
  46. attribute :symlink_before_migrate, :kind_of => Hash, :default => {}
  47. attribute :migrate, :kind_of => [TrueClass, FalseClass], :default => false
  48. attribute :migration_command, :kind_of => [String, NilClass], :default => nil
  49. attribute :packages, :kind_of => [Array, Hash], :default => []
  50. attribute :application_provider
  51. attr_reader :sub_resources
  52. def restart_command(arg=nil, &block)
  53. arg ||= block
  54. set_or_return(:restart_command, arg, :kind_of => [Proc, String])
  55. end
  56. # Callback fires before deploy is started.
  57. def before_deploy(arg=nil, &block)
  58. arg ||= block
  59. set_or_return(:before_deploy, arg, :kind_of => [Proc, String])
  60. end
  61. # Callback fires before migration is run.
  62. def before_migrate(arg=nil, &block)
  63. arg ||= block
  64. set_or_return(:before_migrate, arg, :kind_of => [Proc, String])
  65. end
  66. # Callback fires before symlinking
  67. def before_symlink(arg=nil, &block)
  68. arg ||= block
  69. set_or_return(:before_symlink, arg, :kind_of => [Proc, String])
  70. end
  71. # Callback fires before restart
  72. def before_restart(arg=nil, &block)
  73. arg ||= block
  74. set_or_return(:before_restart, arg, :kind_of => [Proc, String])
  75. end
  76. # Callback fires after restart
  77. def after_restart(arg=nil, &block)
  78. arg ||= block
  79. set_or_return(:after_restart, arg, :kind_of => [Proc, String])
  80. end
  81. def release_path
  82. application_provider.release_path
  83. end
  84. def shared_path
  85. application_provider.shared_path
  86. end
  87. def method_missing(name, *args, &block)
  88. # Build the set of names to check for a valid resource
  89. lookup_path = ["application_#{name}"]
  90. run_context.cookbook_collection.each do |cookbook_name, cookbook_ver|
  91. if cookbook_name.start_with?("application_")
  92. lookup_path << "#{cookbook_name}_#{name}"
  93. end
  94. end
  95. lookup_path << name
  96. resource = nil
  97. # Try to find our resource
  98. lookup_path.each do |resource_name|
  99. begin
  100. Chef::Log.debug "Trying to load application resource #{resource_name} for #{name}"
  101. resource = super(resource_name.to_sym, self.name, &block)
  102. break
  103. rescue NameError => e
  104. # Works on any MRI ruby
  105. if e.name == resource_name.to_sym || e.inspect =~ /\b#{resource_name}\b/
  106. next
  107. else
  108. raise e
  109. end
  110. end
  111. end
  112. raise NameError, "No resource found for #{name}. Tried #{lookup_path.join(', ')}" unless resource
  113. # Enforce action :nothing in case people forget
  114. resource.action :nothing
  115. # Make this a weakref to prevent a cycle between the application resource and the sub resources
  116. resource.application WeakRef.new(self)
  117. resource.type name
  118. @sub_resources << resource
  119. resource
  120. end
  121. def do_i_respond_to?(*args)
  122. name = args.first.to_s
  123. # Build the set of names to check for a valid resource
  124. lookup_path = ["application_#{name}"]
  125. run_context.cookbook_collection.each do |cookbook_name, cookbook_ver|
  126. if cookbook_name.start_with?("application_")
  127. lookup_path << "#{cookbook_name}_#{name}"
  128. end
  129. end
  130. lookup_path << name
  131. found = false
  132. # Try to find our resource
  133. lookup_path.each do |resource_name|
  134. begin
  135. Chef::Log.debug "Looking for application resource #{resource_name} for #{name}"
  136. Chef::Resource.resource_for_node(resource_name.to_sym, node)
  137. found = true
  138. break
  139. rescue NameError => e
  140. # Keep calm and carry on
  141. end
  142. end
  143. found
  144. end
  145. # If we are using a current version of ruby, use respond_to_missing?
  146. # instead of respond_to? so we provide proper behavior
  147. if(Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('1.9.1'))
  148. def respond_to_missing?(*args)
  149. super || do_i_respond_to?(*args)
  150. end
  151. else
  152. def respond_to?(*args)
  153. super || do_i_respond_to?(*args)
  154. end
  155. end
  156. def to_ary
  157. nil
  158. end
  159. alias :to_a :to_ary