Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# we don't want to change the action mailer settings for everything, just temporarily.
def action_mailer_with_temporary_delivery_method
  existing_delivery_method = ActionMailer::Base.delivery_method
  existing_smtp_settings = ActionMailer::Base.smtp_settings
  begin
    ActionMailer::Base.delivery_method = :smtp
    ActionMailer::Base.smtp_settings = {
      :address => "smtp.gmail.com",
      :port => "587",
      :domain => "your.domain.com",
      :authentication => :plain,
      :user_name => "do-not-reply@your.domain.com",
      :password => "PASSword"
    }
    yield
  ensure
    ActionMailer::Base.delivery_method = existing_delivery_method
    ActionMailer::Base.smtp_settings = existing_smtp_settings
  end
end

# capistrano has this neat-o @variables that holds the results of everything that's been "set". Let's see what's in there
def variables 
  result = []
  @variables.reject { |k,v| k.to_s.downcase.eql?('password') }.each_pair do |key, value|
    # since we're going to be printing this out, we don't want to see garbage like 
    # "#<Proc:0xb77fef88@/usr/lib/ruby/gems/1.8/gems/capistrano-2.3.0/lib/capistrano/cli/options.rb:128>"
    value = value.is_a?(Proc) ? value.call : value
    value = value.is_a?(Array) ? value.join(", ") : value
    result << "#{key} : #{value}"
  end
  result.sort.join("\n")
end

# hook into our deploy task so it's not possible to deploy anything without this notification going out
before 'deploy', 'deploy:notify'
namespace :deploy do    
  desc "sends a notification email letting everyone know we're deploying"
  task :notify do
    deployment_name = application.to_s.gsub(/(-.*)?/, '').upcase
    stage_name = stage.to_s.upcase
    action_mailer_with_temporary_delivery_method do
      mail = TMail::Mail.new
      mail.subject,      = "Pending Deploy [#{deployment_name} #{stage}]"
      mail.to, mail.from = "deploy-list@nomnom.com", ActionMailer::Base.smtp_settings[:user_name]
      mail.date          = Time.now
      mail.body          = ActionMailer::Utils.normalize_new_lines(%Q(      
Hello everyone, 

We're making a deploy to the #{stage} environment. The #{stage} site may (or may not) be unavailable for a few minutes sometime within the next 30 minutes (depending on how long it takes to upload the changes).

This message is automated.

---------------------- debugging information ----------------------
#{variables}
))
      ActionMailer::Base.deliver(mail)
      # puts mail.body
    end
  end
end