Class: Teamistrano::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/teamistrano/notifier.rb

Constant Summary collapse

DEFAULT_COLOR =

blue

"0033CC"
GREEN =
"00CC66"
RED =
"CC0000"

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Notifier

Returns a new instance of Notifier.



15
16
17
18
19
20
# File 'lib/teamistrano/notifier.rb', line 15

def initialize( env )
  @env = env
  @settings = Teamistrano::Settings.new( @env )
  @color = DEFAULT_COLOR
  @msg = ""
end

Instance Method Details

#get_bodyObject

Get the body of the POST message as JSON.



63
64
65
66
67
68
69
# File 'lib/teamistrano/notifier.rb', line 63

def get_body
  return {
    'title' => "Deployment Notice",
    'text' => get_msg,
    'themeColor' => @color
  }.to_json
end

#get_msgObject

Get the full message to be delieved.



56
57
58
59
60
# File 'lib/teamistrano/notifier.rb', line 56

def get_msg
  return "#{@msg} for #{@settings.application}.  (#{@settings.stage})
    \n Branch: #{@settings.branch} 
    \n Deployed by: #{@settings.deployer}"
end

#notify_failedObject

Send notice that deployment failed.



49
50
51
52
53
# File 'lib/teamistrano/notifier.rb', line 49

def notify_failed
  @color = RED
  @msg = "Deployment Failed"
  post
end

#notify_finishingObject

Send notice that deployment finished.



36
37
38
39
40
# File 'lib/teamistrano/notifier.rb', line 36

def notify_finishing
  @color = GREEN
  @msg = "Deployment Finished"
  post
end

#notify_finishing_rollbackObject

Send notice that deployment rollback finished.



43
44
45
46
# File 'lib/teamistrano/notifier.rb', line 43

def notify_finishing_rollback
  @msg = "Deployment Rollback Finished"
  post
end

#notify_revertingObject

Send notice that deployment is reverting.



30
31
32
33
# File 'lib/teamistrano/notifier.rb', line 30

def notify_reverting
  @msg = "Deployment Reverting"
  post
end

#notify_updatingObject

Send notice that deployment started.



24
25
26
27
# File 'lib/teamistrano/notifier.rb', line 24

def notify_updating
  @msg = "Deployment Started"
  post
end

#postObject

Post to Teams.



72
73
74
75
76
77
78
# File 'lib/teamistrano/notifier.rb', line 72

def post
  if @settings.use_curl?
    post_with_curl
  else
    post_with_ruby
  end
end

#post_with_curlObject

Post to Teams with curl command.



94
95
96
97
# File 'lib/teamistrano/notifier.rb', line 94

def post_with_curl
  url = @settings.webhook_url
  `curl -is -X POST -H "Content-Type:application/json" -d '#{get_body}' '#{url}'`
end

#post_with_rubyObject

Post to Teams.



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/teamistrano/notifier.rb', line 81

def post_with_ruby
  uri = URI.parse( @settings.webhook_url )
  request = Net::HTTP::Post.new( uri.path )
  request.content_type = 'application/json'
  request.body = get_body
  n = Net::HTTP.new( uri.host, uri.port )
  n.use_ssl = true

  # Send the payload to the endpoint.
  n.start { |http| http.request( request ) }
end