Class: BuildCLICommands

Inherits:
Object
  • Object
show all
Defined in:
lib/commands.rb

Overview

NOTE: This file is generated

Class Method Summary collapse

Class Method Details

.indexObject



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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/commands.rb', line 3

def self.index
  return {
    "ps": {
      cli_details: { :syntax => "build ps -a <app>", :description => "List all the processes of an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/ps?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body != "" && JSON.parse(res.body)&.key?("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{JSON.parse(res.body).[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     processes = JSON.parse(res.body).[]("processes")
                     if processes.empty?
                       puts(CLI::UI.fmt("{{green:›}}   No processes found for app #{app_name}"))
                       exit(1)
                     end
                     processes.each { |process,|
                       puts(CLI::UI.fmt("{{gray:===}} {{bold:{{green:#{process.[]("type")}}} ({{cyan:#{process.[]("size")}}}): {{white:#{process.[]("display")}}} ({{yellow:#{process.[]("quantity")}}})}}"))
                       process.[]("processes").each { |instance,|
                         started_at = Time.parse(instance.[]("started_at")).localtime
                         status = if instance.[]("status") == "Running"
                                    "{{green:up}}"
                                  else
                                    "{{red:down}}"
                                  end
                         days_ago = ((Time.now - started_at) / 86400).to_i
                         hours_ago = ((Time.now - started_at) / 3600).to_i % 24
                         minutes_ago = ((Time.now - started_at) / 60).to_i % 60
                         seconds_ago = (Time.now - started_at).to_i % 60
                         dotiw = ""
                         if days_ago > 0
                           dotiw = "#{days_ago}d "
                         end
                         if hours_ago > 0 || days_ago > 0
                           dotiw += "#{hours_ago}h "
                         end
                         if minutes_ago > 0 || hours_ago > 0 || days_ago > 0
                           dotiw += "#{minutes_ago}m "
                         end
                         if seconds_ago > 0 || minutes_ago > 0 || hours_ago > 0 || days_ago > 0
                           dotiw += "#{seconds_ago}s"
                         end
                         if dotiw == ""
                           dotiw = "0s"
                         end
                         puts(CLI::UI.fmt("{{white:#{process.[]("type")}.#{instance.[]("index")}}}: #{status} {{gray:#{started_at} (~ #{dotiw} ago)}}"))
                       }
                       puts("")
                     }
                   },
    },
    "ps:restart": {
      cli_details: { :syntax => "build ps:restart [PROCESS] -a <app>", :description => "Restart all the processes of an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     if args.length > 1
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Too many arguments provided}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     process = args.first
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     if process
                       query_params.[]=(:process, process)
                     end
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/ps/restart?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body != "" && JSON.parse(res.body)&.key?("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{JSON.parse(res.body).[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     if process
                       puts(CLI::UI.fmt("{{v}}   Restarted process {{cyan:#{process}}} for app {{cyan:#{app_name}}}"))
                     else
                       puts(CLI::UI.fmt("{{v}}   Restarted all processes for app {{cyan:#{app_name}}}"))
                     end
                   },
    },
    "config": {
      cli_details: { :syntax => "build config -a <app>", :description => "Get the config variables for an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/config?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body != "" && JSON.parse(res.body)&.key?("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{JSON.parse(res.body).[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     JSON.parse(res.body).each { |key, value|
                       puts("#{key}=#{value}")
                     }
                   },
    },
    "config:get": {
      cli_details: { :syntax => "build config:get VARIABLE -a <app>", :description => "Get a config variable for an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     config_variable = args.[](0)
                     if config_variable.nil? || config_variable.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required argument VARIABLE}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     query_params.[]=(:key, config_variable)
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/config/get?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body != "" && JSON.parse(res.body)&.key?("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{JSON.parse(res.body).[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     puts(res.body)
                   },
    },
    "config:set": {
      cli_details: { :syntax => "build config:set VARIABLE=VALUE -a <app>", :description => "Set config variables for an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     config_variables = args.map { |arg,|
                       [arg.split("=").[](0), arg.split("=").[](1..)&.join("=") || ""]
                     }.to_h
                     if config_variables.empty?
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:No config variables provided}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     query_params.[]=(:config, config_variables.to_json)
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/config/set?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body != "" && JSON.parse(res.body)&.key?("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{JSON.parse(res.body).[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     puts(CLI::UI.fmt("{{green:›}}   Config variable(s) set successfully"))
                   },
    },
    "config:unset": {
      cli_details: { :syntax => "build config:unset VARIABLE -a <app>", :description => "Un-Set config variables for an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     config_variables = args.compact
                     if config_variables.empty?
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:No config variables provided}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     query_params.[]=(:config, config_variables)
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/config/unset?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body != "" && JSON.parse(res.body)&.key?("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{JSON.parse(res.body).[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     puts(CLI::UI.fmt("{{green:›}}   Config variable(s) unset successfully"))
                   },
    },
    "apps:create": {
      cli_details: { :syntax => "apps:create [options] -a <app>", :description => "Create a new app",
                     :options => [["-t", "--team=<value>", String, "team to use"], ["-r", "--region=<value>", String, "specify region for the app to run in"], ["-s", "--stack=<value>", String, "the stack to create the app on"], ["-a", "--app=<value>", String, "(required) the name of the app to create"]] },
      cli_perform: Proc.new { |args, options|
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     app_name = options.app
                     if app_name.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: no app name specified"))
                       exit(1)
                     end
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     if options.team
                       query_params.[]=(:team, options.team)
                     end
                     if options.region
                       query_params.[]=(:region, options.region)
                     end
                     if options.stack
                       query_params.[]=(:stack, options.stack)
                     end
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/apps/create?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       puts(CLI::UI.fmt("{{red:›}}   Error: not authorized to create app (#{app_name})"))
                       exit(1)
                     end
                     app_name = JSON.parse(res.body).[]("name")
                     puts("https://#{app_name}.#{region}.antimony.io | #{app_name}")
                   },
    },
    "login": {
      cli_details: { :syntax => "build login", :description => "Login to your Build account", :options => [] },
      cli_perform: Proc.new { |args, options|
                     input = CLI::UI.any_key("Press any key to open up the browser to login or q to exit")
                     if input.downcase == "q"
                       exit
                     end
                     (user_token, user_email) = nil
                     client_secret = SecureRandom.uuid
                     oauth_url = "#{ANTIMONY_HOST}/cli_auth/authorize/#{client_secret}"
                     puts(CLI::UI.fmt("Opening browser to {{underline:#{oauth_url}}}"))
                     CLI::UI::Spinner.spin("Waiting for login") { |spinner,|
                       Launchy.open(oauth_url)
                       poll_interval = 1
                       timeout = (5 * 60)
                       start_time = Time.now
                       loop {
                         response = HTTParty.get("#{ANTIMONY_HOST}/api/cli_auth/resolve/#{client_secret}")
                         if response.code == 200 && response.[]("code") == "unresolved"
                           sleep(poll_interval)
                         else
                           if response.code == 200 && response.[]("code") == "resolved"
                             user_token = response.[]("token")
                             user_email = response.[]("email")
                             break
                           else
                             raise("Error: #{response.code}")
                           end
                         end
                         if Time.now - start_time > timeout
                           break
                         end
                       }
                     }
                     if user_token.nil? || user_email.nil? || user_token.empty? || user_email.empty?
                       puts(CLI::UI.fmt("{{red:Login failed}}"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_netrc.[]=("build.io", ["#{user_email}", "#{user_token}"])
                     user_netrc.save
                     puts(CLI::UI.fmt("Logged in as {{green:#{user_email}}}"))
                   },
    },
    "logs": {
      cli_details: { :syntax => "build logs -t -a <app>", :description => "Display recent log output",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"], ["-t", "--tail", TrueClass, "continually stream logs"], ["-n", "--num=<value>", String, "number of lines to show"], ["-p", "--process=<value>", String, "show only logs for a specific Procfile process"], ["-s", "--source=<value>", String, "show only logs from a specific source (such as app or build)"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     if options.num
                       query_params.[]=(:num, options.num)
                     end
                     if options.process
                       query_params.[]=(:process, options.process)
                     end
                     if options.source
                       query_params.[]=(:source, options.source)
                     end
                     if options.trace
                       query_params.[]=(:tail, options.trace)
                     end
                     if options.tail
                       query_params.[]=(:tail, options.tail)
                     end
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/apps/#{app_name}/logs/log_url?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body != "" && JSON.parse(res.body)&.key?("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{JSON.parse(res.body).[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     log_url = res.[]("url")
                     res = HTTParty.get(log_url, { timeout: 1000000 }) { |fragment,|
                       unless fragment.empty?
                         puts(fragment)
                       end
                     }
                     if res.code != 200
                       puts(CLI::UI.fmt("{{red:›}}   Error: Connection to logs failed."))
                       puts(CLI::UI.fmt("{{red:›}}"))
                       puts(CLI::UI.fmt("{{red:›}}   Error ID: connection_failed"))
                       exit(1)
                     end
                   },
    },
    "whoami": {
      cli_details: { :syntax => "build whoami", :description => "Display the current logged in user",
                     :options => [] },
      cli_perform: Proc.new { |args, options|
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/whoami",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     puts(JSON.parse(res.body).[]("email"))
                   },
    },
    "run": {
      cli_details: { :syntax => "build run COMMAND -a <app>", :description => "Run a once-off process in an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     if args.empty?
                       puts(CLI::UI.fmt("{{red:›}}   Error: no command provided"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     if options.app
                       query_params.[]=(:app, options.app)
                     end
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/run?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body != "" && JSON.parse(res.body)&.key?("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{JSON.parse(res.body).[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     ruby = JSON.parse(res.body).[]("ruby")
                     require("shellwords")
                     command = "/cnb/lifecycle/launcher #{Shellwords.join(args)}"
                     eval(ruby)
                   },
    }
  }
end