diff --git a/manifests/params.pp b/manifests/params.pp index 2168875d..c522df5c 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -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 diff --git a/manifests/webhook/config.pp b/manifests/webhook/config.pp index 162b1753..dc2d8547 100644 --- a/manifests/webhook/config.pp +++ b/manifests/webhook/config.pp @@ -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, @@ -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, diff --git a/templates/webhook.bin.erb b/templates/webhook.bin.erb index a2c89989..835f2f6c 100755 --- a/templates/webhook.bin.erb +++ b/templates/webhook.bin.erb @@ -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: @@ -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 @@ -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 @@ -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 = { @@ -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] @@ -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) @@ -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] @@ -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 @@ -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}