default.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #
  2. # Author:: Noah Kantrowitz <noah@opscode.com>
  3. # Cookbook Name:: application
  4. # Provider:: 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. include Chef::Provider::ApplicationBase
  21. action :deploy do
  22. before_compile
  23. before_deploy
  24. run_deploy
  25. end
  26. action :force_deploy do
  27. before_compile
  28. before_deploy
  29. run_deploy(true)
  30. end
  31. action :restart do
  32. before_compile
  33. run_actions_with_context(:before_restart, @run_context)
  34. run_restart
  35. run_actions_with_context(:after_restart, @run_context)
  36. @new_resource.updated_by_last_action(true)
  37. end
  38. protected
  39. def before_compile
  40. new_resource.application_provider self
  41. new_resource.sub_resources.each do |resource|
  42. resource.application_provider self
  43. resource.run_action :before_compile
  44. end
  45. end
  46. def before_deploy
  47. new_resource.packages.each do |pkg,ver|
  48. package pkg do
  49. action :install
  50. version ver if ver && ver.length > 0
  51. end
  52. end
  53. directory new_resource.path do
  54. owner new_resource.owner
  55. group new_resource.group
  56. mode '0755'
  57. recursive true
  58. end
  59. directory "#{new_resource.path}/shared" do
  60. owner new_resource.owner
  61. group new_resource.group
  62. mode '0755'
  63. recursive true
  64. end
  65. if new_resource.deploy_key
  66. file "#{new_resource.path}/id_deploy" do
  67. content new_resource.deploy_key
  68. owner new_resource.owner
  69. group new_resource.group
  70. mode '0600'
  71. end
  72. template "#{new_resource.path}/deploy-ssh-wrapper" do
  73. source "deploy-ssh-wrapper.erb"
  74. cookbook "application"
  75. owner new_resource.owner
  76. group new_resource.group
  77. mode "0755"
  78. variables :id => new_resource.name, :deploy_to => new_resource.path
  79. end
  80. end
  81. ruby_block "#{new_resource.name} before_deploy" do
  82. block do
  83. new_resource.sub_resources.each do |resource|
  84. resource.run_action :before_deploy
  85. end
  86. callback(:before_deploy, new_resource.before_deploy)
  87. end
  88. end
  89. end
  90. def run_deploy(force = false)
  91. # Alias to a variable so I can use in sub-resources
  92. new_resource = @new_resource
  93. app_provider = self
  94. @deploy_resource = send(new_resource.strategy.to_sym, new_resource.name) do
  95. action force ? :force_deploy : :deploy
  96. scm_provider new_resource.scm_provider
  97. revision new_resource.revision
  98. repository new_resource.repository
  99. enable_submodules new_resource.enable_submodules
  100. user new_resource.owner
  101. group new_resource.group
  102. deploy_to new_resource.path
  103. ssh_wrapper "#{new_resource.path}/deploy-ssh-wrapper" if new_resource.deploy_key
  104. shallow_clone true
  105. rollback_on_error new_resource.rollback_on_error
  106. all_environments = ([new_resource.environment]+new_resource.sub_resources.map{|res| res.environment}).inject({}){|acc, val| acc.merge(val)}
  107. environment all_environments
  108. migrate new_resource.migrate
  109. all_migration_commands = ([new_resource.migration_command]+new_resource.sub_resources.map{|res| res.migration_command}).select{|cmd| cmd && !cmd.empty?}
  110. migration_command all_migration_commands.join(' && ')
  111. restart_command do
  112. ([new_resource]+new_resource.sub_resources).each do |res|
  113. cmd = res.restart_command
  114. if cmd.is_a? Proc
  115. version = Chef::Version.new(Chef::VERSION)
  116. provider = if version.major > 10 || version.minor >= 14
  117. Chef::Platform.provider_for_resource(res, :nothing)
  118. else
  119. Chef::Platform.provider_for_resource(res)
  120. end
  121. provider.load_current_resource
  122. provider.instance_eval(&cmd)
  123. elsif cmd && !cmd.empty?
  124. execute cmd do
  125. user new_resource.owner
  126. group new_resource.group
  127. environment all_environments
  128. end
  129. end
  130. end
  131. end
  132. purge_before_symlink new_resource.purge_before_symlink
  133. create_dirs_before_symlink new_resource.create_dirs_before_symlink
  134. symlinks new_resource.symlinks
  135. all_symlinks_before_migrate = [new_resource.symlink_before_migrate]+new_resource.sub_resources.map{|res| res.symlink_before_migrate}
  136. symlink_before_migrate all_symlinks_before_migrate.inject({}){|acc, val| acc.merge(val)}
  137. before_migrate do
  138. app_provider.send(:run_actions_with_context, :before_migrate, @run_context)
  139. end
  140. before_symlink do
  141. app_provider.send(:run_actions_with_context, :before_symlink, @run_context)
  142. end
  143. before_restart do
  144. app_provider.send(:run_actions_with_context, :before_restart, @run_context)
  145. end
  146. after_restart do
  147. app_provider.send(:run_actions_with_context, :after_restart, @run_context)
  148. end
  149. end
  150. end
  151. def run_actions_with_context(action, context)
  152. new_resource.sub_resources.each do |resource|
  153. saved_run_context = resource.instance_variable_get :@run_context
  154. resource.instance_variable_set :@run_context, context
  155. resource.run_action action
  156. resource.instance_variable_set :@run_context, saved_run_context
  157. end
  158. callback(action, new_resource.send(action))
  159. end