module NexClient::Commands::Helpers

Constants

LOG_COLORS
VARS_HEADERS
VARS_TITLE

Public Instance Methods

display_events(events) click to toggle source

Display a list of events

# File lib/nex_client/commands/helpers.rb, line 56
def display_events(events)
  # Content
  events.sort_by { |e| e.id.to_i }.each do |e|
    username = e&.username || 'system'
    session_id = e&.session_id || '-'
    puts [
      e.created_at,
      e.event.ljust(12,' '),
      username.ljust(15,' '),
      session_id.ljust(6,' '),
      e.level.ljust(6,' '),
      e.message
    ].join(" | ")
  end
  puts "\n"
end
display_logs(logs) click to toggle source
# File lib/nex_client/commands/helpers.rb, line 24
def display_logs(logs)
  color_index = 0
  logs.each do |container_id,log_lines|
    color_index = (color_index + 1) % LOG_COLORS.size
    puts "\n"
    puts "Node: #{container_id}".colorize(LOG_COLORS[color_index])
    puts "-"*50
    puts log_lines.join("\n")
  end
  puts "\n"
end
display_raw_vars(list) click to toggle source
# File lib/nex_client/commands/helpers.rb, line 170
def display_raw_vars(list)
  table = [list].flatten.compact.map do |e|
    e.sort_by { |k, v| k }.map { |k,v| "#{k}=#{v}" }
  end.flatten
  puts table
  puts "\n"
end
display_record_errors(record) click to toggle source
# File lib/nex_client/commands/helpers.rb, line 10
def display_record_errors(record)
  record.errors.messages.each do |k,v|
    v.each { |m| puts "#{k} #{m}" }
  end
end
display_vars(list,truncate = true) click to toggle source
# File lib/nex_client/commands/helpers.rb, line 183
def display_vars(list,truncate = true)
  table = Terminal::Table.new title: VARS_TITLE, headings: VARS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      e.sort_by { |k, v| k }.each do |k,v|
        val = truncate ? v.to_s.truncate(100) : v
        t.add_row([k,val])
      end
    end
  end
  puts table
  puts "\n"
end
display_yaml_vars(vars) click to toggle source
# File lib/nex_client/commands/helpers.rb, line 178
def display_yaml_vars(vars)
  puts vars.to_yaml
  puts "\n"
end
error(msg) click to toggle source
# File lib/nex_client/commands/helpers.rb, line 20
def error(msg)
  puts msg.colorize(:red)
end
events(args,opts) click to toggle source

Retrieve resource events

# File lib/nex_client/commands/helpers.rb, line 37
def events(args,opts)
  filters = {}
  filters[:'source.name'] = args.first
  filters[:event] = opts.type if opts.type
  tail_size = (opts.tail || 50).to_i

  events = NexClient::Event.where(filters).order(id: :desc)
  list = events.to_a
  while list.count < tail_size
    events = events.pages.next
    break unless events
    list |= events.to_a
  end
  list = list.first(tail_size)

  self.display_events(list.to_a)
end
format_cmd(cmd,values = {}) click to toggle source
# File lib/nex_client/commands/helpers.rb, line 97
def format_cmd(cmd,values = {})
  formatted = cmd

  # Replace declared values
  values.each do |k,v|
    formatted = formatted.gsub("{{#{k}}}",v)
  end

  # Remove remaining template values
  formatted.gsub(/{{[^{}]+}}/,'')
end
hash_from_file(file) click to toggle source
# File lib/nex_client/commands/helpers.rb, line 127
def hash_from_file(file)
  if file =~ /(\.yml|\.yaml)$/
    YAML.load_file(file)
  else
    JSON.parse(File.read(file))
  end
end
perform_ssh_cmd(ssh_cmd_template, exec_cmd = nil) click to toggle source

Perform an SSH command based on a template Fetch current user key and username

# File lib/nex_client/commands/helpers.rb, line 111
def perform_ssh_cmd(ssh_cmd_template, exec_cmd = nil)
  unless ssh_cmd_template
    error('Error! Cannot perform SSH. Command Template was empty. Are you sure the app is running ?')
    return false
  end

  with_cert_identity do |username,pv_key_path|
    # Format command
    ssh_cmd = format_cmd(ssh_cmd_template, username: username, certfile: pv_key_path)
    ssh_cmd += " #{exec_cmd}" if exec_cmd

    # Run command
    system(ssh_cmd)
  end
end
success(msg) click to toggle source
# File lib/nex_client/commands/helpers.rb, line 16
def success(msg)
  puts msg.colorize(:green)
end
vars_from_file(file,prefix = nil,&block) click to toggle source

Parse plain or yaml file

# File lib/nex_client/commands/helpers.rb, line 136
def vars_from_file(file,prefix = nil,&block)
  # YAML file
  if file =~ /(\.yml|\.yaml)$/
    vars_from_yaml_file(file,prefix,&block)
  else
    vars_from_plain_file(file,prefix,&block)
  end
end
vars_from_plain_file(file,prefix = nil) { |key,val| ... } click to toggle source
# File lib/nex_client/commands/helpers.rb, line 159
def vars_from_plain_file(file,prefix = nil,&block)
  f = File.read(file)
  f.split("\n").each do |e|
    line = e.strip
    next if line.empty? || line.start_with?('#')
    key,val = e.split('=',2)
    key = prefix.present? ? "#{prefix}_#{key}" : key
    yield(key,val)
  end
end
vars_from_yaml_file(file,prefix = nil) { |key,v| ... } click to toggle source

Accept a path to a file or a hash

# File lib/nex_client/commands/helpers.rb, line 146
def vars_from_yaml_file(file,prefix = nil,&block)
  vars = file.is_a?(Hash) ? file : YAML.load_file(file)

  vars.each do |k,v|
    key = prefix.present? ? "#{prefix}_#{k}" : k
    if v.is_a?(Hash)
      self.vars_from_yaml_file(v,key,&block)
    else
      yield(key,v)
    end
  end
end
with_cert_identity() { |username,path| ... } click to toggle source
# File lib/nex_client/commands/helpers.rb, line 73
def with_cert_identity
  # Fetch user
  me = NexClient::Me.find.first
  if me.api_only
    error('Error! Cannot SSH with an api-only user')
    return false
  end

  # Get SSH details
  username = me.handle.downcase
  pv_key = me.private_key

  # Create SSH Key
  pv_key_file = Tempfile.new('nex.sshkey')
  pv_key_file.write(pv_key)
  pv_key_file.close

  begin
    yield(username,pv_key_file.path)
  ensure
    pv_key_file.unlink # delete tmp file
  end
end