Skip to content

Commit

Permalink
Merge 9ab30c0 into 67ad5d2
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Jan 21, 2021
2 parents 67ad5d2 + 9ab30c0 commit 4bdb437
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
6 changes: 1 addition & 5 deletions src/lib/cfa/nm_connection.rb
Expand Up @@ -32,18 +32,14 @@ class NmConnection < BaseModel
"bridge", "connection", "ethernet", "ipv4", "ipv6", "vlan", "wifi", "wifi_security"
].freeze

# @return [String] file path
attr_reader :path

# Constructor
#
# @param path [String] File path
# @param file_handler [.read, .write] Object to read/write the file.
def initialize(path, file_handler: nil)
@path = path
# FIXME: The Networkmanager lense writes the values surrounded by double
# quotes which is not valid
super(AugeasParser.new("Puppet.lns"), @path, file_handler: file_handler)
super(AugeasParser.new("Puppet.lns"), path, file_handler: file_handler)
end

# Returns the augeas tree for the given section
Expand Down
11 changes: 10 additions & 1 deletion src/lib/y2network/network_manager/config_writer.rb
Expand Up @@ -35,9 +35,18 @@ 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, routes_for(conn, config.routing.routes)) # FIXME
end
end

# Finds routes for a given connection
#
# @param conn [ConnectionConfig::Base] Connection configuration
# @param routes [Array<Route>] List of routes to search in
# @return [Array<Route>] List of routes for the given connection
def routes_for(conn, routes)
routes.select { |r| r.interface&.name == conn.name }
end
end
end
end
22 changes: 18 additions & 4 deletions src/lib/y2network/network_manager/connection_config_writer.rb
Expand Up @@ -17,9 +17,12 @@
# 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
Expand All @@ -28,7 +31,13 @@ class ConnectionConfigWriter
SYSTEM_CONNECTIONS_PATH = Pathname.new("/etc/NetworkManager/system-connections").freeze
FILE_EXT = ".nmconnection".freeze

def write(conn, old_conn = nil)
# @param conn [ConnectionConfig::Base] Connection configuration to be
# written
# @param old_conn [ConnectionConfig::Base] Original connection
# configuration
# @param routes [Array<Routes>] routes associated with the connection to be
# written
def write(conn, old_conn = nil, routes = [])
return if conn == old_conn

path = SYSTEM_CONNECTIONS_PATH.join(conn.name).sub_ext(FILE_EXT)
Expand All @@ -38,15 +47,20 @@ def write(conn, old_conn = 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

# Convenience method to ensure the new configuration file permissions
#
# @param path [Pathname] connection configuration file path
def ensure_permissions(path)
::FileUtils.touch(path)
::File.chmod(0o600, path)
final_path = ::File.join(Yast::WFM.scr_root, path)

::FileUtils.touch(final_path)
::File.chmod(0o600, final_path)
end

# Returns the class to handle a given interface type
Expand Down
Expand Up @@ -40,23 +40,39 @@ 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<Y2Network::Route>] routes associated with the connection
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(routes)
update_file(conn)
end

private

# Convenience method to write routing configuration associated with the
# connection config to be written
#
# @param routes [<Array<Y2Network::Route>] routes associated with the connection
def configure_routes(routes)
routes.select(&:default?).each { |r| configure_gateway(r) }
end

# @param route [Y2Network::Route] route to be written
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/wicked/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 @@ -54,7 +54,7 @@
let(:path) { "/etc/NetworkManager/system-connections/eth0.nmconnection" }

let(:file) do
instance_double(CFA::NmConnection, save: nil, path: path)
instance_double(CFA::NmConnection, save: nil)
end

describe "#write" do
Expand All @@ -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 4bdb437

Please sign in to comment.