Class: Elasticsearch::Drain::Node

Inherits:
Base
  • Object
show all
Defined in:
lib/elasticsearch/drain/node.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#client

Instance Method Summary collapse

Constructor Details

#initialize(stats, info, client, asg) ⇒ Node

Returns a new instance of Node.



16
17
18
19
20
21
22
# File 'lib/elasticsearch/drain/node.rb', line 16

def initialize(stats, info, client, asg)
  super(client)
  @stats = stats
  @info = info
  @asg = asg
  @instance_id = nil
end

Instance Attribute Details

#infoObject (readonly)

The Elasticsearch node info json object



10
11
12
# File 'lib/elasticsearch/drain/node.rb', line 10

def info
  @info
end

#instance_idObject

The Elasticsearch node Instance ID



14
15
16
# File 'lib/elasticsearch/drain/node.rb', line 14

def instance_id
  @instance_id
end

#statsObject (readonly)

The Elasticsearch node stats json object



6
7
8
# File 'lib/elasticsearch/drain/node.rb', line 6

def stats
  @stats
end

Instance Method Details

#address(str) ⇒ String

Extract ip:port from string passed in

Parameters:

  • str (String)

    The address object to parse for the ip:port

Returns:

  • (String)

    ip:port pair from the data passed in



95
96
97
# File 'lib/elasticsearch/drain/node.rb', line 95

def address(str)
  str.match(/.+\[\/(.+)\]/)[1]
end

#bytes_storedInteger

Get size in bytes used for indices for a node

Returns:

  • (Integer)

    size in bytes used to store indicies on node



87
88
89
# File 'lib/elasticsearch/drain/node.rb', line 87

def bytes_stored
  stats[1]['indices']['store']['size_in_bytes']
end

#hostnameString

The Elasticsearch node hostname

Returns:

  • (String)

    Elasticsearch node hostname



41
42
43
# File 'lib/elasticsearch/drain/node.rb', line 41

def hostname
  info[1]['host']
end

#http_addressString

The Elasticsearch node HTTP Address

Returns:

  • (String)

    Elasticsearch nodes HTTP Address



80
81
82
# File 'lib/elasticsearch/drain/node.rb', line 80

def http_address
  address(info[1]['http_address'])
end

#idString

The Elasticsearch node id

Returns:

  • (String)

    Elasticsearch node id



27
28
29
# File 'lib/elasticsearch/drain/node.rb', line 27

def id
  info[0]
end

#in_recovery?Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/elasticsearch/drain/node.rb', line 99

def in_recovery?
  recovery = client.cat.recovery(format: 'json', v: true).first.values
  [hostname, name].any? { |a| recovery.include?(a) }
end

#ipaddressString

The Elasticsearch node ipaddress

For ES v1.x, we must extract and modify the IP address from the http_address key. For all later versions, we can use the host key directly. That’s why we check with the if/else statement.

Returns:

  • (String)

    Elasticsearch node ipaddress



60
61
62
63
64
65
66
67
68
# File 'lib/elasticsearch/drain/node.rb', line 60

def ipaddress
  block = /\d{,2}|1\d{2}|2[0-4]\d|25[0-5]/
  re = /\A#{block}\.#{block}\.#{block}\.#{block}\z/
  if info[1]['host'] && re =~ info[1]['host']
    info[1]['host']
  else
    address(info[1]['http_address']).split(':')[0]  
  end
end

#nameString

The Elasticsearch node name

Returns:

  • (String)

    Elasticsearch node name



48
49
50
# File 'lib/elasticsearch/drain/node.rb', line 48

def name
  info[1]['name']
end

#terminateObject



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/elasticsearch/drain/node.rb', line 104

def terminate
  @asg.ec2_client.terminate_instances(
    dry_run: false,
    instance_ids: [instance_id], # required
  )
  # poll for ~5mins seconds
  @asg.ec2_client.wait_until(:instance_terminated,
                             instance_ids: [instance_id]) do |w|
    w.max_attempts = 10
    w.delay = 60
  end
end

#transport_addressString

The Elasticsearch node Transport Address

Returns:

  • (String)

    Elasticsearch node Transport Address



73
74
75
# File 'lib/elasticsearch/drain/node.rb', line 73

def transport_address
  address(info[1]['transport_address'])
end

#versionString

The Elasticsearch node version

Returns:

  • (String)

    Elasticsearch node version



34
35
36
# File 'lib/elasticsearch/drain/node.rb', line 34

def version
  info[1]['version']
end