module NexClient::Commands::CubeInstances

Constants

CUBES_HEADERS
CUBES_TITLE
SNAPSHOTS_HEADERS
SNAPSHOTS_TITLE

Public Class Methods

display_cubes(list) click to toggle source
# File lib/nex_client/commands/cube_instances.rb, line 205
def self.display_cubes(list)
  table = Terminal::Table.new title: CUBES_TITLE, headings: CUBES_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row(self.format_record(e))
    end
  end
  puts table
  puts "\n"
end
display_logs(logs) click to toggle source
# File lib/nex_client/commands/cube_instances.rb, line 235
def self.display_logs(logs)
  puts logs
  puts "\n"
end
display_snapshots(snapshots) click to toggle source
# File lib/nex_client/commands/cube_instances.rb, line 240
def self.display_snapshots(snapshots)
  table = Terminal::Table.new title: SNAPSHOTS_TITLE, headings: SNAPSHOTS_HEADERS do |t|
    [snapshots].flatten.compact.each do |e|
      t.add_row([
        e['created_at'],
        e['ip_address'],
        e['path']
      ])
    end
  end
  puts table
  puts "\n"
end
download(args,opts) click to toggle source

Download a file from a container

# File lib/nex_client/commands/cube_instances.rb, line 114
def self.download(args,opts)
  id = args.first
  remote_path = args[1]
  local_path = args[2] || './'
  e = NexClient::CubeInstance.includes(:compute_rack).find(uuid: id).first

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

  # Get rack and file details
  rack = e.compute_rack
  filename = File.basename(remote_path)

  # Copy and download file
  with_cert_identity do |username,pv_key_path|
    cmds = []
    tmp_folder = "/home/#{username}/tmp"
    tmp_path = "#{tmp_folder}/#{filename}"
    ssh_cmd = format_cmd(rack.ssh_cmd_template, username: username, certfile: pv_key_path)

    # Format tmp folder creation command
    cmds << "#{ssh_cmd} mkdir -p #{tmp_folder}"

    # Format container copy command
    cmds << "#{ssh_cmd} sudo docker cp #{e.container}:#{remote_path} #{tmp_folder}/"

    # Format server copy command
    cmds << format_cmd(rack.scp_cmd_template,
      username: username,
      certfile: pv_key_path,
      remote_path: tmp_path,
      dst_local_file: local_path
    )

    # Cleanup
    cmds << "#{ssh_cmd} rm -f #{tmp_path}"

    # Execute commands
    cmds.each { |cmd| system(cmd) }
  end
end
format_cluster(record) click to toggle source
# File lib/nex_client/commands/cube_instances.rb, line 230
def self.format_cluster(record)
  return '-' unless o = record.cluster
  "#{o.type.singularize}:#{o.name}"
end
format_record(record) click to toggle source
# File lib/nex_client/commands/cube_instances.rb, line 215
def self.format_record(record)
  cluster = self.format_cluster(record)
  container_status = record.respond_to?(:container_status) ? record.container_status : '-'
  [
    record.uuid,
    record.status,
    container_status,
    record.region,
    record.persistent_storage,
    record.ip_address || '-',
    record.port || '-',
    cluster
  ]
end
info(args,opts) click to toggle source
# File lib/nex_client/commands/cube_instances.rb, line 37
def self.info(args,opts)
  id = args.first
  e = NexClient::CubeInstance.includes(:cluster).find(uuid: id).first

  # Display cube information
  self.display_cubes(e)
end
list(args,opts) click to toggle source
# File lib/nex_client/commands/cube_instances.rb, line 13
def self.list(args,opts)
  filters = {}
  filters[:status] = opts.status if opts.status.present?
  filters[:persistent_storage] = opts.storage if opts.storage.present?

  # All option
  filters = {} if opts.all

  # Identification options
  filters[:'app.name'] = opts.app if opts.app.present?
  filters[:'addon.name'] = opts.addon if opts.addon.present?

  # Create table
  list = NexClient::CubeInstance.includes(:cluster).where(filters).order('status')
  self.display_cubes(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_cubes(list)
  end
end
logs(args,opts) click to toggle source

Retrieve application logs from all containers

# File lib/nex_client/commands/cube_instances.rb, line 46
def self.logs(args,opts)
  name = args.first
  e = NexClient::CubeInstance.find(uuid: name).first

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

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

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

  # Retrieve snapshots and display them
  snapshots = e.snapshots.first['snapshots']
  self.display_snapshots(snapshots)
end
ssh(args,opts) click to toggle source

SSH to the addon

# File lib/nex_client/commands/cube_instances.rb, line 99
def self.ssh(args,opts)
  id = args.first
  e = NexClient::CubeInstance.find(uuid: id).first

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

  # Perform command
  perform_ssh_cmd(e.ssh_cmd_template)
end
trigger_action(action,args,opts) click to toggle source
# File lib/nex_client/commands/cube_instances.rb, line 76
def self.trigger_action(action,args,opts)
  id = args.first
  e = NexClient::CubeInstance.find(uuid: id).first

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

  # Perform
  e.send(action)

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

  success("Initiated #{action} for cube #{id}...")
end
upload(args,opts) click to toggle source

Upload a file to the container

# File lib/nex_client/commands/cube_instances.rb, line 160
def self.upload(args,opts)
  id = args.first
  local_path = args[1]
  remote_path = args[2] || "/tmp/"
  e = NexClient::CubeInstance.includes(:compute_rack).find(uuid: id).first

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

  # Get rack and file details
  rack = e.compute_rack
  filename = File.basename(local_path)

  # Upload and copy file
  with_cert_identity do |username,pv_key_path|
    cmds = []
    tmp_folder = "/home/#{username}/tmp"
    tmp_path = "#{tmp_folder}/#{filename}"
    ssh_cmd = format_cmd(rack.ssh_cmd_template, username: username, certfile: pv_key_path)

    # Format tmp folder creation command
    cmds << "#{ssh_cmd} mkdir -p #{tmp_folder}"

    # Format server copy command
    cmds << format_cmd(rack.scp_cmd_template,
      username: username,
      certfile: pv_key_path,
      src_local_file: local_path,
      remote_path: tmp_path
    )

    # Format container copy command
    cmds << "#{ssh_cmd} sudo docker cp #{tmp_path} #{e.container}:#{remote_path}"

    # Cleanup
    cmds << "#{ssh_cmd} rm -f #{tmp_path}"

    # Execute commands
    cmds.each { |cmd| system(cmd) }
  end
end