Skip to content

Commit

Permalink
Merge b6b152f into 455f938
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jul 24, 2019
2 parents 455f938 + b6b152f commit a8e18b5
Show file tree
Hide file tree
Showing 27 changed files with 736 additions and 365 deletions.
11 changes: 7 additions & 4 deletions src/lib/y2network/connection_config/base.rb
Expand Up @@ -38,17 +38,20 @@ class Base
attr_accessor :interface
# @return [BootProtocol] Bootproto
attr_accessor :bootproto
# @return [IPAddr,nil]
attr_accessor :ip_address
# @return [Array<IPAddr>]
attr_accessor :secondary_ip_addresses
# @return [Array<IPConfig>]
attr_accessor :ip_configs
# @return [Integer, nil]
attr_accessor :mtu
# @return [Startmode, nil]
attr_accessor :startmode
# @return [String] Connection's description (e.g., "Ethernet Card 0")
attr_accessor :description

# Constructor
def initialize
@ip_configs = []
end

# Returns the connection type
#
# Any subclass could define this method is the default
Expand Down
50 changes: 50 additions & 0 deletions src/lib/y2network/connection_config/ip_config.rb
@@ -0,0 +1,50 @@
# Copyright (c) [2019] 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.
module Y2Network
module ConnectionConfig
class IPConfig
# @return [IPAddress] IP address
attr_accessor :address
# @return [String,nil] Address label
attr_accessor :label
# @return [IPAddress,nil] Remote IP address of a point to point connection
attr_accessor :remote_address
# @return [IPAddress,nil] Broadcast address
attr_accessor :broadcast
# @return [String] ID (needed for sysconfig backend in order to write suffixes in
attr_accessor :id

# Constructor
#
# @param address [IPAddress]
# @param id [String] ID (needed for sysconfig backend in order to write suffixes in
# ifcfg-* files)
# @param label [String,nil]
# @param remote_address [IPaddress,nil]
# @param broadcast [IPaddress,nil]
def initialize(address, id: "", label: nil, remote_address: nil, broadcast: nil)
@address = address
@id = id
@label = label
@remote_address = remote_address
@broadcast = broadcast
end
end
end
end
29 changes: 16 additions & 13 deletions src/lib/y2network/ip_address.rb
Expand Up @@ -36,13 +36,17 @@ module Y2Network
# @example IPAddress behaviour
# ip = IPAddress.new("192.168.122.1/24")
# ip.to_s #=> "192.168.122.1/24"
#
# @example IPAddress with no prefix
# ip = IPAddress.new("192.168.122.1")
# ip.to_s #=> "192.168.122.1"
class IPAddress
extend Forwardable

# @return [IPAddr] IP address
attr_reader :address
# @return [Integer] Prefix
attr_reader :prefix
attr_accessor :prefix

def_delegators :@address, :ipv4?, :ipv6?

Expand All @@ -54,11 +58,6 @@ def from_string(str)
end
end

# @return [Integer] IPv4 address default prefix
IPV4_DEFAULT_PREFIX = 32
# @return [Integer] IPv6 address default prefix
IPV6_DEFAULT_PREFIX = 128

# Constructor
#
# @param address [String] IP address without the prefix
Expand All @@ -67,12 +66,18 @@ def from_string(str)
def initialize(address, prefix = nil)
@address = IPAddr.new(address)
@prefix = prefix
@prefix ||= @address.ipv4? ? IPV4_DEFAULT_PREFIX : IPV6_DEFAULT_PREFIX
end

# Returns a string representation of the address
def to_s
host? ? @address.to_s : "#{@address}/#{@prefix}"
prefix? ? "#{@address}/#{@prefix}" : @address.to_s
end

# Sets the prefix from a netmask
#
# @param netmask [String] String representation of the netmask
def netmask=(netmask)
self.prefix = IPAddr.new("#{netmask}/#{netmask}").prefix
end

# Determines whether two addresses are equivalent
Expand All @@ -85,13 +90,11 @@ def ==(other)

alias_method :eql?, :==

private

