Class: OsCtld::DistConfig::Network::NetworkManager

Inherits:
Base
  • Object
show all
Defined in:
lib/osctld/dist_config/network/network_manager.rb

Overview

Configure network using NetworkManager keyfiles

Instance Attribute Summary

Attributes inherited from Base

#configurator

Instance Method Summary collapse

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



31
32
33
34
35
36
37
# File 'lib/osctld/dist_config/network/network_manager.rb', line 31

def configure(netifs)
  netifs.each do |netif|
    do_create_connection(netif)
  end

  setup_nm(netifs)
end

#do_create_connection(netif) ⇒ Object (protected)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/osctld/dist_config/network/network_manager.rb', line 54

def do_create_connection(netif)
  tpl_base = File.join('dist_config/network/network_manager')
  ct_base = File.join(rootfs, 'etc', 'NetworkManager/system-connections')
  keyfile = File.join(ct_base, "#{netif.name}.nmconnection")

  return unless writable?(keyfile)

  OsCtld::ErbTemplate.render_to_if_changed(
    File.join(tpl_base, netif.type.to_s),
    { netif: },
    keyfile,
    perm: 0o600
  )
end

#do_remove_connection(name) ⇒ Object (protected)



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/osctld/dist_config/network/network_manager.rb', line 69

def do_remove_connection(name)
  base = File.join(rootfs, 'etc', 'NetworkManager', 'system-connections')
  files = [
    "#{name}.nmconnection"
  ]

  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)



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/osctld/dist_config/network/network_manager.rb', line 88

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/network_manager/nm_conf'),
    { netifs: },
    file
  )
end

#generate_nm_udev_rules(netifs) ⇒ Object (protected)



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/osctld/dist_config/network/network_manager.rb', line 102

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/network_manager/udev_rules'),
    { netifs: },
    file
  )
end

#remove_netif(netifs, netif) ⇒ Object

Cleanup old config files



40
41
42
43
# File 'lib/osctld/dist_config/network/network_manager.rb', line 40

def remove_netif(netifs, netif)
  do_remove_connection(netif.name)
  setup_nm(netifs)
end

#rename_netif(netifs, netif, old_name) ⇒ Object

Rename config files



46
47
48
49
50
# File 'lib/osctld/dist_config/network/network_manager.rb', line 46

def rename_netif(netifs, netif, old_name)
  do_remove_connection(old_name)
  do_create_connection(netif)
  setup_nm(netifs)
end

#setup_nm(netifs) ⇒ Object (protected)



83
84
85
86
# File 'lib/osctld/dist_config/network/network_manager.rb', line 83

def setup_nm(netifs)
  generate_nm_conf(netifs)
  generate_nm_udev_rules(netifs)
end

#usable?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/osctld/dist_config/network/network_manager.rb', line 6

def usable?
  begin
    network_scripts = File.join(rootfs, 'etc/sysconfig/network-scripts')

    Dir.entries(network_scripts).each do |entry|
      return false if entry.start_with?('ifcfg-')
    end
  rescue Errno::ENOENT, Errno::ENOTDIR
    # pass
  end

  return false unless Dir.exist?(File.join(rootfs, 'etc/NetworkManager/conf.d'))
  return false unless Dir.exist?(File.join(rootfs, 'etc/NetworkManager/system-connections'))

  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