Class: CFA::RoutesFile

Inherits:
Object
  • Object
show all
Defined in:
src/lib/cfa/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 = CFA::RoutesFile.new
file.load
file.routes.size #=> 2

Reading routes for the interface eth1

file = CFA::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

Returns a new instance of RoutesFile.

Parameters:

  • file_path (String) (defaults to: DEFAULT_ROUTES_FILE)

    File path



58
59
60
61
# File 'src/lib/cfa/routes_file.rb', line 58

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)

Returns File path.

Returns:

  • (String)

    File path



47
48
49
# File 'src/lib/cfa/routes_file.rb', line 47

def file_path
  @file_path
end

#routesArray<Route>

Returns Routes.

Returns:

  • (Array<Route>)

    Routes



44
45
46
# File 'src/lib/cfa/routes_file.rb', line 44

def routes
  @routes
end

Class Method Details

.find(interface) ⇒ Object

Parameters:

  • interface (String)

    Interface's name



51
52
53
54
# File 'src/lib/cfa/routes_file.rb', line 51

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

Returns:

  • (Array<Hash<String, String>>)

    list of hashes representing routes as provided by SCR agent. keys: destination, gateway, netmask, [device, [extrapara]]



68
69
70
71
72
# File 'src/lib/cfa/routes_file.rb', line 68

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



91
92
93
94
95
# File 'src/lib/cfa/routes_file.rb', line 91

def remove
  return unless Yast::FileUtils.Exists(file_path)

  Yast::SCR.Execute(Yast::Path.new(".target.remove"), file_path)
end

#saveBoolean

Writes configured routes

Returns:

  • (Boolean)

    true on success



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

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