Skip to content

Commit

Permalink
Add basic support for writing routes (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Jan 21, 2021
1 parent 2334a87 commit 5726546
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib/y2network/network_manager/config_writer.rb
Expand Up @@ -35,7 +35,7 @@ class ConfigWriter < Y2Network::ConfigWriter
def write_connections(config, _old_config)
writer = Y2Network::NetworkManager::ConnectionConfigWriter.new
config.connections.each do |conn|
writer.write(conn, nil) # FIXME
writer.write(conn, nil, config.routing.routes) # FIXME
end
end
end
Expand Down
Expand Up @@ -40,23 +40,36 @@ def initialize(file)
# Writes connection information to the interface configuration file
#
# @param conn [Y2Network::ConnectionConfig::Base] Connection to take settings from
def write(conn)
# @param routes [<Array<Route>]
def write(conn, routes = [])
file.connection["id"] = conn.name
file.connection["autoconnect"] = "false" if ["manual", "off"].include? conn.startmode.name
file.connection["permissions"] = nil
file.connection["interface-name"] = conn.interface
file.connection["zone"] = conn.firewall_zone unless ["", nil].include? conn.firewall_zone
conn.bootproto.dhcp? ? configure_dhcp(conn) : add_ips(conn)
conn.bootproto.dhcp? ? configure_dhcp(conn) : configure_ips(conn)
configure_routes(conn, routes)
update_file(conn)
end

private

def configure_routes(conn, routes)
routes.select { |r| (r.interface&.name == conn.name) && r.is_default? }.each do |route|
configure_gateway(route)
end
end

def configure_gateway(route)
section = route.gateway.ipv4? ? file.ipv4 : file.ipv6
section["gateway"] = route.gateway.to_s
end

# FIXME: Gateway is missing
# Convenience method for writing the static IP configuration
#
# @param conn [Y2Network::ConnectionConfig::Base] Connection to take settings from
def add_ips(conn)
def configure_ips(conn)
ips_to_add = conn.ip_aliases.clone
ips_to_add.append(conn.ip) if conn.ip
ipv4 = ips_to_add.select { |i| i&.address&.ipv4? }.map { |i| i.address.to_s }
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2network/sysconfig/config_writer.rb
Expand Up @@ -107,7 +107,7 @@ def find_routes_for_iface(iface, routes)
#
# @param iface [Interface,nil] Interface to search routes for; nil will
# return the global routes file
# @return [Y2Network::ConfigWriters::RoutesFile]
# @return [CFA::RoutesFile]
def routes_file_for(iface)
return CFA::RoutesFile.new unless iface

Expand Down
2 changes: 1 addition & 1 deletion test/y2network/network_manager/config_writer_test.rb
Expand Up @@ -71,7 +71,7 @@
end

it "writes connections configuration" do
expect(conn_config_writer).to receive(:write).with(eth0_conn, nil)
expect(conn_config_writer).to receive(:write).with(eth0_conn, nil, [])
writer.write(config)
end
end
Expand Down
Expand Up @@ -75,7 +75,7 @@

it "uses the appropiate handler" do
expect(writer).to receive(:require).and_return(handler)
expect(handler).to receive(:write).with(conn)
expect(handler).to receive(:write).with(conn, [])
writer.write(conn)
end

Expand Down

0 comments on commit 5726546

Please sign in to comment.