default.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. revision new_resource.revision
  72. repository new_resource.repository
  73. user new_resource.owner
  74. group new_resource.group
  75. deploy_to new_resource.path
  76. ssh_wrapper "#{new_resource.path}/deploy-ssh-wrapper" if new_resource.deploy_key
  77. shallow_clone true
  78. all_environments = ([new_resource.environment]+new_resource.sub_resources.map{|res| res.environment}).inject({}){|acc, val| acc.merge(val)}
  79. environment all_environments
  80. migrate new_resource.migrate
  81. all_migration_commands = ([new_resource.migration_command]+new_resource.sub_resources.map{|res| res.migration_command}).select{|cmd| cmd && !cmd.empty?}
  82. migration_command all_migration_commands.join(' && ')
  83. restart_command do
  84. ([new_resource]+new_resource.sub_resources).each do |res|
  85. cmd = res.restart_command
  86. if cmd.is_a? Proc
  87. provider = Chef::Platform.provider_for_resource(res)
  88. provider.load_current_resource
  89. provider.instance_eval(&cmd)
  90. elsif cmd && !cmd.empty?
  91. execute cmd do
  92. user new_resource.owner
  93. group new_resource.group
  94. environment all_environments
  95. end
  96. end
  97. end
  98. end
  99. purge_before_symlink new_resource.purge_before_symlink
  100. create_dirs_before_symlink new_resource.create_dirs_before_symlink
  101. symlinks new_resource.symlinks
  102. all_symlinks_before_migrate = [new_resource.symlink_before_migrate]+new_resource.sub_resources.map{|res| res.symlink_before_migrate}
  103. symlink_before_migrate all_symlinks_before_migrate.inject({}){|acc, val| acc.merge(val)}
  104. # Yes, this needs to be refactored together
  105. before_migrate do
  106. new_resource.sub_resources.each do |resource|
  107. saved_run_context = resource.instance_variable_get :@run_context
  108. resource.instance_variable_set :@run_context, @run_context
  109. resource.run_action :before_migrate
  110. resource.instance_variable_set :@run_context, saved_run_context
  111. end
  112. callback(:before_migrate, new_resource.before_migrate)
  113. end
  114. before_symlink do
  115. new_resource.sub_resources.each do |resource|
  116. saved_run_context = resource.instance_variable_get :@run_context
  117. resource.instance_variable_set :@run_context, @run_context
  118. resource.run_action :before_symlink
  119. resource.instance_variable_set :@run_context, saved_run_context
  120. end
  121. callback(:before_symlink, new_resource.before_symlink)
  122. end
  123. before_restart do
  124. new_resource.sub_resources.each do |resource|
  125. saved_run_context = resource.instance_variable_get :@run_context
  126. resource.instance_variable_set :@run_context, @run_context
  127. resource.run_action :before_restart
  128. resource.instance_variable_set :@run_context, saved_run_context
  129. end
  130. callback(:before_restart, new_resource.before_restart)
  131. end
  132. after_restart do
  133. new_resource.sub_resources.each do |resource|
  134. saved_run_context = resource.instance_variable_get :@run_context
  135. resource.instance_variable_set :@run_context, @run_context
  136. resource.run_action :after_restart
  137. resource.instance_variable_set :@run_context, saved_run_context
  138. end
  139. callback(:after_restart, new_resource.after_restart)
  140. end
  141. end
  142. end