Skip to content

Commit

Permalink
feat(distrib): Network configuration customization during Kura install (
Browse files Browse the repository at this point in the history
#5419)

* Fixed typo in postinstall debian scripts

Signed-off-by: pierantoniomerlino <[email protected]>

* Implemented default network configuration selection logic

Signed-off-by: pierantoniomerlino <[email protected]>

* Removed useless empty lines in templates; fix variable name

Signed-off-by: pierantoniomerlino <[email protected]>

* Removed commented block

Signed-off-by: pierantoniomerlino <[email protected]>

* Fixed sevaral typos in templates

Signed-off-by: pierantoniomerlino <[email protected]>

* Updated constants in python scripts

Signed-off-by: pierantoniomerlino <[email protected]>

* Fixed args management in Python scritps

Signed-off-by: pierantoniomerlino <[email protected]>

* Fixed ugly for loop management

Signed-off-by: pierantoniomerlino <[email protected]>

* Updated file management in python scripts

Signed-off-by: pierantoniomerlino <[email protected]>

* Added management of NN profiles configuration

Signed-off-by: pierantoniomerlino <[email protected]>

* Improved snapshot management

Signed-off-by: pierantoniomerlino <[email protected]>

* Fixed raspberry nn installation

Signed-off-by: pierantoniomerlino <[email protected]>

* Removed comment

Signed-off-by: pierantoniomerlino <[email protected]>

* Fixed eth and wlan default configuration

Signed-off-by: pierantoniomerlino <[email protected]>

* Removed resources for specific devices

Signed-off-by: pierantoniomerlino <[email protected]>

* Added argparse to python scripts

Signed-off-by: pierantoniomerlino <[email protected]>

* Fixed script usage message

Signed-off-by: pierantoniomerlino <[email protected]>

---------

Signed-off-by: pierantoniomerlino <[email protected]>
  • Loading branch information
pierantoniomerlino authored Oct 16, 2024
1 parent 23a27ee commit 34f4fae
Show file tree
Hide file tree
Showing 35 changed files with 743 additions and 945 deletions.
8 changes: 7 additions & 1 deletion kura/distrib/src/main/ant/build_equinox_distrib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,13 @@ fi]]>
<zipfileset file="src/main/resources/${build.name}/kura.logrotate"
prefix="${build.output.name}/${install.folder}" />

<zipfileset file="src/main/resources/common/find_net_interfaces.py"
<zipfileset file="src/main/resources/common/network_tools.py"
prefix="${build.output.name}/${install.folder}" />
<zipfileset file="src/main/resources/common/customize_iptables.py"
prefix="${build.output.name}/${install.folder}" />
<zipfileset file="src/main/resources/common/customize_kura_properties.py"
prefix="${build.output.name}/${install.folder}" />
<zipfileset file="src/main/resources/common/customize_snapshot.py"
prefix="${build.output.name}/${install.folder}" />
<zipfileset file="src/main/resources/common/comment_interfaces_file.py"
prefix="${build.output.name}/${install.folder}" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (c) 2023 Eurotech and/or its affiliates and others
# Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
Expand All @@ -14,7 +14,7 @@
import shutil
import logging
from os.path import exists
from find_net_interfaces import get_eth_wlan_interfaces_names
from network_tools import get_eth_wlan_interfaces_names

INTERFACES_PATH = "/etc/network/interfaces"
INTERFACES_OLD_PATH = INTERFACES_PATH + ".old"
Expand Down
97 changes: 47 additions & 50 deletions kura/distrib/src/main/resources/common/customize-installation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,78 +38,75 @@ setup_libudev() {
fi
}

