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(ctrc) ⇒ Base

Returns a new instance of Base.

Parameters:



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

def initialize(ctrc)
  @ctrc = ctrc
  @ct = ctrc.ct
  @distribution = ctrc.distribution
  @version = ctrc.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

#ctrcObject (readonly)

Returns the value of attribute ctrc.



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

def ctrc
  @ctrc
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):



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

def add_netif(opts)

end

#bin_path(_opts) ⇒ String

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

Returns:

  • (String)


102
103
104
# File 'lib/osctld/dist_config/base.rb', line 102

def bin_path(_opts)
  '/bin'
end

#dns_resolvers(_opts) ⇒ Object



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

def dns_resolvers(_opts)
  writable?(File.join(ctrc.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)


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

def network(_opts)
  raise NotImplementedError
end

#passwd(opts) ⇒ Object

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

  • user (String)
  • password (String)


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/osctld/dist_config/base.rb', line 87

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):



62
63
64
# File 'lib/osctld/dist_config/base.rb', line 62

def remove_netif(opts)

end

#rename_netif(opts) ⇒ Object

Called when an existing network interface is renamed

Parameters:

  • opts (Hash)

Options Hash (opts):



70
71
72
# File 'lib/osctld/dist_config/base.rb', line 70

def rename_netif(opts)

end

#set_hostname(opts) ⇒ Object

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

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

    previous hostname

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/osctld/dist_config/base.rb', line 44

def set_hostname(opts)
  raise NotImplementedError
end

#stop(opts) ⇒ Object

Gracefully stop the container

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :mode (:stop, :shutdown, :kill)
  • :timeout (Integer)


34
35
36
37
38
39
40
# File 'lib/osctld/dist_config/base.rb', line 34

def stop(opts)
  ContainerControl::Commands::Stop.run!(
    ct,
    opts[:mode],
    timeout: opts[:timeout],
  )
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)


110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/osctld/dist_config/base.rb', line 110

def update_etc_hosts(old_hostname = nil)
  path = File.join(ctrc.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)


129
130
131
132
133
134
135
136
137
138
139
# File 'lib/osctld/dist_config/base.rb', line 129

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

  rescue Errno::ENOENT
    # pass
  end

  yield(path) if block_given?
  true
end