Class: OsCtld::DistConfig::Network::RedHatNetworkManager

Inherits:
Base
  • Object
show all
Includes:
Helpers::RedHat
Defined in:
lib/osctld/dist_config/network/redhat_network_manager.rb

Overview

Configure network using RH style sysconfig with NetworkManager

Instance Attribute Summary

Attributes inherited from Base

#configurator

Instance Method Summary collapse

Methods included from Helpers::RedHat

#set_params

Methods inherited from Base

#add_netif, #initialize

Methods included from Helpers::Common

#systemd_service_enabled?, #systemd_service_masked?, #writable?

Constructor Details

This class inherits a constructor from OsCtld::DistConfig::Network::Base

Instance Method Details

#configure(netifs) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/osctld/dist_config/network/redhat_network_manager.rb', line 24

def configure(netifs)
  set_params(
    File.join(rootfs, 'etc/sysconfig/network'),
    {'NETWORKING' => 'yes'}
  )

  netifs.each do |netif|
    do_create_netif(netif)
  end

  setup_for_nm(netifs)
end

#do_create_netif(netif) ⇒ Object (protected)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/osctld/dist_config/network/redhat_network_manager.rb', line 51

def do_create_netif(netif)
  tpl_base = File.join('dist_config/network/redhat_network_manager')
  ct_base = File.join(rootfs, 'etc', 'sysconfig')
  ifcfg = File.join(ct_base, 'network-scripts', "ifcfg-#{netif.name}")

  return unless writable?(ifcfg)

  OsCtld::ErbTemplate.render_to_if_changed(
    File.join(tpl_base, netif.type.to_s, 'ifcfg'),
    {netif: netif},
    ifcfg
  )

  if netif.type == :routed
    netif.active_ip_versions.each do |ip_v|
      OsCtld::ErbTemplate.render_to_if_changed(
        File.join(tpl_base, netif.type.to_s, "route_v#{ip_v}"),
        {netif: netif},
        File.join(
          ct_base,
          'network-scripts',
          "route#{ip_v == 6 ? '6' : ''}-#{netif.name}"
        )
      )
    end
  end
end

#do_remove_netif(name) ⇒ Object (protected)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osctld/dist_config/network/redhat_network_manager.rb', line 79

def do_remove_netif(name)
  base = File.join(rootfs, 'etc', 'sysconfig', 'network-scripts')
  files = [
    "ifcfg-#{name}",
    "route-#{name}",
    "route6-#{name}"
  ]

  files.each do |f|
    path = File.join(base, f)
    next if !File.exist?(path) || !writable?(path)

    File.unlink(path)
  end
end

#generate_nm_conf(netifs) ⇒ Object (protected)



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/osctld/dist_config/network/redhat_network_manager.rb', line 100

def generate_nm_conf(netifs)
  conf_d = File.join(rootfs, 'etc', 'NetworkManager', 'conf.d')
  return unless Dir.exist?(conf_d)

  file = File.join(conf_d, 'osctl.conf')
  return unless writable?(file)

  OsCtld::ErbTemplate.render_to_if_changed(
    File.join('dist_config/network/redhat_network_manager/nm_conf'),
    {netifs: netifs},
    file,
  )
end

#generate_nm_udev_rules(netifs) ⇒ Object (protected)



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/osctld/dist_config/network/redhat_network_manager.rb', line 114

def generate_nm_udev_rules(netifs)
  rules_d = File.join(rootfs, 'etc', 'udev', 'rules.d')
  return unless Dir.exist?(rules_d)

  file = File.join(rules_d, '86-osctl.rules')
  return unless writable?(file)

  OsCtld::ErbTemplate.render_to_if_changed(
    File.join('dist_config/network/redhat_network_manager/udev_rules'),
    {netifs: netifs},
    file,
  )
end

#remove_netif(netifs, netif) ⇒ Object

Cleanup old config files



38
39
40
41
# File 'lib/osctld/dist_config/network/redhat_network_manager.rb', line 38

def remove_netif(netifs, netif)
  do_remove_netif(netif.name)
  setup_for_nm(netifs)
end

#rename_netif(netifs, netif, old_name) ⇒ Object

Rename config files



44
45
46
47
48
# File 'lib/osctld/dist_config/network/redhat_network_manager.rb', line 44

def rename_netif(netifs, netif, old_name)
  do_remove_netif(old_name)
  do_create_netif(netif)
  setup_for_nm(netifs)
end

#setup_for_nm(netifs) ⇒ Object (protected)



95
96
97
98
# File 'lib/osctld/dist_config/network/redhat_network_manager.rb', line 95

def setup_for_nm(netifs)
  generate_nm_conf(netifs)
  generate_nm_udev_rules(netifs)
end

#usable?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/osctld/dist_config/network/redhat_network_manager.rb', line 9

def usable?
  return false unless Dir.exist?(File.join(rootfs, 'etc/sysconfig/network-scripts'))
  return false unless Dir.exist?(File.join(rootfs, 'etc/NetworkManager/conf.d'))

  service = 'NetworkManager.service'

  # Check the service is not masked
  return false if systemd_service_masked?(service)

  # Check the service is enabled
  return false unless systemd_service_enabled?(service, 'multi-user.target')

  true
end