Class: MCollective::Util::Playbook::Tasks::WebhookTask

Inherits:
Base
  • Object
show all
Defined in:
lib/mcollective/util/playbook/tasks/webhook_task.rb

Constant Summary collapse

USER_AGENT =
"Choria Playbooks http://choria.io".freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#description, #fail_ok

Instance Method Summary collapse

Methods inherited from Base

#initialize, #run_task, #startup_hook, #to_s

Methods included from MCollective::Util::Playbook::TemplateUtil

#__template_process_string, #__template_resolve, #t

Constructor Details

This class inherits a constructor from MCollective::Util::Playbook::Tasks::Base

Instance Attribute Details

#verify_sslObject (readonly)

Returns the value of attribute verify_ssl.



12
13
14
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 12

def verify_ssl
  @verify_ssl
end

Instance Method Details

#choriaObject



74
75
76
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 74

def choria
  @_choria ||= Util::Choria.new(false)
end

#create_uriObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 30

def create_uri
  uri = URI.parse(@uri)

  if @method == "GET"
    query = Array(uri.query)

    @data.each do |k, v|
      query << "%s=%s" % [CGI.escape(k), CGI.escape(v.to_s)]
    end

    uri.query = query.join("&") unless query.empty?
  end

  uri
end

#from_hash(data) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 19

def from_hash(data)
  @headers = data.fetch("headers", {})
  @data = data.fetch("data", {})
  @uri = data["uri"]
  @method = data.fetch("method", "POST").upcase
  @request_id = SSL.uuid
  @verify_ssl = Util.str_to_bool(data.fetch("verify_ssl", true))

  self
end

#http(uri) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 78

def http(uri)
  http = choria.https({:target => uri.host, :port => uri.port}, false)

  if verify_ssl
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  else
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  http.use_ssl = false if uri.scheme == "http"

  http
end

#http_get_request(uri) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 46

def http_get_request(uri)
  headers = {
    "User-Agent" => USER_AGENT,
    "X-Choria-Request-ID" => @request_id
  }.merge(@headers)

  Net::HTTP::Get.new(uri.request_uri, headers)
end

#http_post_request(uri) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 55

def http_post_request(uri)
  headers = {
    "Content-Type" => "application/json",
    "User-Agent" => USER_AGENT,
    "X-Choria-Request-ID" => @request_id
  }.merge(@headers)

  req = Net::HTTP::Post.new(uri.request_uri, headers)
  req.body = @data.to_json
  req
end

#http_request(uri) ⇒ Object



67
68
69
70
71
72
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 67

def http_request(uri)
  return http_get_request(uri) if @method == "GET"
  return http_post_request(uri) if @method == "POST"

  raise("Unknown request method %s" % @method)
end

#runObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 114

def run
  uri = create_uri
  resp = http(uri).request(http_request(uri))

  Log.debug("%s request to %s returned code %s with body: %s" % [@method, uri.to_s, resp.code, resp.body])

  if ["200", "201"].include?(resp.code)
    [true, "Successfully sent %s request to webhook %s with id %s" % [@method, @uri, @request_id], [resp.body]]
  else
    [false, "Failed to send %s request to webhook %s with id %s: %s: %s" % [@method, @uri, @request_id, resp.code, resp.body], [resp.body]]
  end
rescue
  msg = "Could not send %s to webhook %s: %s: %s" % [@method, @uri, $!.class, $!.to_s]
  Log.debug(msg)
  Log.debug($!.backtrace.join("\t\n"))

  [false, msg, []]
end

#to_execution_result(results) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 92

def to_execution_result(results)
  result = {
    "value" => results[2].first,
    "type" => "webhook",
    "fail_ok" => @fail_ok,
    "error" => {
      "msg" => results[1],
      "kind" => "choria.playbook/taskerror",
      "details" => {
        "task" => "webhook",
        "uri" => @uri,
        "method" => @method,
        "request_id" => @request_id
      }
    }
  }

  result.delete("error") if results[0]

  {create_uri.host => result}
end

#validate_configuration!Object



14
15
16
17
# File 'lib/mcollective/util/playbook/tasks/webhook_task.rb', line 14

def validate_configuration!
  raise("A uri is required") unless @uri
  raise("Only GET and POST is supported as methods") unless ["GET", "POST"].include?(@method)
end