customize_kura_properties() {
local BOARD=$1

sed -i "s/device_name/${BOARD}/g" "/opt/eclipse/kura/framework/kura.properties"
customize_snapshot() {
if [ ! -d "/opt/eclipse/kura/user/snapshots/" ]; then
mkdir /opt/eclipse/kura/user/snapshots/
fi

if python3 -V > /dev/null 2>&1
then
python3 /opt/eclipse/kura/install/find_net_interfaces.py /opt/eclipse/kura/framework/kura.properties
mv "/opt/eclipse/kura/install/snapshot_0.xml" "/opt/eclipse/kura/user/snapshots/snapshot_0.xml"
if [ ${IS_NETWORKING_PROFILE} = "true" ]; then
python3 "/opt/eclipse/kura/install/customize_snapshot.py" "--networking_profile"
else
echo "python3 not found. Could not edit the primary network interface name in /opt/eclipse/kura/framework/kura.properties. Defaulted to eth0."
python3 "/opt/eclipse/kura/install/customize_snapshot.py"
fi
}

customize_iptables() {
if [ "${IS_NETWORKING_PROFILE}" = "true" ]; then
mv "/opt/eclipse/kura/install/iptables" "/opt/eclipse/kura/.data/iptables"
python3 "/opt/eclipse/kura/install/customize_iptables.py"
fi
}

customize_network_interfaces() {
customize_kura_properties() {
local BOARD=$1
local TO_BE_PATCHED=()

KURA_PLATFORM=$( uname -m )
sed -i "s/kura_platform/${KURA_PLATFORM}/g" "/opt/eclipse/kura/framework/kura.properties"

TO_BE_PATCHED+=("/opt/eclipse/kura/user/snapshots/snapshot_0.xml")
TO_BE_PATCHED+=("/opt/eclipse/kura/.data/iptables")
python3 "/opt/eclipse/kura/install/customize_kura_properties.py" "${BOARD}"
}

if [ ${#TO_BE_PATCHED[@]} -gt 0 ]
then
if python3 -V > /dev/null 2>&1
then
python3 "/opt/eclipse/kura/install/find_net_interfaces.py" "${TO_BE_PATCHED[@]}"
else
echo "python3 not found. The following files may have wrong interface names: ${TO_BE_PATCHED[*]}. Please correct them manually if they mismatch."
customize_ram() {
local BOARD=$1

if [ ${BOARD} = "generic-device" ]; then
# dynamic RAM assignment
RAM_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}')
RAM_MB=$(expr $RAM_KB / 1024)
RAM_MB_FOR_KURA=$(expr $RAM_MB / 4)

if [ "$RAM_MB" -lt 1024 ]; then
RAM_MB_FOR_KURA="256"
fi
fi

echo "Setting kura RAM to ${RAM_MB_FOR_KURA}"
start_scripts_to_change=("start_kura.sh" "start_kura_debug.sh" "start_kura_background.sh")

RAM_REPLACEMENT_STRING="-Xms${RAM_MB_FOR_KURA}m -Xmx${RAM_MB_FOR_KURA}m"
for installer_name in "${start_scripts_to_change[@]}"; do
echo "Updating RAM values for $installer_name"
sed -i "s/-Xms[0-9]*m -Xmx[0-9]*m/$RAM_REPLACEMENT_STRING/g" "/opt/eclipse/kura/bin/$installer_name"
done

fi
}

KURA_PLATFORM=$( uname -m )
sed -i "s/kura_platform/${KURA_PLATFORM}/g" "/opt/eclipse/kura/framework/kura.properties"
IS_NETWORKING_PROFILE=$1

setup_libudev

BOARD="generic-device"

if uname -a | grep -q 'raspberry' > /dev/null 2>&1
then
BOARD="raspberry"
echo "Customizing installation for Raspberry PI"
fi

if [ ! -d "/opt/eclipse/kura/user/snapshots/" ]; then
mkdir /opt/eclipse/kura/user/snapshots/
fi

mv "/opt/eclipse/kura/install/jdk.dio.properties-${BOARD}" "/opt/eclipse/kura/framework/jdk.dio.properties"
mv "/opt/eclipse/kura/install/snapshot_0.xml-${BOARD}" "/opt/eclipse/kura/user/snapshots/snapshot_0.xml"
mv "/opt/eclipse/kura/install/iptables-${BOARD}" "/opt/eclipse/kura/.data/iptables"

customize_snapshot
customize_kura_properties "${BOARD}"
customize_network_interfaces "${BOARD}"
customize_iptables
customize_ram "${BOARD}"

if [ ${BOARD} = "generic-device" ]; then
# dynamic RAM assignment
RAM_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}')
RAM_MB=$(expr $RAM_KB / 1024)
RAM_MB_FOR_KURA=$(expr $RAM_MB / 4)

if [ "$RAM_MB" -lt 1024 ]; then
RAM_REPLACEMENT_STRING="-Xms256m -Xmx256m"
fi

echo "Setting kura RAM to ${RAM_REPLACEMENT_STRING}"
start_scripts_to_change=("start_kura.sh" "start_kura_debug.sh" "start_kura_background.sh")

RAM_REPLACEMENT_STRING="-Xms${RAM_MB_FOR_KURA}m -Xmx${RAM_MB_FOR_KURA}m"
for installer_name in "${start_scripts_to_change[@]}"; do
echo "Updating RAM values for $installer_name"
sed -i "s/-Xms[0-9]*m -Xmx[0-9]*m/$RAM_REPLACEMENT_STRING/g" "/opt/eclipse/kura/bin/$installer_name"
done

fi
60 changes: 60 additions & 0 deletions kura/distrib/src/main/resources/common/customize_iptables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
#
# Copyright (c) 2024 Eurotech and/or its affiliates and others
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Eurotech
#
import sys
import logging
import os
from network_tools import get_eth_wlan_interfaces_names

def main():
logging.basicConfig(
format='[customize_iptables.py] %(asctime)s %(levelname)s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.StreamHandler()
]
)

IPTABLES_FILENAME = "/opt/eclipse/kura/.data/iptables"

(eth_names, wlan_names) = get_eth_wlan_interfaces_names()

eth_number = len(eth_names)
wlan_number = len(wlan_names)

if eth_number == 0:
logging.info("ERROR: no ethernet interface found")
exit(1)

logging.info("%s : starting editing", IPTABLES_FILENAME)
with open(IPTABLES_FILENAME, 'r', encoding='utf-8') as iptables:
iptables_content = iptables.read()

iptables_content_updated = ""
if wlan_number == 0:
for line in iptables_content.split("\n"):
if 'WIFI_INTERFACE_0' not in line:
iptables_content_updated += (line.replace('ETH_INTERFACE_0', eth_names[0]) + "\n")
else:
iptables_content_updated = iptables_content
iptables_content_updated = iptables_content_updated.replace('ETH_INTERFACE_0', eth_names[0])
iptables_content_updated = iptables_content_updated.replace('WIFI_INTERFACE_0', wlan_names[0])

with open(IPTABLES_FILENAME, 'w', encoding='utf-8') as iptables:
iptables.write(iptables_content_updated)

logging.info("%s : successfully edited", IPTABLES_FILENAME)

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3
#
# Copyright (c) 2024 Eurotech and/or its affiliates and others
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Eurotech
#
import sys
import logging
import os
import argparse
from network_tools import get_eth_wlan_interfaces_names

def main():
logging.basicConfig(
format='[customize_kura_properties.py] %(asctime)s %(levelname)s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.StreamHandler()
]
)

