mirror of
https://git.baguette.netlib.re/Baguette/networkctl
synced 2024-12-18 13:03:30 +00:00
support for !cmd in configuration files
This commit is contained in:
parent
2958e7a60e
commit
12d17aec03
@ -115,7 +115,6 @@ when "OpenBSD"
|
|||||||
NetworkCommands.cmd_dhcp_client = NetworkCommands::OpenBSDDHClientCommand
|
NetworkCommands.cmd_dhcp_client = NetworkCommands::OpenBSDDHClientCommand
|
||||||
NetworkCommands.cmd_sysctl = NetworkCommands::OpenBSDSysctlCommand
|
NetworkCommands.cmd_sysctl = NetworkCommands::OpenBSDSysctlCommand
|
||||||
when "Linux"
|
when "Linux"
|
||||||
puts "I'm on linux, look at me!"
|
|
||||||
key = Context.prefered_network_configuration_program
|
key = Context.prefered_network_configuration_program
|
||||||
key = possible_network_configuration_cmds.keys.find { |key| Autodetect.which(key) } if key.nil?
|
key = possible_network_configuration_cmds.keys.find { |key| Autodetect.which(key) } if key.nil?
|
||||||
# should crash if there is no network command installed
|
# should crash if there is no network command installed
|
||||||
|
@ -119,26 +119,18 @@ class InterfaceConfiguration
|
|||||||
property aliasses_v4 : Array(IPAddress)
|
property aliasses_v4 : Array(IPAddress)
|
||||||
property aliasses_v6 : Array(IPAddress)
|
property aliasses_v6 : Array(IPAddress)
|
||||||
property wireless_networks : Hash(String, WirelessAPSetup)
|
property wireless_networks : Hash(String, WirelessAPSetup)
|
||||||
property dns : NetworkCommands::DNS
|
property dns : NetworkCommands::DNS?
|
||||||
|
property command_lines : Array(String)
|
||||||
def initialize (@name, @up,
|
|
||||||
@description,
|
|
||||||
@mtu,
|
|
||||||
@main_ip_v4, @main_ip_v6, aliasses,
|
|
||||||
@wireless_networks,
|
|
||||||
@dns
|
|
||||||
)
|
|
||||||
|
|
||||||
|
def initialize (@name)
|
||||||
|
@up = false
|
||||||
@aliasses_v4 = Array(IPAddress).new
|
@aliasses_v4 = Array(IPAddress).new
|
||||||
@aliasses_v6 = Array(IPAddress).new
|
@aliasses_v6 = Array(IPAddress).new
|
||||||
|
@command_lines = Array(String).new
|
||||||
aliasses.each do |ip|
|
@main_ip_v4 = NotSetup.new
|
||||||
if ip.ipv4?
|
@main_ip_v6 = NotSetup.new
|
||||||
aliasses_v4 << ip
|
@wireless_networks = Hash(String, WirelessAPSetup).new
|
||||||
else
|
@wireless_networks = Hash(String, WirelessAPSetup).new
|
||||||
aliasses_v6 << ip
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s(io : IO)
|
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"
|
str << "\t#{CRED}Should main ipv6 be obtained from autoconfiguration? DHCP? Static configuration?#{CRESET}\n"
|
||||||
end
|
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?
|
unless wireless_networks.empty?
|
||||||
wireless_networks.each do |k,v|
|
wireless_networks.each do |k,v|
|
||||||
@ -251,6 +249,14 @@ class InterfaceConfiguration
|
|||||||
return
|
return
|
||||||
end
|
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?
|
unless mtu.nil?
|
||||||
NetworkCommands.mtu name, mtu
|
NetworkCommands.mtu name, mtu
|
||||||
end
|
end
|
||||||
@ -261,9 +267,9 @@ class InterfaceConfiguration
|
|||||||
|
|
||||||
# TODO: treat differently wireless and non-wireless interfaces
|
# TODO: treat differently wireless and non-wireless interfaces
|
||||||
if @wireless_networks.empty?
|
if @wireless_networks.empty?
|
||||||
puts "interface #{name} is not wireless"
|
# puts "interface #{name} is not wireless"
|
||||||
else
|
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
|
store_access_point_keys
|
||||||
access_point_connection
|
access_point_connection
|
||||||
end
|
end
|
||||||
@ -313,7 +319,8 @@ class InterfaceConfiguration
|
|||||||
end
|
end
|
||||||
|
|
||||||
# DNS configuration
|
# DNS configuration
|
||||||
dns.execute
|
thedns = @dns
|
||||||
|
thedns.execute unless thedns.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
|
11
src/do.cr
11
src/do.cr
@ -19,4 +19,15 @@ class Do < Process
|
|||||||
Process.run cmd, params, &block
|
Process.run cmd, params, &block
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
@ -21,6 +21,7 @@ class NetworkConfigurationParser
|
|||||||
dns = NetworkCommands::DNS.new
|
dns = NetworkCommands::DNS.new
|
||||||
|
|
||||||
aliasses = [] of IPAddress
|
aliasses = [] of IPAddress
|
||||||
|
command_lines = [] of String
|
||||||
|
|
||||||
wireless_networks = {} of String => WirelessAPSetup
|
wireless_networks = {} of String => WirelessAPSetup
|
||||||
|
|
||||||
@ -164,6 +165,13 @@ class NetworkConfigurationParser
|
|||||||
ipaddr = IPAddress.parse ipstr
|
ipaddr = IPAddress.parse ipstr
|
||||||
dns.addresses << ipaddr.to_s
|
dns.addresses << ipaddr.to_s
|
||||||
|
|
||||||
|
when /^!(?<cmd>.*)/
|
||||||
|
cmd = $~["cmd"].not_nil!
|
||||||
|
|
||||||
|
command_lines << cmd
|
||||||
|
|
||||||
|
puts "new command: #{cmd}"
|
||||||
|
|
||||||
when /^#.*$/
|
when /^#.*$/
|
||||||
# simple comment
|
# simple comment
|
||||||
when /^[ \t]*$/
|
when /^[ \t]*$/
|
||||||
@ -173,12 +181,25 @@ class NetworkConfigurationParser
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
InterfaceConfiguration.new(ifname, up,
|
iface_conf = InterfaceConfiguration.new(ifname)
|
||||||
description,
|
iface_conf.up = up
|
||||||
mtu,
|
iface_conf.description = description
|
||||||
main_ip_v4, main_ip_v6,
|
iface_conf.mtu = mtu
|
||||||
aliasses,
|
iface_conf.main_ip_v4 = main_ip_v4
|
||||||
wireless_networks,
|
iface_conf.main_ip_v6 = main_ip_v6
|
||||||
dns)
|
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
require "./do"
|
||||||
require "./indent"
|
require "./indent"
|
||||||
|
|
||||||
str = "truc
|
str = "truc
|
||||||
@ -8,3 +9,9 @@ lignes"
|
|||||||
indented = indent 2, str
|
indented = indent 2, str
|
||||||
|
|
||||||
puts indented
|
puts indented
|
||||||
|
|
||||||
|
|
||||||
|
cmd = "ls"
|
||||||
|
|
||||||
|
Do.runsh cmd
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user