Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions coriolis/osmorphing/netpreserver/ifcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ def parse_network(self):
"ip_addresses": [ip_address] if ip_address else []
}

def backup_ifcfg_configs(self, device_names, backup_file_suffix=".bak"):
"""Back up ifcfg profiles for the given device names."""
for dev_name in device_names:
cfg_path = "%s/ifcfg-%s" % (
self.network_scripts_path, dev_name)
if self.osmorphing_tool._test_path(cfg_path):
self.osmorphing_tool._exec_cmd_chroot(
'mv "%s" "%s%s"' % (
cfg_path, cfg_path, backup_file_suffix))
LOG.debug("Backed up ifcfg profile '%s'", cfg_path)

def _get_net_config_files(self, network_scripts_path):
dir_content = self.osmorphing_tool._list_dir(network_scripts_path)
return [os.path.join(network_scripts_path, f) for f in
Expand Down
45 changes: 34 additions & 11 deletions coriolis/osmorphing/netpreserver/nmconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,42 @@ def __init__(self, osmorphing_tool):
self.nmconnection_file = "etc/NetworkManager/system-connections"

def check_net_preserver(self):
if self.osmorphing_tool._test_path(self.nmconnection_file):
nmconnection_files = self._get_nmconnection_files(
self.nmconnection_file)
if nmconnection_files:
nmconnection_ethernet = self._get_keyfiles_by_type(
"ethernet", self.nmconnection_file)
if nmconnection_ethernet:
return True
return False
if not self.get_nmconnection_files():
return False
return bool(self._get_keyfiles_by_type(
"ethernet", self.nmconnection_file))

def parse_network(self):
nmconnection_ethernet = self._get_keyfiles_by_type(
def get_nmconnection_files(self):
if not self.osmorphing_tool._test_path(self.nmconnection_file):
return []
return self._get_nmconnection_files(self.nmconnection_file)

def get_ethernet_keyfiles(self):
if not self.osmorphing_tool._test_path(self.nmconnection_file):
return []
return self._get_keyfiles_by_type(
"ethernet", self.nmconnection_file)

def backup_ethernet_keyfiles(self, backup_file_suffix=".bak"):
"""Back up existing ethernet nmconnection profiles."""
for cfg_path, _ in self.get_ethernet_keyfiles():
self.osmorphing_tool._exec_cmd_chroot(
'mv "%s" "%s%s"' % (
cfg_path, cfg_path, backup_file_suffix))
LOG.debug(
"Backed up ethernet nmconnection profile '%s'", cfg_path)

def backup_nmconnection_files(self, backup_file_suffix=".bak"):
"""Back up all existing nmconnection profiles."""
for cfg_path in self.get_nmconnection_files():
self.osmorphing_tool._exec_cmd_chroot(
'mv "%s" "%s%s"' % (
cfg_path, cfg_path, backup_file_suffix))
LOG.debug(
"Backed up nmconnection profile '%s'", cfg_path)

def parse_network(self):
nmconnection_ethernet = self.get_ethernet_keyfiles()
if nmconnection_ethernet:
for nmconn_file, nmconn in nmconnection_ethernet:
name = nmconn.get("interface-name", nmconn.get("id"))
Expand Down
94 changes: 76 additions & 18 deletions coriolis/osmorphing/redhat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from coriolis import exception
from coriolis.osmorphing import base
from coriolis.osmorphing.netpreserver import ifcfg
from coriolis.osmorphing.netpreserver import nmconnection
from coriolis.osmorphing.osdetect import centos as centos_detect
from coriolis.osmorphing.osdetect import redhat as redhat_detect
from coriolis import utils
Expand All @@ -35,12 +37,31 @@
NAME=%(device_name)s
DEVICE=%(device_name)s
ONBOOT=yes
NM_CONTROLLED=no
NM_CONTROLLED=%(nm_controlled)s
"""

NMCONNECTION_TEMPLATE = """[connection]
id=%(device_name)s
uuid=%(connection_uuid)s
type=ethernet
interface-name=%(device_name)s
autoconnect=true

[ethernet]

[ipv4]
method=auto
may-fail=false

[ipv6]
method=auto
addr-gen-mode=default
"""


class BaseRedHatMorphingTools(base.BaseLinuxOSMorphingTools):
_NETWORK_SCRIPTS_PATH = "etc/sysconfig/network-scripts"
_NM_CONNECTIONS_PATH = "etc/NetworkManager/system-connections"
BIOS_GRUB_LOCATION = "/boot/grub2"
UEFI_GRUB_LOCATION = "/boot/efi/EFI/redhat"

Expand Down Expand Up @@ -97,22 +118,33 @@ def _has_systemd(self):
except Exception:
return False

def _get_nmconnection_net_preserver(self):
return nmconnection.NmconnectionNetPreserver(self)

def _get_ifcfg_net_preserver(self):
return ifcfg.IfcfgNetPreserver(self)

def _get_ifcfg_nm_controlled(self):
if self._version_supported_util(self._version, minimum=8):
return "yes"
return "no"

def _set_dhcp_net_config(self, ifcfgs_ethernet):
for ifcfg_file, ifcfg in ifcfgs_ethernet:
if ifcfg.get("BOOTPROTO") == "none":
ifcfg["BOOTPROTO"] = "dhcp"
ifcfg["UUID"] = str(uuid.uuid4())

if 'IPADDR' in ifcfg:
del ifcfg['IPADDR']
if 'GATEWAY' in ifcfg:
del ifcfg['GATEWAY']
if 'NETMASK' in ifcfg:
del ifcfg['NETMASK']
if 'NETWORK' in ifcfg:
del ifcfg['NETWORK']

self._write_config_file(ifcfg_file, ifcfg)
for ifcfg_file, iface_cfg in ifcfgs_ethernet:
if iface_cfg.get("BOOTPROTO") == "none":
iface_cfg["BOOTPROTO"] = "dhcp"
iface_cfg["UUID"] = str(uuid.uuid4())

if 'IPADDR' in iface_cfg:
del iface_cfg['IPADDR']
if 'GATEWAY' in iface_cfg:
del iface_cfg['GATEWAY']
if 'NETMASK' in iface_cfg:
del iface_cfg['NETMASK']
if 'NETWORK' in iface_cfg:
del iface_cfg['NETWORK']

self._write_config_file(ifcfg_file, iface_cfg)

network_cfg_file = "etc/sysconfig/network"
network_cfg = self._read_config_file(network_cfg_file,
Expand All @@ -121,10 +153,14 @@ def _set_dhcp_net_config(self, ifcfgs_ethernet):
del network_cfg["GATEWAY"]
self._write_config_file(network_cfg_file, network_cfg)

def _uses_nmconnection_net_config(self):
return bool(
self._get_nmconnection_net_preserver().get_nmconnection_files())

def _write_nic_configs(self, nics_info):
for idx, _ in enumerate(nics_info):
dev_name = "eth%d" % idx
cfg_path = "etc/sysconfig/network-scripts/ifcfg-%s" % dev_name
cfg_path = "%s/ifcfg-%s" % (self._NETWORK_SCRIPTS_PATH, dev_name)
if self._test_path(cfg_path):
self._exec_cmd_chroot(
"cp %s %s.bak" % (cfg_path, cfg_path)
Expand All @@ -133,8 +169,27 @@ def _write_nic_configs(self, nics_info):
cfg_path,
IFCFG_TEMPLATE % {
"device_name": dev_name,
"nm_controlled": self._get_ifcfg_nm_controlled(),
})

def _write_nmconnection_configs(self, nics_info):
nm_net_preserver = self._get_nmconnection_net_preserver()
nm_net_preserver.backup_nmconnection_files()
device_names = ["eth%d" % idx for idx, _ in enumerate(nics_info)]
self._get_ifcfg_net_preserver().backup_ifcfg_configs(device_names)

for idx, _ in enumerate(nics_info):
dev_name = "eth%d" % idx
cfg_path = "%s/%s.nmconnection" % (
self._NM_CONNECTIONS_PATH, dev_name)
self._write_file_sudo(
cfg_path,
NMCONNECTION_TEMPLATE % {
"device_name": dev_name,
"connection_uuid": str(uuid.uuid4()),
})
self._exec_cmd_chroot("chmod 600 /%s" % cfg_path)

def _comment_keys_from_ifcfg_files(
self, keys, interfaces=None, backup_file_suffix=".bak"):
""" Comments the provided list of keys from all 'ifcfg-*' files.
Expand Down Expand Up @@ -172,7 +227,10 @@ def _comment_keys_from_ifcfg_files(
def set_net_config(self, nics_info, dhcp):
if dhcp:
self.disable_predictable_nic_names()
self._write_nic_configs(nics_info)
if self._uses_nmconnection_net_config():
Comment thread
fabi200123 marked this conversation as resolved.
self._write_nmconnection_configs(nics_info)
else:
self._write_nic_configs(nics_info)
return

LOG.info("Setting static IP configuration")
Expand Down
18 changes: 18 additions & 0 deletions coriolis/tests/osmorphing/netpreserver/test_ifcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,21 @@ def test_parse_network(self, mock_get_ifcfgs_by_type):
}

self.assertEqual(self.netpreserver.interface_info, expected_info)

@mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot')
@mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path')
def test_backup_ifcfg_configs(self, mock_test_path, mock_exec_cmd_chroot):
mock_test_path.return_value = True

self.netpreserver.backup_ifcfg_configs(['eth0', 'eth1'])

mock_exec_cmd_chroot.assert_has_calls([
mock.call(
'mv "etc/sysconfig/network-scripts/ifcfg-eth0" '
'"etc/sysconfig/network-scripts/ifcfg-eth0.bak"'
),
mock.call(
'mv "etc/sysconfig/network-scripts/ifcfg-eth1" '
'"etc/sysconfig/network-scripts/ifcfg-eth1.bak"'
),
])
Loading
Loading