From 80021ceafb10253c15d581ba2b2a1a98b2fb0da0 Mon Sep 17 00:00:00 2001 From: Olivier Meurice Date: Thu, 12 Nov 2015 14:33:04 +0100 Subject: [PATCH] Add network configuration plugin for Slackware Linux --- .../guests/slackware/cap/change_host_name.rb | 19 ++++++++++ .../slackware/cap/configure_networks.rb | 36 +++++++++++++++++++ plugins/guests/slackware/guest.rb | 11 ++++++ plugins/guests/slackware/plugin.rb | 25 +++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 plugins/guests/slackware/cap/change_host_name.rb create mode 100644 plugins/guests/slackware/cap/configure_networks.rb create mode 100644 plugins/guests/slackware/guest.rb create mode 100644 plugins/guests/slackware/plugin.rb diff --git a/plugins/guests/slackware/cap/change_host_name.rb b/plugins/guests/slackware/cap/change_host_name.rb new file mode 100644 index 000000000..07976725f --- /dev/null +++ b/plugins/guests/slackware/cap/change_host_name.rb @@ -0,0 +1,19 @@ +module VagrantPlugins + module GuestSlackware + module Cap + class ChangeHostName + def self.change_host_name(machine, name) + machine.communicate.tap do |comm| + # Only do this if the hostname is not already set + if !comm.test("sudo hostname | grep '#{name}'") + comm.sudo("chmod o+w /etc/hostname") + comm.sudo("echo #{name} > /etc/hostname") + comm.sudo("chmod o-w /etc/hostname") + comm.sudo("hostname -F /etc/hostname") + end + end + end + end + end + end +end diff --git a/plugins/guests/slackware/cap/configure_networks.rb b/plugins/guests/slackware/cap/configure_networks.rb new file mode 100644 index 000000000..dc84080e0 --- /dev/null +++ b/plugins/guests/slackware/cap/configure_networks.rb @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +require "tempfile" + +require "vagrant/util/template_renderer" + +module VagrantPlugins + module GuestSlackware + module Cap + class ConfigureNetworks + include Vagrant::Util + + def self.configure_networks(machine, networks) + interfaces = Array.new + machine.communicate.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, result| + interfaces = result.split("\n") + end + + networks.each do |network| + network[:device] = interfaces[network[:interface]] + + entry = TemplateRenderer.render("guests/slackware/network_#{network[:type]}", options: network) + + temp = Tempfile.new("vagrant") + temp.binmode + temp.write(entry) + temp.close + + machine.communicate.upload(temp.path, "/tmp/vagrant_network") + machine.communicate.sudo("mv /tmp/vagrant_network /etc/rc.d/rc.inet1.conf") + machine.communicate.sudo("/etc/rc.d/rc.inet1") + end + end + end + end + end +end diff --git a/plugins/guests/slackware/guest.rb b/plugins/guests/slackware/guest.rb new file mode 100644 index 000000000..1442f26fc --- /dev/null +++ b/plugins/guests/slackware/guest.rb @@ -0,0 +1,11 @@ +require "vagrant" + +module VagrantPlugins + module GuestSlackware + class Guest < Vagrant.plugin("2", :guest) + def detect?(machine) + machine.communicate.test("cat /etc/slackware-version") + end + end + end +end diff --git a/plugins/guests/slackware/plugin.rb b/plugins/guests/slackware/plugin.rb new file mode 100644 index 000000000..c81db6aa4 --- /dev/null +++ b/plugins/guests/slackware/plugin.rb @@ -0,0 +1,25 @@ +require "vagrant" + +module VagrantPlugins + module GuestSlackware + class Plugin < Vagrant.plugin("2") + name "Slackware guest" + description "Slackware guest support." + + guest("slackware", "linux") do + require File.expand_path("../guest", __FILE__) + Guest + end + + guest_capability("slackware", "change_host_name") do + require_relative "cap/change_host_name" + Cap::ChangeHostName + end + + guest_capability("slackware", "configure_networks") do + require_relative "cap/configure_networks" + Cap::ConfigureNetworks + end + end + end +end