Class: OsCtld::DistConfig::Configurator
- Inherits:
-
Object
- Object
- OsCtld::DistConfig::Configurator
- 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.
Direct Known Subclasses
Distributions::Arch::Configurator, Distributions::Debian::Configurator, Distributions::Gentoo::Configurator, Distributions::Guix::Configurator, Distributions::NixOS::Configurator, Distributions::OpenSuse::Configurator, Distributions::RedHat::Configurator, Distributions::Slackware::Configurator, Distributions::Void::Configurator
Instance Attribute Summary collapse
- #ctid ⇒ String readonly
- #distribution ⇒ String readonly
- #network_backend ⇒ DistConfig::Network::Base? readonly protected
- #rootfs ⇒ String readonly
- #version ⇒ String readonly
Instance Method Summary collapse
-
#add_netif(netifs, netif) ⇒ Object
Called when a new network interface is added to a container.
-
#dns_resolvers(resolvers) ⇒ Object
Configure DNS resolvers.
-
#initialize(ctid, rootfs, distribution, version) ⇒ Configurator
constructor
A new instance of Configurator.
- #instantiate_network_class ⇒ DistConfig::Network::Base? protected
- #log_type ⇒ Object
-
#network(netifs) ⇒ Object
Configure networking.
-
#network_class ⇒ Class, ...
protected
Return a class which is used for network configuration.
-
#remove_netif(netifs, netif) ⇒ Object
Called when a network interface is removed from a container.
-
#rename_netif(netifs, netif, old_name) ⇒ Object
Called when an existing network interface is renamed.
- #set_hostname(new_hostname, old_hostname: nil) ⇒ Object
- #unset_etc_hosts ⇒ Object
- #update_etc_hosts(new_hostname, old_hostname: nil) ⇒ Object
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.
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
#ctid ⇒ String (readonly)
17 18 19 |
# File 'lib/osctld/dist_config/configurator.rb', line 17 def ctid @ctid end |
#distribution ⇒ String (readonly)
23 24 25 |
# File 'lib/osctld/dist_config/configurator.rb', line 23 def distribution @distribution end |
#network_backend ⇒ DistConfig::Network::Base? (readonly, protected)
117 118 119 |
# File 'lib/osctld/dist_config/configurator.rb', line 117 def network_backend @network_backend end |
#rootfs ⇒ String (readonly)
20 21 22 |
# File 'lib/osctld/dist_config/configurator.rb', line 20 def rootfs @rootfs end |
#version ⇒ String (readonly)
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
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
99 100 101 102 103 104 105 106 107 108 |
# 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}") } f.puts('options edns0') end File.rename("#{path}.new", path) end end |
#instantiate_network_class ⇒ DistConfig::Network::Base? (protected)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/osctld/dist_config/configurator.rb', line 136 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_type ⇒ Object
110 111 112 |
# File 'lib/osctld/dist_config/configurator.rb', line 110 def log_type ctid end |
#network(netifs) ⇒ Object
Configure networking
71 72 73 |
# File 'lib/osctld/dist_config/configurator.rb', line 71 def network(netifs) network_backend && network_backend.configure(netifs) end |
#network_class ⇒ Class, ... (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.
131 132 133 |
# File 'lib/osctld/dist_config/configurator.rb', line 131 def network_class raise NotImplementedError end |
#remove_netif(netifs, netif) ⇒ Object
Called when a network interface is removed from a container
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
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
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_hosts ⇒ Object
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
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 |