-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeman.coffee
88 lines (82 loc) · 3.01 KB
/
foreman.coffee
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Description
# A hubot script for interaction with foreman api
#
# Dependencies:
# None
#
# Configuration:
# FOREMAN_AUTH (e.g. user:password for Basic Auth)
# FOREMAN_URL (e.g. https://foreman.example.com)
#
# Commands:
#
# foreman s|search <pattern> - search and list of hosts and their last reports
# foreman c|classes <fqdn> - show puppet classes assigned to host
#
# Authors:
# strato190
#
foreman_url = process.env.FOREMAN_URL
foreman_auth = process.env.FOREMAN_AUTH
foreman_api_version = `process.env.FOREMAN_API ? process.env.FOREMAN_API : "1.1"`
module.exports = (robot) ->
robot.hear /foreman (s|search) (\w+)/i, (msg) ->
foremansearch msg
robot.hear /foreman (c|classes) (\w+)/i, (msg) ->
foremanpuppetclasses msg
foremansearch = (msg, query, cb) ->
data = []
port = ":443"
response = ""
uri = "/api/hosts?search=#{msg.match[2]}"
auth = 'Basic ' + new Buffer(foreman_auth).toString('base64') if foreman_auth
headers = { Accept: "application/json", 'Content-type': 'application/json' }
headers['Authorization'] = auth if auth
msg
.http(foreman_url + port + uri)
.headers(headers)
.get() (err, res, data) ->
unless res.statusCode is 200
console.log(res, uri, port, foreman_url, 'err', res.statusCode)
i = 0
body = JSON.parse(data)
switch foreman_api_version
when "1.1"
while i < body["results"].length
object = body["results"][i]
response += "#{i + 1}. #{object['name']} id: #{object['id']}\n"
response += "#{process.env.FOREMAN_URL}/hosts/#{object['name']}\n"
response += "#{process.env.FOREMAN_URL}/hosts/#{object['name']}/reports/last\n"
i++
else
while i < body.length
object = body[i]
for property of object
response += "#{i + 1}. #{object[property]['name']} id: #{object[property]['id']}\n"
response += "#{process.env.FOREMAN_URL}/hosts/#{object[property]['name']}\n"
response += "#{process.env.FOREMAN_URL}/hosts/#{object[property]['name']}/reports/last\n"
i++
msg.send response
foremanpuppetclasses = (msg) ->
data = []
port = ":443"
response = ""
serverregex = /(\bforeman\b)\ (\S+)\ (\S+)$/
host = msg.match.input.match(serverregex)[3]
uri = "/api/hosts/#{msg.match.input.match(serverregex)[3]}/puppetclasses"
auth = 'Basic ' + new Buffer(foreman_auth).toString('base64') if foreman_auth
headers = { Accept: "application/json", 'Content-type': 'application/json' }
headers['Authorization'] = auth if auth
msg
.http(foreman_url + port + uri)
.headers(headers)
.get() (err, res, data) ->
unless res.statusCode is 200
console.log(res, uri, port, foreman_url, 'err', res.statusCode)
body = JSON.parse(data)
response += "host: #{host}\n"
for property of body
object = body[property]
for item of object
response += "#{object[item].puppetclass['name']}\n"
msg.send response