Skip to content

Commit

Permalink
Merge pull request #1186 from yast/nm_parent_devices
Browse files Browse the repository at this point in the history
NM: add support to bridge and bonding configurations
  • Loading branch information
teclator committed Mar 17, 2021
2 parents e5627e9 + 25636a3 commit 31385c7
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 10 deletions.
7 changes: 7 additions & 0 deletions package/yast2-network.changes
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Wed Mar 17 09:52:08 UTC 2021 - Knut Anderssen <kanderssen@suse.com>

- NetworkManager: Added support to write bridge and bonding
configurations (bsc#1181701)
- 4.3.60

-------------------------------------------------------------------
Mon Mar 15 08:18:11 UTC 2021 - Knut Anderssen <kanderssen@suse.com>

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-network.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2-network
Version: 4.3.59
Version: 4.3.60
Release: 0
Summary: YaST2 - Network Configuration
License: GPL-2.0-only
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cfa/nm_connection.rb
Expand Up @@ -29,7 +29,7 @@ module CFA
# puts file.connection["id"]
class NmConnection < BaseModel
KNOWN_SECTIONS = [
"bridge", "connection", "ethernet", "ipv4", "ipv6", "vlan", "wifi", "wifi_security"
"bond", "bridge", "connection", "ethernet", "ipv4", "ipv6", "vlan", "wifi", "wifi_security"
].freeze

# Constructor
Expand Down
6 changes: 5 additions & 1 deletion src/lib/y2network/network_manager/config_writer.rb
Expand Up @@ -35,7 +35,11 @@ class ConfigWriter < Y2Network::ConfigWriter
def write_connections(config, _old_config)
writer = Y2Network::NetworkManager::ConnectionConfigWriter.new
config.connections.each do |conn|
writer.write(conn, nil, routes_for(conn, config.routing.routes)) # FIXME
opts = {
routes: routes_for(conn, config.routing.routes),
parent: conn.find_master(config.connections)
}.reject { |_k, v| v.nil? }
writer.write(conn, nil, **opts) # FIXME
end
end

Expand Down
7 changes: 3 additions & 4 deletions src/lib/y2network/network_manager/connection_config_writer.rb
Expand Up @@ -35,9 +35,8 @@ class ConnectionConfigWriter
# 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 = [])
# @param opts [Hash] writer options
def write(conn, old_conn = nil, **opts)
return if conn == old_conn

path = SYSTEM_CONNECTIONS_PATH.join(conn.name).sub_ext(FILE_EXT)
Expand All @@ -47,7 +46,7 @@ def write(conn, old_conn = nil, routes = [])

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

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

Expand Down
Expand Up @@ -41,14 +41,16 @@ def initialize(file)
#
# @param conn [Y2Network::ConnectionConfig::Base] Connection to take settings from
# @param routes [<Array<Y2Network::Route>] routes associated with the connection
def write(conn, routes = [])
# @param parent [Y2Network::ConnectionConfig::Bonding, Y2Network::ConnectionConfig::Bridge]
def write(conn, routes: [], parent: nil)
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) : configure_ips(conn)
configure_routes(routes)
configure_as_child(parent) if parent
update_file(conn)
end

Expand Down Expand Up @@ -97,6 +99,18 @@ def configure_dhcp(conn)
file.ipv6["method"] = "auto" if conn.bootproto != Y2Network::BootProtocol::DHCP4
end

# Convenience method to configure the reference to the parent or master device
#
# @param parent [Y2Network::ConnectionConfig::Base] Connection to take settings from
def configure_as_child(parent)
slave_type = "bridge" if parent.type.br?
slave_type = "bond" if parent.type.bonding?
return unless slave_type

file.connection["slave-type"] = slave_type
file.connection["master"] = parent.name
end

# Sets file values from the given connection configuration
#
# @note This method should be redefined by derived classes.
Expand Down
@@ -0,0 +1,42 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2network/network_manager/connection_config_writers/base"

module Y2Network
module NetworkManager
module ConnectionConfigWriters
# This class is responsible for writing the information from a ConnectionConfig::Bonding
# object to the underlying system.
class Bonding < Base
# @see Y2Network::ConnectionConfigWriters::Base#update_file
# @param conn [Y2Network::ConnectionConfig::Bonding] Configuration to write
def update_file(conn)
file.connection["type"] = "bond"
conn.options.to_s.split.each do |option|
k, v = option.split("=")
next if [k, v].any? { |o| o.to_s.empty? }

file.bond[k] = v
end
end
end
end
end
end
@@ -0,0 +1,38 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2network/network_manager/connection_config_writers/base"

module Y2Network
module NetworkManager
module ConnectionConfigWriters
# This class is responsible for writing the information from a ConnectionConfig::Bridge
# object to the underlying system.
class Bridge < Base
# @see Y2Network::ConnectionConfigWriters::Base#update_file
# @param conn [Y2Network::ConnectionConfig::Bonding] Configuration to write
def update_file(conn)
file.connection["type"] = "bridge"
file.bridge["stp"] = conn.stp.to_s
file.bridge["forward-delay"] = conn.forward_delay.to_s
end
end
end
end
end
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, routes: [])
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,0 +1,52 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../../test_helper"
require "y2network/network_manager/connection_config_writers/bonding"
require "cfa/nm_connection"
require "y2network/boot_protocol"
require "y2network/startmode"
require "y2network/connection_config/bonding"

describe Y2Network::NetworkManager::ConnectionConfigWriters::Bonding do
subject(:handler) { described_class.new(file) }
let(:file) { CFA::NmConnection.new("bond0.nm_connection") }

let(:conn) do
Y2Network::ConnectionConfig::Bonding.new.tap do |c|
c.interface = "bond0"
c.description = "Bond 0"
c.startmode = Y2Network::Startmode.create("auto")
c.bootproto = Y2Network::BootProtocol::DHCP
c.options = "mode=active-backup miimon=100"
c.slaves = ["eth0"]
end
end

describe "#write" do
it "sets device and IP relevant attributes" do
handler.write(conn)
expect(file.connection["type"]).to eql("bond")
expect(file.bond["mode"]).to eql("active-backup")
expect(file.bond["miimon"]).to eql("100")
expect(file.ipv4["method"]).to eql("auto")
expect(file.ipv6["method"]).to eql("auto")
end
end
end
@@ -0,0 +1,53 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../../test_helper"
require "y2network/network_manager/connection_config_writers/bridge"
require "cfa/nm_connection"
require "y2network/boot_protocol"
require "y2network/startmode"
require "y2network/connection_config/bridge"

describe Y2Network::NetworkManager::ConnectionConfigWriters::Bridge do
subject(:handler) { described_class.new(file) }
let(:file) { CFA::NmConnection.new("br0.nm_connection") }

let(:conn) do
Y2Network::ConnectionConfig::Bridge.new.tap do |c|
c.interface = "br0"
c.description = "Bridge 0"
c.startmode = Y2Network::Startmode.create("auto")
c.bootproto = Y2Network::BootProtocol::DHCP
c.stp = true
c.forward_delay = 2
c.ports = ["eth0"]
end
end

describe "#write" do
it "sets device and IP relevant attributes" do
handler.write(conn)
expect(file.connection["type"]).to eql("bridge")
expect(file.bridge["stp"]).to eql("true")
expect(file.bridge["forward-delay"]).to eql("2")
expect(file.ipv4["method"]).to eql("auto")
expect(file.ipv6["method"]).to eql("auto")
end
end
end

0 comments on commit 31385c7

Please sign in to comment.