Class: Chef::Provider::Route
Constant Summary
collapse
- MASK =
{'0.0.0.0' => '0',
'128.0.0.0' => '1',
'192.0.0.0' => '2',
'224.0.0.0' => '3',
'240.0.0.0' => '4',
'248.0.0.0' => '5',
'252.0.0.0' => '6',
'254.0.0.0' => '7',
'255.0.0.0' => '8',
'255.128.0.0' => '9',
'255.192.0.0' => '10',
'255.224.0.0' => '11',
'255.240.0.0' => '12',
'255.248.0.0' => '13',
'255.252.0.0' => '14',
'255.254.0.0' => '15',
'255.255.0.0' => '16',
'255.255.128.0' => '17',
'255.255.192.0' => '18',
'255.255.224.0' => '19',
'255.255.240.0' => '20',
'255.255.248.0' => '21',
'255.255.252.0' => '22',
'255.255.254.0' => '23',
'255.255.255.0' => '24',
'255.255.255.128' => '25',
'255.255.255.192' => '26',
'255.255.255.224' => '27',
'255.255.255.240' => '28',
'255.255.255.248' => '29',
'255.255.255.252' => '30',
'255.255.255.254' => '31',
'255.255.255.255' => '32' }
Instance Attribute Summary collapse
#action, #current_resource, #new_resource, #run_context
Instance Method Summary
collapse
#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_with_systems_locale
#popen4
#popen4
#action_nothing, #cleanup_after_converge, #cookbook_name, #define_resource_requirements, #events, #initialize, #node, #process_resource_requirements, #requirements, #resource_collection, #run_action, #set_updated_status, #whyrun_mode?
#method_missing
#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename
Constructor Details
This class inherits a constructor from Chef::Provider
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Chef::DSL::Recipe
Instance Attribute Details
#is_running ⇒ Object
Returns the value of attribute is_running.
27
28
29
|
# File 'lib/chef/provider/route.rb', line 27
def is_running
@is_running
end
|
Instance Method Details
#action_add ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/chef/provider/route.rb', line 129
def action_add
if is_running
Chef::Log.debug("#{@new_resource} route already active - nothing to do")
else
command = generate_command(:add)
converge_by ("run #{ command } to add route") do
run_command( :command => command )
Chef::Log.info("#{@new_resource} added")
end
end
generate_config
end
|
#action_delete ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/chef/provider/route.rb', line 145
def action_delete
if is_running
command = generate_command(:delete)
converge_by ("run #{ command } to delete route ") do
run_command( :command => command )
Chef::Log.info("#{@new_resource} removed")
end
else
Chef::Log.debug("#{@new_resource} route does not exist - nothing to do")
end
end
|
#config_file_contents(action, options = {}) ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/chef/provider/route.rb', line 211
def config_file_contents(action, options={})
content = ''
case action
when :add
content << "#{options[:target]}"
content << "/#{options[:netmask]}" if options[:netmask]
content << " via #{options[:gateway]}" if options[:gateway]
content << "\n"
end
return content
end
|
#generate_command(action) ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/chef/provider/route.rb', line 193
def generate_command(action)
common_route_items = ''
common_route_items << "/#{MASK[@new_resource.netmask.to_s]}" if @new_resource.netmask
common_route_items << " via #{@new_resource.gateway} " if @new_resource.gateway
case action
when :add
command = "ip route replace #{@new_resource.target}"
command << common_route_items
command << " dev #{@new_resource.device} " if @new_resource.device
when :delete
command = "ip route delete #{@new_resource.target}"
command << common_route_items
end
return command
end
|
#generate_config ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/chef/provider/route.rb', line 157
def generate_config
conf = Hash.new
case node[:platform]
when "centos", "redhat", "fedora"
run_context.resource_collection.each do |resource|
if resource.is_a? Chef::Resource::Route
if resource.device
dev = resource.device
else
dev = "eth0"
end
conf[dev] = String.new if conf[dev].nil?
if resource.action == :add
conf[dev] << config_file_contents(:add, :target => resource.target, :netmask => resource.netmask, :gateway => resource.gateway)
else
conf[dev] << config_file_contents(:delete)
end
end
end
conf.each do |k, v|
network_file_name = "/etc/sysconfig/network-scripts/route-#{k}"
converge_by ("write route route.#{k}\n#{conf[k]} to #{ network_file_name }") do
network_file = ::File.new(network_file_name, "w")
network_file.puts(conf[k])
Chef::Log.debug("#{@new_resource} writing route.#{k}\n#{conf[k]}")
network_file.close
end
end
end
end
|
#hex2ip(hex_data) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/chef/provider/route.rb', line 63
def hex2ip(hex_data)
hex_ip = hex_data.to_s.downcase.gsub(/[^0-9a-f]/, '')
return nil if hex_ip.length != hex_data.length || hex_ip.length != 8
octets = hex_ip.scan(/../).reverse.collect { |octet| [octet].pack('H2').unpack("C").first }
ip = octets.join('.')
begin
IPAddr.new(ip, Socket::AF_INET).to_s
rescue ArgumentError
Chef::Log.debug("Invalid IP address data: hex=#{hex_ip}, ip=#{ip}")
return nil
end
end
|
#load_current_resource ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/chef/provider/route.rb', line 87
def load_current_resource
self.is_running = false
if @new_resource.netmask
new_ip = IPAddr.new("#{@new_resource.target}/#{@new_resource.netmask}")
else
new_ip = IPAddr.new(@new_resource.target)
end
if node[:os] == "linux"
route_file = ::File.open("/proc/net/route", "r")
while (line = route_file.gets)
iface,destination,gateway,flags,refcnt,use,metric,mask,mtu,window,irtt = line.split
destination = hex2ip(destination)
gateway = hex2ip(gateway)
mask = hex2ip(mask)
next unless destination && gateway && mask
Chef::Log.debug("#{@new_resource} system has route: dest=#{destination} mask=#{mask} gw=#{gateway}")
running_ip = IPAddr.new("#{destination}/#{mask}")
Chef::Log.debug("#{@new_resource} new ip: #{new_ip.inspect} running ip: #{running_ip.inspect}")
self.is_running = true if running_ip == new_ip && gateway == @new_resource.gateway
end
route_file.close
end
end
|
#whyrun_supported? ⇒ Boolean
83
84
85
|
# File 'lib/chef/provider/route.rb', line 83
def whyrun_supported?
true
end
|