default.rb 5.3 KB

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