Class: OsCtld::DistConfig::Base

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

Direct Known Subclasses

Arch, Debian, Gentoo, NixOS, OpenSuse, RedHat, Slackware, Unsupported, Void

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Constructor Details

#initialize(ct) ⇒ Base

Returns a new instance of Base



22
23
24
25
26
# File 'lib/osctld/dist_config/base.rb', line 22

def initialize(ct)
  @ct = ct
  @distribution = ct.distribution
  @version = ct.version
end

Instance Attribute Details

#ctObject (readonly)

Returns the value of attribute ct



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

def ct
  @ct
end

#distributionObject (readonly)

Returns the value of attribute distribution



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

def distribution
  @distribution
end

#versionObject (readonly)

Returns the value of attribute version



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

def version
  @version
end

Class Method Details

.distribution(n = nil) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/osctld/dist_config/base.rb', line 11

def self.distribution(n = nil)
  if n
    DistConfig.register(n, self)

  else
    n
  end
end

Instance Method Details

#add_netif(opts) ⇒ Object

Called when a new network interface is added to a container

Parameters:

  • opts (Hash)

Options Hash (opts):



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

def add_netif(opts)

end

#bin_path(_opts) ⇒ String

Return path to `/bin` or an alternative, where a shell is looked up

Returns:

  • (String)


88
89
90
# File 'lib/osctld/dist_config/base.rb', line 88

def bin_path(_opts)
  '/bin'
end

#dns_resolvers(_opts) ⇒ Object



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

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

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

#network(_opts) ⇒ Object

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/osctld/dist_config/base.rb', line 34

def network(_opts)
  raise NotImplementedError
end

#passwd(opts) ⇒ Object

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

  • user (String)
  • password (String)


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/osctld/dist_config/base.rb', line 73

def passwd(opts)
  ret = ct_syscmd(
    ct,
    'chpasswd',
    stdin: "#{opts[:user]}:#{opts[:password]}\n",
    run: true,
    valid_rcs: :all
  )

  return true if ret.success?
  log(:warn, ct, "Unable to set password: #{ret.output}")
end

#remove_netif(opts) ⇒ Object

Called when a network interface is removed from a container

Parameters:

  • opts (Hash)

Options Hash (opts):



48
49
50
# File 'lib/osctld/dist_config/base.rb', line 48

def remove_netif(opts)

end

#rename_netif(opts) ⇒ Object

Called when an existing network interface is renamed

Parameters:

  • opts (Hash)

Options Hash (opts):



56
57
58
# File 'lib/osctld/dist_config/base.rb', line 56

def rename_netif(opts)

end

#set_hostname(opts) ⇒ Object

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

  • :original (OsCtl::Lib::Hostname)

    previous hostname

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/osctld/dist_config/base.rb', line 30

def set_hostname(opts)
  raise NotImplementedError
end

#update_etc_hosts(old_hostname = nil) ⇒ Object (protected)

Update hostname in /etc/hosts, optionally removing configuration of old hostname.

Parameters:

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


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

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

  hosts = EtcHosts.new(path)

  if old_hostname
    hosts.replace(old_hostname, ct.hostname)
  else
    hosts.set(ct.hostname)
  end
end

#writable?(path) {|path| ... } ⇒ Boolean (protected)

Check if the file at `path` si writable by its user

If the file doesn't exist, we take it as writable. If a block is given, it is called if `path` is writable.

Yield Parameters:

  • path (String)

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
# File 'lib/osctld/dist_config/base.rb', line 115

def writable?(path)
  begin
    return if (File.stat(path).mode & 0200) != 0200

  rescue Errno::ENOENT
    # pass
  end

  yield(path) if block_given?
  true
end