Class: OsCtld::DistConfig::RedHat

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

Direct Known Subclasses

CentOS, Fedora

Instance Attribute Summary

Attributes inherited from Base

#ct, #distribution, #version

Instance Method Summary collapse

Methods inherited from Base

#add_netif, #bin_path, distribution, #dns_resolvers, #initialize, #passwd, #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

#do_create_netif(netif) ⇒ Object (protected)



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
78
79
# File 'lib/osctld/dist_config/redhat.rb', line 53

def do_create_netif(netif)
  tpl_base = File.join('dist_config/network', template_dir)
  ct_base = File.join(ct.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)



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/osctld/dist_config/redhat.rb', line 81

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

#network(_opts) ⇒ Object



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

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

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

#remove_netif(opts) ⇒ Object

Cleanup old config files



38
39
40
# File 'lib/osctld/dist_config/redhat.rb', line 38

def remove_netif(opts)
  do_remove_netif(opts[:netif].name)
end

#rename_netif(opts) ⇒ Object

Rename config files



43
44
45
46
# File 'lib/osctld/dist_config/redhat.rb', line 43

def rename_netif(opts)
  do_remove_netif(opts[:original_name])
  do_create_netif(opts[:netif])
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(ct.rootfs, 'etc', 'sysconfig', 'network'),
    {'HOSTNAME' => ct.hostname.local}
  )

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

  # Apply hostname if the container is running
  if ct.running?
    begin
      ct_syscmd(ct, "hostname #{ct.hostname}")

    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)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/osctld/dist_config/redhat.rb', line 99

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

#template_dirObject (protected)

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/osctld/dist_config/redhat.rb', line 49

def template_dir
  raise NotImplementedError
end