module NexClient::Commands::Addons

Constants

ADDONS_HEADERS
ADDONS_TITLE
OPTS_HEADERS
OPTS_TITLE
SCALING_DIRECTIONS

Public Class Methods

create(args,opts) click to toggle source
# File lib/nex_client/commands/addons.rb, line 93
def self.create(args,opts)
  svc_name,app_name = args
  app = NexClient::App.find(name: app_name).first

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

  # Attributes
  attrs = {}
  attrs[:service] = svc_name
  attrs[:container_size] = opts.size if opts.size.present?

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

  addon = NexClient::Addon.new(attrs)
  addon.relationships.attributes = { app: { data: { type: 'apps', id: app.id } } }
  addon.save

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

  # Display app
  self.display_addons(NexClient::Addon.includes(:app).find(addon.id).first)
end
destroy(args,opts) click to toggle source
# File lib/nex_client/commands/addons.rb, line 150
def self.destroy(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

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

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

  e.destroy
  success("Successfully destroyed addon: #{name}")
end
display_addons(list) click to toggle source
# File lib/nex_client/commands/addons.rb, line 222
def self.display_addons(list)
  table = Terminal::Table.new title: ADDONS_TITLE, headings: ADDONS_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) click to toggle source
# File lib/nex_client/commands/addons.rb, line 232
def self.display_id(e)
  table = Terminal::Table.new do |t|
    t.add_row(['ADDON ID',e.id])
  end
  puts table
  puts "\n"
end
display_options(list) click to toggle source
# File lib/nex_client/commands/addons.rb, line 240
def self.display_options(list)
  table = Terminal::Table.new title: OPTS_TITLE, headings: OPTS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      e.each { |k,v| t.add_row([k,v]) }
    end
  end
  puts table
  puts "\n"
end
extract_config_options(cli_opts) click to toggle source
# File lib/nex_client/commands/addons.rb, line 273
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_app(record) click to toggle source
# File lib/nex_client/commands/addons.rb, line 263
def self.format_app(record)
  return '-' unless a = record.app
  a.name
end
format_node_count(record) click to toggle source
# File lib/nex_client/commands/addons.rb, line 268
def self.format_node_count(record)
  return '-' unless record.node_count && record.max_node_count
  "#{record.node_count}/#{record.max_node_count}"
end
format_record(record) click to toggle source
# File lib/nex_client/commands/addons.rb, line 250
def self.format_record(record)
  app = self.format_app(record)
  node_count = self.format_node_count(record)
  [
    record.name,
    record.status,
    record.service,
    record.container_size,
    node_count,
    app
  ]
end
info(args,opts) click to toggle source
# File lib/nex_client/commands/addons.rb, line 37
def self.info(args,opts)
  name = args.first
  e = NexClient::Addon.includes(:app).find(name: name).first

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

  # Display app id
  self.display_id(e)

  # Display addon details
  self.display_addons(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 App
  Apps.display_apps(e.app)
end
list(args,opts) click to toggle source
# File lib/nex_client/commands/addons.rb, line 14
def self.list(args,opts)
  filters = {}
  filters[:status] = opts.status || ['active','restarting']
  filters[:service] = opts.service if opts.service.present?

  # All option
  filters = {} if opts.all

  # Scope to specific app
  filters[:'app.name'] = args.first if args.first.present?

  # Create table
  list = NexClient::Addon.includes(:app).where(filters).order('status')
  self.display_addons(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_addons(list)
  end
end
logs(args,opts) click to toggle source
# File lib/nex_client/commands/addons.rb, line 63
def self.logs(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

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

  # Retrieve logs and display them
  logs = e.logs(tail: opts.tail).first
  self.display_logs(logs.log_ret)
end
restart(args,opts) click to toggle source
# File lib/nex_client/commands/addons.rb, line 200
def self.restart(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{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 addon: #{name}...")
end
scale(direction,args,opts) click to toggle source
# File lib/nex_client/commands/addons.rb, line 171
def self.scale(direction,args,opts)
  return false unless SCALING_DIRECTIONS.include?(direction.to_sym)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{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) click to toggle source

SSH to the addon

# File lib/nex_client/commands/addons.rb, line 79
def self.ssh(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

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

  # Perform command
  perform_ssh_cmd(e.ssh_cmd_template)
end
update(args,opts) click to toggle source
# File lib/nex_client/commands/addons.rb, line 125
def self.update(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

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

  # Attributes
  attrs = {}
  attrs[:container_size] = opts.size if opts.size.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))

  # Update
  e.update_attributes(attrs)

  # Display app
  self.display_addons(NexClient::Addon.includes(:app).find(e.id).first)
end