Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A6B4B1819E for ; Thu, 24 Dec 2015 07:41:58 +0000 (UTC) Received: (qmail 35110 invoked by uid 500); 24 Dec 2015 07:41:58 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 35073 invoked by uid 500); 24 Dec 2015 07:41:58 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 35064 invoked by uid 99); 24 Dec 2015 07:41:58 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Dec 2015 07:41:58 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 67BACDFF8A; Thu, 24 Dec 2015 07:41:58 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: akuznetsov@apache.org To: commits@ignite.apache.org Message-Id: <9b04f0dc591f44d78412e192bb578e2f@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: ignite git commit: ignite-1.5 Implemented start of local node with specified configuration and custom environment variables. Date: Thu, 24 Dec 2015 07:41:58 +0000 (UTC) Repository: ignite Updated Branches: refs/heads/ignite-1.5 77e77e40f -> 8f5445ab9 ignite-1.5 Implemented start of local node with specified configuration and custom environment variables. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/8f5445ab Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/8f5445ab Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/8f5445ab Branch: refs/heads/ignite-1.5 Commit: 8f5445ab934c0133f45a87b220b5d4841c9bca43 Parents: 77e77e4 Author: Alexey Kuznetsov Authored: Thu Dec 24 14:41:18 2015 +0700 Committer: Alexey Kuznetsov Committed: Thu Dec 24 14:41:18 2015 +0700 ---------------------------------------------------------------------- .../internal/visor/util/VisorTaskUtils.java | 122 ++++++++++++++++++- 1 file changed, 118 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/8f5445ab/modules/core/src/main/java/org/apache/ignite/internal/visor/util/VisorTaskUtils.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/visor/util/VisorTaskUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/visor/util/VisorTaskUtils.java index 579f50c..83dbda0 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/visor/util/VisorTaskUtils.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/visor/util/VisorTaskUtils.java @@ -20,6 +20,7 @@ package org.apache.ignite.internal.visor.util; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileFilter; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.net.InetAddress; @@ -36,6 +37,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.SortedMap; @@ -101,6 +103,9 @@ public class VisorTaskUtils { /** Log files count limit */ public static final int LOG_FILES_COUNT_LIMIT = 5000; + /** */ + private static final int DFLT_BUFFER_SIZE = 4096; + /** Only task event types that Visor should collect. */ public static final int[] VISOR_TASK_EVTS = { EVT_JOB_STARTED, @@ -558,7 +563,7 @@ public class VisorTaskUtils { try (RandomAccessFile raf = new RandomAccessFile(f, "r")) { FileChannel ch = raf.getChannel(); - ByteBuffer buf = ByteBuffer.allocate(4096); + ByteBuffer buf = ByteBuffer.allocate(DFLT_BUFFER_SIZE); ch.read(buf); @@ -823,6 +828,85 @@ public class VisorTaskUtils { } /** + * Start local node in terminal. + * + * @param log Logger. + * @param cfgPath Path to node configuration to start with. + * @param nodesToStart Number of nodes to start. + * @param quite If {@code true} then start node in quiet mode. + * @param envVars Optional map with environment variables. + * @return List of started processes. + * @throws IOException If failed to start. + */ + public static List startLocalNode(@Nullable IgniteLogger log, String cfgPath, int nodesToStart, + boolean quite, Map envVars) throws IOException { + String quitePar = quite ? "" : "-v"; + + String cmdFile = new File("bin", U.isWindows() ? "ignite.bat" : "ignite.sh").getPath(); + + File cmdFilePath = U.resolveIgnitePath(cmdFile); + + if (cmdFilePath == null || !cmdFilePath.exists()) + throw new FileNotFoundException(String.format("File not found: %s", cmdFile)); + + String ignite = cmdFilePath.getCanonicalPath(); + + File nodesCfgPath = U.resolveIgnitePath(cfgPath); + + if (nodesCfgPath == null || !nodesCfgPath.exists()) + throw new FileNotFoundException(String.format("File not found: %s", cfgPath)); + + String nodeCfg = nodesCfgPath.getCanonicalPath(); + + log(log, String.format("Starting %s local %s with '%s' config", nodesToStart, nodesToStart > 1 ? "nodes" : "node", nodeCfg)); + + List run = new ArrayList<>(); + + try { + for (int i = 0; i < nodesToStart; i++) { + if (U.isMacOs()) { + StringBuilder envs = new StringBuilder(); + + Map macEnv = new HashMap<>(System.getenv()); + + if (envVars != null) { + for (Map.Entry ent : envVars.entrySet()) + if (macEnv.containsKey(ent.getKey())) { + String old = macEnv.get(ent.getKey()); + + if (old == null || old.isEmpty()) + macEnv.put(ent.getKey(), ent.getValue()); + else + macEnv.put(ent.getKey(), old + ':' + ent.getValue()); + } + else + macEnv.put(ent.getKey(), ent.getValue()); + } + + for (Map.Entry entry : macEnv.entrySet()) { + String val = entry.getValue(); + + if (val.indexOf(';') < 0 && val.indexOf('\'') < 0) + envs.append(String.format("export %s='%s'; ", + entry.getKey(), val.replace('\n', ' ').replace("'", "\'"))); + } + + run.add(openInConsole(envs.toString(), ignite, quitePar, nodeCfg)); + } else + run.add(openInConsole(null, envVars, ignite, quitePar, nodeCfg)); + } + + return run; + } + catch (Exception e) { + for (Process proc: run) + proc.destroy(); + + throw e; + } + } + + /** * Run command in separated console. * * @param args A string array containing the program and its arguments. @@ -839,9 +923,22 @@ public class VisorTaskUtils { * @param workFolder Work folder for command. * @param args A string array containing the program and its arguments. * @return Started process. + * @throws IOException in case of error. + */ + public static Process openInConsole(@Nullable File workFolder, String... args) throws IOException { + return openInConsole(workFolder, null, args); + } + + /** + * Run command in separated console. + * + * @param workFolder Work folder for command. + * @param envVars Optional map with environment variables. + * @param args A string array containing the program and its arguments. + * @return Started process. * @throws IOException If failed to start process. */ - public static Process openInConsole(@Nullable File workFolder, String... args) + public static Process openInConsole(@Nullable File workFolder, Map envVars, String... args) throws IOException { String[] commands = args; @@ -862,6 +959,23 @@ public class VisorTaskUtils { if (workFolder != null) pb.directory(workFolder); + if (envVars != null) { + String sep = U.isWindows() ? ";" : ":"; + + Map goalVars = pb.environment(); + + for (Map.Entry var: envVars.entrySet()) { + String envVar = goalVars.get(var.getKey()); + + if (envVar == null || envVar.isEmpty()) + envVar = var.getValue(); + else + envVar += sep + var.getValue(); + + goalVars.put(var.getKey(), envVar); + } + } + return pb.start(); } @@ -873,7 +987,7 @@ public class VisorTaskUtils { * @throws IOException If failed. */ public static byte[] zipBytes(byte[] input) throws IOException { - return zipBytes(input, 4096); + return zipBytes(input, DFLT_BUFFER_SIZE); } /** @@ -904,4 +1018,4 @@ public class VisorTaskUtils { return bos.toByteArray(); } -} \ No newline at end of file +}