Class: OsCtld::DistConfig::Configurator

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::File, OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Helpers::Common
Defined in:
lib/osctld/dist_config/configurator.rb

Overview

Base class for per-distribution configurators

Configurators are used to manipulate the container’s root filesystem. It is called from a forked process with a container-specific mount namespace, but retaining access to all osctld memory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Common

#systemd_service_enabled?, #systemd_service_masked?, #writable?

Constructor Details

#initialize(ctid, rootfs, distribution, version) ⇒ Configurator

Returns a new instance of Configurator.

Parameters:

  • ctid (String)
  • rootfs (String)
  • distribution (String)
  • version (String)


32
33
34
35
36
37
38
# File 'lib/osctld/dist_config/configurator.rb', line 32

def initialize(ctid, rootfs, distribution, version)
  @ctid = ctid
  @rootfs = rootfs
  @distribution = distribution
  @version = version
  @network_backend = instantiate_network_class
end

Instance Attribute Details

#ctidString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/osctld/dist_config/configurator.rb', line 17

def ctid
  @ctid
end

#distributionString (readonly)

Returns:

  • (String)


23
24
25
# File 'lib/osctld/dist_config/configurator.rb', line 23

def distribution
  @distribution
end

#network_backendDistConfig::Network::Base? (readonly, protected)

Returns:



116
117
118
# File 'lib/osctld/dist_config/configurator.rb', line 116

def network_backend
  @network_backend
end

#rootfsString (readonly)

Returns:

  • (String)


20
21
22
# File 'lib/osctld/dist_config/configurator.rb', line 20

def rootfs
  @rootfs
end

#versionString (readonly)

Returns:

  • (String)


26
27
28
# File 'lib/osctld/dist_config/configurator.rb', line 26

def version
  @version
end

Instance Method Details

#add_netif(netifs, netif) ⇒ Object

Called when a new network interface is added to a container

Parameters:



78
79
80
# File 'lib/osctld/dist_config/configurator.rb', line 78

def add_netif(netifs, netif)
  network_backend && network_backend.add_netif(netifs, netif)
end

#dns_resolvers(resolvers) ⇒ Object

Configure DNS resolvers

Parameters:

  • resolvers (Array<String>)


99
100
101
102
103
104
105
106
107
# File 'lib/osctld/dist_config/configurator.rb', line 99

def dns_resolvers(resolvers)
  writable?(File.join(rootfs, 'etc', 'resolv.conf')) do |path|
    File.open("#{path}.new", 'w') do |f|
      resolvers.each { |v| f.puts("nameserver #{v}") }
    end

    File.rename("#{path}.new", path)
  end
end

#instantiate_network_classDistConfig::Network::Base? (protected)

Returns:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/osctld/dist_config/configurator.rb', line 135

def instantiate_network_class
  klass = network_class

  if klass.nil?
    log(:debug, 'Using distribution-specific network configuration')
    nil

  elsif klass.is_a?(Array)
    klass.each do |k|
      inst = k.new(self)

      if inst.usable?
        log(:debug, "Using #{k} for network configuration")
        return inst
      end
    end

    log(:warn, "No network class usable for #{self.class}")
    nil

  else
    log(:debug, "Using #{network_class} for network configuration")
    network_class.new(self)
  end
end

#log_typeObject



109
110
111
# File 'lib/osctld/dist_config/configurator.rb', line 109

def log_type
  ctid
end

#network(netifs) ⇒ Object

Configure networking

Parameters:



71
72
73
# File 'lib/osctld/dist_config/configurator.rb', line 71

def network(netifs)
  network_backend && network_backend.configure(netifs)
end

#network_classClass, ... (protected)

Return a class which is used for network configuration

The class should be a subclass of Network::Base.

If an array of classes is returned, they are instantiated and the first class for which Network::Base#usable? returns true is used. An exception is raised if no class is found to be usable.

If ‘nil` is returned, you are expected to implement #network and other methods for network configuration yourself.

Returns:

  • (Class, Array<Class>, nil)

Raises:

  • (NotImplementedError)


130
131
132
# File 'lib/osctld/dist_config/configurator.rb', line 130

def network_class
  raise NotImplementedError
end

#remove_netif(netifs, netif) ⇒ Object

Called when a network interface is removed from a container

Parameters:



85
86
87
# File 'lib/osctld/dist_config/configurator.rb', line 85

def remove_netif(netifs, netif)
  network_backend && network_backend.remove_netif(netifs, netif)
end

#rename_netif(netifs, netif, old_name) ⇒ Object

Called when an existing network interface is renamed

Parameters:



93
94
95
# File 'lib/osctld/dist_config/configurator.rb', line 93

def rename_netif(netifs, netif, old_name)
  network_backend && network_backend.rename_netif(netifs, netif, old_name)
end

#set_hostname(new_hostname, old_hostname: nil) ⇒ Object

Parameters:

  • new_hostname (OsCtl::Lib::Hostname)
  • old_hostname (OsCtl::Lib::Hostname, nil) (defaults to: nil)

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/osctld/dist_config/configurator.rb', line 42

def set_hostname(new_hostname, old_hostname: nil)
  raise NotImplementedError
end

#unset_etc_hostsObject



61
62
63
64
65
66
67
# File 'lib/osctld/dist_config/configurator.rb', line 61

def unset_etc_hosts
  path = File.join(rootfs, 'etc', 'hosts')
  return unless writable?(path)

  hosts = EtcHosts.new(path)
  hosts.unmanage
end

#update_etc_hosts(new_hostname, old_hostname: nil) ⇒ Object

Parameters:

  • new_hostname (OsCtl::Lib::Hostname)
  • old_hostname (OsCtl::Lib::Hostname, nil) (defaults to: nil)


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/osctld/dist_config/configurator.rb', line 48

def update_etc_hosts(new_hostname, old_hostname: nil)
  path = File.join(rootfs, 'etc', 'hosts')
  return unless writable?(path)

  hosts = EtcHosts.new(path)

  if old_hostname
    hosts.replace(old_hostname, new_hostname)
  else
    hosts.set(new_hostname)
  end
end