Class: Ifupdown2Config

Inherits:
Object
  • Object
show all
Includes:
Puppet::Util::Diff
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.



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

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.



6
7
8
# File 'lib/cumulus/ifupdown2.rb', line 6

def confighash
  @confighash
end

#currenthashObject

Returns the value of attribute currenthash.



6
7
8
# File 'lib/cumulus/ifupdown2.rb', line 6

def currenthash
  @currenthash
end

Instance Method Details

#==(other) ⇒ Object



185
186
187
188
# File 'lib/cumulus/ifupdown2.rb', line 185

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

#build_address(addr_type) ⇒ Object



100
101
102
103
104
# File 'lib/cumulus/ifupdown2.rb', line 100

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



42
43
44
45
46
# File 'lib/cumulus/ifupdown2.rb', line 42

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

#diff_configObject

diff between old and new config



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cumulus/ifupdown2.rb', line 49

def diff_config
  filepath = @resource[:location] + '/' + @resource[:name]
  if Puppet::FileSystem.exist?(filepath)
    tmp_intf = hash_to_if
    if !tmp_intf.empty?
      tmp_filepath = '/tmp/puppet-temp-iface-' + @resource[:name] + rand(36**8).to_s(36)
      begin
        ifacefile = File.open(tmp_filepath, 'w')
        ifacefile.write(tmp_intf)
      ensure
        ifacefile.close
      end
      diff = Puppet::Util::Diff.diff(filepath, tmp_filepath)
      if !diff.empty?
        Puppet.info "config diff for #{@resource[:name]}"
        Puppet.notice "#{diff}"
      end
      if File.exist?(tmp_filepath)
        File.delete(tmp_filepath)
      end
    end
  end
end

#hash_to_ifObject

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



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

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.



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

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



38
39
40
# File 'lib/cumulus/ifupdown2.rb', line 38

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

#update_addr_methodObject



93
94
95
96
97
98
# File 'lib/cumulus/ifupdown2.rb', line 93

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cumulus/ifupdown2.rb', line 106

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



141
142
143
144
145
# File 'lib/cumulus/ifupdown2.rb', line 141

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cumulus/ifupdown2.rb', line 121

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_downObject



159
160
161
162
# File 'lib/cumulus/ifupdown2.rb', line 159

def update_down
  return if @resource[:down].nil?
  @confighash['config']['down'] = @resource[:down]
end

#update_members(attrname, ifupdown_attr) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cumulus/ifupdown2.rb', line 172

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



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

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_upObject



154
155
156
157
# File 'lib/cumulus/ifupdown2.rb', line 154

def update_up
  return if @resource[:up].nil?
  @confighash['config']['up'] = @resource[:up]
end

#update_vrrObject

updates vrr config in config hash



165
166
167
168
169
170
# File 'lib/cumulus/ifupdown2.rb', line 165

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cumulus/ifupdown2.rb', line 192

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