Skip to content

Commit 854cacd

Browse files
committed
Fix useStartStopDaemon in launch script and allow config via conf file
Commit 5a1ee6e added support for disabling use of start-stop-daemon via a placeholder in the default launch script. Unfortunately, that placeholder was subsequently broken in 81a4763. This commit reinstates the placeholder and adds tests to verify that all of the placeholders in the launch script can be replaced and that they have the required default values. Furthermore, it also allows the use of start-stop-daemon to be configured via USE_START_STOP_DAEMON in an app’s .conf file. This allows the configuration to be changed after the app has been built. Closes gh-4985
1 parent 44f5082 commit 854cacd

File tree

5 files changed

+95
-17
lines changed

5 files changed

+95
-17
lines changed

spring-boot-docs/src/main/asciidoc/deployment.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,10 @@ the default behavior in a script or on the command line:
541541
that the `stop\|start\|status\|restart` commands work, or to `run` if you just want to
542542
run the script in the foreground.
543543

544+
|`USE_START_STOP_DAEMON`
545+
|If the `start-stop-daemon` command, when it's available, should be used to control the
546+
process. Defaults to `true`.
547+
544548
|`PID_FOLDER`
545549
|The root name of the pid folder (`/var/run` by default).
546550

@@ -606,8 +610,8 @@ for Gradle and to `${project.name}` for Maven.
606610
|The `chkconfig` section of "`INIT INFO`". Defaults to `2345 99 01`.
607611

608612
|`useStartStopDaemon`
609-
|If the start-stop command should be used to control the process when it's available.
610-
Defaults to `true`.
613+
|If the `start-stop-daemon` command, when it's available, should be used to control the
614+
process. Defaults to `true`.
611615
|===
612616

613617

spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@
5050
import org.springframework.boot.ansi.AnsiColor;
5151

5252
import static org.hamcrest.Matchers.containsString;
53+
import static org.hamcrest.Matchers.is;
54+
import static org.hamcrest.Matchers.not;
5355
import static org.junit.Assert.assertThat;
56+
import static org.junit.Assume.assumeThat;
5457

