Class: Fleet::Unit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller:, name:, state:, load:, active:, sub:, desc:, machine:) ⇒ Unit

Returns a new instance of Unit.



9
10
11
12
13
14
15
16
17
# File 'lib/fleet/unit.rb', line 9

def initialize(controller:, name:, state:, load:, active:, sub:, desc:, machine:)
  @controller = controller
  @name = name
  @state = state
  @load = load
  @active = active
  @sub = sub
  @machine = machine
end

Instance Attribute Details

#activeObject (readonly)

linuxrackers.com/doku.php?id=fedora_systemd_services LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type.



7
8
9
# File 'lib/fleet/unit.rb', line 7

def active
  @active
end

#controllerObject (readonly)

linuxrackers.com/doku.php?id=fedora_systemd_services LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type.



7
8
9
# File 'lib/fleet/unit.rb', line 7

def controller
  @controller
end

#descObject (readonly)

linuxrackers.com/doku.php?id=fedora_systemd_services LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type.



7
8
9
# File 'lib/fleet/unit.rb', line 7

def desc
  @desc
end

#loadObject (readonly)

linuxrackers.com/doku.php?id=fedora_systemd_services LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type.



7
8
9
# File 'lib/fleet/unit.rb', line 7

def load
  @load
end

#machineObject (readonly)

linuxrackers.com/doku.php?id=fedora_systemd_services LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type.



7
8
9
# File 'lib/fleet/unit.rb', line 7

def machine
  @machine
end

#nameObject (readonly)

linuxrackers.com/doku.php?id=fedora_systemd_services LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type.



7
8
9
# File 'lib/fleet/unit.rb', line 7

def name
  @name
end

#stateObject (readonly)

linuxrackers.com/doku.php?id=fedora_systemd_services LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type.



7
8
9
# File 'lib/fleet/unit.rb', line 7

def state
  @state
end

#subObject (readonly)

linuxrackers.com/doku.php?id=fedora_systemd_services LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type.



7
8
9
# File 'lib/fleet/unit.rb', line 7

def sub
  @sub
end

Instance Method Details

#==(other_unit) ⇒ Object Also known as: eql?



79
80
81
# File 'lib/fleet/unit.rb', line 79

def ==(other_unit)
  name == other_unit.name
end

#creating?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/fleet/unit.rb', line 31

def creating?
  active == 'activating' && sub == 'start-pre'
end

#docker_inspect(container_name = name) ⇒ Object

returns a JSON object representing the container assumes that this unit corresponds to a docker container



74
75
76
77
# File 'lib/fleet/unit.rb', line 74

def docker_inspect(container_name = name)
  raw = ssh('docker', 'inspect', container_name)
  JSON.parse(raw)
end

#docker_port(internal_port, container_name = name) ⇒ Object

gets the external port corresponding to the internal port specified assumes that this unit corresponds to a docker container TODO: split this sort of docker-related functionality out into a separate class



53
54
55
56
57
58
59
60
61
# File 'lib/fleet/unit.rb', line 53

def docker_port(internal_port, container_name = name)
  docker_runner = Fleetctl::Runner::SSH.new('docker', 'port', container_name, internal_port)
  docker_runner.run(host: ip)
  output = docker_runner.output
  if output
    output.rstrip!
    output.split(':').last
  end
end

#failed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/fleet/unit.rb', line 35

def failed?
  active == 'failed' && sub == 'failed'
end

#ipObject



27
28
29
# File 'lib/fleet/unit.rb', line 27

def ip
  machine && machine.ip
end

#running?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/fleet/unit.rb', line 39

def running?
  active == 'active' && sub == 'running'
end

#ssh(*command, port: 22) ⇒ Object

run the command on host (string, array of command + args, whatever) and return stdout



44
45
46
47
48
# File 'lib/fleet/unit.rb', line 44

def ssh(*command, port: 22)
  runner = Fleetctl::Runner::SSH.new([*command].flatten.compact.join(' '))
  runner.run(host: ip, ssh_options: { port: port })
  runner.output
end