Class: RemoteConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_resource/remote_connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, token) ⇒ RemoteConnection

Returns a new instance of RemoteConnection.



3
4
5
# File 'lib/active_resource/remote_connection.rb', line 3

def initialize(url, token)
  @url, @token = url, token
end

Instance Method Details

#startObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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!
      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!
  end
end

#start_logs(container) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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

#uriObject



7
8
9
# File 'lib/active_resource/remote_connection.rb', line 7

def uri
  "#{@url}?token=#{@token}"
end