# Determines whether it is a host address
# Determines whether a prefix is defined
#
# @return [Boolean]
def host?
(ipv4? && prefix == IPV4_DEFAULT_PREFIX) || (ipv6? && prefix == IPV6_DEFAULT_PREFIX)
def prefix?
!!@prefix
end
end
end
1 change: 1 addition & 0 deletions src/lib/y2network/sysconfig/connection_config_reader.rb
Expand Up @@ -34,6 +34,7 @@ class ConnectionConfigReader
# @return [Y2Network::ConnectionConfig::Base]
def read(name, type)
file = Y2Network::Sysconfig::InterfaceFile.new(name)
file.load
handler_class = find_handler_class(type || file.type)
return nil if handler_class.nil?
handler_class.new(file).connection_config
Expand Down
116 changes: 116 additions & 0 deletions src/lib/y2network/sysconfig/connection_config_readers/base.rb
@@ -0,0 +1,116 @@
# Copyright (c) [2019] 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/connection_config/ip_config"
require "y2network/ip_address"
require "y2network/boot_protocol"
require "y2network/startmode"

module Y2Network
module Sysconfig
module ConnectionConfigReaders
# This is the base class for connection config readers.
#
# The derived classes should implement {#update_connection_config} method.
# methods.
class Base
# @return [Y2Network::Sysconfig::InterfaceFile] Interface's configuration file
attr_reader :file

# Constructor
#
# @param file [Y2Network::Sysconfig::InterfaceFile] Interface's configuration file
def initialize(file)
@file = file
end

# Builds a connection configuration object
#
# @return [Y2Network::ConnectionConfig::Base]
def connection_config
connection_class.new.tap do |conn|
conn.bootproto = BootProtocol.from_name(file.bootproto || "static")
conn.description = file.name
conn.interface = file.interface
conn.ip_configs = ip_configs
conn.startmode = Startmode.create(file.startmode || "manual")
conn.startmode.priority = file.ifplugd_priority if conn.startmode.name == "ifplugd"
update_connection_config(conn)
end
end

private

# Returns the class of the connection configuration
#
# @return [Class]
def connection_class
class_name = self.class.to_s.split("::").last
file_name = class_name.gsub(/(\w)([A-Z])/, "\\1_\\2").downcase
require "y2network/connection_config/#{file_name}"
Y2Network::ConnectionConfig.const_get(class_name)
end

# Sets connection config settings from the given file
#
# @note This method should be redefined by derived classes.
#
# @param _conn [Y2Network::ConnectionConfig::Base]
def update_connection_config(_conn)
raise NotImplementedError
end

# Returns the IPs configuration from the file
#
# @return [Array<Y2Network::ConnectionConfig::IPAdress>] IP addresses configuration
# @see Y2Network::ConnectionConfig::IPConfig
def ip_configs
configs = file.ipaddrs.map do |id, ip|
next unless ip.is_a?(Y2Network::IPAddress)
ip_address = build_ip(ip, file.prefixlens[id], file.netmasks[id])
Y2Network::ConnectionConfig::IPConfig.new(
ip_address,
id: id,
label: file.labels[id],
remote_address: file.remote_ipaddrs[id],
broadcast: file.broadcasts[id]
)
end
# The one without suffix comes first.
configs.sort_by { |c| c.id.nil? ? -1 : 0 }
end

# Builds an IP address
#
# It takes an IP address and, optionally, a prefix or a netmask.
#
# @param ip [Y2Network::IPAddress] IP address
# @param prefix [Integer,nil] Address prefix
# @param netmask [String,nil] Netmask
def build_ip(ip, prefix, netmask)
ipaddr = ip.clone
return ipaddr if ip.prefix?
ipaddr.netmask = netmask if netmask
ipaddr.prefix = prefix if prefix
ipaddr
end
end
end
end
end
29 changes: 5 additions & 24 deletions src/lib/y2network/sysconfig/connection_config_readers/ethernet.rb
Expand Up @@ -17,37 +17,18 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2network/connection_config/ethernet"
require "y2network/boot_protocol"
require "y2network/startmode"
require "y2network/sysconfig/connection_config_readers/base"

