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, #ctrc, #distribution, #version

Instance Method Summary collapse

Methods inherited from Base

#add_netif, #bin_path, distribution, #dns_resolvers, #initialize, #remove_netif, #rename_netif, #unset_etc_hosts, #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



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

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(
    ctrc.rootfs,
    'etc/runit/core-services',
    '90-vpsadminos-network.sh'
  )
  File.write(sv, cmds.join("\n")) if writable?(sv)
end

#passwd(opts) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/osctld/dist_config/void.rb', line 77

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



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
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/osctld/dist_config/void.rb', line 20

def set_hostname(opts)
  # /etc/hostname
  writable?(File.join(ctrc.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(
    ctrc.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(old_hostname: 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

#stop(opts) ⇒ Object

See man runit-init



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/osctld/dist_config/void.rb', line 8

def stop(opts)
  # Configure runit for halt
  begin
    ContainerControl::Commands::StopRunit.run!(ct)
  rescue ContainerControl::Error => e
    log(:warn, ct, "Unable to gracefully shutdown runit: #{e.message}")
  end

  # Run standard stop process
  super
end