default.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #
  2. # Author:: Noah Kantrowitz <noah@opscode.com>
  3. # Cookbook Name:: application
  4. # Library:: 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. class Chef
  21. class Resource
  22. # Globally update the blocklists to prevent infinite recursion in #to_json and similar
  23. FORBIDDEN_IVARS.concat [:@application, :@application_provider]
  24. HIDDEN_IVARS.concat [:@application, :@application_provider]
  25. class Application
  26. module OptionsCollector
  27. def options
  28. @options ||= {}
  29. end
  30. def method_missing(method_sym, value=nil, &block)
  31. super
  32. rescue NameError
  33. value ||= block
  34. method_sym = method_sym.to_s.chomp('=').to_sym
  35. options[method_sym] = value if value
  36. options[method_sym] ||= nil
  37. end
  38. end
  39. end
  40. module ApplicationBase
  41. def self.included(klass)
  42. klass.actions :before_compile, :before_deploy, :before_migrate, :before_symlink, :before_restart, :after_restart
  43. klass.attribute :id, :kind_of => String, :name_attribute => true
  44. klass.attribute :environment, :kind_of => Hash, :default => {}
  45. klass.attribute :purge_before_symlink, :kind_of => Array, :default => []
  46. klass.attribute :create_dirs_before_symlink, :kind_of => Array, :default => []
  47. klass.attribute :symlinks, :kind_of => Hash, :default => {}
  48. klass.attribute :symlink_before_migrate, :kind_of => Hash, :default => {}
  49. klass.attribute :migration_command, :kind_of => [String, NilClass], :default => nil
  50. klass.attribute :application
  51. klass.attribute :application_provider
  52. klass.attribute :type
  53. end
  54. def restart_command(arg=nil, &block)
  55. arg ||= block
  56. raise "Invalid restart command" unless !arg || arg.is_a?(String) || arg.is_a?(Proc)
  57. @restart_command = arg if arg
  58. @restart_command
  59. end
  60. def method_missing(name, *args)
  61. if application.respond_to? name
  62. application.send(name, *args)
  63. else
  64. super
  65. end
  66. end
  67. class OptionsBlock
  68. include Chef::Resource::Application::OptionsCollector
  69. end
  70. def options_block(options=nil, &block)
  71. options ||= {}
  72. if block
  73. collector = OptionsBlock.new
  74. collector.instance_eval(&block)
  75. options.update(collector.options)
  76. end
  77. options
  78. end
  79. def find_matching_role(role, single=true, &block)
  80. return nil if !role
  81. nodes = []
  82. if node['roles'].include? role
  83. nodes << node
  84. end
  85. if !single || nodes.empty?
  86. search(:node, "role:#{role} AND chef_environment:#{node.chef_environment}") do |n|
  87. nodes << n
  88. end
  89. end
  90. if block
  91. nodes.each do |n|
  92. yield n
  93. end
  94. else
  95. if single
  96. nodes.first
  97. else
  98. nodes
  99. end
  100. end
  101. end
  102. def find_database_server(role)
  103. dbm = find_matching_role(role)
  104. Chef::Log.warn("No node with role #{role}") if role && !dbm
  105. if respond_to?(:database) && database.has_key?('host')
  106. database['host']
  107. elsif dbm && dbm.attribute?('cloud')
  108. dbm['cloud']['local_ipv4']
  109. elsif dbm
  110. dbm['ipaddress']
  111. end
  112. end
  113. end
  114. end
  115. class Provider
  116. module ApplicationBase
  117. def self.included(klass)
  118. klass.extend Chef::Mixin::FromFile
  119. end
  120. def deploy_provider
  121. @deploy_provider ||= begin
  122. deploy_provider = Chef::Platform.provider_for_resource(@deploy_resource)
  123. deploy_provider.load_current_resource
  124. deploy_provider
  125. end
  126. end
  127. def release_path
  128. deploy_provider.release_path
  129. end
  130. def shared_path
  131. @deploy_resource.shared_path
  132. end
  133. def callback(what, callback_code=nil)
  134. Chef::Log.debug("Got callback #{what}: #{callback_code.inspect}")
  135. @collection = Chef::ResourceCollection.new
  136. case callback_code
  137. when Proc
  138. Chef::Log.info "#{@new_resource} running callback #{what}"
  139. recipe_eval(&callback_code)
  140. when String
  141. callback_file = "#{release_path}/#{callback_code}"
  142. unless ::File.exist?(callback_file)
  143. raise RuntimeError, "Can't find your callback file #{callback_file}"
  144. end
  145. run_callback_from_file(callback_file)
  146. when nil
  147. nil
  148. else
  149. raise RuntimeError, "You gave me a callback I don't know what to do with: #{callback_code.inspect}"
  150. end
  151. end
  152. def run_callback_from_file(callback_file)
  153. if ::File.exist?(callback_file)
  154. Dir.chdir(release_path) do
  155. Chef::Log.info "#{@new_resource} running deploy hook #{callback_file}"
  156. recipe_eval { from_file(callback_file) }
  157. end
  158. end
  159. end
  160. end
  161. end
  162. end