aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2019-11-20 09:53:04 +0100
committerMartin Polden <mpolden@mpolden.no>2019-11-20 09:53:04 +0100
commit7a75f4c2276d41df6380fb197d34ea36b94898fb (patch)
tree84142729a71abcb4863f00d3faf7e3d3bd9c3f81 /node-admin
parente8e051bad5111fd6a6c4fe03afa638bf145ac5ea (diff)
Add sudo support to SystemCtl
Diffstat (limited to 'node-admin')
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtl.java53
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java22
-rw-r--r--node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTest.java7
3 files changed, 54 insertions, 28 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtl.java
index 88d9ef1040b..4f06ee47504 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtl.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtl.java
@@ -2,6 +2,7 @@
package com.yahoo.vespa.hosted.node.admin.task.util.systemd;
import com.yahoo.vespa.hosted.node.admin.component.TaskContext;
+import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandLine;
import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandResult;
import com.yahoo.vespa.hosted.node.admin.task.util.process.Terminal;
@@ -25,6 +26,7 @@ public class SystemCtl {
private static final Pattern ACTIVE_STATE_PROPERTY_PATTERN = createPropertyPattern("ActiveState");
private final Terminal terminal;
+ private boolean useSudo = false;
private static Pattern createPropertyPattern(String propertyName) {
if (!PROPERTY_NAME_PATTERN.matcher(propertyName).matches()) {
@@ -41,10 +43,20 @@ public class SystemCtl {
this.terminal = terminal;
}
+ /** Call all commands through sudo */
+ public SystemCtl withSudo() {
+ this.useSudo = true;
+ return this;
+ }
+
+ /** Returns whether this is configured to use sudo */
+ public boolean useSudo() {
+ return useSudo;
+ }
+
public void daemonReload(TaskContext taskContext) {
- terminal.newCommandLine(taskContext)
- .add("systemctl", "daemon-reload")
- .execute();
+ newCommandLine(taskContext).add("systemctl", "daemon-reload")
+ .execute();
}
public SystemCtlEnable enable(String unit) { return new SystemCtlEnable(unit); }
@@ -54,7 +66,7 @@ public class SystemCtl {
public SystemCtlRestart restart(String unit) { return new SystemCtlRestart(unit); }
public boolean serviceExists(TaskContext context, String unit) {
- return terminal.newCommandLine(context)
+ return newCommandLine(context)
.add("systemctl", "list-unit-files", unit + ".service").executeSilently()
.mapOutput(output -> {
// Last line of the form: "1 unit files listed."
@@ -69,13 +81,21 @@ public class SystemCtl {
/** Returns true if the unit exists and is active (i.e. running). unit is e.g. "docker". */
public boolean isActive(TaskContext context, String unit) {
- return terminal.newCommandLine(context)
+ return newCommandLine(context)
.add("systemctl", "--quiet", "is-active", unit + ".service")
.ignoreExitCode()
.executeSilently()
.map(CommandResult::getExitCode) == 0;
}
+ private CommandLine newCommandLine(TaskContext context) {
+ var commandLine = terminal.newCommandLine(context);
+ if (useSudo) {
+ commandLine.add("sudo");
+ }
+ return commandLine;
+ }
+
public class SystemCtlEnable extends SystemCtlCommand {
private SystemCtlEnable(String unit) {
super("enable", unit);
@@ -144,21 +164,17 @@ public class SystemCtl {
if (isAlreadyConverged(context)) {
return false;
}
-
- terminal.newCommandLine(context)
- .add("systemctl", command, unit)
- .execute();
-
+ newCommandLine(context).add("systemctl", command, unit)
+ .execute();
return true;
}
/** Returns true if unit is enabled */
boolean isUnitEnabled(TaskContext context) {
- return terminal.newCommandLine(context)
- .add("systemctl", "--quiet", "is-enabled", unit)
- .ignoreExitCode()
- .executeSilently()
- .map(CommandResult::getExitCode) == 0;
+ return newCommandLine(context).add("systemctl", "--quiet", "is-enabled", unit)
+ .ignoreExitCode()
+ .executeSilently()
+ .map(CommandResult::getExitCode) == 0;
}
/**
@@ -167,10 +183,9 @@ public class SystemCtl {
* @return The matched group from the 'systemctl show' output.
*/
String getSystemCtlProperty(TaskContext context, Pattern propertyPattern) {
- return terminal.newCommandLine(context)
- .add("systemctl", "show", unit)
- .executeSilently()
- .mapOutput(output -> extractProperty(output, propertyPattern));
+ return newCommandLine(context).add("systemctl", "show", unit)
+ .executeSilently()
+ .mapOutput(output -> extractProperty(output, propertyPattern));
}
}
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java
index 74ed7ae4a82..56a2b2aeca2 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java
@@ -31,6 +31,10 @@ public class SystemCtlTester extends SystemCtl {
runningUnits.add(unit);
}
+ private void expectCommand(String command, int exitCode, String output) {
+ terminal.expectCommand((useSudo() ? "sudo " : "") + command, exitCode, output);
+ }
+
public static class Expectation {
private final String unit;
@@ -58,22 +62,22 @@ public class SystemCtlTester extends SystemCtl {
/** Expect that given unit will be restarted */
public Expectation toRestart() {
- systemCtl.terminal.expectCommand("systemctl restart " + unit + " 2>&1", 0, "");
+ systemCtl.expectCommand("systemctl restart " + unit + " 2>&1", 0, "");
systemCtl.startUnit(unit);
return this;
}
/** Expect that this will be stopped */
public Expectation toStop() {
- systemCtl.terminal.expectCommand("systemctl stop " + unit + " 2>&1", 0, "");
+ systemCtl.expectCommand("systemctl stop " + unit + " 2>&1", 0, "");
systemCtl.runningUnits.remove(unit);
return this;
}
/** Expect query for state of this */
public Expectation toQueryState() {
- systemCtl.terminal.expectCommand("systemctl --quiet is-active " + unit + ".service 2>&1",
- systemCtl.runningUnits.contains(unit) ? 0 : 1, "");
+ systemCtl.expectCommand("systemctl --quiet is-active " + unit + ".service 2>&1",
+ systemCtl.runningUnits.contains(unit) ? 0 : 1, "");
return this;
}
@@ -88,19 +92,19 @@ public class SystemCtlTester extends SystemCtl {
}
private Expectation toStart(boolean start) {
- systemCtl.terminal.expectCommand("systemctl show " + unit + " 2>&1", 0,
- "ActiveState=" + (start ? "inactive" : "active"));
+ systemCtl.expectCommand("systemctl show " + unit + " 2>&1", 0,
+ "ActiveState=" + (start ? "inactive" : "active"));
if (start) {
- systemCtl.terminal.expectCommand("systemctl start " + unit + " 2>&1", 0, "");
+ systemCtl.expectCommand("systemctl start " + unit + " 2>&1", 0, "");
systemCtl.startUnit(unit);
}
return this;
}
private Expectation toEnable(boolean enable) {
- systemCtl.terminal.expectCommand("systemctl --quiet is-enabled " + unit + " 2>&1", enable ? 1 : 0, "");
+ systemCtl.expectCommand("systemctl --quiet is-enabled " + unit + " 2>&1", enable ? 1 : 0, "");
if (enable) {
- systemCtl.terminal.expectCommand("systemctl enable " + unit + " 2>&1", 0, "");
+ systemCtl.expectCommand("systemctl enable " + unit + " 2>&1", 0, "");
}
return this;
}
diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTest.java
index c5a4ab63f1b..23a68321059 100644
--- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTest.java
+++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTest.java
@@ -143,4 +143,11 @@ public class SystemCtlTest {
}
}
+ @Test
+ public void withSudo() {
+ SystemCtl systemCtl = new SystemCtl(terminal).withSudo();
+ terminal.expectCommand("sudo systemctl restart docker 2>&1", 0, "");
+ assertTrue(systemCtl.restart("docker").converge(taskContext));
+ }
+
}