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



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

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



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/osctld/dist_config/void.rb', line 93

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/osctld/dist_config/void.rb', line 36

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(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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/osctld/dist_config/void.rb', line 8

def stop(opts)
  # Configure runit for halt
  begin
    ContainerControl::Commands::RunBlock.run!(
      ct,
      block: Proc.new do
        next unless Dir.exist?('/etc/runit')

        # Only the existence of the reboot file can trigger reboot
        if File.exist?('/etc/runit/reboot')
          File.open('/etc/runit/reboot', 'w', 0) {}
          File.chmod(0, '/etc/runit/reboot')
        end

        File.open('/etc/runit/stopit', 'w', 0100) {}
        File.chmod(0100, '/etc/runit/stopit')

        nil
      end
    )
  rescue ContainerControl::Error => e
    log(:warn, ct, "Unable to gracefully shutdown runit: #{e.message}")
  end

  # Run standard stop process
  super
end