Class: Phut::Link

Inherits:
Object
  • Object
show all
Includes:
ShellRunner
Defined in:
lib/phut/link.rb

Overview

Virtual link

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellRunner

#sh, #sudo

Constructor Details

#initialize(name1, name2, link_id = Link.all.size) ⇒ Link

Returns a new instance of Link.



32
33
34
35
36
# File 'lib/phut/link.rb', line 32

def initialize(name1, name2, link_id = Link.all.size)
  raise if name1 == name2
  @ends = [Veth.new(name: name1, link_id: link_id),
           Veth.new(name: name2, link_id: link_id)].sort
end

Instance Attribute Details

#endsObject (readonly)

Returns the value of attribute ends.



30
31
32
# File 'lib/phut/link.rb', line 30

def ends
  @ends
end

Class Method Details

.allObject



10
11
12
13
14
# File 'lib/phut/link.rb', line 10

def self.all
  link = Hash.new { [] }
  Veth.all.each { |each| link[each.link_id] += [each.name] }
  link.map { |link_id, names| new(names.first, names.second, link_id) }
end

.create(end1, end2) ⇒ Object



20
21
22
# File 'lib/phut/link.rb', line 20

def self.create(end1, end2)
  new(end1, end2).start
end

.destroy_allObject



24
25
26
# File 'lib/phut/link.rb', line 24

def self.destroy_all
  all.each(&:destroy)
end

.find(end1, end2) ⇒ Object



16
17
18
# File 'lib/phut/link.rb', line 16

def self.find(end1, end2)
  all.find { |each| each.ends.map(&:name) == [end1, end2].map(&:to_s).sort }
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  ends == other.ends
end

#destroyObject Also known as: stop



45
46
47
48
# File 'lib/phut/link.rb', line 45

def destroy
  sudo "ip link delete #{end1} || true"
  sudo "ip link delete #{end2} || true"
end

#device(name) ⇒ Object



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

def device(name)
  ends.find { |each| each.name == name.to_s }
end

#startObject



38
39
40
41
42
43
# File 'lib/phut/link.rb', line 38

def start
  return self if up?
  add
  up
  self
end