Class: OsCtld::DistConfig::RedHat

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

Direct Known Subclasses

AlmaLinux, CentOS, Fedora, Rocky

Instance Attribute Summary

Attributes inherited from Base

#ct, #ctrc, #distribution, #version

Instance Method Summary collapse

Methods inherited from Base

#add_netif, #bin_path, distribution, #dns_resolvers, #initialize, #passwd, #stop, #unset_etc_hosts, #update_etc_hosts, #writable?

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Constructor Details

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

Instance Method Details

#config_backend:network_manager, :initscripts (protected)

Returns:

  • (:network_manager, :initscripts)

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/osctld/dist_config/redhat.rb', line 54

def config_backend
  raise NotImplementedError
end

#do_create_netif(netif) ⇒ Object (protected)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/osctld/dist_config/redhat.rb', line 70

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

  return unless writable?(ifcfg)

  OsCtld::ErbTemplate.render_to(
    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(
        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)



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

def do_remove_netif(name)
  base = File.join(ctrc.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_confObject (protected)



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/osctld/dist_config/redhat.rb', line 119

def generate_nm_conf
  conf_d = File.join(ctrc.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(
    File.join('dist_config/network/redhat_nm/nm_conf'),
    {netifs: ct.netifs},
    file,
  )
end

#generate_nm_udev_rulesObject (protected)



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/osctld/dist_config/redhat.rb', line 133

def generate_nm_udev_rules
  rules_d = File.join(ctrc.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(
    File.join('dist_config/network/redhat_nm/udev_rules'),
    {netifs: ct.netifs},
    file,
  )
end

#network(_opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/osctld/dist_config/redhat.rb', line 26

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

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

  setup_for_nm if use_nm?
end

#remove_netif(opts) ⇒ Object

Cleanup old config files



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

def remove_netif(opts)
  do_remove_netif(opts[:netif].name)
  setup_for_nm if use_nm?
end

#rename_netif(opts) ⇒ Object

Rename config files



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

def rename_netif(opts)
  do_remove_netif(opts[:original_name])
  do_create_netif(opts[:netif])
  setup_for_nm if use_nm?
end

#set_hostname(opts) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/osctld/dist_config/redhat.rb', line 5

def set_hostname(opts)
  # /etc/sysconfig/network
  set_params(
    File.join(ctrc.rootfs, 'etc', 'sysconfig', 'network'),
    {'HOSTNAME' => ct.hostname.local}
  )

  # Entry in /etc/hosts
  update_etc_hosts(old_hostname: opts[:original])

  # Apply hostname if the container is running
  if ct.running?
    begin
      ct_syscmd(ct, ['hostname', ct.hostname.fqdn])

    rescue SystemCommandFailed => e
      log(:warn, ct, "Unable to apply hostname: #{e.message}")
    end
  end
end

#set_params(file, params) ⇒ Object (protected)

Parameters:

  • file (String)
  • params (Hash)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/osctld/dist_config/redhat.rb', line 149

def set_params(file, params)
  return unless writable?(file)

  regenerate_file(file, 0644) do |new, old|
    if old
      # Overwrite existing params and keep unchanged ones
      old.each_line do |line|
        param, value = params.detect { |k, v| /^#{k}=/ =~ line }

        if param
          new.puts("#{param}=\"#{value}\"")
          params.delete(param)

        else
          new.write(line)
        end
      end

      # Write new params
      params.each do |k, v|
        new.puts("#{k}=\"#{v}\"")
      end

    else
      # File did not exist, write all params
      params.each do |k, v|
        new.puts("#{k}=\"#{v}\"")
      end
    end
  end
end

#setup_for_nmObject (protected)



114
115
116
117
# File 'lib/osctld/dist_config/redhat.rb', line 114

def setup_for_nm
  generate_nm_conf
  generate_nm_udev_rules
end

#template_dirObject (protected)



66
67
68
# File 'lib/osctld/dist_config/redhat.rb', line 66

def template_dir
  use_nm? ? 'redhat_nm' : 'redhat_initscripts'
end

#use_initscripts?Boolean (protected)

Returns:

  • (Boolean)


62
63
64
# File 'lib/osctld/dist_config/redhat.rb', line 62

def use_initscripts?
  config_backend == :initscripts
end

#use_nm?Boolean (protected)

Returns:

  • (Boolean)


58
59
60
# File 'lib/osctld/dist_config/redhat.rb', line 58

def use_nm?
  config_backend == :network_manager
end