Class: OsCtld::DistConfig::Void

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

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, #remove_netif, #rename_netif, #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

#network(_opts) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/osctld/dist_config/void.rb', line 46

def network(_opts)
  tpl_base = 'dist_config/network/void'

  cmds = ct.netifs.map do |netif|
    OsCtld::ErbTemplate.render(
      File.join(tpl_base, netif.type.to_s),
      {netif: netif}
    )
  end

  sv = File.join(
    ct.rootfs,
    'etc/runit/core-services',
    '90-vpsadminos-network.sh'
  )
  File.write(sv, cmds.join("\n")) if writable?(sv)
end

#passwd(opts) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/osctld/dist_config/void.rb', line 64

def passwd(opts)
  # Without the -c switch, the password is not set (bug?)
  ret = ct_syscmd(
    ct,
    'chpasswd -c SHA512',
    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

#set_hostname(opts) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/osctld/dist_config/void.rb', line 7

def set_hostname(opts)
  # /etc/hostname
  writable?(File.join(ct.rootfs, 'etc', 'hostname')) do |path|
    regenerate_file(path, 0644) do |f|
      f.puts(ct.hostname.local)
    end
  end

  # Hostname in void is set by /etc/runit/core-services/05-misc.sh.
  # Unfortunately, it tries to set it by writing to /proc/sys/kernel/hostname,
  # which an unprivileged container cannot do. We add out own service
  # to set the hostname using /bin/hostname, which uses a syscall that works.
  sv = File.join(
    ct.rootfs,
    'etc/runit/core-services',
    '10-vpsadminos-hostname.sh',
  )
  if writable?(sv)
    OsCtld::ErbTemplate.render_to(
      'dist_config/network/void/hostname',
      {},
      sv
    )
  end

  # 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.local}")

    rescue SystemCommandFailed => e
      log(:warn, ct, "Unable to apply hostname: #{e.message}")
    end
  end
end