parser = argparse.ArgumentParser(description="Customize kura properties file", usage='%(prog)s device_name')
parser.add_argument('device_name', help='The name of the device')
args = parser.parse_args()

KURA_PROPERTIES_FILENAME = "/opt/eclipse/kura/framework/kura.properties"

(eth_names, wlan_names) = get_eth_wlan_interfaces_names()

if len(eth_names) == 0:
logging.info("ERROR: no ethernet interface found")
exit(1)

logging.info("%s : starting editing", KURA_PROPERTIES_FILENAME)
with open(KURA_PROPERTIES_FILENAME, 'r', encoding='utf-8') as kura_properties:
kura_properties_content = kura_properties.read()

kura_properties_content = kura_properties_content.replace('device_name', args.device_name)
kura_properties_content = kura_properties_content.replace('eth0', eth_names[0])

with open(KURA_PROPERTIES_FILENAME, 'w', encoding='utf-8') as kura_properties:
kura_properties.write(kura_properties_content)

logging.info("%s : successfully edited", KURA_PROPERTIES_FILENAME)

if __name__ == "__main__":
main()
111 changes: 111 additions & 0 deletions kura/distrib/src/main/resources/common/customize_snapshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env python3
#
# Copyright (c) 2024 Eurotech and/or its affiliates and others
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Eurotech
#
import sys
import logging
import os
import argparse
from network_tools import get_eth_wlan_interfaces_names