5558
/**
5659
* Integration tests for Spring Boot's launch script on OSs that use SysVinit.
@@ -188,6 +191,13 @@ public void launchWithMultipleJavaOpts() throws Exception {
188191
doLaunch("launch-with-multiple-java-opts.sh");
189192
}
190193

194+
@Test
195+
public void launchWithUseOfStartStopDaemonDisabled() throws Exception {
196+
// CentOS doesn't have start-stop-daemon
197+
assumeThat(this.os, is(not("CentOS")));
198+
doLaunch("launch-with-use-of-start-stop-daemon-disabled.sh");
199+
}
200+
191201
private void doLaunch(String script) throws Exception {
192202
assertThat(doTest(script), containsString("Launched"));
193203
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source ./test-functions.sh
2+
chmod -x $(type -p start-stop-daemon)
3+
echo 'USE_START_STOP_DAEMON=false' > /spring-boot-app.conf
4+
install_service
5+
start_service
6+
await_app
7+
curl -s http://127.0.0.1:8080/

spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ configfile="$(basename "${jarfile%.*}.conf")"
5151
! [[ -x "$PID_FOLDER" ]] && PID_FOLDER="/tmp"
5252
! [[ -x "$LOG_FOLDER" ]] && LOG_FOLDER="/tmp"
5353

54-
# Setup defaults
54+
# Set up defaults
5555
[[ -z "$MODE" ]] && MODE="{{mode:auto}}" # modes are "auto", "service" or "run"
56+
[[ -z "$USE_START_STOP_DAEMON" ]] && USE_START_STOP_DAEMON="{{useStartStopDaemon:true}}"
5657

5758
# Create an identity for log/pid files
5859
if [[ -z "$identity" ]]; then
@@ -146,7 +147,7 @@ do_start() {
146147
chown "$run_user" "$PID_FOLDER"
147148
chown "$run_user" "$pid_file"
148149
chown "$run_user" "$log_file"
149-
if [ "${useStartStopDaemon:-true}" = true ] && type start-stop-daemon > /dev/null 2>&1; then
150+
if [ $USE_START_STOP_DAEMON = true ] && type start-stop-daemon > /dev/null 2>&1; then
150151
arguments=(-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $jarfile $RUN_ARGS "$@")
151152
start-stop-daemon --start --quiet \
152153
--chuid "$run_user" \

spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,8 @@
1717
package org.springframework.boot.loader.tools;
1818

1919
import java.io.File;
20-
import java.util.Properties;
20+
import java.util.HashMap;
21+
import java.util.Map;
2122

2223
import org.junit.Rule;
2324
import org.junit.Test;
@@ -33,6 +34,7 @@
3334
* Tests for {@link DefaultLaunchScript}.
3435
*
3536
* @author Phillip Webb
37+
* @author Andy Wilkinson
3638
*/
3739
public class DefaultLaunchScriptTests {
3840

@@ -44,6 +46,49 @@ public void loadsDefaultScript() throws Exception {
4446
DefaultLaunchScript script = new DefaultLaunchScript(null, null);
4547
String content = new String(script.toByteArray());
4648
assertThat(content, containsString("Spring Boot Startup Script"));
49+
}
50+
51+
@Test
52+
public void initInfoProvidesCanBeReplaced() throws Exception {
53+
assertThatPlaceholderCanBeReplaced("initInfoProvides");
54+
}
55+
56+
@Test
57+
public void initInfoShortDescriptionCanBeReplaced() throws Exception {
58+
assertThatPlaceholderCanBeReplaced("initInfoShortDescription");
59+
}
60+
61+
@Test
62+
public void initInfoDescriptionCanBeReplaced() throws Exception {
63+
assertThatPlaceholderCanBeReplaced("initInfoDescription");
64+
}
65+
66+
@Test
67+
public void initInfoChkconfigCanBeReplaced() throws Exception {
68+
assertThatPlaceholderCanBeReplaced("initInfoChkconfig");
69+
}
70+
71+
@Test
72+
public void modeCanBeReplaced() throws Exception {
73+
assertThatPlaceholderCanBeReplaced("mode");
74+
}
75+
76+
@Test
77+
public void useStartStopDaemonCanBeReplaced() throws Exception {
78+
assertThatPlaceholderCanBeReplaced("useStartStopDaemon");
79+
}
80+
81+
@Test
82+
public void defaultForUseStartStopDaemonIsTrue() throws Exception {
83+
DefaultLaunchScript script = new DefaultLaunchScript(null, null);
84+
String content = new String(script.toByteArray());
85+
assertThat(content, containsString("USE_START_STOP_DAEMON=\"true\""));
86+
}
87+
88+
@Test
89+
public void defaultForModeIsAuto() throws Exception {
90+
DefaultLaunchScript script = new DefaultLaunchScript(null, null);
91+
String content = new String(script.toByteArray());
4792
assertThat(content, containsString("MODE=\"auto\""));
4893
}
4994

@@ -60,10 +105,8 @@ public void loadFromFile() throws Exception {
60105
public void expandVariables() throws Exception {
61106
File file = this.temporaryFolder.newFile();
62107
FileCopyUtils.copy("h{{a}}ll{{b}}".getBytes(), file);
63-
Properties properties = new Properties();
64-
properties.put("a", "e");
65-
properties.put("b", "o");
66-
DefaultLaunchScript script = new DefaultLaunchScript(file, properties);
108+
DefaultLaunchScript script = new DefaultLaunchScript(file,
109+
createProperties("a:e", "b:o"));
67110
String content = new String(script.toByteArray());
68111
assertThat(content, equalTo("hello"));
69112
}
@@ -72,10 +115,8 @@ public void expandVariables() throws Exception {
72115
public void expandVariablesMultiLine() throws Exception {
73116
File file = this.temporaryFolder.newFile();
74117
FileCopyUtils.copy("h{{a}}l\nl{{b}}".getBytes(), file);
75-
Properties properties = new Properties();
76-
properties.put("a", "e");
77-
properties.put("b", "o");
78-
DefaultLaunchScript script = new DefaultLaunchScript(file, properties);
118+
DefaultLaunchScript script = new DefaultLaunchScript(file,
119+
createProperties("a:e", "b:o"));
79120
String content = new String(script.toByteArray());
80121
assertThat(content, equalTo("hel\nlo"));
81122
}
@@ -93,9 +134,8 @@ public void expandVariablesWithDefaults() throws Exception {
93134
public void expandVariablesWithDefaultsOverride() throws Exception {
94135
File file = this.temporaryFolder.newFile();
95136
FileCopyUtils.copy("h{{a:e}}ll{{b:o}}".getBytes(), file);
96-
Properties properties = new Properties();
97-
properties.put("a", "a");
98-
DefaultLaunchScript script = new DefaultLaunchScript(file, properties);
137+
DefaultLaunchScript script = new DefaultLaunchScript(file,
138+
createProperties("a:a"));
99139
String content = new String(script.toByteArray());
100140
assertThat(content, equalTo("hallo"));
101141
}
@@ -109,4 +149,20 @@ public void expandVariablesMissingAreUnchanged() throws Exception {
109149
assertThat(content, equalTo("h{{a}}ll{{b}}"));
110150
}
111151

152+
private void assertThatPlaceholderCanBeReplaced(String placeholder) throws Exception {
153+
DefaultLaunchScript script = new DefaultLaunchScript(null,
154+
createProperties(placeholder + ":__test__"));
155+
String content = new String(script.toByteArray());
156+
assertThat(content, containsString("__test__"));
157+
}
158+
159+
private Map<?, ?> createProperties(String... pairs) {
160+
Map<Object, Object> properties = new HashMap<Object, Object>();
161+
for (String pair : pairs) {
162+
String[] keyValue = pair.split(":");
163+
properties.put(keyValue[0], keyValue[1]);
164+
}
165+
return properties;
166+
}
167+
112168
}

0 commit comments

Comments
 (0)