Skip to content

Commit

Permalink
Merge 0dccce7 into 2334a87
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Jan 21, 2021
2 parents 2334a87 + 0dccce7 commit 234453e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 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
24 changes: 21 additions & 3 deletions src/lib/y2network/network_manager/connection_config_writer.rb
Expand Up @@ -17,33 +17,51 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"
require "cfa/nm_connection"
require "pathname"

Yast.import "Installation"

module Y2Network
module NetworkManager
class ConnectionConfigWriter
include Yast::Logger

SYSTEM_CONNECTIONS_PATH = Pathname.new("/etc/NetworkManager/system-connections").freeze
FILE_EXT = ".nmconnection".freeze
# @return [String] base directory
attr_reader :basedir

# Constructor
#
# @param basedir [String]
def initialize(basedir = Yast::Installation.destdir)
@basedir = basedir
end

def write(conn, old_conn = nil)
def write(conn, old_conn = nil, routes = [])
return if conn == old_conn

path = SYSTEM_CONNECTIONS_PATH.join(conn.name).sub_ext(FILE_EXT)
path = path_for(conn)
file = CFA::NmConnection.new(path)
handler_class = find_handler_class(conn.type)
return nil if handler_class.nil?

ensure_permissions(path) unless ::File.exist?(path)

handler_class.new(file).write(conn)
handler_class.new(file).write(conn, routes)
file.save
end

private

def path_for(conn)
Yast.import "Installation"
conn_file_path = SYSTEM_CONNECTIONS_PATH.join(conn.name).sub_ext(FILE_EXT)
Pathname.new(File.join(basedir, conn_file_path))
end

def ensure_permissions(path)
::FileUtils.touch(path)
::File.chmod(0o600, path)
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 234453e

Please sign in to comment.