Class: Elasticsearch::Drain::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/elasticsearch/drain/cli.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_nodesObject

Returns the value of attribute active_nodes.



9
10
11
# File 'lib/elasticsearch/drain/cli.rb', line 9

def active_nodes
  @active_nodes
end

#drainerObject (readonly)

Returns the value of attribute drainer.



8
9
10
# File 'lib/elasticsearch/drain/cli.rb', line 8

def drainer
  @drainer
end

Instance Method Details

#asgObject

rubocop:enable Metrics/LineLength



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# File 'lib/elasticsearch/drain/cli.rb', line 20

def asg # rubocop:disable Metrics/MethodLength
  @drainer = Elasticsearch::Drain.new(options[:host],
                                      options[:asg],
                                      options[:region])

  ensure_cluster_healthy
  @active_nodes = drainer.active_nodes_in_asg

  # If :nodes are specified, :number has no effect
  if options[:nodes]
    say "Nodes #{options[:nodes].join(', ')} have been specified, the --number option has no effect"
    number_to_drain = nil
    currently_draining_nodes = nil
  else
    number_to_drain = options[:number]
    currently_draining_nodes = drainer.cluster.currently_draining('_id')
  end

  # If a node or nodes are specified, only drain the requested node(s)
  @active_nodes = active_nodes.find_all do |n|
    instance_id = drainer.asg.instance(n.ipaddress).instance_id
    options[:nodes].include?(instance_id)
  end if options[:nodes]

  do_exit { say_status 'Complete', 'Nothing to do', :green } if active_nodes.empty?
  say_status 'Found Nodes', "AutoScalingGroup: #{instances(active_nodes)}", :magenta

  until active_nodes.empty?
    ensure_cluster_healthy

    nodes = active_nodes

    # If there are nodes in cluster settings "transient.cluster.routing.allocation.exclude"
    # test if those nodes are still in the ASG. If so, work on them first unless nodes are
    # specified.
    if currently_draining_nodes
      nodes_to_drain = active_nodes.find_all { |n| currently_draining_nodes.split(',').include?(n.id) }

      # If the list of nodes_to_drain isn't empty, we want to set nodes to the list of nodes
      # we've already been working on.
      unless nodes_to_drain.empty?
        nodes = nodes_to_drain

        say_status 'Active Nodes', "Resuming drain process on #{instances(nodes)}", :magenta
      end

      # We should only process currently_draining_nodes once
      currently_draining_nodes = nil
    end

    # If we specify a number but DON'T specify nodes, sample the active_nodes.
    if number_to_drain
      nodes = nodes.sample(number_to_drain.to_i)
      say_status 'Active Nodes', "Sampled #{number_to_drain} nodes and got #{instances(nodes)}", :magenta
    end

    @active_nodes = nodes unless options[:continue]

    drain_nodes(nodes)
    remove_nodes(nodes)

    # Remove the drained nodes from the list of active_nodes
    @active_nodes -= nodes

    unless active_nodes.empty?
      say_status 'Drain Nodes', "#{active_nodes.length} nodes remaining", :green

      sleep_time = wait_sleep_time
      say_status 'Waiting', "Sleeping for #{sleep_time} seconds before the next iteration", :green
      sleep sleep_time
    end
  end
  say_status 'Complete', 'Draining nodes complete!', :green
end