default.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #
  2. # Author:: Noah Kantrowitz <noah@opscode.com>
  3. # Cookbook Name:: application
  4. # Library:: default
  5. #
  6. # Copyright:: 2011, 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 += [:@application, :@application_provider]
  24. HIDDEN_IVARS += [:@application, :@application_provider]
  25. module ApplicationBase
  26. def self.included(klass)
  27. klass.actions :before_compile, :before_deploy, :before_migrate, :before_symlink, :before_restart, :after_restart
  28. klass.attribute :id, :kind_of => String, :name_attribute => true
  29. klass.attribute :environment, :kind_of => Hash, :default => {}
  30. klass.attribute :purge_before_symlink, :kind_of => Array, :default => []
  31. klass.attribute :create_dirs_before_symlink, :kind_of => Array, :default => []
  32. klass.attribute :symlinks, :kind_of => Hash, :default => {}
  33. klass.attribute :symlink_before_migrate, :kind_of => Hash, :default => {}
  34. klass.attribute :migration_command, :kind_of => [String, NilClass], :default => 'rake db:migrate'
  35. klass.attribute :restart_command, :kind_of => [String, NilClass], :default => nil
  36. klass.attribute :application
  37. klass.attribute :application_provider
  38. end
  39. def method_missing(name, *args)
  40. if application.respond_to? name
  41. application.send(name, *args)
  42. else
  43. super
  44. end
  45. end
  46. def release_path
  47. application_provider.release_path
  48. end
  49. end
  50. end
  51. class Provider
  52. module ApplicationBase
  53. def self.included(klass)
  54. klass.extend Chef::Mixin::FromFile
  55. end
  56. def release_path
  57. if !@deploy_provider
  58. #@deploy_provider = Chef::Platform.provider_for_resource(@run_context.resource_collection.find(:deploy_revision => @new_resource.id))
  59. @deploy_provider = Chef::Platform.provider_for_resource(@deploy_resource)
  60. @deploy_provider.load_current_resource
  61. end
  62. @deploy_provider.release_path
  63. end
  64. def callback(what, callback_code=nil)
  65. Chef::Log.debug("Got callback #{what}: #{callback_code.inspect}")
  66. @collection = Chef::ResourceCollection.new
  67. case callback_code
  68. when Proc
  69. Chef::Log.info "#{@new_resource} running callback #{what}"
  70. recipe_eval(&callback_code)
  71. when String
  72. callback_file = "#{release_path}/#{callback_code}"
  73. unless ::File.exist?(callback_file)
  74. raise RuntimeError, "Can't find your callback file #{callback_file}"
  75. end
  76. run_callback_from_file(callback_file)
  77. when nil
  78. nil
  79. else
  80. raise RuntimeError, "You gave me a callback I don't know what to do with: #{callback_code.inspect}"
  81. end
  82. end
  83. def run_callback_from_file(callback_file)
  84. if ::File.exist?(callback_file)
  85. Dir.chdir(release_path) do
  86. Chef::Log.info "#{@new_resource} running deploy hook #{callback_file}"
  87. recipe_eval { from_file(callback_file) }
  88. end
  89. end
  90. end
  91. end
  92. end
  93. end