Module: NexClient::Commands::Apps

Extended by:
Helpers
Defined in:
lib/nex_client/commands/apps.rb

Constant Summary collapse

SCALING_DIRECTIONS =
[:up,:down]
APPS_TITLE =
"App Details".colorize(:green)
APPS_HEADERS =
['name','status','image','ssl','storage','preferred region','size','nodes','owner','description','tags'].map(&:upcase)
OPTS_TITLE =
"Options".colorize(:blue)
OPTS_HEADERS =
['key','value'].map(&:upcase)
SCM_TITLE =
"Source Control Management".colorize(:yellow)
SCM_HEADERS =
['key','value'].map(&:upcase)

Constants included from Helpers

Helpers::LOG_COLORS, Helpers::VARS_HEADERS, Helpers::VARS_TITLE

Class Method Summary collapse

Methods included from Helpers

display_events, display_logs, display_raw_vars, display_record_errors, display_vars, display_yaml_vars, error, events, format_cmd, hash_from_file, perform_ssh_cmd, success, vars_from_file, vars_from_plain_file, vars_from_yaml_file, with_cert_identity

Class Method Details

.create(args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/apps.rb', line 126

def self.create(args,opts)
  image_info = args.first.split(':')
  attrs = { image: image_info[0], image_tag: image_info[1] }

  # Meta attributes
  attrs[:ssl_enabled] = !opts.no_ssl if opts.no_ssl.present?
  attrs[:persistent_storage] = opts.storage if opts.storage.present?
  attrs[:container_size] = opts.size if opts.size.present?
  attrs[:description] = opts.desc if opts.desc.present?
  attrs[:tag_list] = opts.tags if opts.tags.present?
  attrs[:preferred_region] = opts.region if opts.region.present?

  # Additional options (log drain, region balancing)
  attrs[:opts] = self.extract_config_options(opts)

  # Env variables via command line
  if opts.env.present?
    attrs[:vars] ||= {}
    opts.env.each do |e|
      key,val = e.split('=',2)
      attrs[:vars][key] = val
    end
  end

  # Env variables from file
  if opts.env_file.present?
    attrs[:vars] ||= {}
    vars_from_file(opts.env_file) do |key,val|
      attrs[:vars][key] = val
    end
  end

  # Create the app
  e = NexClient::App.new(attrs)

  # Add owner if specified
  if opts.owner.present?
    o = NexClient::Organization.find(handle:opts.owner).first
    unless o
      error("Error! Could not find organization: #{opts.owner}")
      return false
    end
    e.relationships.attributes = { owner: { data: { type: o.type, id: o.id } } }
  end

  # Save the resource
  e.save

  # Display errors if any
  if e.errors.any?
    self.display_record_errors(e)
    return false
  end

  # Display app
  self.display_apps(NexClient::App.includes(:owner).find(e.id).first)
end

.destroy(args, opts) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/nex_client/commands/apps.rb', line 225

def self.destroy(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Ask confirmation
  answer = ask("Enter the name of this app to confirm: ")
  unless answer == e.name
    error("Aborting deletion...")
    return false
  end

  e.destroy
  success("Successfully destroyed app: #{name}")
end

.display_apps(list) ⇒ Object



434
435
436
437
438
439
440
441
442
# File 'lib/nex_client/commands/apps.rb', line 434

def self.display_apps(list)
  table = Terminal::Table.new title: APPS_TITLE, headings: APPS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row(self.format_record(e))
    end
  end
  puts table
  puts "\n"
end

.display_id(e) ⇒ Object



426
427
428
429
430
431
432
# File 'lib/nex_client/commands/apps.rb', line 426

def self.display_id(e)
  table = Terminal::Table.new do |t|
    t.add_row(['APP ID',e.id])
  end
  puts table
  puts "\n"
end

.display_options(list) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/nex_client/commands/apps.rb', line 444

def self.display_options(list)
  table = Terminal::Table.new title: OPTS_TITLE, headings: OPTS_HEADERS, style: { all_separators: true} do |t|
    [list].flatten.compact.each do |e|
      e.each do |k,v|
        val = v.is_a?(Hash) ? JSON.pretty_generate(v) : v
        t.add_row([k,val])
      end
    end
  end
  puts table
  puts "\n"
end

.display_scm(list) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/nex_client/commands/apps.rb', line 457

def self.display_scm(list)
  table = Terminal::Table.new title: SCM_TITLE, headings: SCM_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row(['provider',e['provider']])
      t.add_row(['repo',e['repo']])
      t.add_row(['branch',e['branch']])
      t.add_row(['last_commit',e['last_commit'] || '- nothing pushed yet -'])
      t.add_row(['webhook',e['webhook_enabled'].to_s == 'true' ? e['webhook_url'] : '- Not enabled -'])
    end
  end
  puts table
  puts "\n"
end

.extract_config_options(cli_opts) ⇒ Object



499
500
501
502
503
504
505
506
507
# File 'lib/nex_client/commands/apps.rb', line 499

def self.extract_config_options(cli_opts)
  hash = {}
  %w(http_log_drain region_balancing).each do |opt_name|
    if cli_opts.method_missing(opt_name.to_sym).present?
      hash[opt_name.to_s] = cli_opts.method_missing(opt_name.to_sym)
    end
  end
  return hash
end

.format_node_count(record) ⇒ Object



494
495
496
497
# File 'lib/nex_client/commands/apps.rb', line 494

def self.format_node_count(record)
  return '-' unless record.node_count && record.max_node_count
  "#{record.node_count}/#{record.max_node_count}"
end

.format_owner(record) ⇒ Object



489
490
491
492
# File 'lib/nex_client/commands/apps.rb', line 489

def self.format_owner(record)
  return '-' unless o = record.owner
  "#{o.type[0]}:#{o.handle}"
end

.format_record(record) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/nex_client/commands/apps.rb', line 471

def self.format_record(record)
  owner = self.format_owner(record)
  node_count = self.format_node_count(record)
  [
    record.name,
    record.status,
    [record.image,record.image_tag].compact.join(':'),
    record.ssl_enabled,
    record.persistent_storage,
    record.preferred_region,
    record.container_size,
    node_count,
    owner,
    record.description,
    (record.tag_list || []).join(',')
  ]
end

.info(args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/apps.rb', line 43

def self.info(args,opts)
  name = args.first
  e = NexClient::App.includes(:addons,:owner,:domains,:ssl_certificates, :cube_instances).find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Display app id
  self.display_id(e)

  # Display app details
  self.display_apps(e)

  # Display all vars
  self.display_vars(e.all_vars,!opts.full_vars)

  # Display options
  self.display_options(e.opts) if e.respond_to?(:opts)

  # Display Source Control Management
  self.display_scm(e.scm)

  # Display custom domains
  Domains.display_domains(e.domains)

  # Display SSL certificates
  SslCertificates.display_certs(e.ssl_certificates)

  # Display all addons
  Addons.display_addons(e.addons.select { |a| a.status == 'active'})

  # Display Cubes
  CubeInstances.display_cubes(e.cube_instances)
end

.list(args, opts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nex_client/commands/apps.rb', line 19

def self.list(args,opts)
  filters = {}
  filters[:status] = opts.status || ['active','restarting']
  filters[:ssl_enabled] = opts.ssl if opts.ssl.present?
  filters[:persistent_storage] = opts.storage if opts.storage.present?
  filters[:'owner.handle'] = opts.owner if opts.owner.present?
  filters[:image] = opts.image if opts.image.present?
  filters[:tags] = opts.tags if opts.tags.present?

  # All option
  filters = {} if opts.all

  # Display
  list = NexClient::App.includes(:owner).where(filters).order('status')
  self.display_apps(list)

  # Loop through results
  while (list.pages.links||{})['next']
    return true if ask("Press enter for next page ('q' to quit)") =~ /q/
    list = list.pages.next
    self.display_apps(list)
  end
end

.logs(args, opts) ⇒ Object

Retrieve application logs from all containers



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nex_client/commands/apps.rb', line 96

def self.logs(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Retrieve logs and display them
  logs = e.logs(tail: opts.tail).first
  self.display_logs(logs.log_ret)
end

.manage_scm(args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/apps.rb', line 387

def self.manage_scm(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Link SCM
  if opts.link
    provider,repo,branch = opts.link.split(':')
    credentials = opts.credentials
    attrs = {
      provider: provider,
      config: {
        repo: repo,
        branch: branch,
        credentials: credentials
      }
    }

    # Update and reload the resource
    e.link_scm({data: { attributes: attrs } })
    e = NexClient::App.find(e.id).first
  end

  # Unlink SCM
  if !opts.link && opts.unlink
    # Update and reload the resource
    e.unlink_scm
    e = NexClient::App.find(e.id).first
  end

  # Display SCM
  self.display_scm(e.scm)
end

.manage_vars(args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/apps.rb', line 331

def self.manage_vars(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Add/Delete vars
  if opts.add.present? || opts.delete.present? || opts.env_file.present?
    vars = (e.vars || {}).dup

    # Add vars
    (opts.add || []).each do |v|
      key,val = v.split('=',2)
      vars[key] = val
    end

    # Add vars from file
    if opts.env_file.present?
      vars_from_file(opts.env_file,opts.env_prefix) do |key,val|
        vars[key] = val
      end
    end

    # Delete vars
    (opts.delete || []).each { |k| vars.delete(k) }
    # Delete all vars
    if opts.delete_all
      # Ask confirmation
      answer = ask('Enter the name of this app to confirm: ')
      unless answer == e.name
        error('Aborting vars deletion')
        return false
      end
      vars = {}
    end

    # Update and reload the resource
    e.update_attributes(vars: vars)
    e = NexClient::App.find(e.id).first
    success("Successfully updated env vars. You may want to restart your app...")
  end

  # Display all vars
  if opts.yaml
    self.display_yaml_vars(e.all_vars)
  elsif opts.raw
    self.display_raw_vars(e.all_vars)
  else
    self.display_vars(e.all_vars,!opts.full_vars)
  end
end

.open(args, opts) ⇒ Object

Open application in Browser



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nex_client/commands/apps.rb', line 82

def self.open(args,opts)
  name = args.first
  app = NexClient::App.includes(:domains).find(name: name).first
  # Display error
  unless app
    error("Error! Could not find app: #{name}")
    return false
  end
  url = app.preferred_url
  success "Opening #{url}"
  Launchy.open(url)
end

.restart(args, opts) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/nex_client/commands/apps.rb', line 280

def self.restart(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Perform
  e.restart

  # Display errors if any
  if e.errors.any?
    display_record_errors(e)
    return false
  end

  success("Initiated phased restart for app: #{name}...")
end

.scale(direction, args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/apps.rb', line 302

def self.scale(direction,args,opts)
  return false unless SCALING_DIRECTIONS.include?(direction.to_sym)
  name = args.first
  e = NexClient::App.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Scaling attributes
  attrs = {}
  count = opts.count || 1
  attrs[:count] = count
  attrs[:preferred_region] = opts.region if opts.region.present?

  # Perform
  e.send("scale_#{direction}",{data: { attributes: attrs } })

  # Display errors if any
  if e.errors.any?
    display_record_errors(e)
    return false
  end

  success("Successfully requested to scale #{name} #{direction} by #{count} #{'node'.pluralize(count)}...")
end

.ssh(args, opts) ⇒ Object

SSH to the app



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nex_client/commands/apps.rb', line 112

def self.ssh(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Perform command
  perform_ssh_cmd(e.ssh_cmd_template)
end

.transfer(args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/apps.rb', line 246

def self.transfer(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Get owner
  handle = args.last
  o = NexClient::Organization.find(handle: handle).first ||
    NexClient::User.find(handle: handle).first

  # Display error if owner not found
  unless o
    error("Error! Could not find owner: #{handle}")
    return false
  end

  # Update and save record
  e.relationships.attributes = { owner: { data: { type: o.type, id: o.id } } }
  e.save

  # Display errors if any
  if e.errors.any?
    display_record_errors(e)
    return false
  end

  success("Successfully transfered #{name} to #{handle}")
end

.update(args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/apps.rb', line 184

def self.update(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find app: #{name}")
    return false
  end

  # Attributes
  attrs = {}
  attrs[:container_size] = opts.size if opts.size.present?
  attrs[:description] = opts.desc if opts.desc.present?
  attrs[:tag_list] = opts.tags if opts.tags.present?
  attrs[:image_tag] = opts.image_tag if opts.image_tag.present?
  attrs[:preferred_region] = opts.region if opts.region.present?

  # Additional options (log drain, region balancing)
  # Hash#dup is required for the resource to detect changes
  attrs[:opts] = (e.opts || {}).dup.merge(self.extract_config_options(opts))

  if opts.waf_rules
    waf_rules = begin
      if opts.waf_rules.is_a?(String)
        hash_from_file(opts.waf_rules)
      else
        val = ask("Copy/paste your WAF configuration below in JSON format:") { |q| q.gather = "" }
        JSON.parse(val.join(""))
      end
    end
    attrs[:opts]['waf_rules'] = waf_rules
  end

  # Update
  e.update_attributes(attrs)

  # Display app
  self.display_apps(NexClient::App.includes(:owner).find(e.id).first)
end