Class: OsCtld::DistConfig::Network::Netctl

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

Overview

Configure network using netctl

wiki.archlinux.org/title/netctl

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

#configure(netifs) ⇒ Object



12
13
14
15
16
# File 'lib/osctld/dist_config/network/netctl.rb', line 12

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

#deprecated_service_path(name) ⇒ Object (protected)



81
82
83
# File 'lib/osctld/dist_config/network/netctl.rb', line 81

def deprecated_service_path(name)
  File.join(rootfs, 'etc/systemd/system', service_name(name))
end

#do_create_netif(netif) ⇒ Object (protected)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/osctld/dist_config/network/netctl.rb', line 34

def do_create_netif(netif)
  profile = netctl_profile(netif.name)
  return unless writable?(profile)

  # Create netctl profile
  OsCtld::ErbTemplate.render_to_if_changed(
    File.join('dist_config/network/netctl', netif.type.to_s),
    { netif: },
    profile
  )

  s_name = service_name(netif.name)

  # Remove deprecated systemd override
  unlink_if_exists(deprecated_service_path(netif.name))

  # Start the service on boot
  s_link = service_symlink(netif.name)

  return unless !File.symlink?(s_link) && !File.exist?(s_link)

  File.symlink(File.join('/etc/systemd/system', s_name), s_link)
end

#do_remove_netif(name) ⇒ Object (protected)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/osctld/dist_config/network/netctl.rb', line 58

def do_remove_netif(name)
  profile = netctl_profile(name)
  return unless writable?(profile)

  # Disable the service
  s_link = service_symlink(name)
  File.unlink(s_link) if File.symlink?(s_link)

  # Remove deprecated service override file
  unlink_if_exists(deprecated_service_path(name))

  # Remove netctl profile
  FileUtils.rm_f(profile)
end

#netctl_profile(name) ⇒ Object (protected)



73
74
75
# File 'lib/osctld/dist_config/network/netctl.rb', line 73

def netctl_profile(name)
  File.join(rootfs, 'etc/netctl', name)
end

#remove_netif(_netifs, netif) ⇒ Object

Remove netctl profile and systemd service



19
20
21
# File 'lib/osctld/dist_config/network/netctl.rb', line 19

def remove_netif(_netifs, netif)
  do_remove_netif(netif.name)
end

#rename_netif(_netifs, netif, old_name) ⇒ Object

Rename netctl profile and systemd service



24
25
26
27
28
29
30
# File 'lib/osctld/dist_config/network/netctl.rb', line 24

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

  # Create the new interface
  do_create_netif(netif)
end

#service_name(name) ⇒ Object (protected)



77
78
79
# File 'lib/osctld/dist_config/network/netctl.rb', line 77

def service_name(name)
  "netctl@#{name}.service"
end


85
86
87
88
89
90
91
# File 'lib/osctld/dist_config/network/netctl.rb', line 85

def service_symlink(name)
  File.join(
    rootfs,
    'etc/systemd/system/multi-user.target.wants',
    service_name(name)
  )
end

#usable?Boolean

Returns:

  • (Boolean)


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

def usable?
  Dir.exist?(File.join(rootfs, 'etc/netctl'))
end