From 12d17aec030f88d52aa5075f58962c62c6c3b191 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Sat, 16 Nov 2019 20:12:58 +0100
Subject: [PATCH] support for !cmd in configuration files
---
src/cli.cr | 1 -
src/configuration.cr | 49 ++++++++++++++++-------------
src/do.cr | 11 +++++++
src/network_configuration_parser.cr | 35 ++++++++++++++++-----
src/tests.cr | 7 +++++
5 files changed, 74 insertions(+), 29 deletions(-)
diff --git a/src/cli.cr b/src/cli.cr
index d103213..d68385c 100644
--- a/src/cli.cr
+++ b/src/cli.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
diff --git a/src/configuration.cr b/src/configuration.cr
index f734228..2297408 100644
--- a/src/configuration.cr
+++ b/src/configuration.cr
@@ -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
diff --git a/src/do.cr b/src/do.cr
index f8db0a5..6cc2b72 100644
--- a/src/do.cr
+++ b/src/do.cr
@@ -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
diff --git a/src/network_configuration_parser.cr b/src/network_configuration_parser.cr
index c08286e..396751f 100644
--- a/src/network_configuration_parser.cr
+++ b/src/network_configuration_parser.cr
@@ -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"].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
diff --git a/src/tests.cr b/src/tests.cr
index 4b2f4cd..64c9fb5 100644
--- a/src/tests.cr
+++ b/src/tests.cr
@@ -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
+