default.rb 5.4 KB

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