Class: Phut::Veth

Inherits:
Object
  • Object
show all
Extended by:
ShellRunner
Defined in:
lib/phut/veth.rb

Overview

Virtual eth device

Constant Summary collapse

PREFIX =
'L'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellRunner

sh, sudo

Constructor Details

#initialize(name:, link_id:) ⇒ Veth

Returns a new instance of Veth.



31
32
33
34
# File 'lib/phut/veth.rb', line 31

def initialize(name:, link_id:)
  @name = valid_ipaddress?(name) ? IPAddr.new(name, Socket::AF_INET) : name
  @link_id = link_id
end

Instance Attribute Details

rubocop:enable Metrics/AbcSize



29
30
31
# File 'lib/phut/veth.rb', line 29

def link_id
  @link_id
end

Class Method Details

.allObject

rubocop:disable Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/phut/veth.rb', line 15

def self.all
  link_devices = sh('ip link show').split("\n").map do |each|
    case each
    when /^\d+: #{PREFIX}(\d+)_(\h{8})[@:]/
      ip_addr = IPAddr.new($LAST_MATCH_INFO[2].hex, Socket::AF_INET)
      new(name: ip_addr, link_id: $LAST_MATCH_INFO[1].to_i)
    when /^\d+: #{PREFIX}(\d+)_([^:]*?)[@:]/
      new(name: $LAST_MATCH_INFO[2], link_id: $LAST_MATCH_INFO[1].to_i)
    end
  end
  (Netns.all.map(&:device) + link_devices).compact
end

Instance Method Details

#<=>(other) ⇒ Object



55
56
57
# File 'lib/phut/veth.rb', line 55

def <=>(other)
  device <=> other.device
end

#==(other) ⇒ Object



51
52
53
# File 'lib/phut/veth.rb', line 51

def ==(other)
  name == other.name && link_id == other.link_id
end

#deviceObject Also known as: to_s, to_str



40
41
42
43
44
45
46
47
# File 'lib/phut/veth.rb', line 40

def device
  if @name.is_a?(IPAddr)
    hex = format('%x', @name.to_i)
    "#{PREFIX}#{@link_id}_#{hex}"
  else
    "#{PREFIX}#{@link_id}_#{@name}"
  end
end

#nameObject



36
37
38
# File 'lib/phut/veth.rb', line 36

def name
  @name.to_s
end