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
Constant Summary collapse
- NETWORK_MANAGER_DNS_LEGACY_CONFIG =
"[main]\ndns=none\n".freeze
- NETWORK_MANAGER_DNS_CONFIG =
<<~CONFIG.freeze # Generated and managed by osctld. Do not edit. [main] dns=none CONFIG
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.
- #network_manager_dns_config_state(path) ⇒ Object protected
- #network_manager_dns_default ⇒ Object protected
- #network_manager_dns_none ⇒ Object protected
-
#prepare_dns_resolvers ⇒ Boolean
Whether NetworkManager needs a live reload.
-
#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_dns_resolvers ⇒ Object
- #unset_etc_hosts ⇒ Object
- #update_etc_hosts(new_hostname, old_hostname: nil) ⇒ Object
- #write_dns_resolvers(resolvers) ⇒ 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)
178 179 180 |
# File 'lib/osctld/dist_config/configurator.rb', line 178 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 |
# File 'lib/osctld/dist_config/configurator.rb', line 99 def dns_resolvers(resolvers) prepare_dns_resolvers write_dns_resolvers(resolvers) end |
#instantiate_network_class ⇒ DistConfig::Network::Base? (protected)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/osctld/dist_config/configurator.rb', line 197 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
124 125 126 |
# File 'lib/osctld/dist_config/configurator.rb', line 124 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.
192 193 194 |
# File 'lib/osctld/dist_config/configurator.rb', line 192 def network_class raise NotImplementedError end |
#network_manager_dns_config_state(path) ⇒ Object (protected)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/osctld/dist_config/configurator.rb', line 161 def network_manager_dns_config_state(path) stat = File.lstat(path) return :custom unless stat.file? content = File.binread(path) return :current if content == NETWORK_MANAGER_DNS_CONFIG return :legacy if content == NETWORK_MANAGER_DNS_LEGACY_CONFIG :custom rescue Errno::ENOENT :absent rescue SystemCallError => e log(:warn, "Unable to inspect #{path}: #{e.}") :custom end |
#network_manager_dns_default ⇒ Object (protected)
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/osctld/dist_config/configurator.rb', line 149 def network_manager_dns_default path = File.join(rootfs, 'etc', 'NetworkManager', 'conf.d', '10-osctl-dns.conf') return false unless Dir.exist?(File.dirname(path)) state = network_manager_dns_config_state(path) return true if state == :absent return false if state == :custom || !writable?(path) File.unlink(path) true end |
#network_manager_dns_none ⇒ Object (protected)
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/osctld/dist_config/configurator.rb', line 137 def network_manager_dns_none path = File.join(rootfs, 'etc', 'NetworkManager', 'conf.d', '10-osctl-dns.conf') return false unless Dir.exist?(File.dirname(path)) state = network_manager_dns_config_state(path) return true if state == :current return false if state == :custom || !writable?(path) File.write(path, NETWORK_MANAGER_DNS_CONFIG) true end |
#prepare_dns_resolvers ⇒ Boolean
Returns whether NetworkManager needs a live reload.
105 106 107 |
# File 'lib/osctld/dist_config/configurator.rb', line 105 def prepare_dns_resolvers network_manager_dns_none 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_dns_resolvers ⇒ Object
120 121 122 |
# File 'lib/osctld/dist_config/configurator.rb', line 120 def unset_dns_resolvers network_manager_dns_default 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 |
#write_dns_resolvers(resolvers) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/osctld/dist_config/configurator.rb', line 109 def write_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 |