default.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. ruby_block "write_key" do
  48. block do
  49. f = ::File.open("#{new_resource.path}/id_deploy", "w")
  50. f.print(new_resource.deploy_key)
  51. f.close
  52. end
  53. not_if do ::File.exists?("#{new_resource.path}/id_deploy"); end
  54. end
  55. file "#{new_resource.path}/id_deploy" do
  56. owner new_resource.owner
  57. group new_resource.group
  58. mode '0600'
  59. end
  60. template "#{new_resource.path}/deploy-ssh-wrapper" do
  61. source "deploy-ssh-wrapper.erb"
  62. owner new_resource.owner
  63. group new_resource.group
  64. mode "0755"
  65. variables :id => new_resource.id, :deploy_to => new_resource.path
  66. end
  67. end
  68. ruby_block "#{new_resource.id} before_deploy" do
  69. block do
  70. new_resource.sub_resources.each do |resource|
  71. resource.run_action :before_deploy
  72. end
  73. callback(:before_deploy, new_resource.before_deploy)
  74. end
  75. end
  76. @deploy_resource = deploy_revision new_resource.id do
  77. revision new_resource.revision
  78. repository new_resource.repository
  79. user new_resource.owner
  80. group new_resource.group
  81. deploy_to new_resource.path
  82. ssh_wrapper "#{new_resource.path}/deploy-ssh-wrapper" if new_resource.deploy_key
  83. shallow_clone true
  84. all_environments = [new_resource.environment]+new_resource.sub_resources.map{|res| res.environment}
  85. environment all_environments.inject({}){|acc, val| acc.merge(val)}
  86. migrate new_resource.migrate
  87. all_migration_commands = ([new_resource.migration_command]+new_resource.sub_resources.map{|res| res.migration_command}).select{|res| res}
  88. migration_command all_migration_commands.join(';')
  89. all_restart_commands = ([new_resource.restart_command]+new_resource.sub_resources.map{|res| res.restart_command}).select{|res| res}
  90. restart_command all_restart_commands.join(';')
  91. purge_before_symlink new_resource.purge_before_symlink
  92. create_dirs_before_symlink new_resource.create_dirs_before_symlink
  93. symlinks new_resource.symlinks
  94. all_symlinks_before_migrate = [new_resource.symlink_before_migrate]+new_resource.sub_resources.map{|res| res.symlink_before_migrate}
  95. symlink_before_migrate all_symlinks_before_migrate.inject({}){|acc, val| acc.merge(val)}
  96. # Yes, this needs to be refactored together
  97. before_migrate do
  98. new_resource.sub_resources.each do |resource|
  99. saved_run_context = resource.instance_variable_get :@run_context
  100. resource.instance_variable_set :@run_context, @run_context
  101. resource.run_action :before_migrate
  102. resource.instance_variable_set :@run_context, saved_run_context
  103. end
  104. callback(:before_migrate, new_resource.before_migrate)
  105. end
  106. before_symlink 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_symlink
  111. resource.instance_variable_set :@run_context, saved_run_context
  112. end
  113. callback(:before_symlink, new_resource.before_symlink)
  114. end
  115. before_restart 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_restart
  120. resource.instance_variable_set :@run_context, saved_run_context
  121. end
  122. callback(:before_restart, new_resource.before_restart)
  123. end
  124. after_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 :after_restart
  129. resource.instance_variable_set :@run_context, saved_run_context
  130. end
  131. callback(:after_restart, new_resource.after_restart)
  132. end
  133. end
  134. end