Class: Ifupdown2Config

Inherits:
Object
  • Object
show all
Defined in:
lib/cumulus/ifupdown2.rb

Overview

defines functions that read and write ifquery output

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Ifupdown2Config

Returns a new instance of Ifupdown2Config.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/cumulus/ifupdown2.rb', line 5

def initialize(resource)
  @resource = resource
  @confighash = {
    'addr_family' => nil,
    'addr_method' => nil,
    'auto' => true,
    'name' => resource[:name],
    'config' => {}
  }
  @currenthash = if_to_hash
end

Instance Attribute Details

#confighashObject

Returns the value of attribute confighash.



4
5
6
# File 'lib/cumulus/ifupdown2.rb', line 4

def confighash
  @confighash
end

#currenthashObject

Returns the value of attribute currenthash.



4
5
6
# File 'lib/cumulus/ifupdown2.rb', line 4

def currenthash
  @currenthash
end

Instance Method Details

#==(other) ⇒ Object



147
148
149
150
# File 'lib/cumulus/ifupdown2.rb', line 147

def ==(other)
  remove_nil_entries(@confighash) ==
    remove_nil_entries(other.confighash)
end

#build_address(addr_type) ⇒ Object



72
73
74
75
76
# File 'lib/cumulus/ifupdown2.rb', line 72

def build_address(addr_type)
  return nil.to_s if @resource[addr_type.to_sym].nil?
  Puppet.debug "updating #{addr_type} info #{@resource[:name]}"
  @resource[addr_type.to_sym]
end

#compare_with_currentObject



40
41
42
43
# File 'lib/cumulus/ifupdown2.rb', line 40

def compare_with_current
  remove_nil_entries(@confighash) ==
    remove_nil_entries(@currenthash)
end

#hash_to_ifObject

Use ifquery to generate a configuration from a hash and return the configuration.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cumulus/ifupdown2.rb', line 49

def hash_to_if
  intf = ''
  cmd = "/sbin/ifquery -i - -t json #{@resource[:name]}"
  IO.popen(cmd, mode: 'r+') do |ifquery|
    ifquery.write([@confighash].to_json)
    ifquery.close_write
    intf = ifquery.read
    ifquery.close
  end
  Puppet.debug("hash_to_if hash before text:\n#{@confighash}")
  Puppet.debug("hash_to_if ifupdown2 text:\n#{intf}")
  intf
rescue StandardError => ex
  Puppet.warning("ifquery failed: #{ex}")
end

#if_to_hashObject

Use ifquery to generate a JSON representation of an interface and return the hash.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cumulus/ifupdown2.rb', line 21

def if_to_hash
  filepath = @resource[:location] + '/' + @resource[:name]
  return {} unless File.exist?(filepath)
  json = ''
  IO.popen("/sbin/ifquery #{@resource[:name]} -o json", :err=>[:child, :out]) do |ifquery|
    json = ifquery.read
  end
  JSON.parse(json)[0]
rescue StandardError => ex
  Puppet.debug("ifquery failed: #{ex}")
  {}
end

#remove_nil_entries(myhash) ⇒ Object

before 2.5.4 null entries where left in place in hash in 2.5.4 and higher null entries were removed



36
37
38
# File 'lib/cumulus/ifupdown2.rb', line 36

def remove_nil_entries(myhash)
  myhash.delete_if { |_key, value| value.nil? }
end

#update_addr_methodObject



65
66
67
68
69
70
# File 'lib/cumulus/ifupdown2.rb', line 65

def update_addr_method
  return if @resource[:addr_method].nil?
  Puppet.info "updating address method #{@resource[:name]}"
  @confighash['addr_method'] = @resource[:addr_method].to_s
  @confighash['addr_family'] = 'inet'
end

#update_addressObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cumulus/ifupdown2.rb', line 78

def update_address
  ipv4_list = build_address('ipv4')
  ipv6_list = build_address('ipv6')

  addresslist = []
  addresslist += ipv4_list unless ipv4_list.empty?
  addresslist += ipv6_list unless ipv6_list.empty?
  # ifquery sets hash value to a string if address list length == 1
  # otherwise it sets it to an array
  addresslist = addresslist.length == 1 ? addresslist.join : addresslist
  return if addresslist.empty?

  @confighash['config']['address'] = addresslist
end

#update_alias_nameObject

updates alias name in confighash



113
114
115
116
117
# File 'lib/cumulus/ifupdown2.rb', line 113

def update_alias_name
  return if @resource[:alias_name].nil?
  Puppet.debug "updating alias #{@resource[:name]}"
  @confighash['config']['alias'] = @resource[:alias_name]
end

#update_attr(attr, suffix = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cumulus/ifupdown2.rb', line 93

def update_attr(attr, suffix = nil)
  resource_value = @resource[attr.to_sym]
  ifupdown_value = ''
  return if resource_value.nil?
  if resource_value == true
    ifupdown_value = 'yes'
  elsif resource_value == false
    ifupdown_value = 'no'
  elsif resource_value.is_a?(Array)
    ifupdown_value = resource_value.join(' ')
  else
    ifupdown_value = resource_value.to_s
  end
  # ifquery uses dash not underscore to define attributes
  attr.gsub! '_', '-'
  configattr = (suffix.nil?) ? attr : "#{suffix}-#{attr}"
  @confighash['config'][configattr] = ifupdown_value
end

#update_members(attrname, ifupdown_attr) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cumulus/ifupdown2.rb', line 134

def update_members(attrname, ifupdown_attr)
  result = []
  @resource[attrname.to_sym].each do |port_entry|
    if port_entry.match('-')
      final_port_entry = 'glob ' + port_entry
    else
      final_port_entry = port_entry
    end
    result.push(final_port_entry)
  end
  @confighash['config'][ifupdown_attr] = result.join(' ')
end

#update_speedObject



119
120
121
122
123
124
# File 'lib/cumulus/ifupdown2.rb', line 119

def update_speed
  return if @resource[:speed].nil?
  Puppet.debug "configuring speed #{@resource[:name]}"
  @confighash['config']['link-speed'] = @resource[:speed].to_s
  @confighash['config']['link-duplex'] = 'full'
end

#update_vrrObject

updates vrr config in config hash



127
128
129
130
131
132
# File 'lib/cumulus/ifupdown2.rb', line 127

def update_vrr
  return if @resource[:virtual_ip].nil?
  vrrstring = @resource[:virtual_mac] + ' ' + @resource[:virtual_ip]
  @confighash['config']['address-virtual'] = vrrstring
  Puppet.debug "updating vrr config #{vrrstring}"
end

#write_configObject

convert hash to text using ifquery write to interfaces file



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cumulus/ifupdown2.rb', line 154

def write_config
  Puppet.info "write config for #{@resource[:name]}"
  intf = hash_to_if
  if intf.empty?
    Puppet.err "ifquery could not interpret config #{@confighash}" \
      "into ifupdown2 text. Not modifying #{@resource[:name]} config"
    return
  end
  filepath = @resource[:location] + '/' + @resource[:name]
  Puppet.debug "file location: #{filepath}"
  begin
    ifacefile = File.open(filepath, 'w')
    ifacefile.write(intf)
  ensure
    ifacefile.close
  end
end