2016-05-31 03:17:02 +00:00
|
|
|
require "tempfile"
|
2013-04-04 19:09:40 +00:00
|
|
|
|
2016-05-29 03:17:40 +00:00
|
|
|
require_relative "../../../../lib/vagrant/util/retryable"
|
|
|
|
require_relative "../../../../lib/vagrant/util/template_renderer"
|
2013-04-04 19:09:40 +00:00
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module GuestRedHat
|
|
|
|
module Cap
|
|
|
|
class ConfigureNetworks
|
2013-04-08 18:02:03 +00:00
|
|
|
extend Vagrant::Util::Retryable
|
2013-04-08 17:47:19 +00:00
|
|
|
include Vagrant::Util
|
2013-04-04 19:09:40 +00:00
|
|
|
|
|
|
|
def self.configure_networks(machine, networks)
|
2016-06-05 23:09:12 +00:00
|
|
|
case machine.guest.capability(:flavor)
|
2014-08-31 05:49:13 +00:00
|
|
|
when :rhel_7
|
2014-09-03 15:07:55 +00:00
|
|
|
configure_networks_rhel7(machine, networks)
|
2014-08-31 05:49:13 +00:00
|
|
|
else
|
2014-09-03 15:07:55 +00:00
|
|
|
configure_networks_default(machine, networks)
|
2014-08-31 05:49:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure_networks_rhel7(machine, networks)
|
2016-06-05 23:09:12 +00:00
|
|
|
# This is kind of jank but the configure networks is the same as
|
|
|
|
# Fedora at this point.
|
|
|
|
require_relative "../../fedora/cap/configure_networks"
|
|
|
|
::VagrantPlugins::GuestFedora::Cap::ConfigureNetworks
|
|
|
|
.configure_networks(machine, networks)
|
2014-08-31 05:49:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure_networks_default(machine, networks)
|
2016-06-05 23:09:12 +00:00
|
|
|
comm = machine.communicate
|
2014-05-06 20:26:44 +00:00
|
|
|
|
2016-06-05 23:09:12 +00:00
|
|
|
network_scripts_dir = machine.guest.capability(:network_scripts_dir)
|
2014-05-06 20:26:44 +00:00
|
|
|
|
2016-06-05 23:09:12 +00:00
|
|
|
interfaces = []
|
|
|
|
commands = []
|
2014-05-06 20:26:44 +00:00
|
|
|
|
2016-06-18 11:06:29 +00:00
|
|
|
comm.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout|
|
2016-06-05 23:09:12 +00:00
|
|
|
interfaces = stdout.split("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
networks.each.with_index do |network, i|
|
|
|
|
network[:device] = interfaces[network[:interface]]
|
2014-05-06 20:26:44 +00:00
|
|
|
|
2016-06-05 23:09:12 +00:00
|
|
|
# Render a new configuration
|
2014-05-06 12:48:01 +00:00
|
|
|
entry = TemplateRenderer.render("guests/redhat/network_#{network[:type]}",
|
2016-06-05 23:09:12 +00:00
|
|
|
options: network,
|
|
|
|
)
|
2013-04-04 19:09:40 +00:00
|
|
|
|
2016-06-05 23:09:12 +00:00
|
|
|
# Upload the new configuration
|
|
|
|
remote_path = "/tmp/vagrant-network-entry-#{network[:device]}-#{Time.now.to_i}-#{i}"
|
|
|
|
Tempfile.open("vagrant-redhat-configure-networks") do |f|
|
2016-05-31 03:17:02 +00:00
|
|
|
f.binmode
|
2016-05-29 03:17:40 +00:00
|
|
|
f.write(entry)
|
|
|
|
f.fsync
|
|
|
|
f.close
|
2016-06-05 23:09:12 +00:00
|
|
|
machine.communicate.upload(f.path, remote_path)
|
2016-05-29 03:17:40 +00:00
|
|
|
end
|
2013-04-04 19:09:40 +00:00
|
|
|
|
2016-06-05 23:09:12 +00:00
|
|
|
# Add the new interface and bring it back up
|
|
|
|
final_path = "#{network_scripts_dir}/ifcfg-#{network[:device]}"
|
|
|
|
commands << <<-EOH.gsub(/^ {14}/, '')
|
|
|
|
# Down the interface before munging the config file. This might
|
|
|
|
# fail if the interface is not actually set up yet so ignore
|
|
|
|
# errors.
|
|
|
|
/sbin/ifdown '#{network[:device]}' || true
|
2014-05-06 20:26:44 +00:00
|
|
|
|
2016-06-05 23:09:12 +00:00
|
|
|
# Move new config into place
|
|
|
|
mv '#{remote_path}' '#{final_path}'
|
2014-05-06 20:26:44 +00:00
|
|
|
|
2016-06-05 23:09:12 +00:00
|
|
|
# Bring the interface up
|
|
|
|
ARPCHECK=no /sbin/ifup '#{network[:device]}'
|
|
|
|
EOH
|
2013-04-04 19:09:40 +00:00
|
|
|
end
|
2016-06-05 23:09:12 +00:00
|
|
|
|
|
|
|
comm.sudo(commands.join("\n"))
|
2013-04-04 19:09:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|