Class: OsCtld::DistConfig::Gentoo

Inherits:
Base
  • Object
show all
Defined in:
lib/osctld/dist_config/gentoo.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, #passwd, #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

#add_init_script(name) ⇒ Object (protected)



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/osctld/dist_config/gentoo.rb', line 81

def add_init_script(name)
  # Create init script
  begin
    File.lstat(script_path(name))

  rescue Errno::ENOENT
    File.symlink('/etc/init.d/net.lo', script_path(name))
  end

  # Enable the init script in the default runlevel
  begin
    File.lstat(rc_path(name))

  rescue Errno::ENOENT
    File.symlink(File.join('/etc/init.d', "net.#{name}"), rc_path(name))
  end
end

#del_init_script(name) ⇒ Object (protected)



99
100
101
102
103
104
105
# File 'lib/osctld/dist_config/gentoo.rb', line 99

def del_init_script(name)
  # Disable init script
  File.unlink(rc_path(name)) if File.symlink?(rc_path(name))

  # Remove init script
  File.unlink(script_path(name)) if File.symlink?(script_path(name))
end

#do_create_netif(netif) ⇒ Object (protected)



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/osctld/dist_config/gentoo.rb', line 57

def do_create_netif(netif)
  return unless writable?(netifrc_conf(netif.name))

  # Create netifrc config
  OsCtld::ErbTemplate.render_to(
    File.join('dist_config/network/gentoo', netif.type.to_s),
    {netif: netif},
    netifrc_conf(netif.name)
  )

  # Create init script
  add_init_script(netif.name)
end

#do_remove_netif(name) ⇒ Object (protected)



71
72
73
74
75
76
77
78
79
# File 'lib/osctld/dist_config/gentoo.rb', line 71

def do_remove_netif(name)
  return unless writable?(netifrc_conf(name))

  # Remove init script
  del_init_script(name)

  # Remove netifrc config
  File.unlink(netifrc_conf(name)) if File.exist?(netifrc_conf(name))
end

#netifrc_conf(name) ⇒ Object (protected)



168
169
170
# File 'lib/osctld/dist_config/gentoo.rb', line 168

def netifrc_conf(name)
  File.join(ct.rootfs, 'etc/conf.d', "net.#{name}")
end

#network(_opts) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/osctld/dist_config/gentoo.rb', line 30

def network(_opts)
  ct.netifs.each do |netif|
    do_create_netif(netif)
  end

  update_confd_net
end

#rc_path(name) ⇒ Object (protected)



176
177
178
# File 'lib/osctld/dist_config/gentoo.rb', line 176

def rc_path(name)
  File.join(ct.rootfs, 'etc/runlevels/default', "net.#{name}")
end

#remove_netif(opts) ⇒ Object

Remove init script



39
40
41
42
# File 'lib/osctld/dist_config/gentoo.rb', line 39

def remove_netif(opts)
  do_remove_netif(opts[:netif].name)
  update_confd_net
end

#rename_netif(opts) ⇒ Object

Rename init script



45
46
47
48
49
50
51
52
53
54
# File 'lib/osctld/dist_config/gentoo.rb', line 45

def rename_netif(opts)
  # Remove old network interface
  do_remove_netif(opts[:original_name])

  # Create the new interface
  do_create_netif(opts[:netif])

  # Update /etc/conf.d/net
  update_confd_net
end

#script_path(name) ⇒ Object (protected)



172
173
174
# File 'lib/osctld/dist_config/gentoo.rb', line 172

def script_path(name)
  File.join(ct.rootfs, 'etc/init.d', "net.#{name}")
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
# File 'lib/osctld/dist_config/gentoo.rb', line 7

def set_hostname(opts)
  # /etc/hostname
  writable?(File.join(ct.rootfs, 'etc', 'conf.d', 'hostname')) do |path|
    regenerate_file(path, 0644) do |f|
      f.puts('# Set to the hostname of this machine')
      f.puts("hostname=\"#{ct.hostname}\"")
    end
  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

#update_confd_netObject (protected)



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/osctld/dist_config/gentoo.rb', line 107

def update_confd_net
  config = File.join(ct.rootfs, 'etc/conf.d/net')
  return unless writable?(config)

  heading = "# BEGIN osctld generated content"
  banner = <<-END
#
# Do not edit lines within BEGIN and END. This section is generated by osctld
# from vpsAdminOS. You can move this block around and add your own configuration
# above and below this block. To stop osctld from manipulating this file, run
#
#   chmod u-w /etc/conf.d/net

  END
  ending = "\n# END osctld generated content"

  cfg = ct.netifs.map do |netif|
    ". #{File.join('/etc/conf.d', "net.#{netif.name}")}"
  end.join("\n")

  regenerate_file(config, 0644) do |new, old|
    if old.nil?
      # /etc/conf.d/net did not exist, create it
      new.puts(heading)
      new.puts(banner)
      new.puts(cfg)
      new.puts(ending)
      next
    end

    in_block = false
    done = false

    old.each_line do |line|
      if !done && line.strip.start_with?('# BEGIN osctld')
        in_block = true

      elsif in_block && !done && line.strip.start_with?('# END osctld')
        in_block = false
        done = true

        new.puts(heading)
        new.puts(banner)
        new.puts(cfg)
        new.puts(ending)

      elsif !in_block
        new.write(line)
      end
    end

    # Block not found
    unless done
      new.puts(heading)
      new.puts(banner)
      new.puts(cfg)
      new.puts(ending)
    end
  end
end