examples/development/vagrant/setup_private_network

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

require "fileutils"

interfaces = `ip link show`.split("\n").grep(/enp/).map do |l|
  l =~ /.*(enp\ds\d):/
  Regexp.last_match[1]
end

if interfaces.size != 2
  $stderr.write("Expeted 2 network interfaces, found #{interfaces.size}\n")
  exit(1)
end

internal_interface = interfaces.find do |dev|
  `/sbin/ifconfig #{dev}`.split("\n").grep(/inet addr:/).empty?
end

if internal_interface.nil?
  puts "Cannot find network interface used by private network"
  puts "Assuming everything has already been configured"
  exit(0)
end

target_cfg = "/etc/sysconfig/network/ifcfg-#{internal_interface}"

if File.exist?("/etc/sysconfig/network/ifcfg-eth1")
  FileUtils.mv("/etc/sysconfig/network/ifcfg-eth1", target_cfg)
  cfg = File.read(target_cfg).gsub("eth1", internal_interface)
  File.open(target_cfg, "w") do |file|
    file.write(cfg)
    file.write("STARTMODE='auto'\n")
  end
  if ARGV.empty?
    puts "Restarting network configuration"
    system("systemctl restart network")
  else
    system("/sbin/ifconfig #{internal_interface} #{ARGV[0]} up")
  end
elsif File.exist?(target_cfg)
  puts "Previous network configuration found for #{internal_interface}"
  puts "Reloading network configuration"
  system("systemctl reload network")
else
  $stderr.write("Cannot find configuration for the chosen internal interface\n")
  exit(1)
end