default.rb 4.6 KB

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