default.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. user new_resource.owner
  100. group new_resource.group
  101. deploy_to new_resource.path
  102. ssh_wrapper "#{new_resource.path}/deploy-ssh-wrapper" if new_resource.deploy_key
  103. shallow_clone true
  104. rollback_on_error new_resource.rollback_on_error
  105. all_environments = ([new_resource.environment]+new_resource.sub_resources.map{|res| res.environment}).inject({}){|acc, val| acc.merge(val)}
  106. environment all_environments
  107. migrate new_resource.migrate
  108. all_migration_commands = ([new_resource.migration_command]+new_resource.sub_resources.map{|res| res.migration_command}).select{|cmd| cmd && !cmd.empty?}
  109. migration_command all_migration_commands.join(' && ')
  110. restart_command do
  111. ([new_resource]+new_resource.sub_resources).each do |res|
  112. cmd = res.restart_command
  113. if cmd.is_a? Proc
  114. provider = Chef::Platform.provider_for_resource(res)
  115. provider.load_current_resource
  116. provider.instance_eval(&cmd)
  117. elsif cmd && !cmd.empty?
  118. execute cmd do
  119. user new_resource.owner
  120. group new_resource.group
  121. environment all_environments
  122. end
  123. end
  124. end
  125. end
  126. purge_before_symlink new_resource.purge_before_symlink
  127. create_dirs_before_symlink new_resource.create_dirs_before_symlink
  128. symlinks new_resource.symlinks
  129. all_symlinks_before_migrate = [new_resource.symlink_before_migrate]+new_resource.sub_resources.map{|res| res.symlink_before_migrate}
  130. symlink_before_migrate all_symlinks_before_migrate.inject({}){|acc, val| acc.merge(val)}
  131. before_migrate do
  132. app_provider.send(:run_actions_with_context, :before_migrate, @run_context)
  133. end
  134. before_symlink do
  135. app_provider.send(:run_actions_with_context, :before_symlink, @run_context)
  136. end
  137. before_restart do
  138. app_provider.send(:run_actions_with_context, :before_restart, @run_context)
  139. end
  140. after_restart do
  141. app_provider.send(:run_actions_with_context, :after_restart, @run_context)
  142. end
  143. end
  144. end
  145. def run_actions_with_context(action, context)
  146. new_resource.sub_resources.each do |resource|
  147. saved_run_context = resource.instance_variable_get :@run_context
  148. resource.instance_variable_set :@run_context, context
  149. resource.run_action action
  150. resource.instance_variable_set :@run_context, saved_run_context
  151. end
  152. callback(action, new_resource.send(action))
  153. end