module Y2Network
module Sysconfig
module ConnectionConfigReaders
# This class is able to build a ConnectionConfig::Ethernet object given a
# Sysconfig::InterfaceFile object.
class Ethernet
# @return [Y2Network::Sysconfig::InterfaceFile]
attr_reader :file
class Ethernet < Base
private

# Constructor
#
# @param file [Y2Network::Sysconfig::InterfaceFile] File to get interface configuration from
def initialize(file)
@file = file
end

# @return [Y2Network::ConnectionConfig::Ethernet]
def connection_config
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
# for defauls see man ifcfg
conn.bootproto = BootProtocol.from_name(file.bootproto || "static")
conn.description = file.name
conn.interface = file.interface
conn.ip_address = file.ip_address
conn.startmode = Startmode.create(file.startmode || "manual")
conn.startmode.priority = file.ifplugd_priority if conn.startmode.name == "ifplugd"
end
# @see Y2Network::Sysconfig::ConnectionConfigReaders::Base#update_connection_config
def update_connection_config(_conn)
end
end
end
Expand Down
57 changes: 24 additions & 33 deletions src/lib/y2network/sysconfig/connection_config_readers/wireless.rb
Expand Up @@ -17,48 +17,39 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2network/connection_config/wireless"
require "y2network/boot_protocol"
require "y2network/startmode"
require "y2network/sysconfig/connection_config_readers/base"

module Y2Network
module Sysconfig
module ConnectionConfigReaders
# This class is able to build a ConnectionConfig::Wireless object given a
# Sysconfig::InterfaceFile object.
class Wireless
# @return [Y2Network::Sysconfig::InterfaceFile]
attr_reader :file
class Wireless < Base
private

def initialize(file)
@file = file
# @see Y2Network::Sysconfig::ConnectionConfigReaders::Base#update_connection_config
def update_connection_config(conn)
conn.ap = file.wireless_ap
conn.ap_scanmode = file.wireless_ap_scanmode
conn.auth_mode = file.wireless_auth_mode
conn.default_key = file.wireless_default_key
conn.eap_auth = file.wireless_eap_auth
conn.eap_mode = file.wireless_eap_mode
conn.essid = file.wireless_essid
conn.key_length = file.wireless_key_length
conn.keys = wireless_keys
conn.mode = file.wireless_mode
conn.nwid = file.wireless_nwid
conn.wpa_password = file.wireless_wpa_password
conn.wpa_psk = file.wireless_wpa_psk
end

# Returns a wireless connection configuration
#
# @return [ConnectionConfig::Wireless]
def connection_config
Y2Network::ConnectionConfig::Wireless.new.tap do |conn|
conn.ap = file.wireless_ap
conn.ap_scanmode = file.wireless_ap_scanmode
conn.auth_mode = file.wireless_auth_mode
conn.bootproto = BootProtocol.from_name(file.bootproto || "static")
conn.default_key = file.wireless_default_key
conn.description = file.name
conn.eap_auth = file.wireless_eap_auth
conn.eap_mode = file.wireless_eap_mode
conn.essid = file.wireless_essid
conn.interface = file.interface
conn.ip_address = file.ip_address
conn.key_length = file.wireless_key_length
conn.keys = file.wireless_keys
conn.mode = file.wireless_mode
conn.nwid = file.wireless_nwid
conn.startmode = Startmode.create(file.startmode || "manual")
conn.startmode.priority = file.ifplugd_priority if conn.startmode.name == "ifplugd"
conn.wpa_password = file.wireless_wpa_password
conn.wpa_psk = file.wireless_wpa_psk
end
# Max number of wireless keys
MAX_WIRELESS_KEYS = 4

# Reads the array of wireless keys from the file
def wireless_keys
(0..MAX_WIRELESS_KEYS - 1).map { |i| file.wireless_keys["_#{i}"] }
end
end
end
Expand Down

0 comments on commit a8e18b5

Please sign in to comment.