From 4f66bbcb1993ef0c681bdded41a328713ad90602 Mon Sep 17 00:00:00 2001 From: Pierantonio Merlino Date: Fri, 12 May 2023 22:39:20 +0200 Subject: [PATCH] fix(net.admin): added dhclient hook files (#4645) * Added dhclient hook scripts Signed-off-by: pierantoniomerlino * Improved dhclient hook scripts managemenet Signed-off-by: pierantoniomerlino * Updated hook script; improved installation Signed-off-by: pierantoniomerlino --------- Signed-off-by: pierantoniomerlino --- .../src/main/ant/build_equinox_distrib.xml | 2 + .../resources/common/kura-dhclient-enter-hook | 3 + .../intel-up2-ubuntu-20/kura_install.sh | 3 + .../nvidia-jetson-nano/kura_install.sh | 3 + .../raspberry-pi-ubuntu-20/kura_install.sh | 3 + .../resources/raspberry-pi/kura_install.sh | 3 + .../META-INF/MANIFEST.MF | 2 +- .../linux/net/dhcp/DhcpClientManager.java | 13 +- .../META-INF/MANIFEST.MF | 2 +- .../visitor/linux/DhcpClientConfigWriter.java | 128 ++++++++++++++++++ .../visitor/linux/LinuxWriteVisitor.java | 4 +- 11 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 kura/distrib/src/main/resources/common/kura-dhclient-enter-hook create mode 100644 kura/org.eclipse.kura.net.admin/src/main/java/org/eclipse/kura/net/admin/visitor/linux/DhcpClientConfigWriter.java diff --git a/kura/distrib/src/main/ant/build_equinox_distrib.xml b/kura/distrib/src/main/ant/build_equinox_distrib.xml index 8ee078325bd..112d728ac14 100644 --- a/kura/distrib/src/main/ant/build_equinox_distrib.xml +++ b/kura/distrib/src/main/ant/build_equinox_distrib.xml @@ -554,6 +554,8 @@ fi]]> prefix="${build.output.name}/${install.folder}" /> + lanInterfaceNames = new ArrayList<>(); + List> netInterfaceConfigs = config + .getModifiedNetInterfaceConfigs(); + for (NetInterfaceConfig netInterfaceConfig : netInterfaceConfigs) { + boolean isLan = ((AbstractNetInterface) netInterfaceConfig) + .getInterfaceStatus() == NetInterfaceStatus.netIPv4StatusEnabledLAN; + if (isLan && (netInterfaceConfig.getType() == NetInterfaceType.ETHERNET + || netInterfaceConfig.getType() == NetInterfaceType.WIFI)) { + lanInterfaceNames.add(netInterfaceConfig.getName()); + } + } + + lanInterfaceNames.sort(null); + writeDhcpClientConfig(lanInterfaceNames); + } + + private void writeDhcpClientConfig(List interfaceNames) { + String hookScriptFileName = DhcpClientManager.getHookScriptFileName(); + if (hookScriptFileName == null || hookScriptFileName.isEmpty()) { + logger.debug("Hook script file name not defined. Do nothing."); + return; + } + + try { + writeDhclientHookScript(interfaceNames, hookScriptFileName); + } catch (KuraIOException e) { + logger.error("Failed to write dhclient hook script", e); + } + } + + private void writeDhclientHookScript(List interfaceNames, String hookScriptFileName) + throws KuraIOException { + StringBuilder interfacesLine = new StringBuilder("interfaces=\""); + interfacesLine.append(interfaceNames.stream().collect(Collectors.joining(" "))); + interfacesLine.append("\"\n"); + String hookScriptContent = DEFAULT_HOOK_SCRIPT.replace("interfaces=\"\"\n", interfacesLine); + + Path hookScriptFilePath = Paths.get(hookScriptFileName); + try { + if (Files.exists(hookScriptFilePath) && Files.isReadable(hookScriptFilePath)) { + String currentHookScriptContent = new String(Files.readAllBytes(hookScriptFilePath), + StandardCharsets.UTF_8); + if (!hookScriptContent.equals(currentHookScriptContent)) { + writeFile(hookScriptFilePath, hookScriptContent); + } + } else { + writeFile(hookScriptFilePath, hookScriptContent); + } + } catch (IOException e) { + throw new KuraIOException(e, "Failed to update dhclient hook script"); + } + } + + private void writeFile(Path path, String content) throws IOException { + Files.write(path, content.getBytes(StandardCharsets.UTF_8)); + } + +} diff --git a/kura/org.eclipse.kura.net.admin/src/main/java/org/eclipse/kura/net/admin/visitor/linux/LinuxWriteVisitor.java b/kura/org.eclipse.kura.net.admin/src/main/java/org/eclipse/kura/net/admin/visitor/linux/LinuxWriteVisitor.java index 19d0e3da645..2c982aae50b 100644 --- a/kura/org.eclipse.kura.net.admin/src/main/java/org/eclipse/kura/net/admin/visitor/linux/LinuxWriteVisitor.java +++ b/kura/org.eclipse.kura.net.admin/src/main/java/org/eclipse/kura/net/admin/visitor/linux/LinuxWriteVisitor.java @@ -33,6 +33,7 @@ private LinuxWriteVisitor() { this.visitors.add(new WifiConfigWriter()); this.visitors.add(new PppConfigWriter()); this.visitors.add(new DhcpConfigWriter()); + this.visitors.add(new DhcpClientConfigWriter()); this.visitors.add(new FirewallAutoNatConfigWriter()); } @@ -51,7 +52,8 @@ public void visit(NetworkConfiguration config) throws KuraException { visitor.visit(config); } - // After every visit, unset the executorService. This must be set before every call. + // After every visit, unset the executorService. This must be set before every + // call. this.executorService = null; }