class RemoteConnection

Public Class Methods

new(url, token) click to toggle source
# File lib/active_resource/remote_connection.rb, line 3
def initialize(url, token)
  @url, @token = url, token
end

Public Instance Methods

start() click to toggle source
# File lib/active_resource/remote_connection.rb, line 11
def start
  begin
    EM.run do
      ws = Faye::WebSocket::Client.new(uri, nil, {ping: 10 })
      IO.console.raw! if IO.console
      EM.open_keyboard(KeyboardHandler, ws)
      ws.on :message do |event|
        content = Base64.decode64(event.data)
        STDOUT.print content
      end
      ws.on :close do |event|
        EM.stop
        ws = nil
      end
    end
  ensure
    IO.console.cooked! if IO.console
  end
end
start_logs(container) click to toggle source
# File lib/active_resource/remote_connection.rb, line 31
def start_logs(container)
  begin
    EM.run do
      ws = Faye::WebSocket::Client.new(uri, nil, {ping: 10 })
      pastel = Pastel.new
      ws.on :message do |event|
        content = event.data.split(' ')
        timestamp = Time.rfc3339(content[1])
        formated_time = timestamp.to_formatted_s(:short)
        puts pastel.red("#{container.name} | ")+ pastel.green("#{formated_time} | ") + content.last(content.size-2).join(' ')
      end
      ws.on :close do |event|
        EM.stop
        ws = nil
      end
    end
  ensure
    IO.console.cooked!
  end
end
start_status(service) click to toggle source
# File lib/active_resource/remote_connection.rb, line 52
def start_status(service)
  begin
    items = Hash.new
    service.instanceIds.each do |id|
      container = Container.find(id)
      items[container.externalId] = {
        id: container.id,
        name: container.name,
        state: container.state,
        cpu: 0
      }
    end
    EM.run do
      ws = Faye::WebSocket::Client.new("#{uri}&&sockId=5", nil, {ping: 10 })
      pastel = Pastel.new
      i = 0
      ws.on :message do |event|
        content = JSON.parse(event.data).first
        table = TTY::Table.new header: ['name','state','memory', 'cpu']

        memory = content['memory']['usage'].to_f
        memory_limit = content['memLimit'].to_f
        memory_rate = (memory/memory_limit)
        formated_memory = ActiveSupport::NumberHelper::number_to_human_size(memory)

        if items[content['id']][:cpu_total]
          delta_total_usage = (content['cpu']['usage']['total'] - items[content['id']][:cpu_total]).to_f / content['cpu']['usage']['total']
          delta_system_usage = (content['cpu']['usage']['system'] - items[content['id']][:cpu_system_total]).to_f / content['cpu']['usage']['system']
          if delta_system_usage>0
            y = (delta_total_usage / delta_system_usage)*content['cpu']['usage']['per_cpu_usage'].size * 100.0
          else
            y = 0
          end

        end

        items[content['id']][:cpu_total] = content['cpu']['usage']['total']
        items[content['id']][:cpu_system_total] = content['cpu']['usage']['system']
        items[content['id']][:cpu] = y


        items[content['id']][:memory] = memory
        items[content['id']][:memory_limit] = memory_limit
        items[content['id']][:memory_rate] = memory_rate
        items[content['id']][:formated_memory] = formated_memory
        items.each_with_index.each do |item, index|
          table << [item[1][:name], item[1][:state], item[1][:formated_memory], "#{items[content['id']][:cpu].to_f.round(1)}%"]
        end

        lines = "#{table.render(:ascii, border:{separator: :each_row})}".lines.count
        print "\r" + ("\e[A\e[J"*(lines-1)) if i>table.rows_size
        STDOUT.syswrite "#{table.render(:ascii, border:{separator: :each_row})}" if i>table.rows_size-1
        i+=1
      end
      ws.on :close do |event|
        EM.stop
        ws = nil
      end
    end
  ensure
    IO.console.cooked!
  end
end
uri() click to toggle source
# File lib/active_resource/remote_connection.rb, line 7
def uri
  "#{@url}?token=#{@token}"
end