Class: OsCtld::DistConfig::Network::Netifrc

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

Overview

Configure network using netifrc

wiki.gentoo.org/wiki/Netifrc

Instance Attribute Summary

Attributes inherited from Base

#configurator

Instance Method Summary collapse

Methods inherited from Base

#add_netif, #initialize

Methods included from Helpers::Common

#systemd_service_enabled?, #systemd_service_masked?, #writable?

Constructor Details

This class inherits a constructor from OsCtld::DistConfig::Network::Base

Instance Method Details

#add_init_script(name) ⇒ Object (protected)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/osctld/dist_config/network/netifrc.rb', line 65

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

#configure(netifs) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/osctld/dist_config/network/netifrc.rb', line 13

def configure(netifs)
  netifs.each do |netif|
    do_create_netif(netif)
  end

  update_confd_net(netifs)
end

#del_init_script(name) ⇒ Object (protected)



81
82
83
84
85
86
87
# File 'lib/osctld/dist_config/network/netifrc.rb', line 81

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)



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/osctld/dist_config/network/netifrc.rb', line 41

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

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

  # Create init script
  add_init_script(netif.name)
end

#do_remove_netif(name) ⇒ Object (protected)



55
56
57
58
59
60
61
62
63
# File 'lib/osctld/dist_config/network/netifrc.rb', line 55

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

  # Remove init script
  del_init_script(name)

  # Remove netifrc config
  FileUtils.rm_f(netifrc_conf(name))
end

#netifrc_conf(name) ⇒ Object (protected)



150
151
152
# File 'lib/osctld/dist_config/network/netifrc.rb', line 150

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

#rc_path(name) ⇒ Object (protected)



158
159
160
# File 'lib/osctld/dist_config/network/netifrc.rb', line 158

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

#remove_netif(netifs, netif) ⇒ Object

Remove init script



22
23
24
25
# File 'lib/osctld/dist_config/network/netifrc.rb', line 22

def remove_netif(netifs, netif)
  do_remove_netif(netif.name)
  update_confd_net(netifs)
end

#rename_netif(netifs, netif, old_name) ⇒ Object

Rename init script



28
29
30
31
32
33
34
35
36
37
# File 'lib/osctld/dist_config/network/netifrc.rb', line 28

def rename_netif(netifs, netif, old_name)
  # Remove old network interface
  do_remove_netif(old_name)

  # Create the new interface
  do_create_netif(netif)

  # Update /etc/conf.d/net
  update_confd_net(netifs)
end

#script_path(name) ⇒ Object (protected)



154
155
156
# File 'lib/osctld/dist_config/network/netifrc.rb', line 154

def script_path(name)
  File.join(rootfs, 'etc/init.d', "net.#{name}")
end

#update_confd_net(netifs) ⇒ Object (protected)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
# File 'lib/osctld/dist_config/network/netifrc.rb', line 89

def update_confd_net(netifs)
  config = File.join(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 = netifs.map do |netif|
    ". #{File.join('/etc/conf.d', "net.#{netif.name}")}"
  end.join("\n")

  regenerate_file(config, 0o644) 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

#usable?Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/osctld/dist_config/network/netifrc.rb', line 8

def usable?
  File.executable?(File.join(rootfs, 'etc/init.d/net.lo')) \
    && Dir.exist?(File.join(rootfs, 'etc/conf.d'))
end