Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport | Support old ubuntu netplan #1032

Merged
merged 6 commits into from
Sep 12, 2024
Merged
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
57 changes: 54 additions & 3 deletions virt-v2v/cold/scripts/rhel/run/network_config_util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,46 @@ extract_mac_ip() {
fi
}

# Get a netplan setting by specifying a nested key like "ethernets.eth0.addresses"
# For example:
# netplan_get_py ethernets
# Will return the yaml struct of all the thernet interfaces.
netplan_get_py() {
python -c "
import os
import yaml
import sys

netplan_dir = os.getenv('NETPLAN_DIR', '') + '/etc/netplan'
args = sys.argv[1].split('.')

def find_yaml_files(directory):
yaml_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(('.yaml', '.yml')):
yaml_files.append(os.path.join(root, file))

return yaml_files

def get_yaml_path(yaml_data, keys):
for key in keys:
if yaml_data is None:
return None
yaml_data = yaml_data.get(key)
return yaml_data

yaml_files = find_yaml_files(netplan_dir)

for yaml_file in yaml_files:
with open(yaml_file, 'r') as file:
yaml_data = yaml.safe_load(file)
result = get_yaml_path(yaml_data, ['network'] + args)
if result is not None:
print(yaml.dump(result, default_flow_style=False))
" "$@"
}

# Network infrastructure reading functions
# ----------------------------------------

Expand All @@ -70,7 +110,7 @@ udev_from_ifcfg() {
fi

# Find the matching network script file
IFCFG=$(grep -l "IPADDR=$S_IP" "$NETWORK_SCRIPTS_DIR"/*)
IFCFG=$(grep -l "IPADDR=.*$S_IP.*$" "$NETWORK_SCRIPTS_DIR"/*)
if [ -z "$IFCFG" ]; then
log "Info: no ifcg config file name foud for $S_IP."
continue
Expand Down Expand Up @@ -108,7 +148,7 @@ udev_from_nm() {
fi

# Find the matching NetworkManager connection file
NM_FILE=$(grep -El "address[0-9]*=$S_IP" "$NETWORK_CONNECTIONS_DIR"/*)
NM_FILE=$(grep -El "address[0-9]*=.*$S_IP.*$" "$NETWORK_CONNECTIONS_DIR"/*)
if [ -z "$NM_FILE" ]; then
log "Info: no nm config file name foud for $S_IP."
continue
Expand All @@ -133,9 +173,20 @@ udev_from_netplan() {
return 0
fi

# Function to check if netplan supports the 'get' subcommand
netplan_supports_get() {
netplan get 2>&3
return $?
}

# netplan with root dir
netplan_get() {
netplan get --root-dir "$NETPLAN_DIR" "$@" 2>/dev/null
if netplan_supports_get; then
netplan get --root-dir "$NETPLAN_DIR" "$@" 2>&3
else
log 'Info: netplan not supporting get subcomment, using python'
netplan_get_py "$@" 2>&3
fi
}

# Loop over all interface names and treturn the one with target_ip, or null
Expand Down
Loading