default.rb 5.1 KB

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