Skip to content

Commit

Permalink
Merge pull request #751 from yast/network-ng-autoinst-profile
Browse files Browse the repository at this point in the history
Network ng autoinst profile
  • Loading branch information
imobachgs committed Mar 29, 2019
2 parents 897214d + e989fc3 commit da81156
Show file tree
Hide file tree
Showing 7 changed files with 819 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/lib/y2network/autoinst_profile/networking_section.rb
@@ -0,0 +1,66 @@
# 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/autoinst_profile/routing_section"

module Y2Network
module AutoinstProfile
# This class represents an AutoYaST <networking> section
#
# <networking>
# <routing>
# <!-- the routing configuration -->
# </routing>
# </networking>
#
# @see RoutingSection
class NetworkingSection
# @return [RoutingSection]
attr_accessor :routing

# Creates an instance based on the profile representation used by the AutoYaST modules
# (hash with nested hashes and arrays).
#
# @param hash [Hash] Networking section from an AutoYaST profile
# @return [NetworkingSection]
def self.new_from_hashes(hash)
result = new
result.routing = RoutingSection.new_from_hashes(hash["routing"]) if hash["routing"]
result
end

# Creates an instance based on the network configuration representation
#
# @param config [Y2Network::Config]
# @return [NetworkingSection]
def self.new_from_network(config)
result = new
result.routing = RoutingSection.new_from_network(config.routing)
result
end

# Export the section to a hash so it might be used when cloning the system
#
# @return [Hash]
def to_hashes
{ "routing" => routing.to_hashes }
end
end
end
end
136 changes: 136 additions & 0 deletions src/lib/y2network/autoinst_profile/route_section.rb
@@ -0,0 +1,136 @@
# 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/autoinst_profile/section_with_attributes"

module Y2Network
module AutoinstProfile
# This class represents an AutoYaST <route> section under <routing>
#
# <route>
# <destination>192.168.1.0</destination>
# <device>eth0</device>
# <extrapara>foo</extrapara>
# <gateway>-</gateway>
# <netmask>-</netmask>
# </route>
#
# @see RoutingSection
class RouteSection < SectionWithAttributes
def self.attributes
[
{ name: :destination },
{ name: :netmask },
{ name: :device },
{ name: :gateway },
{ name: :extrapara }
]
end

define_attr_accessors

# @!attribute destination
# @return [String] Route destination

# @!attribute device
# @return [String] Interface name

# @!attribute extrapara
# @return [String] Route options

# @!attribute gateway
# @return [String] Route gateway

# @!attribute netmask
# @return [String] Netmask

# Clones a network route into an AutoYaST route section
#
# @param route [Y2Network::Route] Network route
# @return [RouteSection]
def self.new_from_network(route)
result = new
result.init_from_route(route)
result
end

# Method used by {.new_from_network} to populate the attributes when cloning a network route
#
# @param route [Y2Network::Route] Network route
# @return [Boolean]
def init_from_route(route)
@destination = destination_from_route(route)
@netmask = netmask_from_route(route)
@device = device_from_route(route)
@gateway = gateway_from_route(route)
@extrapara = extrapara_from_route(route)
true
end

private

# Returns the destination for the given route
#
# @param route [Route] Route to get the destination from
# @return [String] Route destination
def destination_from_route(route)
route.default? ? "default" : route.to.to_s
end

IPV4_MASK = "255.255.255.255".freeze
IPV6_MASK = "fffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff".freeze

# Returns the netmask
#
# @param route [Route] Route to get the destination from
# @return [String] Route netmask
def netmask_from_route(route)
return "-" if route.default?
mask = route.to.ipv4? ? IPV4_MASK : IPV6_MASK
IPAddr.new(mask).mask(route.to.prefix).to_s
end

# Returns the device (interface) for the given route
#
# @param route [Route] Route to get the device from
# @return [String] Device name
def device_from_route(route)
return "-" unless route.interface.respond_to?(:name)
route.interface.name
end

# Returns the gateway for the given route
#
# @param route [Route] Route to get the gateway from
# @return [String] Gateway address
def gateway_from_route(route)
return "-" unless route.gateway
route.gateway.to_s
end

# Returns the extra parameters for the given route
#
# @param route [Route] Route to get the options from
# @return [String] Extra parameters
def extrapara_from_route(route)
route.options.to_s
end
end
end
end
112 changes: 112 additions & 0 deletions src/lib/y2network/autoinst_profile/routing_section.rb
@@ -0,0 +1,112 @@
# 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/autoinst_profile/section_with_attributes"
require "y2network/autoinst_profile/route_section"

module Y2Network
module AutoinstProfile
# This class represents an AutoYaST <routing> section under <networking>
#
# <routing>
# <ipv4_forward config:type="boolean">false</ipv4_forward>
# <ipv6_forward config:type="boolean">false</ipv6_forward>
# <routes config:type="list">
# <route> <!-- see RouteSection class -->
# <destination>192.168.1.0</destination>
# <device>eth0</device>
# <extrapara>foo</extrapara>
# <gateway>-</gateway>
# <netmask>-</netmask>
# </route>
# </routes>
# </routing>
#
# @see NetworkingSection
class RoutingSection < SectionWithAttributes
def self.attributes
[
{ name: :ipv4_forward },
{ name: :ipv6_forward },
{ name: :routes }
]
end

define_attr_accessors

# @!attribute ipv4_forward
# @return [Boolean]

# @!attribute ipv6_forward
# @return [Boolean]

# @!attribute routes
# @return [Array<RouteSection>]

# Clones network routing settings into an AutoYaST routing section
#
# @param routing [Y2Network::Routing] Routing settings
# @return [RoutingSection]
def self.new_from_network(routing)
result = new
initialized = result.init_from_network(routing)
initialized ? result : nil
end

# Constructor
def initialize(*_args)
super
@routes = []
end

# Method used by {.new_from_hashes} to populate the attributes when importing a profile
#
# @param hash [Hash] see {.new_from_hashes}
def init_from_hashes(hash)
super
@routes = routes_from_hash(hash)
end

# Method used by {.new_from_network} to populate the attributes when cloning routing settings
#
# @param routing [Y2Network::Routing] Network settings
# @return [Boolean] Result true on success or false otherwise
def init_from_network(routing)
@ipv4_forward = routing.forward_ipv4
@ipv6_forward = routing.forward_ipv6
@routes = routes_section(routing.routes)
true
end

private

# Returns an array of routing sections
#
# @param hash [Hash] Routing section hash
def routes_from_hash(hash)
hashes = hash["routes"] || []
hashes.map { |h| RouteSection.new_from_hashes(h) }
end

def routes_section(routes)
routes.map { |r| Y2Network::AutoinstProfile::RouteSection.new_from_network(r) }
end
end
end
end

0 comments on commit da81156

Please sign in to comment.