1818 */
1919package org.elasticsearch.gradle.test
2020
21+ import org.apache.tools.ant.taskdefs.condition.Os
2122import org.elasticsearch.gradle.VersionProperties
2223import org.gradle.api.InvalidUserDataException
2324import org.gradle.api.Project
@@ -48,6 +49,9 @@ class NodeInfo {
4849 /* * config directory */
4950 File confDir
5051
52+ /* * THE config file */
53+ File configFile
54+
5155 /* * working directory for the node process */
5256 File cwd
5357
@@ -66,8 +70,14 @@ class NodeInfo {
6670 /* * arguments to start the node with */
6771 List<String > args
6872
73+ /* * Executable to run the bin/elasticsearch with, either cmd or sh */
74+ String executable
75+
6976 /* * Path to the elasticsearch start script */
70- String esScript
77+ File esScript
78+
79+ /* * script to run when running in the background */
80+ File wrapperScript
7181
7282 /* * buffer for ant output when starting this node */
7383 ByteArrayOutputStream buffer = new ByteArrayOutputStream ()
@@ -81,35 +91,74 @@ class NodeInfo {
8191 pidFile = new File (baseDir, ' es.pid' )
8292 homeDir = homeDir(baseDir, config. distribution)
8393 confDir = confDir(baseDir, config. distribution)
94+ configFile = new File (confDir, ' elasticsearch.yml' )
8495 cwd = new File (baseDir, " cwd" )
8596 failedMarker = new File (cwd, ' run.failed' )
8697 startLog = new File (cwd, ' run.log' )
8798 pluginsTmpDir = new File (baseDir, " plugins tmp" )
8899
100+ args = []
101+ if (Os . isFamily(Os . FAMILY_WINDOWS )) {
102+ executable = ' cmd'
103+ args. add(' /C' )
104+ args. add(' "' ) // quote the entire command
105+ wrapperScript = new File (cwd, " run.bat" )
106+ esScript = new File (homeDir, ' bin/elasticsearch.bat' )
107+ } else {
108+ executable = ' sh'
109+ wrapperScript = new File (cwd, " run" )
110+ esScript = new File (homeDir, ' bin/elasticsearch' )
111+ }
112+ if (config. daemonize) {
113+ args. add(" ${ wrapperScript} " )
114+ } else {
115+ args. add(" ${ esScript} " )
116+ }
117+
89118 env = [
90119 ' JAVA_HOME' : project. javaHome,
91120 ' ES_GC_OPTS' : config. jvmArgs // we pass these with the undocumented gc opts so the argline can set gc, etc
92121 ]
93- args = config. systemProperties. collect { key , value -> " -D${ key} =${ value} " }
122+ args. addAll( config. systemProperties. collect { key , value -> " -D${ key} =${ value} " })
94123 for (Map.Entry < String , String > property : System . properties. entrySet()) {
95124 if (property. getKey(). startsWith(' es.' )) {
96125 args. add(" -D${ property.getKey()} =${ property.getValue()} " )
97126 }
98127 }
99- args. add(" -Des.default.path.conf=${ confDir} " )
100- // running with cmd on windows will look for this with the .bat extension
101- esScript = new File (homeDir, ' bin/elasticsearch' ). toString()
128+ args. add(" -Des.path.conf=${ confDir} " )
129+ if (Os . isFamily(Os . FAMILY_WINDOWS )) {
130+ args. add(' "' ) // end the entire command, quoted
131+ }
102132 }
103133
104134 /* * Returns debug string for the command that started this node. */
105135 String getCommandString () {
106- String esCommandString = " Elasticsearch node ${ nodeNum} command: ${ esScript} "
107- esCommandString + = args. join(' ' )
108- esCommandString + = ' \n environment:'
109- env. each { k , v -> esCommandString + = " \n ${ k} : ${ v} " }
136+ String esCommandString = " \n Node ${ nodeNum} configuration:\n "
137+ esCommandString + = " |-----------------------------------------\n "
138+ esCommandString + = " | cwd: ${ cwd} \n "
139+ esCommandString + = " | command: ${ executable} ${ args.join(' ')} \n "
140+ esCommandString + = ' | environment:\n '
141+ env. each { k , v -> esCommandString + = " | ${ k} : ${ v} \n " }
142+ if (config. daemonize) {
143+ esCommandString + = " |\n | [${ wrapperScript.name} ]\n "
144+ wrapperScript. eachLine(' UTF-8' , { line -> esCommandString + = " ${ line} \n " })
145+ }
146+ esCommandString + = ' |\n | [elasticsearch.yml]\n '
147+ configFile. eachLine(' UTF-8' , { line -> esCommandString + = " | ${ line} \n " })
148+ esCommandString + = " |-----------------------------------------"
110149 return esCommandString
111150 }
112151
152+ void writeWrapperScript () {
153+ String argsPasser = ' "$@"'
154+ String exitMarker = " ; if [ \$ ? != 0 ]; then touch run.failed; fi"
155+ if (Os . isFamily(Os . FAMILY_WINDOWS )) {
156+ argsPasser = ' %*'
157+ exitMarker = " \r\n if \" %errorlevel%\" neq \" 0\" ( type nul >> run.failed )"
158+ }
159+ wrapperScript. setText(" \" ${ esScript} \" ${ argsPasser} > run.log 2>&1 ${ exitMarker} " , ' UTF-8' )
160+ }
161+
113162 /* * Returns the http port for this node */
114163 int httpPort () {
115164 return config. baseHttpPort + nodeNum
0 commit comments