Skip to content

Commit

Permalink
0003560: Unable to use SymmetricDS service in Ubuntu 18.04 / bionic
Browse files Browse the repository at this point in the history
beaver (Systemd in general)
  • Loading branch information
philipmarzullo64 committed Aug 8, 2019
1 parent ffa2568 commit 3b2fca1
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 21 deletions.
Expand Up @@ -50,8 +50,16 @@ public class UnixService extends WrapperService {

private static final String INITD_DIR = "/etc/init.d";

private static final String SYSTEMD_INSTALL_DIR = "/lib/systemd/system";
private static final String SYSTEMD_RUNTIME_DIR = "/run/systemd/system";

private static final String INITD_SCRIPT_START = "start";
private static final String INITD_SCRIPT_STOP = "stop";

private static final String SYSTEMD_SCRIPT_START = "start";
private static final String SYSTEMD_SCRIPT_STOP = "stop";
private static final String SYSTEMD_SCRIPT_ENABLE = "enable";
private static final String SYSTEMD_SCRIPT_DISABLE = "disable";

@Override
protected boolean setWorkingDirectory(String dir) {
Expand All @@ -60,13 +68,57 @@ protected boolean setWorkingDirectory(String dir) {

@Override
public void install() {
String rcDir = getRunCommandDir();
String runFile = INITD_DIR + "/" + config.getName();

if (!isPrivileged()) {
throw new WrapperException(Constants.RC_MUST_BE_ROOT, 0, "Must be root to install");
}

System.out.println("Installing " + config.getName() + " ...");

if(isSystemdRunning()) {
installSystemd();
} else {
installInitd();
}
System.out.println("Done");
}

private boolean isSystemdRunning() {
File systemddir = new File(SYSTEMD_RUNTIME_DIR);
return systemddir.exists();
}

private void installSystemd() {
String runFile = SYSTEMD_INSTALL_DIR + "/" + config.getName() + ".service";
try(FileWriter writer = new FileWriter(runFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(
"/symmetricds.systemd"))))
{
String line = null;
while((line = reader.readLine()) != null) {
line = line.replaceAll("\\$\\{wrapper.description}", config.getDescription());
line = line.replaceAll("\\$\\{wrapper.pidfile}", getWrapperPidFile());
line = line.replaceAll("\\$\\{wrapper.home}", config.getWorkingDirectory().getAbsolutePath());
line = line.replaceAll("\\$\\{wrapper.jarfile}", config.getWrapperJarPath());
line = line.replaceAll("\\$\\{wrapper.java.command}", config.getJavaCommand());
line = line.replaceAll("\\$\\{wrapper.run.as.user}",
config.getRunAsUser() == null || config.getRunAsUser().length() == 0 ? "root" : config.getRunAsUser());
writer.write(line + "\n");
}
} catch(IOException e) {
throw new WrapperException(Constants.RC_FAIL_INSTALL, 0, "Failed while writing run file", e);
}
runServiceCommand(getSystemdCommand(SYSTEMD_SCRIPT_ENABLE, config.getName()));
}

private String getWrapperPidFile() throws IOException {
// Make location absolute (starting with / )
return (config.getWrapperPidFile() != null && config.getWrapperPidFile().startsWith("/") ? config.getWrapperPidFile() :
config.getWorkingDirectory().getCanonicalPath() + "/" + config.getWrapperPidFile());
}

private void installInitd() {
String rcDir = getRunCommandDir();
String runFile = INITD_DIR + "/" + config.getName();

try {
FileWriter writer = new FileWriter(runFile);
Expand Down Expand Up @@ -99,28 +151,42 @@ public void install() {
CLibrary.INSTANCE.symlink(runFile, rcDir + "/rc" + runLevel + ".d/K" + RUN_SEQUENCE_STOP
+ config.getName());
}
System.out.println("Done");
}

@Override
public void uninstall() {
String rcDir = getRunCommandDir();
String runFile = INITD_DIR + "/" + config.getName();

if (!isPrivileged()) {
throw new WrapperException(Constants.RC_MUST_BE_ROOT, 0, "Must be root to uninstall");
}

System.out.println("Uninstalling " + config.getName() + " ...");

if(isSystemdRunning()) {
uninstallSystemd();
} else {
uninstallInitd();
}

for (String runLevel : RUN_LEVELS_START) {
System.out.println("Done");
}

private void uninstallSystemd() {
runServiceCommand(getSystemdCommand(SYSTEMD_SCRIPT_DISABLE, config.getName()));
String runFile = SYSTEMD_INSTALL_DIR + "/" + config.getName() + ".service";
new File(runFile).delete();
}

private void uninstallInitd() {
String rcDir = getRunCommandDir();
String runFile = INITD_DIR + "/" + config.getName();
for (String runLevel : RUN_LEVELS_START) {
new File(rcDir + "/rc" + runLevel + ".d/S" + RUN_SEQUENCE_START + config.getName()).delete();
}
for (String runLevel : RUN_LEVELS_STOP) {
new File(rcDir + "/rc" + runLevel + ".d/K" + RUN_SEQUENCE_STOP + config.getName()).delete();
}
new File(runFile).delete();
System.out.println("Done");
}

protected String getRunCommandDir() {
Expand All @@ -142,7 +208,11 @@ public boolean isPrivileged() {

@Override
public boolean isInstalled() {
return new File(INITD_DIR + "/" + config.getName()).exists();
if(isSystemdRunning()) {
return new File(SYSTEMD_INSTALL_DIR + "/" + config.getName() + ".service").exists();
} else {
return new File(INITD_DIR + "/" + config.getName()).exists();
}
}

@Override
Expand Down Expand Up @@ -223,6 +293,14 @@ private ArrayList<String> getServiceCommand(String command) {
return s;
}

private ArrayList<String> getSystemdCommand(String command, String serviceName) {
ArrayList<String> s = new ArrayList<String>();
s.add("systemctl");
s.add(command);
s.add(serviceName);
return s;
}

@Override
public void start() {
if(isInstalled()) {
Expand All @@ -236,14 +314,26 @@ public void start() {
stopProcesses(true);
System.out.println("Waiting for server to start");

boolean success = true;
if(shouldRunService()) {
success = runServiceCommand(getServiceCommand(INITD_SCRIPT_START));
if(isSystemdRunning()) {
boolean success = true;
if(shouldRunService()) {
success = runServiceCommand(getSystemdCommand(SYSTEMD_SCRIPT_START, config.getName()));
} else {
super.start();
}
if (! success) {
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 0, "Server did not start");
}
} else {
super.start();
}
if (! success) {
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 0, "Server did not start");
boolean success = true;
if(shouldRunService()) {
success = runServiceCommand(getServiceCommand(INITD_SCRIPT_START));
} else {
super.start();
}
if (! success) {
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 0, "Server did not start");
}
}
} else {
super.start();
Expand Down Expand Up @@ -334,11 +424,19 @@ protected void stopProcesses(boolean isStopAbandoned) {
}
}

if(shouldRunService()) {
runServiceCommand(getServiceCommand(INITD_SCRIPT_STOP));
} else {
super.stopProcesses(isStopAbandoned);
}
if(isSystemdRunning()) {
if(shouldRunService()) {
runServiceCommand(getSystemdCommand(SYSTEMD_SCRIPT_STOP, config.getName()));
} else {
super.stopProcesses(isStopAbandoned);
}
} else {
if(shouldRunService()) {
runServiceCommand(getServiceCommand(INITD_SCRIPT_STOP));
} else {
super.stopProcesses(isStopAbandoned);
}
}
} else {
super.stopProcesses(isStopAbandoned);
}
Expand Down
18 changes: 18 additions & 0 deletions symmetric-wrapper/src/main/resources/symmetricds.systemd
@@ -0,0 +1,18 @@
[Unit]
Description=${wrapper.description}
After=network-online.target

[Service]
Type=forking

Environment="SYM_HOME=${wrapper.home}"

ExecStart=${wrapper.java.command} -jar ${wrapper.jarfile} start
ExecStop=${wrapper.java.command} -jar ${wrapper.jarfile} stop
ExecReload=${wrapper.java.command} -jar ${wrapper.jarfile} restart
Restart=no
PIDFile=${wrapper.pidfile}
User=${wrapper.run.as.user}

[Install]
WantedBy=multi-user.target

0 comments on commit 3b2fca1

Please sign in to comment.