Add ALT Linux platforms guest detection and support

* ALT Linux platforms is an original rpm based distribution
forked as Mandrake Russian Edition Spring at 2001 year
* Distributions of ALT Linux use etcnet (https://www.altlinux.org/Etcnet) as
internal network configuration system needs for configure_networks support
This commit is contained in:
Evgeny Sinelnikov 2017-07-02 18:38:20 +03:00
parent 0ff65a249c
commit 3dd90aa955
11 changed files with 294 additions and 0 deletions

View File

@ -0,0 +1,44 @@
module VagrantPlugins
module GuestALT
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
comm = machine.communicate
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
basename = name.split('.', 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Save current hostname saved in /etc/hosts
CURRENT_HOSTNAME_FULL="$(hostname -f)"
CURRENT_HOSTNAME_SHORT="$(hostname -s)"
# New hostname to be saved in /etc/hosts
NEW_HOSTNAME_FULL='#{name}'
NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}"
# Update sysconfig
sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
# Set the hostname - use hostnamectl if available
if command -v hostnamectl; then
hostnamectl set-hostname --static '#{name}'
hostnamectl set-hostname --transient '#{name}'
else
hostname '#{name}'
fi
# Update ourselves in /etc/hosts
if grep -w "$CURRENT_HOSTNAME_FULL" /etc/hosts; then
sed -i -e "s/\(\s\)$CURRENT_HOSTNAME_FULL\(\s\)/\1$NEW_HOSTNAME_FULL\2/g" -e "s/\(\s\)$CURRENT_HOSTNAME_FULL$/\1$NEW_HOSTNAME_FULL/g" /etc/hosts
fi
if grep -w "$CURRENT_HOSTNAME_SHORT" /etc/hosts; then
sed -i -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT\(\s\)/\1$NEW_HOSTNAME_SHORT\2/g" -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT$/\1$NEW_HOSTNAME_SHORT/g" /etc/hosts
fi
EOH
end
end
end
end
end
end

View File

@ -0,0 +1,118 @@
require "tempfile"
require_relative "../../../../lib/vagrant/util/template_renderer"
module VagrantPlugins
module GuestALT
module Cap
class ConfigureNetworks
include Vagrant::Util
extend Vagrant::Util::GuestInspection::Linux
def self.configure_networks(machine, networks)
comm = machine.communicate
network_scripts_dir = machine.guest.capability(:network_scripts_dir)
commands = {:start => [], :middle => [], :end => []}
interfaces = machine.guest.capability(:network_interfaces)
# Check if NetworkManager is installed on the system
nmcli_installed = nmcli?(comm)
networks.each.with_index do |network, i|
network[:device] = interfaces[network[:interface]]
extra_opts = machine.config.vm.networks[i].last.dup
if nmcli_installed
# Now check if the device is actively being managed by NetworkManager
nm_controlled = nm_controlled?(comm, network[:device])
end
if !extra_opts.key?(:nm_controlled)
extra_opts[:nm_controlled] = !!nm_controlled
end
extra_opts[:nm_controlled] = case extra_opts[:nm_controlled]
when true
"yes"
when false, nil
"no"
else
extra_opts[:nm_controlled].to_s
end
if extra_opts[:nm_controlled] == "yes" && !nmcli_installed
raise Vagrant::Errors::NetworkManagerNotInstalled, device: network[:device]
end
# Render a new configuration
template_options = network.merge(extra_opts)
options_entry = TemplateRenderer.render("guests/alt/network_#{network[:type]}", options: template_options)
# Upload the new configuration
options_remote_path = "/tmp/vagrant-network-entry-#{network[:device]}-#{Time.now.to_i}-#{i}"
ipv4_address_remote_path = "/tmp/vagrant-network-ipv4-address-entry-#{network[:device]}-#{Time.now.to_i}-#{i}"
ipv4_route_remote_path = "/tmp/vagrant-network-ipv4-route-entry-#{network[:device]}-#{Time.now.to_i}-#{i}"
Tempfile.open("vagrant-alt-configure-networks") do |f|
f.binmode
f.write(options_entry)
f.fsync
f.close
machine.communicate.upload(f.path, options_remote_path)
end
# Add the new interface and bring it back up
iface_path = "#{network_scripts_dir}/ifaces/#{network[:device]}"
if network[:type].to_sym == :static
ipv4_address_entry = TemplateRenderer.render("guests/alt/network_ipv4address", options: template_options)
# Upload the new ipv4address configuration
Tempfile.open("vagrant-alt-configure-ipv4-address") do |f|
f.binmode
f.write(ipv4_address_entry)
f.fsync
f.close
machine.communicate.upload(f.path, ipv4_address_remote_path)
end
ipv4_route_entry = TemplateRenderer.render("guests/alt/network_ipv4route", options: template_options)
# Upload the new ipv4route configuration
Tempfile.open("vagrant-alt-configure-ipv4-route") do |f|
f.binmode
f.write(ipv4_route_entry)
f.fsync
f.close
machine.communicate.upload(f.path, ipv4_route_remote_path)
end
end
if nm_controlled
commands[:start] << "nmcli d disconnect iface '#{network[:device]}'"
else
commands[:start] << "/sbin/ifdown '#{network[:device]}'"
end
commands[:middle] << "mkdir -p '#{iface_path}'"
commands[:middle] << "mv -f '#{options_remote_path}' '#{iface_path}/options'"
if network[:type].to_sym == :static
commands[:middle] << "mv -f '#{ipv4_address_remote_path}' '#{iface_path}/ipv4address'"
commands[:middle] << "mv -f '#{ipv4_route_remote_path}' '#{iface_path}/ipv4route'"
end
if extra_opts[:nm_controlled] == "no"
commands[:end] << "/sbin/ifup '#{network[:device]}'"
end
end
if nmcli_installed
commands[:middle] << "(test -f /etc/init.d/NetworkManager && /etc/init.d/NetworkManager restart) || " \
"((systemctl | grep NetworkManager.service) && systemctl restart NetworkManager)"
end
commands = commands[:start] + commands[:middle] + commands[:end]
comm.sudo(commands.join("\n"))
comm.wait_for_ready(5)
end
end
end
end
end

View File

@ -0,0 +1,37 @@
module VagrantPlugins
module GuestALT
module Cap
class Flavor
def self.flavor(machine)
# Read the version file
if !comm.test("test -f /etc/os-release")
version = nil
machine.communicate.sudo("grep VERSION_ID /etc/os-release") do |type, data|
if type == :stdout
version = data.split("=")[1].chomp.to_i
end
end
if version.nil?
return :alt
else
return :"alt_#{version}"
end
else
output = ""
machine.communicate.sudo("cat /etc/altlinux-release") do |_, data|
output = data
end
# Detect various flavors we care about
if output =~ /(ALT Workstation K|ALT Linux starter kit)\s*8( .+)?/i
return :alt_8
else
return :alt
end
end
end
end
end
end
end

View File

@ -0,0 +1,11 @@
module VagrantPlugins
module GuestALT
module Cap
class NetworkScriptsDir
def self.network_scripts_dir(machine)
"/etc/net"
end
end
end
end
end

View File

@ -0,0 +1,13 @@
module VagrantPlugins
module GuestALT
module Cap
class RSync
def self.rsync_install(machine)
machine.communicate.sudo <<-EOH.gsub(/^ {12}/, '')
apt-get install -y -qq install rsync
EOH
end
end
end
end
end

View File

@ -0,0 +1,9 @@
module VagrantPlugins
module GuestALT
class Guest < Vagrant.plugin("2", :guest)
def detect?(machine)
machine.communicate.test("cat /etc/altlinux-release")
end
end
end
end

View File

@ -0,0 +1,40 @@
require "vagrant"
module VagrantPlugins
module GuestALT
class Plugin < Vagrant.plugin("2")
name "ALT Platform guest"
description "ALT Platform guest support."
guest(:alt, :redhat) do
require_relative "guest"
Guest
end
guest_capability(:alt, :change_host_name) do
require_relative "cap/change_host_name"
Cap::ChangeHostName
end
guest_capability(:alt, :configure_networks) do
require_relative "cap/configure_networks"
Cap::ConfigureNetworks
end
guest_capability(:alt, :flavor) do
require_relative "cap/flavor"
Cap::Flavor
end
guest_capability(:alt, :network_scripts_dir) do
require_relative "cap/network_scripts_dir"
Cap::NetworkScriptsDir
end
guest_capability(:alt, :rsync_install) do
require_relative "cap/rsync"
Cap::RSync
end
end
end
end

View File

@ -0,0 +1,7 @@
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
TYPE=eth
NM_CONTROLLED=<%= options.fetch(:nm_controlled, "no") %>
BOOTPROTO=dhcp
ONBOOT=yes
#VAGRANT-END

View File

@ -0,0 +1,3 @@
#VAGRANT-BEGIN
<%= options[:ip] %>/<%= options[:netmask] %>
#VAGRANT-END

View File

@ -0,0 +1,5 @@
#VAGRANT-BEGIN
<% if options[:gateway] %>
default via <%= options[:gateway] %>
<% end %>
#VAGRANT-END

View File

@ -0,0 +1,7 @@
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
TYPE=eth
NM_CONTROLLED=<%= options.fetch(:nm_controlled, "no") %>
BOOTPROTO=static
ONBOOT=yes
#VAGRANT-END