mirror of
https://github.com/flatcar/scripts.git
synced 2026-05-05 04:06:33 +02:00
build_library: Refresh our Vagrant support
The CoreOS support we have been carrying was merged upstream years ago. The network configuration still needs special handling because our cloud-init is called coreos-cloudinit, breaking the detection. Flatcar's documentation used to point users at coreos-vagrant. This is long dead and used the old Ignition VirtualBox support, which no longer works. Rather than revive that, we can capture the essence of what coreos-vagrant did in our base Vagrantfile. Signed-off-by: James Le Cuirot <jlecuirot@microsoft.com>
This commit is contained in:
parent
649e88437b
commit
b991daa0e3
16
build_library/oem/vagrant/build/box/Vagrantfile
vendored
16
build_library/oem/vagrant/build/box/Vagrantfile
vendored
@ -1,11 +1,8 @@
|
||||
# -*- mode: ruby -*-
|
||||
# # vi: set ft=ruby :
|
||||
|
||||
if Vagrant::VERSION < "1.6.0"
|
||||
raise "Need at least vagrant version 1.6.0, please update"
|
||||
end
|
||||
Vagrant.require_version ">= 2.2.5"
|
||||
|
||||
require_relative 'change_host_name.rb'
|
||||
require_relative 'configure_networks.rb'
|
||||
require_relative 'base_mac.rb'
|
||||
|
||||
@ -27,6 +24,10 @@ Vagrant.configure("2") do |config|
|
||||
# Fix docker not being able to resolve private registry in VirtualBox
|
||||
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
|
||||
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
|
||||
|
||||
if File.exist?("config.ign")
|
||||
vb.customize ["guestproperty", "set", :id, "/Ignition/Config", File.read("config.ign")]
|
||||
end
|
||||
end
|
||||
|
||||
config.vm.provider :vmware_fusion do |vf|
|
||||
@ -38,4 +39,9 @@ Vagrant.configure("2") do |config|
|
||||
prl.check_guest_tools = false
|
||||
prl.functional_psf = false
|
||||
end
|
||||
end
|
||||
|
||||
if File.exist?("user-data")
|
||||
config.vm.provision :file, :source => "user-data", :destination => "/tmp/vagrantfile-user-data"
|
||||
config.vm.provision :shell, :inline => "mv /tmp/vagrantfile-user-data /var/lib/flatcar-vagrant/", :privileged => true
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
# -*- mode: ruby -*-
|
||||
# # vi: set ft=ruby :
|
||||
|
||||
# NOTE: This monkey-patching of the coreos guest plugin is a terrible
|
||||
# hack that needs to be removed once the upstream plugin works with
|
||||
# alpha CoreOS images.
|
||||
|
||||
require 'tempfile'
|
||||
require Vagrant.source_root.join("plugins/guests/coreos/cap/change_host_name.rb")
|
||||
|
||||
CLOUD_CONFIG = <<EOF
|
||||
#cloud-config
|
||||
|
||||
hostname: %s
|
||||
EOF
|
||||
|
||||
module VagrantPlugins
|
||||
module GuestCoreOS
|
||||
module Cap
|
||||
class ChangeHostName
|
||||
def self.change_host_name(machine, name)
|
||||
machine.communicate.tap do |comm|
|
||||
temp = Tempfile.new("coreos-vagrant")
|
||||
temp.binmode
|
||||
temp.write(CLOUD_CONFIG % [name])
|
||||
temp.close
|
||||
|
||||
path = "/var/tmp/hostname.yml"
|
||||
path_esc = path.gsub("/", "-")[1..-1]
|
||||
comm.upload(temp.path, path)
|
||||
comm.sudo("systemctl start system-cloudinit@#{path_esc}.service")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,140 +1,18 @@
|
||||
# -*- mode: ruby -*-
|
||||
# # vi: set ft=ruby :
|
||||
|
||||
# NOTE: This monkey-patching of the coreos guest plugin is a terrible
|
||||
# hack that needs to be removed once the upstream plugin works with
|
||||
# alpha CoreOS images.
|
||||
# NOTE: This monkey-patching is done to force cloud-init over NetworkManager.
|
||||
# Vagrant attempts to detect cloud-init, but Flatcar doesn't have an executable
|
||||
# under that name, only coreos-cloudinit.
|
||||
|
||||
require 'tempfile'
|
||||
require 'ipaddr'
|
||||
require 'log4r'
|
||||
require Vagrant.source_root.join("plugins/guests/coreos/cap/configure_networks.rb")
|
||||
|
||||
BASE_CLOUD_CONFIG = <<EOF
|
||||
#cloud-config
|
||||
|
||||
write_files:
|
||||
- path: /etc/environment
|
||||
content: |
|
||||
COREOS_PUBLIC_IPV4=%s
|
||||
COREOS_PRIVATE_IPV4=%s
|
||||
coreos:
|
||||
units:
|
||||
EOF
|
||||
|
||||
NETWORK_UNIT = <<EOF
|
||||
- name: %s
|
||||
runtime: no
|
||||
content: |
|
||||
[Match]
|
||||
%s
|
||||
|
||||
[Network]
|
||||
Address=%s
|
||||
EOF
|
||||
|
||||
# Borrowed from http://stackoverflow.com/questions/1825928/netmask-to-cidr-in-ruby
|
||||
IPAddr.class_eval do
|
||||
def to_cidr
|
||||
self.to_i.to_s(2).count("1")
|
||||
end
|
||||
end
|
||||
|
||||
module VagrantPlugins
|
||||
module GuestCoreOS
|
||||
module Cap
|
||||
class ConfigureNetworks
|
||||
@@logger = Log4r::Logger.new("vagrant::guest::coreos::configure_networks")
|
||||
|
||||
def self.configure_networks(machine, networks)
|
||||
public_ipv4, private_ipv4 = get_environment_ips(machine, "127.0.0.1")
|
||||
cfg = BASE_CLOUD_CONFIG % [public_ipv4, private_ipv4]
|
||||
|
||||
# Define network units by mac address if possible.
|
||||
match_rules = {}
|
||||
if false
|
||||
#if machine.provider.capability?(:nic_mac_addresses)
|
||||
# untested, required feature hasn't made it into a release yet
|
||||
match_rules = match_by_mac(machine)
|
||||
else
|
||||
match_rules = match_by_name(machine)
|
||||
end
|
||||
|
||||
@@logger.debug("Networks: #{networks.inspect}")
|
||||
@@logger.debug("Interfaces: #{match_rules.inspect}")
|
||||
|
||||
# Generate any static networks, let DHCP handle the rest
|
||||
networks.each do |network|
|
||||
next if network[:type].to_sym != :static
|
||||
interface = network[:interface].to_i
|
||||
unit_name = "50-vagrant%d.network" % [interface]
|
||||
|
||||
match = match_rules[interface]
|
||||
if match.nil?
|
||||
@@logger.warn("Could not find match rule for network #{network.inspect}")
|
||||
next
|
||||
end
|
||||
|
||||
cidr = IPAddr.new(network[:netmask]).to_cidr
|
||||
address = "%s/%s" % [network[:ip], cidr]
|
||||
cfg << NETWORK_UNIT % [unit_name, match, address]
|
||||
end
|
||||
|
||||
machine.communicate.tap do |comm|
|
||||
temp = Tempfile.new("coreos-vagrant")
|
||||
temp.binmode
|
||||
temp.write(cfg)
|
||||
temp.close
|
||||
|
||||
path = "/var/tmp/networks.yml"
|
||||
path_esc = path.gsub("/", "-")[1..-1]
|
||||
comm.upload(temp.path, path)
|
||||
comm.sudo("systemctl start system-cloudinit@#{path_esc}.service")
|
||||
end
|
||||
end
|
||||
|
||||
# Find IP addresses to export in /etc/environment. This only works
|
||||
# for static addresses defined in the user's Vagrantfile.
|
||||
def self.get_environment_ips(machine, default)
|
||||
public_ipv4 = nil
|
||||
private_ipv4 = nil
|
||||
|
||||
machine.config.vm.networks.each do |type, options|
|
||||
next if !options[:ip]
|
||||
if type == :public_network
|
||||
public_ipv4 = options[:ip]
|
||||
elsif type == :private_network
|
||||
private_ipv4 = options[:ip]
|
||||
end
|
||||
end
|
||||
|
||||
# Fall back to localhost if no static networks are configured.
|
||||
private_ipv4 ||= default
|
||||
public_ipv4 ||= private_ipv4
|
||||
return [public_ipv4, private_ipv4]
|
||||
end
|
||||
|
||||
def self.match_by_name(machine)
|
||||
match = {}
|
||||
machine.communicate.tap do |comm|
|
||||
comm.sudo("ifconfig -a | grep '^en\\|^eth' | cut -f1 -d:") do |_, result|
|
||||
result.split("\n").each_with_index do |name, interface|
|
||||
match[interface] = "Name=#{name}"
|
||||
end
|
||||
end
|
||||
end
|
||||
match
|
||||
end
|
||||
|
||||
def self.match_by_mac(machine)
|
||||
match = {}
|
||||
macs = machine.provider.capability(:nic_mac_addresses)
|
||||
macs.each do |adapter, address|
|
||||
# The adapter list from VirtualBox is 1 indexed instead of 0
|
||||
interface = adapter.to_i - 1
|
||||
match[interface] = "MACAddress=#{address}"
|
||||
end
|
||||
match
|
||||
configure_networks_cloud_init(machine, networks)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
1
changelog/changes/2026-04-29-vagrant.md
Normal file
1
changelog/changes/2026-04-29-vagrant.md
Normal file
@ -0,0 +1 @@
|
||||
- Refreshed the Vagrant and Vagrant Parallels images for use with recent Vagrant releases, adding implicit support for provisioning with Ignition (VirtualBox only) or cloud-config. Vagrant 2.2.5 is now required. See the revised documentation for further details.
|
||||
Loading…
x
Reference in New Issue
Block a user