default.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. end
  27. new_resource.packages.each do |pkg,ver|
  28. package pkg do
  29. action :install
  30. version ver if ver && ver.length > 0
  31. end
  32. end
  33. directory new_resource.path do
  34. owner new_resource.owner
  35. group new_resource.group
  36. mode '0755'
  37. recursive true
  38. end
  39. directory "#{new_resource.path}/shared" do
  40. owner new_resource.owner
  41. group new_resource.group
  42. mode '0755'
  43. recursive true
  44. end
  45. if new_resource.deploy_key
  46. ruby_block "write_key" do
  47. block do
  48. f = ::File.open("#{new_resource.path}/id_deploy", "w")
  49. f.print(new_resource.deploy_key)
  50. f.close
  51. end
  52. not_if do ::File.exists?("#{new_resource.path}/id_deploy"); end
  53. end
  54. file "#{new_resource.path}/id_deploy" do
  55. owner new_resource.owner
  56. group new_resource.group
  57. mode '0600'
  58. end
  59. template "#{new_resource.path}/deploy-ssh-wrapper" do
  60. source "deploy-ssh-wrapper.erb"
  61. owner new_resource.owner
  62. group new_resource.group
  63. mode "0755"
  64. variables :id => new_resource.id, :deploy_to => new_resource.path
  65. end
  66. end
  67. new_resource.sub_resources.each do |resource|
  68. resource.run_action :before_compile
  69. end
  70. ruby_block "#{new_resource.id} before_deploy" do
  71. block do
  72. new_resource.sub_resources.each do |resource|
  73. resource.run_action :before_deploy
  74. end
  75. callback(:before_deploy, new_resource.before_deploy)
  76. end
  77. end
  78. @deploy_resource = deploy_revision new_resource.id do
  79. revision new_resource.revision
  80. repository new_resource.repository
  81. user new_resource.owner
  82. group new_resource.group
  83. deploy_to new_resource.path
  84. ssh_wrapper "#{new_resource.path}/deploy-ssh-wrapper" if new_resource.deploy_key
  85. shallow_clone true
  86. all_environments = [new_resource.environment]+new_resource.sub_resources.map{|res| res.environment}
  87. environment all_environments.inject({}){|acc, val| acc.merge(val)}
  88. migrate new_resource.migrate
  89. all_migration_commands = ([new_resource.migration_command]+new_resource.sub_resources.map{|res| res.migration_command}).select{|res| res}
  90. migration_command all_migration_commands.join(';')
  91. all_restart_commands = ([new_resource.restart_command]+new_resource.sub_resources.map{|res| res.restart_command}).select{|res| res}
  92. restart_command all_restart_commands.join(';')
  93. purge_before_symlink new_resource.purge_before_symlink
  94. create_dirs_before_symlink new_resource.create_dirs_before_symlink
  95. symlinks new_resource.symlinks
  96. all_symlinks_before_migrate = [new_resource.symlink_before_migrate]+new_resource.sub_resources.map{|res| res.symlink_before_migrate}
  97. symlink_before_migrate all_symlinks_before_migrate.inject({}){|acc, val| acc.merge(val)}
  98. # Yes, this needs to be refactored together
  99. before_migrate do
  100. new_resource.sub_resources.each do |resource|
  101. saved_run_context = resource.instance_variable_get :@run_context
  102. resource.instance_variable_set :@run_context, @run_context
  103. resource.run_action :before_migrate
  104. resource.instance_variable_set :@run_context, saved_run_context
  105. end
  106. callback(:before_migrate, new_resource.before_migrate)
  107. end
  108. before_symlink do
  109. new_resource.sub_resources.each do |resource|
  110. saved_run_context = resource.instance_variable_get :@run_context
  111. resource.instance_variable_set :@run_context, @run_context
  112. resource.run_action :before_symlink
  113. resource.instance_variable_set :@run_context, saved_run_context
  114. end
  115. callback(:before_symlink, new_resource.before_symlink)
  116. end
  117. before_restart do
  118. new_resource.sub_resources.each do |resource|
  119. saved_run_context = resource.instance_variable_get :@run_context
  120. resource.instance_variable_set :@run_context, @run_context
  121. resource.run_action :before_restart
  122. resource.instance_variable_set :@run_context, saved_run_context
  123. end
  124. callback(:before_restart, new_resource.before_restart)
  125. end
  126. after_restart do
  127. new_resource.sub_resources.each do |resource|
  128. saved_run_context = resource.instance_variable_get :@run_context
  129. resource.instance_variable_set :@run_context, @run_context
  130. resource.run_action :after_restart
  131. resource.instance_variable_set :@run_context, saved_run_context
  132. end
  133. callback(:after_restart, new_resource.after_restart)
  134. end
  135. end
  136. end