class Fleet::Controller

Attributes

cluster[RW]
units[W]

Public Class Methods

new() click to toggle source
# File lib/fleet/controller.rb, line 6
def initialize
  @cluster = Fleet::Cluster.new(controller: self)
end

Public Instance Methods

[](unit_name) click to toggle source

find a unitfile of a specific name

# File lib/fleet/controller.rb, line 31
def [](unit_name)
  units.detect { |u| u.name == unit_name }
end
destroy(*unit_names) click to toggle source
# File lib/fleet/controller.rb, line 59
def destroy(*unit_names)
  runner = Fleetctl::Command.run('destroy', unit_names)
  clear_units
  runner.exit_code == 0
end
load(*unit_file_or_files) click to toggle source

accepts one or more File objects, or an array of File objects

# File lib/fleet/controller.rb, line 52
def load(*unit_file_or_files)
  unitfiles = [*unit_file_or_files].flatten
  out = unitfile_operation(:load, unitfiles)
  clear_units
  out
end
machines() click to toggle source

returns an array of Fleet::Machine instances

# File lib/fleet/controller.rb, line 11
def machines
  cluster.machines
end
start(*unit_file_or_files) click to toggle source

accepts one or more File objects, or an array of File objects

# File lib/fleet/controller.rb, line 36
def start(*unit_file_or_files)
  unitfiles = [*unit_file_or_files].flatten
  out = unitfile_operation(:start, unitfiles)
  clear_units
  out
end
submit(*unit_file_or_files) click to toggle source

accepts one or more File objects, or an array of File objects

# File lib/fleet/controller.rb, line 44
def submit(*unit_file_or_files)
  unitfiles = [*unit_file_or_files].flatten
  out = unitfile_operation(:submit, unitfiles)
  clear_units
  out
end
sync() click to toggle source

refreshes local state to match the fleet cluster

# File lib/fleet/controller.rb, line 24
def sync
  build_fleet
  fetch_units
  true
end
units() click to toggle source

returns an array of Fleet::Unit instances

# File lib/fleet/controller.rb, line 16
def units
  return @units.to_a if @units
  machines
  fetch_units
  @units.to_a
end

Private Instance Methods

build_fleet() click to toggle source
# File lib/fleet/controller.rb, line 67
def build_fleet
  cluster.discover!
end
clear_units() click to toggle source
# File lib/fleet/controller.rb, line 75
def clear_units
  @units = nil
end
fetch_units(host: fleet_host) click to toggle source
# File lib/fleet/controller.rb, line 92
def fetch_units(host: fleet_host)
  Fleetctl.logger.info 'Fetching units from host: '+host.inspect
  @units = Fleet::ItemSet.new
  Fleetctl::Command.new('list-units', '-l') do |runner|
    runner.run(host: host)
    parse_units(runner.output)
  end
  @units.to_a
end
fleet_host() click to toggle source
# File lib/fleet/controller.rb, line 71
def fleet_host
  cluster.fleet_host
end
parse_units(raw_table) click to toggle source
# File lib/fleet/controller.rb, line 102
def parse_units(raw_table)
  unit_hashes = Fleetctl::TableParser.parse(raw_table)
  unit_hashes.each do |unit_attrs|
    if unit_attrs[:machine]
      machine_id, machine_ip = unit_attrs[:machine].split('/')
      unit_attrs[:machine] = cluster.add_or_find(Fleet::Machine.new(id: machine_id, ip: machine_ip))
    end
    unit_attrs[:name] = unit_attrs.delete(:unit)
    unit_attrs[:controller] = self
    @units.add_or_find(Fleet::Unit.new(unit_attrs))
  end
end
unitfile_operation(command, files) click to toggle source
# File lib/fleet/controller.rb, line 79
def unitfile_operation(command, files)
  clear_units
  if Fleetctl.options.runner_class.to_s == 'Shell'
    runner = Fleetctl::Command.run(command.to_s, files.map(&:path))
  else
    runner = nil
    Fleetctl::RemoteTempfile.open(*files) do |*remote_filenames|
      runner = Fleetctl::Command.run(command.to_s, remote_filenames)
    end
  end
  runner.exit_code == 0
end