This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
/
client.rb
335 lines (284 loc) · 9.79 KB
/
client.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
323
324
325
326
327
328
329
330
331
332
333
334
335
require 'restclient'
require 'launchy'
require 'optparse'
require 'netrc'
require 'colored'
module Heroku
module Kensa
class Client
attr_accessor :options
def initialize(args, options = {})
@args = args
@options = OptParser.parse(args).merge(options)
end
class CommandInvalid < Exception; end
def run!
command = @args.shift || @options[:command]
raise CommandInvalid unless command && respond_to?(command)
send(command)
end
def init
manifest = Manifest.new(@options)
protect_current_manifest!
manifest.write
screen.message "Initialized new addon manifest in #{filename}\n"
if @options[:foreman]
screen.message "Initialized new .env file\n"
end
end
def test
case check = @args.shift
when "manifest"
run_check ManifestCheck
when "provision"
run_check ManifestCheck, ProvisionCheck
when "deprovision"
id = @args.shift || abort("! no id specified; see usage")
run_check ManifestCheck, DeprovisionCheck, :id => id
when "planchange"
id = @args.shift || abort("! no id specified; see usage")
plan = @args.shift || abort("! no plan specified; see usage")
run_check ManifestCheck, PlanChangeCheck, :id => id, :plan => plan
when "sso"
id = @args.shift || abort("! no id specified; see usage")
run_check ManifestCheck, SsoCheck, :id => id
when "all"
run_check AllCheck
when nil
run_check AllCheck
else
abort "! Unknown test '#{check}'; see usage"
end
end
def run
abort "! missing command to run; see usage" if @args.empty?
run_check AllCheck, :args => @args
end
def sso
id = @args.shift || abort("! no id specified; see usage")
data = decoded_manifest
sso = Sso.new(data.merge(@options).merge(:id => id)).start
puts sso.message
Launchy.open sso.sso_url
end
def push
user, password = ask_for_credentials
host = heroku_host
data = decoded_manifest
resource = RestClient::Resource.new(host, user, password)
manifest = resource['provider/addons'].post(resolve_manifest, headers)
puts "-----> Manifest for \"#{data['id']}\" was pushed successfully"
puts " Continue at #{(heroku_host)}/provider/addons/#{data['id']}"
# Update local manifest with response from addons.heroku.com
File.open(filename, 'w') { |f| f.puts manifest } unless manifest.strip.empty?
rescue RestClient::UnprocessableEntity, RestClient::BadRequest => e
abort("FAILED: #{e.response}")
rescue RestClient::Unauthorized
abort("Authentication failure")
rescue RestClient::Forbidden
abort("Not authorized to push this manifest. Please make sure you have permissions to push it")
end
def pull
addon = @args.first || abort('usage: kensa pull <add-on name>')
protect_current_manifest!
user, password = ask_for_credentials
host = heroku_host
resource = RestClient::Resource.new(host, user, password)
manifest = resource["provider/addons/#{addon}"].get(headers)
File.open(filename, 'w') { |f| f.puts manifest }
puts "-----> Manifest for \"#{addon}\" received successfully"
end
def version
puts "Kensa #{VERSION}"
end
private
def protect_current_manifest!
if manifest_exists?
print "Manifest already exists. Replace it? (y/n) "
abort unless gets.strip.downcase == 'y'
puts
end
end
def filename
@options[:filename]
end
def screen
@screen ||= @options[:silent] ? NilScreen.new : Screen.new
end
def headers
{ :accept => :json, "X-Kensa-Version" => "1", "User-Agent" => "kensa/#{VERSION}" }
end
def heroku_host
ENV['ADDONS_URL'] || 'https://addons.heroku.com'
end
def resolve_manifest
if manifest_exists?
File.read(filename)
else
abort("fatal: #{filename} not found")
end
end
def decoded_manifest
OkJson.decode(resolve_manifest)
rescue OkJson::Error => e
raise CommandInvalid, "#{filename} includes invalid JSON"
end
def manifest_exists?
File.exists?(filename)
end
def run_check(*args)
options = {}
options = args.pop if args.last.is_a?(Hash)
args.each do |klass|
data = decoded_manifest
check = klass.new(data.merge(@options.merge(options)), screen)
result = check.call
screen.finish
exit 1 if !result && !(@options[:test])
end
end
def running_on_windows?
RUBY_PLATFORM =~ /mswin32|mingw32/
end
def echo_off
system "stty -echo"
end
def echo_on
system "stty echo"
end
def ask_for_credentials
netrc_creds = Netrc.read['api.heroku.com']
if netrc_creds
print "Found credentials for #{netrc_creds.login}, proceed? (y/N) "
if gets.chomp.downcase == "y"
return [ netrc_creds.login, netrc_creds.password]
end
end
puts "Enter your Heroku Provider credentials."
print "Email: "
user = gets.strip
print "Password: "
password = running_on_windows? ? ask_for_password_on_windows : ask_for_password
[ user, password ]
end
def ask_for_password_on_windows
require "Win32API"
char = nil
password = ''
while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do
break if char == 10 || char == 13 # received carriage return or newline
if char == 127 || char == 8 # backspace and delete
password.slice!(-1, 1)
else
password << char.chr
end
end
puts
return password
end
def ask_for_password
echo_off
password = gets.strip
puts
echo_on
return password
end
class Screen
def test(msg)
$stdout.puts
$stdout.puts
$stdout.print "Testing #{msg}"
end
def check(msg)
$stdout.puts
$stdout.print " Check #{msg}"
end
def error(msg)
$stdout.print "\n", " #{msg}".red
end
def warning(msg)
$stdout.print "\n", " #{msg}".yellow
end
def result(status)
msg = status ? "[PASS]".green : "[FAIL]".red.bold
$stdout.print " #{msg}"
end
def message(msg)
$stdout.print msg
end
def finish
$stdout.puts
$stdout.puts
$stdout.puts "done."
end
end
class OptParser
def self.parse(args)
defaults.merge(self.parse_options(args))
end
def self.defaults
{
:filename => 'addon-manifest.json',
:env => "test",
:async => false,
}
end
# OptionParser errors out on unnamed options so we have to pull out all the --flags and --flag=somethings
KNOWN_ARGS = %w{file async production without-sso help plan version sso foreman template}
def self.pre_parse(args)
args.partition do |token|
token.match(/^--/) && !token.match(/^--(#{KNOWN_ARGS.join('|')})/)
end.reverse
end
def self.parse_provision(flags, args)
{}.tap do |options|
flags.each do |arg|
key, value = arg.split('=')
unless value
peek = args[args.index(key) + 1]
value = peek && !peek.match(/^--/) ? peek : 'true'
end
key = key.sub(/^--/,'')
options[key] = value
end
end
end
def self.parse_command_line(args)
{}.tap do |options|
OptionParser.new do |o|
o.on("-f file", "--filename") { |filename| options[:filename] = filename }
o.on("--async") { options[:async] = true }
o.on("--production") { options[:env] = "production" }
o.on("--without-sso") { options[:sso] = false }
o.on("-h", "--help") { command = "help" }
o.on("-p plan", "--plan") { |plan| options[:plan] = plan }
o.on("-v", "--version") { options[:command] = "version" }
o.on("-s sso", "--sso") { |method| options[:method] = method }
o.on("--foreman") { options[:foreman] = true }
o.on("-t name", "--template") do |template|
options[:template] = template
end
#note: have to add these to KNOWN_ARGS
begin
o.parse!(args)
rescue OptionParser::InvalidOption => e
raise CommandInvalid, e.message
end
end
end
end
def self.parse(args)
if args[0] == 'test' && args[1] == 'provision'
safe_args, extra_params = self.pre_parse(args)
self.defaults.tap do |options|
options.merge! self.parse_command_line(safe_args)
options.merge! :options => self.parse_provision(extra_params, args)
end
else
self.defaults.merge(self.parse_command_line(args))
end
end
end
end
end
end