Skip to content

Commit

Permalink
Merge pull request #485 from 0x6d617474/webhook_improvements
Browse files Browse the repository at this point in the history
Webhook improvements
  • Loading branch information
bastelfreak authored May 11, 2019
2 parents c36a64e + fa33970 commit 461f8d1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
$webhook_slack_channel = undef
$webhook_slack_username = undef
$webhook_slack_proxy_url = undef
$webhook_slack_icon = undef
$webhook_rocketchat_webhook = undef
$webhook_rocketchat_channel = undef
$webhook_rocketchat_username = undef
Expand Down
2 changes: 2 additions & 0 deletions manifests/webhook/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
$slack_channel = $r10k::params::webhook_slack_channel,
$slack_username = $r10k::params::webhook_slack_username,
$slack_proxy_url = $r10k::params::webhook_slack_proxy_url,
$slack_icon = $r10k::params::webhook_slack_icon,
$rocketchat_webhook = $r10k::params::webhook_rocketchat_webhook,
$rocketchat_channel = $r10k::params::webhook_rocketchat_channel,
$rocketchat_username = $r10k::params::webhook_rocketchat_username,
Expand Down Expand Up @@ -87,6 +88,7 @@
'slack_channel' => $slack_channel,
'slack_username' => $slack_username,
'slack_proxy_url' => $slack_proxy_url,
'slack_icon' => $slack_icon,
'rocketchat_webhook' => $rocketchat_webhook,
'rocketchat_channel' => $rocketchat_channel,
'rocketchat_username' => $rocketchat_username,
Expand Down
49 changes: 34 additions & 15 deletions templates/webhook.bin.erb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class Server < Sinatra::Base
module_name = sanitize_input(module_name)
$logger.info("Deploying module #{module_name} in the background.")
Process.detach(fork { deploy_module(module_name) })

return 202, {:status => :accepted, :message => 'accepted' }.to_json
end

# Simulate a github post:
Expand Down Expand Up @@ -213,6 +215,8 @@ class Server < Sinatra::Base
else
$logger.info("Deploying environment #{env} in the background")
Process.detach(fork { deploy(env, deleted) })

return 202, {:status => :accepted, :message => 'accepted' }.to_json
end
end

Expand Down Expand Up @@ -261,8 +265,7 @@ class Server < Sinatra::Base
file.flock(File::LOCK_EX)

message = "triggered: #{command}"
exec "#{command} &"
exit_status = 0
stdout, stderr, exit_status = Open3.capture3(command)
raise "#{stdout}\n#{stderr}" if exit_status != 0
end
message
Expand Down Expand Up @@ -296,6 +299,12 @@ class Server < Sinatra::Base
slack_user = 'r10k'
end

if $config['slack_icon']
slack_icon = $config['slack_icon']
else
slack_icon = ':ocean:'
end

if $config['slack_proxy_url']
uri = URI($config['slack_proxy_url'])
http_options = {
Expand All @@ -310,19 +319,24 @@ class Server < Sinatra::Base
notifier = Slack::Notifier.new $config['slack_webhook'] do
defaults channel: slack_channel,
username: slack_user,
icon_emoji: ":ocean:",
icon_emoji: slack_icon,
http_options: http_options
end

if status_message[:branch]
if status_message[:environment] # generate_types
target = status_message[:environment]
msg = "Generation of Puppet resource types for #{target}"
elsif status_message[:branch] # deploy
target = status_message[:branch]
elsif status_message[:module]
msg = "Deployment of Puppet environment #{target}"
elsif status_message[:module] # deploy_module
target = status_message[:module]
msg = "Deployment of Puppet module #{target}"
end

message = {
author: 'r10k for Puppet',
title: "r10k deployment of Puppet environment #{target}"
title: msg
}

case status_message[:status_code]
Expand All @@ -340,7 +354,7 @@ class Server < Sinatra::Base
)
end

notifier.post text: message[:fallback], attachments: [message]
notifier.post text: 'r10k deployment', attachments: [message]
end

def notify_rocketchat(status_message)
Expand All @@ -360,16 +374,21 @@ class Server < Sinatra::Base
notifier.username = rocketchat_user
notifier.channel = rocketchat_channel

if status_message[:branch]
if status_message[:environment] # generate_types
target = status_message[:environment]
msg = "Generation of Puppet resource types for #{target}"
elsif status_message[:branch] # deploy
target = status_message[:branch]
elsif status_message[:module]
msg = "Deployment of Puppet environment #{target}"
elsif status_message[:module] # deploy_module
target = status_message[:module]
msg = "Deployment of Puppet module #{target}"
end

message = "r10k deployment of Puppet environment/module #{target}"
message = "r10k deployment"
attachments = {
author: 'r10k for Puppet',
title: "r10k deployment of Puppet environment #{target}",
title: msg,
}

case status_message[:status_code]
Expand Down Expand Up @@ -401,7 +420,7 @@ class Server < Sinatra::Base
end
message = run_command(command)
$logger.info("message: #{message} module_name: #{module_name}")
status_message = {:status => :success, :message => message.to_s, :module_name => module_name, :status_code => 202}
status_message = {:status => :success, :message => message.to_s, :module_name => module_name, :status_code => 200}
notify_slack(status_message) if slack?
notify_rocketchat(status_message) if rocketchat?
status_message.to_json
Expand Down Expand Up @@ -433,13 +452,13 @@ class Server < Sinatra::Base
end
message = run_command(command)
end
status_message = {:status => :success, :message => message.to_s, :branch => branch, :status_code => 202}
status_message = {:status => :success, :message => message.to_s, :branch => branch, :status_code => 200}
$logger.info("message: #{message} branch: #{branch}")
notify_slack(status_message) if slack?
notify_rocketchat(status_message) if rocketchat?
unless deleted
generate_types(branch) if types?
end
notify_slack(status_message) if slack?
notify_rocketchat(status_message) if rocketchat?
status_message.to_json
rescue => e
status_message = {:status => :fail, :message => e.message, :trace => e.backtrace, :branch => branch, :status_code => 500}
Expand Down

0 comments on commit 461f8d1

Please sign in to comment.