support for !cmd in configuration files

dev
Philippe PITTOLI 3 years ago
parent 2958e7a60e
commit 12d17aec03
  1. 1
      src/cli.cr
  2. 49
      src/configuration.cr
  3. 11
      src/do.cr
  4. 35
      src/network_configuration_parser.cr
  5. 7
      src/tests.cr

@ -115,7 +115,6 @@ when "OpenBSD"
NetworkCommands.cmd_dhcp_client = NetworkCommands::OpenBSDDHClientCommand
NetworkCommands.cmd_sysctl = NetworkCommands::OpenBSDSysctlCommand
when "Linux"
puts "I'm on linux, look at me!"
key = Context.prefered_network_configuration_program
key = possible_network_configuration_cmds.keys.find { |key| Autodetect.which(key) } if key.nil?
# should crash if there is no network command installed

@ -119,26 +119,18 @@ class InterfaceConfiguration
property aliasses_v4 : Array(IPAddress)
property aliasses_v6 : Array(IPAddress)
property wireless_networks : Hash(String, WirelessAPSetup)
property dns : NetworkCommands::DNS
def initialize (@name, @up,
@description,
@mtu,
@main_ip_v4, @main_ip_v6, aliasses,
@wireless_networks,
@dns
)
property dns : NetworkCommands::DNS?
property command_lines : Array(String)
def initialize (@name)
@up = false
@aliasses_v4 = Array(IPAddress).new
@aliasses_v6 = Array(IPAddress).new
aliasses.each do |ip|
if ip.ipv4?
aliasses_v4 << ip
else
aliasses_v6 << ip
end
end
@command_lines = Array(String).new
@main_ip_v4 = NotSetup.new
@main_ip_v6 = NotSetup.new
@wireless_networks = Hash(String, WirelessAPSetup).new
@wireless_networks = Hash(String, WirelessAPSetup).new
end
def to_s(io : IO)
@ -191,7 +183,13 @@ class InterfaceConfiguration
str << "\t#{CRED}Should main ipv6 be obtained from autoconfiguration? DHCP? Static configuration?#{CRESET}\n"
end
str << indent(1, dns.to_s) unless dns.addresses.empty?
thedns = @dns
if thedns.nil?
else
unless thedns.addresses.empty?
str << indent(1, thedns.to_s)
end
end
unless wireless_networks.empty?
wireless_networks.each do |k,v|
@ -251,6 +249,14 @@ class InterfaceConfiguration
return
end
unless command_lines.empty?
puts "Launch these lines:"
command_lines.each do |cmd|
puts "cmd: #{cmd}"
Do.runsh cmd
end
end
unless mtu.nil?
NetworkCommands.mtu name, mtu
end
@ -261,9 +267,9 @@ class InterfaceConfiguration
# TODO: treat differently wireless and non-wireless interfaces
if @wireless_networks.empty?
puts "interface #{name} is not wireless"
# puts "interface #{name} is not wireless"
else
puts "interface #{name} is wireless: connection to the access point"
# puts "interface #{name} is wireless: connection to the access point"
store_access_point_keys
access_point_connection
end
@ -313,7 +319,8 @@ class InterfaceConfiguration
end
# DNS configuration
dns.execute
thedns = @dns
thedns.execute unless thedns.nil?
end
def down

@ -19,4 +19,15 @@ class Do < Process
Process.run cmd, params, &block
end
end
def self.runsh(cmd : String)
if @@simulation
puts "simulation, do in a shell : #{cmd}"
Process::Status.new 0
else
r = Process.run "sh", ["-c", cmd],
output: Process::Redirect::Inherit,
error: Process::Redirect::Inherit
end
end
end

@ -21,6 +21,7 @@ class NetworkConfigurationParser
dns = NetworkCommands::DNS.new
aliasses = [] of IPAddress
command_lines = [] of String
wireless_networks = {} of String => WirelessAPSetup
@ -164,6 +165,13 @@ class NetworkConfigurationParser
ipaddr = IPAddress.parse ipstr
dns.addresses << ipaddr.to_s
when /^!(?<cmd>.*)/
cmd = $~["cmd"].not_nil!
command_lines << cmd
puts "new command: #{cmd}"
when /^#.*$/
# simple comment
when /^[ \t]*$/
@ -173,12 +181,25 @@ class NetworkConfigurationParser
end
end
InterfaceConfiguration.new(ifname, up,
description,
mtu,
main_ip_v4, main_ip_v6,
aliasses,
wireless_networks,
dns)
iface_conf = InterfaceConfiguration.new(ifname)
iface_conf.up = up
iface_conf.description = description
iface_conf.mtu = mtu
iface_conf.main_ip_v4 = main_ip_v4
iface_conf.main_ip_v6 = main_ip_v6
aliasses.each do |ip|
if ip.ipv4?
iface_conf.aliasses_v4 << ip
else
iface_conf.aliasses_v6 << ip
end
end
iface_conf.wireless_networks = wireless_networks
iface_conf.dns = dns
iface_conf.command_lines = command_lines
iface_conf
end
end

@ -1,3 +1,4 @@
require "./do"
require "./indent"
str = "truc
@ -8,3 +9,9 @@ lignes"
indented = indent 2, str
puts indented
cmd = "ls"
Do.runsh cmd

Loading…
Cancel
Save