def main():
logging.basicConfig(
format='[customize_snapshot.py] %(asctime)s %(levelname)s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.StreamHandler()
]
)

SNAPSHOT_FILENAME = "/opt/eclipse/kura/user/snapshots/snapshot_0.xml"

parser = argparse.ArgumentParser(description="Customize snapshot_0.xml file", usage='%(prog)s [--networking_profile]')
parser.add_argument('--networking_profile', action='store_true', help='Specifies if this is a profile with or without networking')
parser.set_defaults(networking_profile=False)
args = parser.parse_args()

if not args.networking_profile:
with open(SNAPSHOT_FILENAME, 'r', encoding='utf-8') as snapshot:
snapshot_content = snapshot.read()

snapshot_content_updated = ""
for line in snapshot_content.split("\n"):
if 'NETWORK_CONFIGURATION' not in line and 'FIREWALL_CONFIGURATION' not in line:
snapshot_content_updated += (line + "\n")

with open(SNAPSHOT_FILENAME, 'w', encoding='utf-8') as snapshot:
snapshot.write(snapshot_content_updated)
logging.info("%s : successfully edited", SNAPSHOT_FILENAME)

else:
(eth_names, wlan_names) = get_eth_wlan_interfaces_names()

eth_number = len(eth_names)
wlan_number = len(wlan_names)

if eth_number == 0:
logging.info("ERROR: no ethernet interface found")
exit(1)

network_configuration_template = "/opt/eclipse/kura/install/template_one_eth_no_wlan"
firewall_configuration_template = "/opt/eclipse/kura/install/template_firewall_eth"
if eth_number == 1:
if wlan_number == 0:
network_configuration_template = "/opt/eclipse/kura/install/template_one_eth_no_wlan"
firewall_configuration_template = "/opt/eclipse/kura/install/template_firewall_eth"
else:
network_configuration_template = "/opt/eclipse/kura/install/template_one_eth_one_wlan"
firewall_configuration_template = "/opt/eclipse/kura/install/template_firewall_eth_wlan"
else:
if wlan_number == 0:
network_configuration_template = "/opt/eclipse/kura/install/template_multiple_eth_no_wlan"
firewall_configuration_template = "/opt/eclipse/kura/install/template_firewall_eth"
else:
network_configuration_template = "/opt/eclipse/kura/install/template_multiple_eth_one_wlan"
firewall_configuration_template = "/opt/eclipse/kura/install/template_firewall_eth_wlan"


logging.info("%s : starting editing", SNAPSHOT_FILENAME)
with open(SNAPSHOT_FILENAME, 'r', encoding='utf-8') as snapshot:
snapshot_content = snapshot.read()

with open(network_configuration_template, 'r', encoding='utf-8') as network_template:
network_template_content = network_template.read()

with open(firewall_configuration_template, 'r', encoding='utf-8') as firewall_template:
firewall_template_content = firewall_template.read()

snapshot_content = snapshot_content.replace('NETWORK_CONFIGURATION', network_template_content)
snapshot_content = snapshot_content.replace('FIREWALL_CONFIGURATION', firewall_template_content)

interfaces_list = "lo"

for i, eth_name in enumerate(eth_names[:2]):
snapshot_content = snapshot_content.replace('ETH_INTERFACE_' + str(i), eth_name)
interfaces_list += "," + eth_name
logging.info("%s : replaced ETH_INTERFACE_%s with %s", SNAPSHOT_FILENAME, str(i), eth_name)

for i, wlan_name in enumerate(wlan_names[:1]):
snapshot_content = snapshot_content.replace('WIFI_INTERFACE_' + str(i), wlan_name)
interfaces_list += "," + wlan_name
logging.info("%s : replaced WIFI_INTERFACE_%s with %s", SNAPSHOT_FILENAME, str(i), wlan_name)

snapshot_content = snapshot_content.replace('INTERFACES_LIST', interfaces_list)

with open(SNAPSHOT_FILENAME, 'w', encoding='utf-8') as snapshot:
snapshot.write(snapshot_content)

logging.info("%s : successfully edited", SNAPSHOT_FILENAME)

if __name__ == "__main__":
main()
Loading

0 comments on commit 34f4fae

Please sign in to comment.