This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.rb
322 lines (277 loc) · 7.72 KB
/
init.rb
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
module Heroku::Command
class Ranger < BaseWithApp
def initialize(*args)
super
@config_vars = heroku.config_vars(app)
@ranger_api_key = ENV["RANGER_API_KEY"] || @config_vars["RANGER_API_KEY"]
@ranger_app_id = ENV["RANGER_APP_ID"] || @config_vars["RANGER_APP_ID"]
@app_owner = heroku.info(app)[:owner]
abort(" ! Please add the ranger addon first.") unless @ranger_api_key
end
# ranger
#
# show current app status
#
def index
if get_status
dependencies = @current_status
puts "\nRanger Status"
puts "------------------------------------------"
dependencies.each do |record|
url = record["dependency"]["url"]
code = record["dependency"]["latest_response_code"]
puts "#{url} #{up_or_down(code)}"
end
watchers_list
else
no_domains_monitored
end
end
# ranger:domains
#
# list domains being monitored
#
def domains
if args.empty?
domain_list
return
end
case args.shift
when "add"
url = args.shift
create_dependency(url)
puts "Added #{url} to the monitoring list"
return
when "remove"
url = args.shift
remove_url(url)
return
when "clear"
clear_all_dependencies
puts "All domains removed from the monitoring list"
return
end
raise(CommandFailed, "see: heroku help ranger")
end
# ranger:add_domain DOMAIN
#
# start monitoring a domain
#
def add_domain
url = args.shift
create_dependency(url)
puts "Added #{url} to the monitoring list"
end
# ranger:remove_domain DOMAIN
#
# stop monitoring a domain
#
def remove_domain
url = args.shift
remove_url(url)
end
# ranger:clear_domains
#
# stop monitoring all domains
#
def clear_domains
clear_all_dependencies
puts "All domains removed from the monitoring list"
end
# ranger:watchers
#
# list current app watchers
#
def watchers
if args.empty?
watchers_list
return
end
case args.shift
when "add"
email = args.shift
create_watcher(email)
puts "Added #{email} as a watcher"
return
when "remove"
email = args.shift
delete_watcher(email)
return
when "clear"
clear_all_watchers
puts "All watchers removed"
return
end
raise(CommandFailed, "see: heroku help ranger")
end
# ranger:add_watcher EMAIL
#
# add a watcher
#
def add_watcher
email = args.shift
create_watcher(email)
puts "Added #{email} as a watcher"
end
# ranger:remove_watcher EMAIL
#
# remove a watcher
#
def remove_watcher
email = args.shift
if delete_watcher_from_email(email)
puts "Removed #{email} as a watcher"
else
puts "No watchers with that email found in the watcher list"
end
end
# ranger:clear_watchers
#
# remove all watchers
#
def clear_watchers
clear_all_watchers
puts "All watchers removed"
end
protected
def authenticated_resource(path)
host = "https://rangerapp.com/api/v1"
RestClient::Resource.new("#{host}#{path}")
end
def up_or_down(code)
case code
when 200
"is UP"
when nil
"=> not checked yet"
else
"is DOWN with status code #{code}"
end
end
def get_status
resource = authenticated_resource("/status/#{@ranger_app_id}?api_key=#{@ranger_api_key}")
begin
@current_status = MultiJson.load(resource.get)
true
rescue MultiJson::ParseError => e
false
end
end
def get_dependencies
resource = authenticated_resource("/apps/#{@ranger_app_id}/dependencies.json?api_key=#{@ranger_api_key}")
resource.get
end
def create_dependency(url)
resource = authenticated_resource("/apps/#{@ranger_app_id}/dependencies.json")
params = { :dependency => { :name => "Website", :url => url, :check_every => "1" }, :api_key => @ranger_api_key}
resource.post(params)
end
def remove_url(url)
if delete_dependency_from_url(url)
puts "Removed #{url} from the monitoring list"
else
puts "No domain with that URL found in the monitoring list"
end
end
def delete_dependency_from_url(url)
dependencies = MultiJson.load(get_dependencies)
dependency_id = nil
dependencies.each do |record|
if record["dependency"]["url"] == url
dependency_id = record["dependency"]["id"]
delete_dependency(dependency_id)
end
end
return false if dependency_id.nil?
return true
end
def delete_dependency(id)
resource = authenticated_resource("/apps/#{@ranger_app_id}/dependencies/#{id}.json?api_key=#{@ranger_api_key}")
begin
resource.delete
rescue RestClient::ResourceNotFound => e
false
end
end
def clear_all_dependencies
dependencies = MultiJson.load(get_dependencies)
dependencies.each do |record|
delete_dependency(record["dependency"]["id"])
end
end
def clear_all_watchers
watchers = get_watchers
watchers.each do |record|
delete_watcher_from_id(record["watcher"]["id"])
end
end
def no_domains_monitored
puts "\n---------------------------------------------"
puts "No domains are being monitored for this app."
puts "-----------------------------------------------"
puts "\nMonitor a domain like this:"
puts "\n heroku ranger:add_domain http://yourapp.heroku.com\n\n"
end
def domain_list
if get_status
dependencies = @current_status
puts "\nDomains Being Monitored"
puts "------------------------------------------"
dependencies.each do |record|
url = record["dependency"]["url"]
code = record["dependency"]["latest_response_code"]
puts "#{url}"
end
puts ""
else
no_domains_monitored
end
end
def get_watchers
resource = authenticated_resource("/apps/#{@ranger_app_id}/watchers.json?api_key=#{@ranger_api_key}")
@current_watchers = MultiJson.load(resource.get)
end
def watchers_list
get_watchers
puts "\nApp Watchers"
puts "------------------------------------------"
@current_watchers.each do |record|
email = record["watcher"]["email"]
puts "#{email}"
end
puts ""
end
def create_watcher(email)
resource = authenticated_resource("/apps/#{@ranger_app_id}/watchers.json")
params = { :watcher => { :email => email }, :api_key => @ranger_api_key}
resource.post(params)
end
def delete_watcher(email)
if delete_watcher_from_email(email)
puts "Removed #{email} as a watcher"
else
puts "No watchers with that email found in the watcher list"
end
end
def delete_watcher_from_email(email)
watchers = get_watchers
watcher_id = nil
watchers.each do |record|
if record["watcher"]["email"] == email
watcher_id = record["watcher"]["id"]
delete_watcher(watcher_id)
end
end
return false if watcher_id.nil?
return true
end
def delete_watcher_from_id(id)
resource = authenticated_resource("/apps/#{@ranger_app_id}/watchers/#{id}.json?api_key=#{@ranger_api_key}")
begin
resource.delete
rescue RestClient::ResourceNotFound => e
false
end
end
end
end