capistrano_mailer 3.2.7 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cap_mailer.rb CHANGED
@@ -1,180 +1,226 @@
1
- class CapMailer < ActionMailer::Base
2
-
3
- @@default_base_config ||= {
4
- :sender_address => %("#{(defined?(Rails) ? Rails.env.capitalize : defined?(RAILS_ENV) ? RAILS_ENV.capitalize : defined?(ENV) ? ENV['RAILS_ENV'] : "")} Capistrano Deployment" <capistrano.mailer@example.com>),
5
- :recipient_addresses => [],
6
- # Customize the subject line
7
- :subject_prepend => "[DEPLOYMENT]-[#{(defined?(Rails) ? Rails.env.capitalize : defined?(RAILS_ENV) ? RAILS_ENV.capitalize : defined?(ENV) ? ENV['RAILS_ENV'] : "")}] ",
8
- :subject_append => nil,
9
- # Include which sections of the deployment email?
10
- :sections => %w(deployment release_data source_control latest_release previous_release other_deployment_info extra_information),
11
- :site_name => "",
12
- :email_content_type => "text/html",
13
- :template_root => "#{File.dirname(__FILE__)}/../views"
14
- }
15
-
16
- cattr_accessor :default_base_config
17
- attr_accessor :config, :options
18
- attr_accessor :date, :time, :inferred_command, :task_name, :repo_end
19
-
20
- def self.configure(&block)
21
- yield @@default_base_config
22
- end
23
-
24
- def self.configure_capistrano_mailer(&block)
25
- puts "Deprecated 'configure_capistrano_mailer'. Please update your capistrano_mailer configuration to use 'configure' instead of 'configure_capistrano_mailer'"
26
- end
27
-
28
- self.template_root = default_base_config[:template_root]
29
-
30
- def self.reloadable?() false end
31
-
32
- def notification_email(cap, config = {}, *args)
33
- @options = { :release_data => {}, :extra_information => {}, :data => {} }.merge(args.extract_options!)
34
- @config = default_base_config.merge(config.reverse_merge({
35
- :rails_env => cap.rails_env,
36
- :host => cap.host,
37
- :task_name => cap.task_name,
38
- :application => cap.application,
39
- :repository => cap.repository,
40
- :scm => cap.scm,
41
- :deploy_via => cap.deploy_via,
42
- :deploy_to => cap.deploy_to,
43
- :revision => cap.revision,
44
- :real_revision => cap.real_revision,
45
- :release_name => cap.release_name,
46
- :version_dir => cap.version_dir,
47
- :shared_dir => cap.shared_dir,
48
- :current_dir => cap.current_dir,
49
- :releases_path => cap.releases_path,
50
- :shared_path => cap.shared_path,
51
- :current_path => cap.current_path,
52
- :release_path => cap.release_path,
53
- :releases => cap.releases,
54
- :current_release => cap.current_release,
55
- :previous_release => cap.previous_release,
56
- :current_revision => cap.current_revision,
57
- :latest_revision => cap.latest_revision,
58
- :previous_revision => cap.previous_revision,
59
- :run_method => cap.run_method,
60
- :latest_release => cap.latest_release
61
-
62
- #This does not appear to be a capistrano variable:
63
- #:site_url => cap.site_url
64
- }))
65
-
66
- @date = Date.today.to_s
67
- @time = Time.now.strftime("%I:%M %p").to_s
68
- @inferred_command = "cap #{@config[:rails_env]} #{@config[:task_name]}"
69
- @task_name = @config[:task_name] || "unknown"
70
-
71
- repo = @config[:repository]
72
- x = repo.include?('/') ? repo.rindex('/') - 1 : repo.length
73
- front = repo.slice(0..x)
74
- back = repo.sub(front, '')
75
- unless back == 'trunk'
76
- x = front.include?('/') ? front.rindex('/') - 1 : front.length
77
- front = front.slice(0..x)
78
- end
79
- @repo_end = repo.sub(front, '')
80
-
81
- subject subject_line
82
- recipients @config[:recipient_addresses]
83
- from @config[:sender_address]
84
- content_type @config[:email_content_type]
85
-
86
- body body_data_hash
87
- end
88
-
89
- private
90
-
91
- def subject_line
92
- #The subject prepend and append are useful for people to setup filters in mail clients.
93
- user = config[:user] ? " by #{config[:user]}" : ""
94
- middle = config[:subject] ? config[:subject] : "[#{config[:rails_env].upcase}][#{repo_end}] #{inferred_command}#{user}"
95
- "#{config[:subject_prepend]}#{middle}#{config[:subject_append]}"
96
- end
97
-
98
- def body_data_hash
99
- options[:data].merge({
100
- :section_data => section_data_hash,
101
- :date => date,
102
- :time => time,
103
- :task_name => task_name,
104
- :inferred_command => inferred_command,
105
- :repo_end => repo_end,
106
- :site_name => config[:site_name],
107
- :site_url => config[:site_url],
108
- :application => config[:application],
109
- :sections => config[:sections]
110
- })
111
- end
112
-
113
- def section_data_hash
114
- {
115
- :deployment => section_hash_deployment,
116
- :source_control => section_hash_source_control,
117
- :latest_release => section_hash_latest_release,
118
- :previous_release => section_hash_previous_release,
119
- :other_deployment_info => section_hash_other_deployment_info,
120
- :release_data => options[:release_data],
121
- :extra_information => options[:extra_information]
122
- }
123
- end
124
-
125
- def section_hash_deployment
126
- {
127
- :date => date,
128
- :time => time,
129
- :rails_env => config[:rails_env],
130
- :task_name => task_name,
131
- :inferred_command => inferred_command,
132
- :host => config[:host],
133
- :release_name => config[:release_name]
134
- }
135
- end
136
-
137
- def section_hash_source_control
138
- {
139
- :revision => config[:revision],
140
- :released => repo_end,
141
- :repository => config[:repository],
142
- :branch => config[:branch],
143
- :scm => config[:scm],
144
- :deploy_via => config[:deploy_via],
145
- :deploy_to => config[:deploy_to]
146
- }
147
- end
148
-
149
- def section_hash_latest_release
150
- {
151
- :latest_release => config[:latest_release],
152
- :latest_revision => config[:latest_revision],
153
- :release_path => config[:release_path],
154
- :real_revision => config[:real_revision],
155
- :current_path => config[:current_path]
156
- }
157
- end
158
-
159
- def section_hash_previous_release
160
- {
161
- :current_release => config[:current_release],
162
- :current_revision => config[:current_revision],
163
- :previous_release => config[:previous_release],
164
- :previous_revision => config[:previous_revision],
165
- :releases => config[:releases]
166
- }
167
- end
168
-
169
- def section_hash_other_deployment_info
170
- {
171
- :version_dir => config[:version_dir],
172
- :shared_dir => config[:shared_dir],
173
- :current_dir => config[:current_dir],
174
- :releases_path => config[:releases_path],
175
- :shared_path => config[:shared_path],
176
- :run_method => config[:run_method],
177
- :ip_address => config[:ip_address]
178
- }
179
- end
180
- end
1
+ class CapMailer < ActionMailer::Base
2
+
3
+ # In capistrano_mailer 4+ this uses ActiveSupport::InheritableOptions from Rails 3+
4
+ @@default_base_config ||= {
5
+ :sender_address => %("#{(defined?(Rails) ? Rails.env.capitalize : defined?(RAILS_ENV) ? RAILS_ENV.capitalize : defined?(ENV) ? ENV['RAILS_ENV'] : "")} Capistrano Deployment" <capistrano.mailer@example.com>),
6
+ :recipient_addresses => [],
7
+ # Customize the subject line
8
+ :subject_prepend => "[DEPLOYMENT]-[#{(defined?(Rails) ? Rails.env.capitalize : defined?(RAILS_ENV) ? RAILS_ENV.capitalize : defined?(ENV) ? ENV['RAILS_ENV'] : "")}] ",
9
+ :subject_append => nil,
10
+ # Include which sections of the deployment email?
11
+ :sections => %w(deployment release_data source_control latest_release previous_release other_deployment_info extra_information),
12
+ :site_name => "",
13
+ :email_content_type => "text/html",
14
+ # In Capistrano 4+ template_root becomes template_path
15
+ :template_root => "#{File.dirname(__FILE__)}/../views",
16
+ :template_prefixes => { :success => nil, :failure => "failed" },
17
+ :attach_log_on => :failure
18
+ }
19
+
20
+ cattr_accessor :default_base_config
21
+ attr_accessor :config, :options
22
+ attr_accessor :date, :time, :inferred_command, :task_name, :repo_end
23
+
24
+ def self.configure(&block)
25
+ yield @@default_base_config
26
+ end
27
+
28
+ def self.configure_capistrano_mailer(&block)
29
+ raise "'configure_capistrano_mailer' is deprecated. Please update your capistrano_mailer configuration to use 'configure' instead of 'configure_capistrano_mailer'"
30
+ end
31
+
32
+ self.template_root = default_base_config[:template_root]
33
+
34
+ def self.reloadable?() false end
35
+
36
+ def config_from_cap_hash(cap, config = {})
37
+ default_base_config.dup.merge(config.reverse_merge({
38
+ :rails_env => cap[:rails_env],
39
+ :host => cap[:host],
40
+ :task_name => cap[:task_name],
41
+ :application => cap[:application],
42
+ :repository => cap[:repository],
43
+ :scm => cap[:scm],
44
+ :deploy_via => cap[:deploy_via],
45
+ :deploy_to => cap[:deploy_to],
46
+ :revision => cap[:revision],
47
+ :real_revision => cap[:real_revision],
48
+ :release_name => cap[:release_name],
49
+ :version_dir => cap[:version_dir],
50
+ :shared_dir => cap[:shared_dir],
51
+ :current_dir => cap[:current_dir],
52
+ :releases_path => cap[:releases_path],
53
+ :shared_path => cap[:shared_path],
54
+ :current_path => cap[:current_path],
55
+ :release_path => cap[:release_path],
56
+ :releases => cap[:releases],
57
+ :current_release => cap[:current_release],
58
+ :previous_release => cap[:previous_release],
59
+ :current_revision => cap[:current_revision],
60
+ :latest_revision => cap[:latest_revision],
61
+ :previous_revision => cap[:previous_revision],
62
+ :run_method => cap[:run_method],
63
+ :latest_release => cap[:latest_release]
64
+
65
+ #This does not appear to be a capistrano variable:
66
+ #:site_url => cap[:site_url]
67
+ }))
68
+ end
69
+
70
+ def notification_email(cap, config = {}, *args)
71
+ @config = config_from_cap_hash(cap.is_a?(Array) ? cap.first : cap, config)
72
+ @options = { :release_data => {}, :extra_information => {}, :data => {} }.merge(args.extract_options!)
73
+
74
+ @date = Date.today.to_s
75
+ @time = Time.now.strftime("%I:%M %p").to_s
76
+ @inferred_command = "cap #{@config[:rails_env]} #{@config[:task_name]}"
77
+ @task_name = @config[:task_name] || "unknown"
78
+
79
+ repo = @config[:repository]
80
+ x = repo.include?('/') ? repo.rindex('/') - 1 : repo.length
81
+ front = repo.slice(0..x)
82
+ back = repo.sub(front, '')
83
+ unless back == 'trunk'
84
+ x = front.include?('/') ? front.rindex('/') - 1 : front.length
85
+ front = front.slice(0..x)
86
+ end
87
+ @repo_end = repo.sub(front, '')
88
+
89
+ log = cap.fetch(:full_log)
90
+ fail_pattern = /^failed|rolling back/i
91
+ @job_status = (log =~ fail_pattern) ? :failure : cap.fetch(:mailer_status, :success)
92
+ template_prefix = @config[:template_prefixes][@job_status] ? "#{@config[:template_prefixes][@job_status]}." : ""
93
+ template_name = @config[:template_name] || "#{template_prefix}#{action_name}"
94
+
95
+ attach_log = case @config[:attach_log_on]
96
+ when Symbol, String
97
+ @job_status == @config[:attach_log_on].to_sym
98
+ when Array
99
+ @config[:attach_log_on].collect(&:to_sym).include? @job_status
100
+ else
101
+ false
102
+ end
103
+
104
+ # See: http://guides.rubyonrails.org/v2.3.11/action_mailer_basics.html#sending-emails-with-attachments
105
+ if attach_log
106
+ content_type "multipart/mixed"
107
+
108
+ part "multipart/alternative" do |alternative|
109
+
110
+ alternative.part "text/html" do |html|
111
+ html.body = render_message("#{template_name}.html", body_data_hash)
112
+ end if @config[:email_content_type] == "text/html"
113
+
114
+ alternative.part "text/plain" do |plain|
115
+ plain.body = render_message("#{template_name}.plain", body_data_hash)
116
+ end if @config[:email_content_type] == "text/plain"
117
+
118
+ end
119
+
120
+ attachment :content_type => "text/plain",
121
+ :body => log,
122
+ :filename => "deploy-log-#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.txt"
123
+ else
124
+ content_type @config[:email_content_type]
125
+ body body_data_hash
126
+ end
127
+
128
+ subject "#{@job_status.to_s.upcase}: #{subject_line}"
129
+ recipients @config[:recipient_addresses]
130
+ from @config[:sender_address]
131
+
132
+ end
133
+
134
+ private
135
+
136
+ def subject_line
137
+ #The subject prepend and append are useful for people to setup filters in mail clients.
138
+ user = config[:user] ? " by #{config[:user]}" : ""
139
+ middle = config[:subject] ? config[:subject] : "[#{config[:rails_env].upcase}][#{repo_end}] #{inferred_command}#{user}"
140
+ "#{config[:subject_prepend]}#{middle}#{config[:subject_append]}"
141
+ end
142
+
143
+ def body_data_hash
144
+ options[:data].merge({
145
+ :section_data => section_data_hash,
146
+ :date => date,
147
+ :time => time,
148
+ :task_name => task_name,
149
+ :inferred_command => inferred_command,
150
+ :repo_end => repo_end,
151
+ :site_name => config[:site_name],
152
+ :site_url => config[:site_url],
153
+ :application => config[:application],
154
+ :sections => config[:sections]
155
+ })
156
+ end
157
+
158
+ def section_data_hash
159
+ {
160
+ :deployment => section_hash_deployment,
161
+ :source_control => section_hash_source_control,
162
+ :latest_release => section_hash_latest_release,
163
+ :previous_release => section_hash_previous_release,
164
+ :other_deployment_info => section_hash_other_deployment_info,
165
+ :release_data => options[:release_data],
166
+ :extra_information => options[:extra_information]
167
+ }
168
+ end
169
+
170
+ def section_hash_deployment
171
+ {
172
+ :date => date,
173
+ :time => time,
174
+ :rails_env => config[:rails_env],
175
+ :task_name => task_name,
176
+ :inferred_command => inferred_command,
177
+ :host => config[:host],
178
+ :release_name => config[:release_name],
179
+ :release_notes => config[:release_notes]
180
+ }
181
+ end
182
+
183
+ def section_hash_source_control
184
+ {
185
+ :revision => config[:revision],
186
+ :released => repo_end,
187
+ :repository => config[:repository],
188
+ :branch => config[:branch],
189
+ :scm => config[:scm],
190
+ :deploy_via => config[:deploy_via],
191
+ :deploy_to => config[:deploy_to]
192
+ }
193
+ end
194
+
195
+ def section_hash_latest_release
196
+ {
197
+ :latest_release => config[:latest_release],
198
+ :latest_revision => config[:latest_revision],
199
+ :release_path => config[:release_path],
200
+ :real_revision => config[:real_revision],
201
+ :current_path => config[:current_path]
202
+ }
203
+ end
204
+
205
+ def section_hash_previous_release
206
+ {
207
+ :current_release => config[:current_release],
208
+ :current_revision => config[:current_revision],
209
+ :previous_release => config[:previous_release],
210
+ :previous_revision => config[:previous_revision],
211
+ :releases => config[:releases]
212
+ }
213
+ end
214
+
215
+ def section_hash_other_deployment_info
216
+ {
217
+ :version_dir => config[:version_dir],
218
+ :shared_dir => config[:shared_dir],
219
+ :current_dir => config[:current_dir],
220
+ :releases_path => config[:releases_path],
221
+ :shared_path => config[:shared_path],
222
+ :run_method => config[:run_method],
223
+ :ip_address => config[:ip_address]
224
+ }
225
+ end
226
+ end
@@ -1,25 +1,45 @@
1
- require 'rubygems' unless defined?(Rubygems)
2
- require 'capistrano' unless defined?(Capistrano)
3
-
4
- unless Capistrano::Configuration.respond_to?(:instance)
5
- abort "capistrano/mailer requires Capistrano 2"
6
- end
7
-
8
- require 'action_mailer' unless defined?(ActionMailer)
9
-
10
- require 'cap_mailer' unless defined?(CapMailer)
11
-
12
-
13
- module Capistrano
14
- class Configuration
15
- module CapistranoMailer
16
- def send_notification_email(cap, config = {}, *args)
17
- CapMailer.deliver_notification_email(cap, config, *args)
18
- end
19
- end
20
-
21
- include CapistranoMailer
22
- end
23
- end
24
-
25
- Capistrano.plugin :mailer, Capistrano::Configuration::CapistranoMailer
1
+ require 'capistrano_mailer/version'
2
+
3
+ require 'capistrano'
4
+
5
+ unless Capistrano::Configuration.respond_to?(:instance)
6
+ abort 'capistrano/mailer requires Capistrano 2'
7
+ end
8
+
9
+ require 'capistrano/log_with_awesome'
10
+ require 'action_mailer'
11
+
12
+ require 'cap_mailer'
13
+
14
+ module CapistranoMailer
15
+ end
16
+
17
+ module Capistrano
18
+ class Configuration
19
+ module CapistranoMailer
20
+ def send_notification_email(cap, config = {}, *args)
21
+ CapMailer.deliver_notification_email(cap, config, *args)
22
+ end
23
+ end
24
+
25
+ include CapistranoMailer
26
+
27
+ module Execution
28
+ protected
29
+ def __rollback_with_mailer!
30
+ set :mailer_status, :failure
31
+ find_and_execute_task "deploy:notify"
32
+ __rollback_without_mailer!
33
+ end
34
+
35
+ alias_method :__rollback_without_mailer!, :rollback!
36
+ alias_method :rollback!, :__rollback_with_mailer!
37
+ end
38
+ end
39
+ end
40
+
41
+ Capistrano.plugin :mailer, Capistrano::Configuration::CapistranoMailer
42
+
43
+ if cap = Capistrano::Configuration.instance
44
+ cap.load("#{File.expand_path(File.dirname(__FILE__))}/mailer_recipes.rb")
45
+ end