default.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 :force, :kind_of => [TrueClass, FalseClass], :default => false
  41. attribute :rollback_on_error, :kind_of => [TrueClass, FalseClass], :default => true
  42. attribute :purge_before_symlink, :kind_of => Array, :default => []
  43. attribute :create_dirs_before_symlink, :kind_of => Array, :default => []
  44. attribute :symlinks, :kind_of => Hash, :default => {}
  45. attribute :symlink_before_migrate, :kind_of => Hash, :default => {}
  46. attribute :migrate, :kind_of => [TrueClass, FalseClass], :default => false
  47. attribute :migration_command, :kind_of => [String, NilClass], :default => nil
  48. attribute :restart_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. # Callback fires before deploy is started.
  53. def before_deploy(arg=nil, &block)
  54. arg ||= block
  55. set_or_return(:before_deploy, arg, :kind_of => [Proc, String])
  56. end
  57. # Callback fires before migration is run.
  58. def before_migrate(arg=nil, &block)
  59. arg ||= block
  60. set_or_return(:before_migrate, arg, :kind_of => [Proc, String])
  61. end
  62. # Callback fires before symlinking
  63. def before_symlink(arg=nil, &block)
  64. arg ||= block
  65. set_or_return(:before_symlink, arg, :kind_of => [Proc, String])
  66. end
  67. # Callback fires before restart
  68. def before_restart(arg=nil, &block)
  69. arg ||= block
  70. set_or_return(:before_restart, arg, :kind_of => [Proc, String])
  71. end
  72. # Callback fires after restart
  73. def after_restart(arg=nil, &block)
  74. arg ||= block
  75. set_or_return(:after_restart, arg, :kind_of => [Proc, String])
  76. end
  77. def release_path
  78. application_provider.release_path
  79. end
  80. def shared_path
  81. application_provider.shared_path
  82. end
  83. def method_missing(name, *args, &block)
  84. # Build the set of names to check for a valid resource
  85. lookup_path = ["application_#{name}"]
  86. run_context.cookbook_collection.each do |cookbook_name, cookbook_ver|
  87. if cookbook_name.start_with?("application_")
  88. lookup_path << "#{cookbook_name}_#{name}"
  89. end
  90. end
  91. lookup_path << name
  92. resource = nil
  93. # Try to find our resource
  94. lookup_path.each do |resource_name|
  95. begin
  96. Chef::Log.debug "Trying to load application resource #{resource_name} for #{name}"
  97. resource = super(resource_name.to_sym, self.name, &block)
  98. break
  99. rescue NameError => e
  100. # Works on any MRI ruby
  101. if e.name == resource_name.to_sym || e.inspect =~ /\b#{resource_name}\b/
  102. next
  103. else
  104. raise e
  105. end
  106. end
  107. end
  108. raise NameError, "No resource found for #{name}. Tried #{lookup_path.join(', ')}" unless resource
  109. # Enforce action :nothing in case people forget
  110. resource.action :nothing
  111. # Make this a weakref to prevent a cycle between the application resource and the sub resources
  112. resource.application WeakRef.new(self)
  113. resource.type name
  114. @sub_resources << resource
  115. resource
  116. end
  117. def to_ary
  118. nil
  119. end
  120. alias :to_a :to_ary