default.rb 4.2 KB

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