IGNITE-19254 Move ignite-ssh to extensions (#208)
diff --git a/modules/ssh-ext/README.txt b/modules/ssh-ext/README.txt
new file mode 100644
index 0000000..a0876e7
--- /dev/null
+++ b/modules/ssh-ext/README.txt
@@ -0,0 +1,32 @@
+Apache Ignite SSH Module
+------------------------
+
+Apache Ignite SSH module provides capabilities to start Apache Ignite nodes on remote machines via SSH.
+
+To enable SSH module when starting a standalone node, move 'optional/ignite-ssh' folder to
+'libs' folder before running 'ignite.{sh|bat}' script. The content of the module folder will
+be added to classpath in this case.
+
+Importing SSH Module In Maven Project
+-------------------------------------
+
+If you are using Maven to manage dependencies of your project, you can add SSH module
+dependency like this (replace '${ignite.version}' with actual Ignite version you are
+interested in):
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+ http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ ...
+ <dependencies>
+ ...
+ <dependency>
+ <groupId>org.apache.ignite</groupId>
+ <artifactId>ignite-ssh</artifactId>
+ <version>${ignite.version}</version>
+ </dependency>
+ ...
+ </dependencies>
+ ...
+</project>
diff --git a/modules/ssh-ext/licenses/apache-2.0.txt b/modules/ssh-ext/licenses/apache-2.0.txt
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/modules/ssh-ext/licenses/apache-2.0.txt
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/modules/ssh-ext/pom.xml b/modules/ssh-ext/pom.xml
new file mode 100644
index 0000000..a8d24c8
--- /dev/null
+++ b/modules/ssh-ext/pom.xml
@@ -0,0 +1,100 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<!--
+ POM file.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.ignite</groupId>
+ <artifactId>ignite-parent-ext-internal</artifactId>
+ <version>1</version>
+ <relativePath>../../parent-internal/pom.xml</relativePath>
+ </parent>
+
+ <artifactId>ignite-ssh-ext</artifactId>
+
+ <url>http://ignite.apache.org</url>
+
+ <dependencies>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>ignite-core</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>com.jcraft</groupId>
+ <artifactId>jsch</artifactId>
+ <version>${jsch.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>ignite-spring</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>ignite-log4j2</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>ignite-core</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>ignite-tools</artifactId>
+ <version>${ignite.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <!-- Generate the OSGi MANIFEST.MF for this bundle. -->
+ <!-- This is a fragment because it's an internal processor module. -->
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <configuration>
+ <instructions>
+ <Fragment-Host>${project.groupId}.ignite-core</Fragment-Host>
+ </instructions>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-deploy-plugin</artifactId>
+ <version>2.8.2</version>
+ <configuration>
+ <skip>false</skip>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/modules/ssh-ext/src/main/java/org/apache/ignite/internal/util/nodestart/IgniteSshHelperImpl.java b/modules/ssh-ext/src/main/java/org/apache/ignite/internal/util/nodestart/IgniteSshHelperImpl.java
new file mode 100644
index 0000000..c2370b6
--- /dev/null
+++ b/modules/ssh-ext/src/main/java/org/apache/ignite/internal/util/nodestart/IgniteSshHelperImpl.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.util.nodestart;
+
+/**
+ * Implementation of {@link IgniteSshHelper}.
+ */
+public class IgniteSshHelperImpl implements IgniteSshHelper {
+ /** {@inheritDoc} */
+ @Override public StartNodeCallable nodeStartCallable(IgniteRemoteStartSpecification spec, int timeout) {
+ return new StartNodeCallableImpl(spec, timeout);
+ }
+}
diff --git a/modules/ssh-ext/src/main/java/org/apache/ignite/internal/util/nodestart/StartNodeCallableImpl.java b/modules/ssh-ext/src/main/java/org/apache/ignite/internal/util/nodestart/StartNodeCallableImpl.java
new file mode 100644
index 0000000..57bb2eb
--- /dev/null
+++ b/modules/ssh-ext/src/main/java/org/apache/ignite/internal/util/nodestart/StartNodeCallableImpl.java
@@ -0,0 +1,598 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.util.nodestart;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.InterruptedIOException;
+import java.io.PrintStream;
+import java.nio.charset.Charset;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.UUID;
+import java.util.regex.Pattern;
+import com.jcraft.jsch.ChannelExec;
+import com.jcraft.jsch.ChannelShell;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
+import com.jcraft.jsch.Session;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.ClusterStartNodeResult;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.internal.cluster.ClusterStartNodeResultImpl;
+import org.apache.ignite.internal.processors.timeout.GridTimeoutObject;
+import org.apache.ignite.internal.processors.timeout.GridTimeoutObjectAdapter;
+import org.apache.ignite.internal.processors.timeout.GridTimeoutProcessor;
+import org.apache.ignite.internal.util.typedef.X;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.SB;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.resources.IgniteInstanceResource;
+import org.apache.ignite.resources.LoggerResource;
+
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_SSH_HOST;
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_SSH_USER_NAME;
+
+/**
+ * SSH-based node starter.
+ */
+public class StartNodeCallableImpl implements StartNodeCallable {
+ /** Default Ignite home path for Windows (taken from environment variable). */
+ private static final String DFLT_IGNITE_HOME_WIN = "%IGNITE_HOME%";
+
+ /** Default Ignite home path for Linux (taken from environment variable). */
+ private static final String DFLT_IGNITE_HOME_LINUX = "$IGNITE_HOME";
+
+ /** Windows console encoding */
+ private static final String WINDOWS_ENCODING = "IBM866";
+
+ /** Default start script path for Windows. */
+ private static final String DFLT_SCRIPT_WIN = "bin\\ignite.bat -v -np";
+
+ /** Default start script path for Linux. */
+ private static final String DFLT_SCRIPT_LINUX = "bin/ignite.sh -v";
+
+ /** Date format for log file name. */
+ private static final DateTimeFormatter FILE_NAME_DATE_FORMAT =
+ DateTimeFormatter.ofPattern("MM-dd-yyyy--HH-mm-ss").withZone(ZoneId.systemDefault());
+
+ /** Used to register successful node start in log */
+ private static final String SUCCESSFUL_START_MSG = "Successfully bound to TCP port";
+
+ /** */
+ private static final long EXECUTE_WAIT_TIME = 1000;
+
+ /** */
+ private static final long NODE_START_CHECK_PERIOD = 2000;
+
+ /** */
+ private static final long NODE_START_CHECK_LIMIT = 15;
+
+ /** Specification. */
+ private final IgniteRemoteStartSpecification spec;
+
+ /** Connection timeout. */
+ private final int timeout;
+
+ /** Timeout processor. */
+ private GridTimeoutProcessor proc;
+
+ /** Logger. */
+ @LoggerResource
+ private transient IgniteLogger log;
+
+ /** Ignite. */
+ @IgniteInstanceResource
+ private transient Ignite ignite;
+
+ /**
+ * Required by Externalizable.
+ */
+ public StartNodeCallableImpl() {
+ spec = null;
+
+ timeout = 0;
+
+ assert false;
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param spec Specification.
+ * @param timeout Connection timeout.
+ */
+ public StartNodeCallableImpl(IgniteRemoteStartSpecification spec, int timeout) {
+ assert spec != null;
+
+ this.spec = spec;
+
+ this.timeout = timeout;
+ }
+
+ /** {@inheritDoc} */
+ @Override public ClusterStartNodeResult call() {
+ JSch ssh = new JSch();
+
+ Session ses = null;
+
+ try {
+ proc = ((IgniteEx)ignite).context().timeout();
+
+ if (spec.key() != null)
+ ssh.addIdentity(spec.key().getAbsolutePath());
+
+ ses = ssh.getSession(spec.username(), spec.host(), spec.port());
+
+ if (spec.password() != null)
+ ses.setPassword(spec.password());
+
+ ses.setConfig("StrictHostKeyChecking", "no");
+
+ ses.connect(timeout);
+
+ boolean win = isWindows(ses);
+
+ info("Windows mode: " + win, spec.logger(), log);
+
+ char separator = win ? '\\' : '/';
+
+ spec.fixPaths(separator);
+
+ String igniteHome = spec.igniteHome();
+
+ if (igniteHome == null)
+ igniteHome = win ? DFLT_IGNITE_HOME_WIN : DFLT_IGNITE_HOME_LINUX;
+
+ String script = spec.script();
+
+ if (script == null)
+ script = win ? DFLT_SCRIPT_WIN : DFLT_SCRIPT_LINUX;
+
+ String cfg = spec.configuration();
+
+ if (cfg == null)
+ cfg = "";
+
+ String id = FILE_NAME_DATE_FORMAT.format(Instant.now()) + '-' +
+ UUID.randomUUID().toString().substring(0, 8);
+
+ String scriptOutputFileName = id + ".log";
+
+ int spaceIdx = script.indexOf(' ');
+
+ String scriptPath = spaceIdx > -1 ? script.substring(0, spaceIdx) : script;
+
+ String scriptArgs = spaceIdx > -1 ? script.substring(spaceIdx + 1) : "";
+
+ String rmtLogArgs = buildRemoteLogArguments(spec.username(), spec.host());
+
+ String scriptOutputDir;
+
+ String dfltTmpDir = igniteHome + separator + "work" + separator + "log";
+
+ if (win) {
+ String tmpDir = env(ses, "%TMPDIR%", dfltTmpDir, WINDOWS_ENCODING);
+
+ if ("%TMPDIR%".equals(tmpDir))
+ tmpDir = dfltTmpDir;
+
+ scriptOutputDir = tmpDir + "\\ignite-startNodes";
+ }
+ else { // Assume Unix.
+ String logDir = env(ses, "$TMPDIR", dfltTmpDir);
+
+ scriptOutputDir = logDir + "/ignite-startNodes";
+ }
+
+ shell(ses, "mkdir " + scriptOutputDir);
+
+ String scriptOutputPath = scriptOutputDir + separator + scriptOutputFileName;
+
+ String findSuccess;
+
+ if (win) {
+ String scriptFileName = scriptOutputDir + '\\' + id + ".bat";
+
+ String createScript = new SB()
+ .a("echo \"").a(igniteHome).a('\\').a(scriptPath).a("\" ")
+ .a(scriptArgs)
+ .a(!cfg.isEmpty() ? " \"" : "").a(cfg).a(!cfg.isEmpty() ? "\"" : "")
+ .a(rmtLogArgs)
+ .a(" ^> ").a(scriptOutputPath).a(" ^2^>^&^1")
+ .a(" > ").a(scriptFileName)
+ .toString();
+
+ info("Create script with command: " + createScript, spec.logger(), log);
+
+ shell(ses, createScript);
+
+ try {
+ String createTask = new SB()
+ .a("schtasks /create /f /sc onstart")
+ .a(" /ru ").a(spec.username())
+ .a(" /rp ").a(spec.password())
+ .a(" /tn ").a(id)
+ .a(" /np /tr \"").a(scriptFileName).a('\"')
+ .toString();
+
+ info("Create task with command: " + createTask, spec.logger(), log);
+
+ shell(ses, createTask);
+
+ String runTask = "schtasks /run /i /tn " + id;
+
+ info("Run task with command: " + runTask, spec.logger(), log);
+
+ shell(ses, runTask);
+ }
+ finally {
+ String deleteTask = "schtasks /delete /f /tn " + id;
+
+ info("Delete task with command: " + deleteTask, spec.logger(), log);
+
+ shell(ses, deleteTask);
+ }
+
+ findSuccess = "find \"" + SUCCESSFUL_START_MSG + "\" " + scriptOutputPath;
+ }
+ else { // Assume Unix.
+ // Mac os don't support ~ in double quotes. Trying get home path from remote system.
+ if (igniteHome.startsWith("~")) {
+ String homeDir = env(ses, "$HOME", "~");
+
+ igniteHome = igniteHome.replaceFirst("~", homeDir);
+ }
+
+ String prepareStartCmd = new SB()
+ // Ensure diagnostics in the log even in case if start node breaks silently.
+ .a("nohup echo \"Preparing to start remote node...\" > ")
+ .a(scriptOutputDir).a('/').a(scriptOutputFileName).a(" 2>& 1 &")
+ .toString();
+
+ shell(ses, prepareStartCmd);
+
+ String startNodeCmd = new SB()
+ // Console output is consumed, started nodes must use Ignite file appenders for log.
+ .a("nohup ")
+ .a("\"").a(igniteHome).a('/').a(scriptPath).a("\"")
+ .a(" ").a(scriptArgs)
+ .a(!cfg.isEmpty() ? " \"" : "").a(cfg).a(!cfg.isEmpty() ? "\"" : "")
+ .a(rmtLogArgs)
+ .a(" > ").a(scriptOutputDir).a('/').a(scriptOutputFileName).a(" 2>& 1 &")
+ .toString();
+
+ info("Starting remote node with SSH command: " + startNodeCmd, spec.logger(), log);
+
+ // Execute command via ssh and wait until id of new process will be found in the output.
+ shell(ses, startNodeCmd, "\\[(\\d)\\] (\\d)+");
+
+ findSuccess = "grep \"" + SUCCESSFUL_START_MSG + "\" " + scriptOutputPath;
+ }
+
+ for (int i = 0; i < NODE_START_CHECK_LIMIT; ++i) {
+ Thread.sleep(NODE_START_CHECK_PERIOD);
+
+ String res = exec(ses, findSuccess, win ? WINDOWS_ENCODING : null);
+
+ info("Find result: " + res, spec.logger(), log);
+
+ if (res != null && res.contains(SUCCESSFUL_START_MSG))
+ return new ClusterStartNodeResultImpl(spec.host(), true, null);
+ }
+
+ return new ClusterStartNodeResultImpl(spec.host(), false, "Remote node could not start. " +
+ "See log for details: " + scriptOutputPath);
+ }
+ catch (IgniteInterruptedCheckedException e) {
+ return new ClusterStartNodeResultImpl(spec.host(), false, e.getMessage());
+ }
+ catch (Exception e) {
+ return new ClusterStartNodeResultImpl(spec.host(), false, X.getFullStackTrace(e));
+ }
+ finally {
+ if (ses != null && ses.isConnected())
+ ses.disconnect();
+ }
+ }
+
+ /**
+ * Executes command using {@code shell} channel.
+ *
+ * @param ses SSH session.
+ * @param cmd Command.
+ * @throws JSchException In case of SSH error.
+ * @throws IOException If IO error occurs.
+ * @throws IgniteInterruptedCheckedException If thread was interrupted while waiting.
+ */
+ private void shell(Session ses, String cmd) throws JSchException, IOException, IgniteInterruptedCheckedException {
+ shell(ses, cmd, null);
+ }
+
+ /**
+ * Executes command using {@code shell} channel.
+ *
+ * @param ses SSH session.
+ * @param cmd Command.
+ * @param regexp Regular expression to wait until it will be found in stream from node.
+ * @throws JSchException In case of SSH error.
+ * @throws IOException If IO error occurs.
+ * @throws IgniteInterruptedCheckedException If thread was interrupted while waiting.
+ */
+ private void shell(Session ses, String cmd, String regexp)
+ throws JSchException, IOException, IgniteInterruptedCheckedException {
+ ChannelShell ch = null;
+
+ GridTimeoutObject to = null;
+
+ try {
+ ch = (ChannelShell)ses.openChannel("shell");
+
+ ch.connect();
+
+ try (PrintStream out = new PrintStream(ch.getOutputStream(), true)) {
+ out.println(cmd);
+ }
+
+ if (regexp != null) {
+ Pattern ptrn = Pattern.compile(regexp);
+
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(ch.getInputStream()))) {
+ String line;
+
+ boolean first = true;
+
+ while ((line = reader.readLine()) != null) {
+ if (ptrn.matcher(line).find()) {
+ // Wait for a while until process from regexp really will be started.
+ U.sleep(50);
+
+ break;
+ }
+ else if (first) {
+ to = initTimer(cmd);
+
+ first = false;
+ }
+ }
+ }
+ catch (InterruptedIOException ignore) {
+ // No-op.
+ }
+ finally {
+ if (to != null) {
+ boolean r = proc.removeTimeoutObject(to);
+
+ assert r || to.endTime() <= U.currentTimeMillis() : "Timeout object was not removed: " + to;
+ }
+ }
+
+ }
+ else
+ U.sleep(EXECUTE_WAIT_TIME);
+ }
+ finally {
+ if (ch != null && ch.isConnected())
+ ch.disconnect();
+ }
+ }
+
+ /**
+ * Checks whether host is running Windows OS.
+ *
+ * @param ses SSH session.
+ * @return Whether host is running Windows OS.
+ * @throws JSchException In case of SSH error.
+ */
+ private boolean isWindows(Session ses) throws JSchException {
+ try {
+ return exec(ses, "cmd.exe") != null;
+ }
+ catch (IOException ignored) {
+ return false;
+ }
+ }
+
+ /**
+ * Gets the value of the specified environment variable.
+ *
+ * @param ses SSH session.
+ * @param name environment variable name.
+ * @param dflt default value.
+ * @return environment variable value.
+ * @throws JSchException In case of SSH error.
+ */
+ private String env(Session ses, String name, String dflt) throws JSchException {
+ return env(ses, name, dflt, null);
+ }
+
+ /**
+ * Gets the value of the specified environment variable.
+ *
+ * @param ses SSH session.
+ * @param name environment variable name.
+ * @param dflt default value.
+ * @param encoding Process output encoding, {@code null} for default charset encoding.
+ * @return environment variable value.
+ * @throws JSchException In case of SSH error.
+ */
+ private String env(Session ses, String name, String dflt, String encoding) throws JSchException {
+ try {
+ String res = exec(ses, "echo " + name, encoding);
+
+ if (res == null)
+ return dflt;
+
+ res = res.trim();
+
+ return res.isEmpty() ? dflt : res;
+ }
+ catch (IOException ignored) {
+ return dflt;
+ }
+ }
+
+ /**
+ * Executes command using {@code exec} channel.
+ *
+ * @param ses SSH session.
+ * @param cmd Command.
+ * @return Output result.
+ * @throws JSchException In case of SSH error.
+ * @throws IOException If failed.
+ */
+ private String exec(Session ses, String cmd) throws JSchException, IOException {
+ return exec(ses, cmd, null);
+ }
+
+ /**
+ * Executes command using {@code exec} channel with setting encoding.
+ *
+ * @param ses SSH session.
+ * @param cmd Command.
+ * @param encoding Process output encoding, {@code null} for default charset encoding.
+ * @return Output result.
+ * @throws JSchException In case of SSH error.
+ * @throws IOException If failed.
+ */
+ private String exec(Session ses, final String cmd, String encoding) throws JSchException, IOException {
+ ChannelExec ch = null;
+
+ try {
+ ch = (ChannelExec)ses.openChannel("exec");
+
+ ch.setCommand(cmd);
+
+ ch.connect();
+
+ if (encoding == null)
+ encoding = Charset.defaultCharset().name();
+
+ GridTimeoutObject to = null;
+
+ SB out = null;
+
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(ch.getInputStream(), encoding))) {
+ String line;
+
+ boolean first = true;
+
+ while ((line = reader.readLine()) != null) {
+ if (first)
+ out = new SB();
+ else
+ out.a('\n');
+
+ out.a(line);
+
+ if (first) {
+ to = initTimer(cmd);
+
+ first = false;
+ }
+ }
+ }
+ catch (InterruptedIOException ignore) {
+ // No-op.
+ }
+ finally {
+ if (to != null) {
+ boolean r = proc.removeTimeoutObject(to);
+
+ assert r || to.endTime() <= U.currentTimeMillis() : "Timeout object was not removed: " + to;
+ }
+ }
+
+ return out == null ? null : out.toString();
+ }
+ finally {
+ if (ch != null && ch.isConnected())
+ ch.disconnect();
+ }
+ }
+
+ /**
+ * Initialize timer to wait for command execution.
+ *
+ * @param cmd Command to log.
+ */
+ private GridTimeoutObject initTimer(String cmd) {
+ GridTimeoutObject to = new GridTimeoutObjectAdapter(EXECUTE_WAIT_TIME) {
+ private final Thread thread = Thread.currentThread();
+
+ @Override public void onTimeout() {
+ thread.interrupt();
+ }
+
+ @Override public String toString() {
+ return S.toString("GridTimeoutObject", "cmd", cmd, "thread", thread);
+ }
+ };
+
+ boolean wasAdded = proc.addTimeoutObject(to);
+
+ assert wasAdded : "Timeout object was not added: " + to;
+
+ return to;
+ }
+
+ /**
+ * Builds ignite.sh attributes to set up SSH username and password and log directory for started node.
+ *
+ * @param username SSH user name.
+ * @param host Host.
+ * @return {@code ignite.sh} script arguments.
+ */
+ private String buildRemoteLogArguments(String username, String host) {
+ assert username != null;
+ assert host != null;
+
+ SB sb = new SB();
+
+ sb.a(" -J-D").a(IGNITE_SSH_HOST).a("=\"").a(host).a("\"").
+ a(" -J-D").a(IGNITE_SSH_USER_NAME).a("=\"").a(username).a("\"");
+
+ return sb.toString();
+ }
+
+ /**
+ * @param log Logger.
+ * @return This callable for chaining method calls.
+ */
+ public StartNodeCallable setLogger(IgniteLogger log) {
+ this.log = log;
+
+ return this;
+ }
+
+ /**
+ * Log info message to loggers.
+ *
+ * @param msg Message text.
+ * @param loggers Loggers.
+ */
+ private void info(String msg, IgniteLogger... loggers) {
+ for (IgniteLogger logger : loggers)
+ if (logger != null && logger.isInfoEnabled())
+ logger.info(msg);
+ }
+}
diff --git a/modules/ssh-ext/src/test/java/org/apache/ignite/internal/IgniteNodeStartUtilsSelfTest.java b/modules/ssh-ext/src/test/java/org/apache/ignite/internal/IgniteNodeStartUtilsSelfTest.java
new file mode 100644
index 0000000..0173160
--- /dev/null
+++ b/modules/ssh-ext/src/test/java/org/apache/ignite/internal/IgniteNodeStartUtilsSelfTest.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal;
+
+import java.io.File;
+import java.util.Collection;
+import java.util.Map;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.CFG;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.HOST;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.IGNITE_HOME;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.KEY;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.NODES;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.PASSWD;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.PORT;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.SCRIPT;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.UNAME;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.parseFile;
+
+/**
+ * Tests for {@link org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils}.
+ */
+public class IgniteNodeStartUtilsSelfTest extends GridCommonAbstractTest {
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testParseFile() throws Exception {
+ File file = U.resolveIgnitePath("modules/core/src/test/config/start-nodes.ini");
+
+ IgniteBiTuple<Collection<Map<String, Object>>, Map<String, Object>> t = parseFile(file);
+
+ assert t != null;
+
+ Collection<Map<String, Object>> hosts = t.get1();
+
+ assert hosts != null;
+ assert hosts.size() == 2;
+
+ for (Map<String, Object> host : hosts) {
+ assert host != null;
+
+ assert "192.168.1.1".equals(host.get(HOST)) || "192.168.1.2".equals(host.get(HOST));
+
+ if ("192.168.1.1".equals(host.get(HOST))) {
+ assert (Integer)host.get(PORT) == 1;
+ assert "uname1".equals(host.get(UNAME));
+ assert "passwd1".equals(host.get(PASSWD));
+ assert new File("key1").equals(host.get(KEY));
+ assert (Integer)host.get(NODES) == 1;
+ assert "ggHome1".equals(host.get(IGNITE_HOME));
+ assert "cfg1".equals(host.get(CFG));
+ assert "script1".equals(host.get(SCRIPT));
+ }
+ else if ("192.168.1.2".equals(host.get(HOST))) {
+ assert (Integer)host.get(PORT) == 2;
+ assert "uname2".equals(host.get(UNAME));
+ assert "passwd2".equals(host.get(PASSWD));
+ assert new File("key2").equals(host.get(KEY));
+ assert (Integer)host.get(NODES) == 2;
+ assert "ggHome2".equals(host.get(IGNITE_HOME));
+ assert "cfg2".equals(host.get(CFG));
+ assert "script2".equals(host.get(SCRIPT));
+ }
+ }
+
+ Map<String, Object> dflts = t.get2();
+
+ assert dflts != null;
+
+ assert (Integer)dflts.get(PORT) == 3;
+ assert "uname3".equals(dflts.get(UNAME));
+ assert "passwd3".equals(dflts.get(PASSWD));
+ assert new File("key3").equals(dflts.get(KEY));
+ assert (Integer)dflts.get(NODES) == 3;
+ assert "ggHome3".equals(dflts.get(IGNITE_HOME));
+ assert "cfg3".equals(dflts.get(CFG));
+ assert "script3".equals(dflts.get(SCRIPT));
+ }
+}
diff --git a/modules/ssh-ext/src/test/java/org/apache/ignite/internal/IgniteProjectionStartStopRestartSelfTest.java b/modules/ssh-ext/src/test/java/org/apache/ignite/internal/IgniteProjectionStartStopRestartSelfTest.java
new file mode 100644
index 0000000..7e330c8
--- /dev/null
+++ b/modules/ssh-ext/src/test/java/org/apache/ignite/internal/IgniteProjectionStartStopRestartSelfTest.java
@@ -0,0 +1,824 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal;
+
+import java.io.File;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCluster;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.cluster.ClusterStartNodeResult;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.events.Event;
+import org.apache.ignite.events.EventType;
+import org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.internal.util.typedef.X;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteClosure;
+import org.apache.ignite.lang.IgnitePredicate;
+import org.apache.ignite.testframework.config.GridTestProperties;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.jetbrains.annotations.Nullable;
+import org.junit.Test;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.CFG;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.DFLT_TIMEOUT;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.IGNITE_HOME;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.KEY;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.NODES;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.PASSWD;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.SCRIPT;
+import static org.apache.ignite.internal.util.nodestart.IgniteNodeStartUtils.UNAME;
+
+/**
+ * Tests for {@code startNodes(..)}, {@code stopNodes(..)}
+ * and {@code restartNodes(..)} methods.
+ * <p>
+ * Environment (obtained via {@link System#getenv(String)}) or, alternatively, {@code tests.properties} file must
+ * specify either username and password or private key path in the environment properties (@code test.ssh.username},
+ * {@code test.ssh.password}, {@code ssh.key} or in test file entries {@code ssh.username} {@code ssh.password},
+ * {@code ssh.key}respectively.</p>
+ * <p>
+ * Configured target host must run ssh server and accept ssh connections at configured port from user with specified
+ * credentials.</p>
+ */
+@SuppressWarnings("ConstantConditions")
+public class IgniteProjectionStartStopRestartSelfTest extends GridCommonAbstractTest {
+ /** */
+ private static final String SSH_UNAME = getProperty("test.ssh.username", "ssh.username");
+
+ /** */
+ private static final String SSH_PWD = getProperty("test.ssh.password", "ssh.password");
+
+ /** */
+ private static final String SSH_KEY = getProperty("ssh.key", "ssh.key");
+
+ /** */
+ private static final String CUSTOM_SCRIPT_WIN = "modules/core/src/test/bin/start-nodes-custom.bat";
+
+ /** */
+ private static final String CUSTOM_SCRIPT_LINUX = "modules/core/src/test/bin/start-nodes-custom.sh";
+
+ /** */
+ private static final String CFG_NO_ATTR = "modules/core/src/test/config/spring-start-nodes.xml";
+
+ /** */
+ private static final String CFG_ATTR = "modules/core/src/test/config/spring-start-nodes-attr.xml";
+
+ /** */
+ private static final String CUSTOM_CFG_ATTR_KEY = "grid.node.ssh.started";
+
+ /** */
+ private static final String CUSTOM_CFG_ATTR_VAL = "true";
+
+ /** */
+ private static final long WAIT_TIMEOUT = 90 * 1000;
+
+ /** */
+ private String pwd;
+
+ /** */
+ private File key;
+
+ /** */
+ private Ignite ignite;
+
+ /** */
+ private static final String HOST = "127.0.0.1";
+
+ /** */
+ private final AtomicInteger joinedCnt = new AtomicInteger();
+
+ /** */
+ private final AtomicInteger leftCnt = new AtomicInteger();
+
+ /** */
+ private volatile CountDownLatch joinedLatch;
+
+ /** */
+ private volatile CountDownLatch leftLatch;
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+ return super.getConfiguration(igniteInstanceName).setIncludeEventTypes(EventType.EVTS_ALL);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() {
+ if (SSH_KEY != null) {
+ key = new File(SSH_KEY);
+
+ assert key.exists() : "Private key doesn't exist: " + key.getAbsolutePath();
+ assert key.isFile() : "Private key is not a file: " + key.getAbsolutePath();
+ }
+ else
+ pwd = SSH_PWD;
+
+ log.info("Username: " + SSH_UNAME);
+ log.info("Password: " + pwd);
+ log.info("Key path: " + key);
+
+ ignite = G.start(CFG_NO_ATTR);
+
+ ignite.events().localListen((IgnitePredicate<Event>)evt -> {
+ info("Received event: " + evt.shortDisplay());
+
+ if (evt.type() == EVT_NODE_JOINED) {
+ joinedCnt.incrementAndGet();
+
+ if (joinedLatch != null)
+ joinedLatch.countDown();
+ }
+ else if (evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED) {
+ leftCnt.incrementAndGet();
+
+ if (leftLatch != null)
+ leftLatch.countDown();
+ }
+
+ return true;
+ }, EVT_NODE_JOINED, EVT_NODE_LEFT, EVT_NODE_FAILED);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+
+ joinedLatch = null;
+ leftLatch = null;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected long getTestTimeout() {
+ return 90 * 1000;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStartOneNode() throws Exception {
+ joinedLatch = new CountDownLatch(1);
+
+ Collection<ClusterStartNodeResult> res =
+ startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 1, U.getIgniteHome(), CFG_NO_ATTR, null),
+ false, 0, 16);
+
+ assert res.size() == 1;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert joinedCnt.get() == 1;
+ assert leftCnt.get() == 0;
+
+ assert ignite.cluster().nodes().size() == 2;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStartThreeNodes() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ Collection<ClusterStartNodeResult> res =
+ startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 3, U.getIgniteHome(), CFG_NO_ATTR, null),
+ false, DFLT_TIMEOUT, 1);
+
+ assert res.size() == 3;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert joinedCnt.get() == 3;
+ assert leftCnt.get() == 0;
+
+ assert ignite.cluster().nodes().size() == 4;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStartThreeNodesAndDoEmptyCall() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ startCheckNodes();
+
+ assert joinedCnt.get() == 3;
+ assert leftCnt.get() == 0;
+
+ assert ignite.cluster().nodes().size() == 4;
+
+ Collection<ClusterStartNodeResult> res = startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 3, U.getIgniteHome(), CFG_NO_ATTR, null),
+ false, 0, 16);
+
+ assert res.isEmpty();
+
+ assert joinedCnt.get() == 3;
+ assert leftCnt.get() == 0;
+
+ assert ignite.cluster().nodes().size() == 4;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStartThreeNodesAndTryToStartOneNode() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ startCheckNodes();
+
+ assert joinedCnt.get() == 3;
+ assert leftCnt.get() == 0;
+
+ Collection<ClusterStartNodeResult> res = startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 1, U.getIgniteHome(), CFG_NO_ATTR, null),
+ false, 0, 16);
+
+ assert res.isEmpty();
+
+ assert joinedCnt.get() == 3;
+ assert leftCnt.get() == 0;
+
+ assert ignite.cluster().nodes().size() == 4;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStartFiveNodesInTwoCalls() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ startCheckNodes();
+
+ assert joinedCnt.get() == 3;
+ assert leftCnt.get() == 0;
+
+ joinedLatch = new CountDownLatch(2);
+
+ Collection<ClusterStartNodeResult> res = startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 5, U.getIgniteHome(), CFG_NO_ATTR, null),
+ false, 0, 16);
+
+ assert res.size() == 2;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert joinedCnt.get() == 5;
+ assert leftCnt.get() == 0;
+
+ assert ignite.cluster().nodes().size() == 6;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStartFiveWithTwoSpecs() throws Exception {
+ joinedLatch = new CountDownLatch(5);
+
+ Collection<ClusterStartNodeResult> res =
+ startNodes(ignite.cluster(),
+ F.asList(map(pwd, key, 2, U.getIgniteHome()),
+ map(pwd, key, 3, U.getIgniteHome())),
+ false, 0, 16);
+
+ assert res.size() == 5;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert joinedCnt.get() == 5;
+ assert leftCnt.get() == 0;
+
+ assert ignite.cluster().nodes().size() == 6;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStartThreeNodesAndRestart() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ startCheckNodes();
+
+ assert joinedCnt.get() == 3;
+ assert leftCnt.get() == 0;
+
+ joinedLatch = new CountDownLatch(3);
+ leftLatch = new CountDownLatch(3);
+
+ Collection<ClusterStartNodeResult> res = startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 3, U.getIgniteHome(), CFG_NO_ATTR, null),
+ true, 0, 16);
+
+ assert res.size() == 3;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+ assert leftLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert joinedCnt.get() == 6;
+ assert leftCnt.get() == 3;
+
+ assert ignite.cluster().nodes().size() == 4;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testCustomScript() throws Exception {
+ joinedLatch = new CountDownLatch(1);
+
+ String script = U.isWindows() ? CUSTOM_SCRIPT_WIN : CUSTOM_SCRIPT_LINUX;
+
+ script = Paths.get(U.getIgniteHome()).relativize(U.resolveIgnitePath(script).toPath()).toString();
+
+ Collection<ClusterStartNodeResult> res =
+ startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 1, U.getIgniteHome(), null, script),
+ false, 0, 16);
+
+ assert res.size() == 1;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert joinedCnt.get() == 1;
+ assert leftCnt.get() == 0;
+
+ assert ignite.cluster().nodes().size() == 2;
+
+ Iterator<ClusterNode> iter = ignite.cluster().nodes().iterator();
+
+ iter.next();
+
+ ClusterNode node = iter.next();
+
+ assert CUSTOM_CFG_ATTR_VAL.equals(node.<String>attribute(CUSTOM_CFG_ATTR_KEY));
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStopNodes() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ Collection<ClusterStartNodeResult> res =
+ startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, null, 3, U.getIgniteHome(), CFG_NO_ATTR,
+ null), false, 0, 16);
+
+ assert res.size() == 3;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 4;
+
+ stopAllGrids();
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStopNodesFiltered() throws Exception {
+ joinedLatch = new CountDownLatch(2);
+
+ Collection<ClusterStartNodeResult> res =
+ startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 2, U.getIgniteHome(), CFG_ATTR, null),
+ false, 0, 16);
+
+ assert res.size() == 2;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ joinedLatch = new CountDownLatch(1);
+
+ res = startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 3, U.getIgniteHome(), CFG_NO_ATTR, null),
+ false, 0, 16);
+
+ assert res.size() == 1;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 4;
+
+ leftLatch = new CountDownLatch(2);
+
+ Collection<UUID> ids = F.transform(ignite.cluster().forAttribute(CUSTOM_CFG_ATTR_KEY, CUSTOM_CFG_ATTR_VAL).nodes(),
+ (IgniteClosure<ClusterNode, UUID>)ClusterNode::id);
+
+ ignite.cluster().forAttribute(CUSTOM_CFG_ATTR_KEY, CUSTOM_CFG_ATTR_VAL).nodes();
+
+ ignite.cluster().stopNodes(ids);
+
+ assert leftLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 2;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStopNodeById() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ startCheckNodes();
+
+ leftLatch = new CountDownLatch(1);
+
+ UUID toStop = null;
+
+ Collection<ClusterNode> nodes = ignite.cluster().forRemotes().nodes();
+
+ for (ClusterNode node : nodes) {
+ if (!Objects.equals(node.id(), ignite.cluster().localNode().id())) {
+ toStop = node.id();
+
+ break;
+ }
+ }
+
+ assert toStop != null;
+
+ ignite.cluster().stopNodes(Collections.singleton(toStop));
+
+ assert leftLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 3;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testStopNodesByIds() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ startCheckNodes();
+
+ leftLatch = new CountDownLatch(2);
+
+ Iterator<ClusterNode> it = ignite.cluster().nodes().iterator();
+
+ it.next();
+
+ Collection<UUID> ids = new HashSet<>();
+
+ ids.add(it.next().id());
+ ids.add(it.next().id());
+
+ ignite.cluster().stopNodes(ids);
+
+ assert leftLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 2;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testRestartNodes() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ startCheckNodes();
+
+ joinedLatch = new CountDownLatch(3);
+ leftLatch = new CountDownLatch(3);
+
+ ignite.cluster().restartNodes();
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+ assert leftLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 4;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testRestartNodesFiltered() throws Exception {
+ joinedLatch = new CountDownLatch(2);
+
+ Collection<ClusterStartNodeResult> res =
+ startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 2, U.getIgniteHome(), CFG_ATTR, null),
+ false, 0, 16);
+
+ assert res.size() == 2;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ joinedLatch = new CountDownLatch(1);
+
+ res = startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 3, U.getIgniteHome(), CFG_NO_ATTR, null),
+ false, 0, 16);
+
+ assert res.size() == 1;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 4;
+
+ joinedLatch = new CountDownLatch(2);
+ leftLatch = new CountDownLatch(2);
+
+ X.println("Restarting nodes with " + CUSTOM_CFG_ATTR_KEY);
+
+ Collection<UUID> ids = F.transform(ignite.cluster().forAttribute(CUSTOM_CFG_ATTR_KEY, CUSTOM_CFG_ATTR_VAL).nodes(),
+ (IgniteClosure<ClusterNode, UUID>)ClusterNode::id
+ );
+
+ ignite.cluster().restartNodes(ids);
+
+ assert leftLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 4;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testRestartNodeById() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ startCheckNodes();
+
+ joinedLatch = new CountDownLatch(1);
+ leftLatch = new CountDownLatch(1);
+
+ ignite.cluster().restartNodes(Collections.singleton(F.first(ignite.cluster().forRemotes().nodes()).id()));
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+ assert leftLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 4;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ @Test
+ public void testRestartNodesByIds() throws Exception {
+ joinedLatch = new CountDownLatch(3);
+
+ startCheckNodes();
+
+ joinedLatch = new CountDownLatch(2);
+ leftLatch = new CountDownLatch(2);
+
+ Iterator<ClusterNode> it = ignite.cluster().nodes().iterator();
+
+ it.next();
+
+ ignite.cluster().restartNodes(F.asList(it.next().id(), it.next().id()));
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+ assert leftLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 4;
+ }
+
+ /**
+ * @param passwd Password.
+ * @param key Private key file.
+ * @param nodes Number of nodes.
+ * @param igniteHome Ignite home.
+ * @return Parameters map.
+ */
+ private Map<String, Object> map(
+ @Nullable String passwd,
+ @Nullable File key,
+ @Nullable Integer nodes,
+ @Nullable String igniteHome) {
+ assert IgniteProjectionStartStopRestartSelfTest.HOST != null;
+
+ Map<String, Object> params = new HashMap<>();
+
+ params.put(IgniteNodeStartUtils.HOST, IgniteProjectionStartStopRestartSelfTest.HOST);
+ params.put(UNAME, IgniteProjectionStartStopRestartSelfTest.SSH_UNAME);
+ params.put(PASSWD, passwd);
+ params.put(KEY, key);
+ params.put(NODES, nodes);
+ params.put(IGNITE_HOME, igniteHome);
+ params.put(CFG, IgniteProjectionStartStopRestartSelfTest.CFG_NO_ATTR);
+ params.put(SCRIPT, null);
+
+ return params;
+ }
+
+ /**
+ * @param hosts Hostnames.
+ * @param passwd Password.
+ * @param key Private key file.
+ * @param nodes Number of nodes.
+ * @param igniteHome Ignite home.
+ * @param cfg Configuration file path.
+ * @param script Startup script path.
+ * @return Parameters map.
+ */
+ private Collection<Map<String, Object>> maps(
+ Collection<String> hosts,
+ @Nullable String passwd,
+ @Nullable File key,
+ @Nullable Integer nodes,
+ @Nullable String igniteHome,
+ @Nullable String cfg,
+ @Nullable String script) {
+ assert HOST != null;
+
+ Collection<Map<String, Object>> maps = new ArrayList<>(hosts.size());
+
+ for (String host : hosts) {
+ Map<String, Object> params = new HashMap<>();
+
+ params.put(IgniteNodeStartUtils.HOST, host);
+ params.put(UNAME, IgniteProjectionStartStopRestartSelfTest.SSH_UNAME);
+ params.put(PASSWD, passwd);
+ params.put(KEY, key);
+ params.put(NODES, nodes);
+ params.put(IGNITE_HOME, igniteHome);
+ params.put(CFG, cfg);
+ params.put(SCRIPT, script);
+
+ maps.add(params);
+ }
+
+ return maps;
+ }
+
+ /**
+ * @throws InterruptedException If failed.
+ */
+ private void startCheckNodes() throws InterruptedException {
+ joinedLatch = new CountDownLatch(3);
+
+ Collection<ClusterStartNodeResult> res =
+ startNodes(ignite.cluster(),
+ maps(Collections.singleton(HOST), pwd, key, 3, U.getIgniteHome(), CFG_NO_ATTR, null),
+ false, 0, 16);
+
+ assert res.size() == 3;
+
+ res.forEach(t -> {
+ assert t.getHostName().equals(HOST);
+
+ if (!t.isSuccess())
+ throw new IgniteException(t.getError());
+ });
+
+ assert joinedLatch.await(WAIT_TIMEOUT, MILLISECONDS);
+
+ assert ignite.cluster().nodes().size() == 4;
+ }
+
+ /**
+ * @param cluster Cluster.
+ * @param hosts Hosts.
+ * @param restart Restart flag.
+ * @param timeout Timeout.
+ * @param maxConn Maximum connections.
+ * @return Results collection.
+ */
+ private Collection<ClusterStartNodeResult> startNodes(IgniteCluster cluster,
+ Collection<Map<String, Object>> hosts,
+ boolean restart,
+ int timeout,
+ int maxConn) {
+ return cluster.startNodesAsync(hosts, null, restart, timeout, maxConn).get(WAIT_TIMEOUT);
+ }
+
+ /** */
+ private static String getProperty(String envName, String gridTestName) {
+ String candidate = System.getenv(envName);
+
+ if (candidate != null)
+ return candidate;
+
+ return GridTestProperties.getProperty(gridTestName);
+ }
+}
diff --git a/modules/ssh-ext/src/test/java/org/apache/ignite/internal/IgniteStartStopRestartTestSuite.java b/modules/ssh-ext/src/test/java/org/apache/ignite/internal/IgniteStartStopRestartTestSuite.java
new file mode 100644
index 0000000..d06596c
--- /dev/null
+++ b/modules/ssh-ext/src/test/java/org/apache/ignite/internal/IgniteStartStopRestartTestSuite.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Start nodes tests.
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+ IgniteNodeStartUtilsSelfTest.class,
+ IgniteProjectionStartStopRestartSelfTest.class
+})
+public class IgniteStartStopRestartTestSuite {
+}
diff --git a/pom.xml b/pom.xml
index 2900b52..c1f8a1a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -62,6 +62,7 @@
<module>modules/yarn-ext</module>
<module>modules/cloud-ext</module>
<module>modules/osgi-ext</module>
+ <module>modules/ssh-ext</module>
</modules>
<profiles>