Restart avahi-daemon on hostname changes (#161)

* Restart avahi-daemon on hostname changes

* Update NetworkManager.java

* Spotless
This commit is contained in:
Matt
2020-12-03 12:16:05 -08:00
committed by GitHub
parent 5e385b5925
commit d2b0e465ff
2 changed files with 21 additions and 9 deletions

View File

@@ -46,12 +46,7 @@ public class NetworkManager {
}
var config = ConfigManager.getInstance().getConfig().getNetworkConfig();
logger.info(
"Setting static ip to \""
+ config.staticIp
+ "\" and hostname to \""
+ config.hostname
+ "\"");
logger.info("Setting " + config.connectionType + " with team team " + config.teamNumber);
if (Platform.isLinux()) {
if (!Platform.isRoot) {
logger.error("Cannot manage network without root!");
@@ -64,8 +59,13 @@ public class NetworkManager {
var shell = new ShellExec(true, false);
shell.executeBashCommand("cat /etc/hostname | tr -d \" \\t\\n\\r\"");
var oldHostname = shell.getOutput().replace("\n", "");
var setHostnameRetCode =
shell.executeBashCommand("hostnamectl set-hostname" + config.hostname);
shell.executeBashCommand(
"echo $NEW_HOSTNAME > /etc/hostname".replace("$NEW_HOSTNAME", config.hostname));
setHostnameRetCode =
shell.executeBashCommand("hostnamectl set-hostname " + config.hostname);
// Add to /etc/hosts
var addHostRetCode =
shell.executeBashCommand(
@@ -73,14 +73,18 @@ public class NetworkManager {
"sed -i \"s/127.0.1.1.*%s/127.0.1.1\\t%s/g\" /etc/hosts",
oldHostname, config.hostname));
shell.executeBashCommand("sudo service avahi-daemon restart");
var success = setHostnameRetCode == 0 && addHostRetCode == 0;
if (!success) {
logger.error(
"Setting hostname returned non-zero codes "
"Setting hostname returned non-zero codes (hostname/hosts) "
+ setHostnameRetCode
+ "|"
+ addHostRetCode
+ "!");
} else {
logger.info("Set hostname to " + config.hostname);
}
} catch (Exception e) {
logger.error("Failed to set hostname!", e);

View File

@@ -18,10 +18,14 @@
package org.photonvision.common.util;
import java.io.*;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
/** Execute external process and optionally read output buffer. */
@SuppressWarnings({"unused", "ConstantConditions"})
public class ShellExec {
private static final Logger logger = new Logger(ShellExec.class, LogGroup.General);
private int exitCode;
private boolean readOutput, readError;
private StreamGobbler errorGobbler, outputGobbler;
@@ -43,6 +47,8 @@ public class ShellExec {
* @return true if bash got started, but your command may have failed.
*/
public int executeBashCommand(String command) throws IOException {
logger.debug("Executing \"" + command + "\"");
boolean wait = true;
boolean success = false;
Runtime r = Runtime.getRuntime();
@@ -57,7 +63,9 @@ public class ShellExec {
// Consume streams, older jvm's had a memory leak if streams were not read,
// some other jvm+OS combinations may block unless streams are consumed.
return doProcess(wait, process);
int retcode = doProcess(wait, process);
logger.debug("Got exit code " + retcode);
return retcode;
}
/**