Class: Y2Network::Sysconfig::RoutesFile

Inherits:
Object
  • Object
show all
Defined in:
src/lib/y2network/sysconfig/routes_file.rb

Overview

This class represents a file containing a set of routes

Examples:

Reading the default location, i.e., /etc/sysconfig/network/routes

file = Y2Network::Sysconfig::RoutesFile.new
file.load
file.routes.size #=> 2

Reading routes for the interface eth1

file = Y2Network::Sysconfig::RoutesFile.new("/etc/sysconfig/network/ifroute-eth1")
file.load
file.routes.size #=> 1

Constant Summary collapse

DEFAULT_ROUTES_FILE =
"/etc/sysconfig/network/routes".freeze
SYSCONFIG_NETWORK_DIR =
"/etc/sysconfig/network".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = DEFAULT_ROUTES_FILE) ⇒ RoutesFile


59
60
61
62
# File 'src/lib/y2network/sysconfig/routes_file.rb', line 59

def initialize(file_path = DEFAULT_ROUTES_FILE)
  @register_agent = file_path != DEFAULT_ROUTES_FILE
  @file_path = file_path
end

Instance Attribute Details

#file_pathString (readonly)


48
49
50
# File 'src/lib/y2network/sysconfig/routes_file.rb', line 48

def file_path
  @file_path
end

#routesArray<Route>


45
46
47
# File 'src/lib/y2network/sysconfig/routes_file.rb', line 45

def routes
  @routes
end

Class Method Details

.find(interface) ⇒ Object


52
53
54
55
# File 'src/lib/y2network/sysconfig/routes_file.rb', line 52

def find(interface)
  file_path = File.join(SYSCONFIG_NETWORK_DIR, "ifroute-#{interface}")
  new(file_path)
end

Instance Method Details

#loadArray<Hash<String, String>>

Loads routes from system


69
70
71
72
73
# File 'src/lib/y2network/sysconfig/routes_file.rb', line 69

def load
  entries = with_registered_ifroute_agent(file_path) { |a| Yast::SCR.Read(a) }
  entries = entries ? normalize_entries(entries.uniq) : []
  @routes = entries.map { |r| serializer.from_hash(r) }
end

#removeObject

Removes the file


92
93
94
95
# File 'src/lib/y2network/sysconfig/routes_file.rb', line 92

def remove
  return unless Yast::FileUtils.Exists(file_path)
  Yast::SCR.Execute(Yast::Path.new(".target.remove"), file_path)
end

#saveBoolean

Writes configured routes


78
79
80
81
82
83
84
85
86
87
88
89
# File 'src/lib/y2network/sysconfig/routes_file.rb', line 78

def save
  # create if not exists, otherwise backup
  if Yast::FileUtils.Exists(file_path)
    Yast::Execute.on_target("/bin/cp", file_path, file_path + ".YaST2save")
  end

  with_registered_ifroute_agent(file_path) do |scr|
    # work around bnc#19476
    Yast::SCR.Write(Yast::Path.new(".target.string"), file_path, "")
    Yast::SCR.Write(scr, routes.map { |r| serializer.to_hash(r) })
  end
end