Class: Fleet::Cluster

Inherits:
ItemSet
  • Object
show all
Defined in:
lib/fleet/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ItemSet

#add_or_find

Constructor Details

#initialize(*args, controller: nil) ⇒ Cluster

Returns a new instance of Cluster.



5
6
7
8
# File 'lib/fleet/cluster.rb', line 5

def initialize(*args, controller: nil)
  @controller = controller
  super(*args)
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



3
4
5
# File 'lib/fleet/cluster.rb', line 3

def controller
  @controller
end

Instance Method Details

#build_from(*ip_addrs) ⇒ Object

attempts to rebuild the cluster from any of the hosts passed as arguments returns the first ip that worked, else nil



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fleet/cluster.rb', line 25

def build_from(*ip_addrs)
  ip_addrs = [*ip_addrs].flatten.compact
  begin
    Fleetctl.logger.info 'building from hosts: ' + ip_addrs.inspect
    built_from = ip_addrs.detect { |ip_addr| fetch_machines(ip_addr) }
    Fleetctl.logger.info 'built successfully from host: ' + built_from.inspect if built_from
    built_from
  rescue => e
    Fleetctl.logger.error 'ERROR building from hosts: ' + ip_addrs.inspect
    Fleetctl.logger.error e.message
    Fleetctl.logger.error e.backtrace.join("\n")
    nil
  end
end

#discover!Object

attempts to rebuild the cluster by the specified fleet host, then hosts that it has built previously, and finally by using the discovery url



67
68
69
70
71
72
73
74
75
76
# File 'lib/fleet/cluster.rb', line 67

def discover!
  known_hosts = [Fleetctl.options.fleet_host] | fleet_hosts.to_a
  clear
  success_host = build_from(known_hosts) || build_from(Fleet::Discovery.hosts)
  if success_host
    Fleetctl.logger.info 'Successfully recovered from host: ' + success_host.inspect
  else
    Fleetctl.logger.info 'Unable to recover!'
  end
end

#fetch_machines(host) ⇒ Object

attempts a list-machines action on the given host. returns true if successful, else false



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fleet/cluster.rb', line 42

def fetch_machines(host)
  Fleetctl.logger.info 'Fetching machines from host: '+host.inspect
  clear
  Fleetctl::Command.new('list-machines', '-l') do |runner|
    runner.run(host: host)
    new_machines = parse_machines(runner.output)
    if runner.exit_code == 0
      return true
    else
      return false
    end
  end
end

#fleet_hostObject



14
15
16
# File 'lib/fleet/cluster.rb', line 14

def fleet_host
  fleet_hosts.sample
end

#fleet_hostsObject



10
11
12
# File 'lib/fleet/cluster.rb', line 10

def fleet_hosts
  map(&:ip)
end

#machinesObject



18
19
20
21
# File 'lib/fleet/cluster.rb', line 18

def machines
  discover! if empty?
  to_a
end

#parse_machines(raw_table) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/fleet/cluster.rb', line 56

def parse_machines(raw_table)
  machine_hashes = Fleetctl::TableParser.parse(raw_table)
  machine_hashes.map do |machine_attrs|
    machine_attrs[:id] = machine_attrs.delete(:machine)
    machine_attrs[:cluster] = self
    add_or_find(Fleet::Machine.new(machine_attrs))
  end
end