This is an automated email from the ASF dual-hosted git repository.

namelchev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 7fbb90a7515 IGNITE-19379 Control.sh: added the cache create command 
(#10673)
7fbb90a7515 is described below

commit 7fbb90a75151f74564a16d7867277777cc07f026
Author: Nikita Amelchev <[email protected]>
AuthorDate: Fri Apr 28 19:07:24 2023 +0300

    IGNITE-19379 Control.sh: added the cache create command (#10673)
---
 docs/_docs/tools/control-script.adoc               |  27 +++++
 modules/control-utility/pom.xml                    |   6 +
 .../internal/commandline/cache/CacheCreate.java    | 124 +++++++++++++++++++++
 .../commandline/cache/CacheSubcommands.java        |   5 +
 .../util/GridCommandHandlerClusterByClassTest.java |  36 ++++++
 .../config/cache/cache-create-correct.xml          |  30 +++++
 .../config/cache/cache-create-no-configs.xml       |  26 +++++
 .../internal/visor/cache/VisorCacheCreateTask.java |  97 ++++++++++++++++
 ...mandHandlerClusterByClassTest_cache_help.output |   6 +
 ...dlerClusterByClassWithSSLTest_cache_help.output |   6 +
 10 files changed, 363 insertions(+)

diff --git a/docs/_docs/tools/control-script.adoc 
b/docs/_docs/tools/control-script.adoc
index 9afe87d8c30..592c0c89b21 100644
--- a/docs/_docs/tools/control-script.adoc
+++ b/docs/_docs/tools/control-script.adoc
@@ -421,6 +421,33 @@ control.sh|bat --cache list . --seq
 control.sh|bat --cache list counter-.* --seq
 ----
 
+== Creating Caches
+
+You can use the control script to create specific caches.
+
+NOTE: The 'ignite-spring' module should be enabled.
+
+[source, shell]
+----
+control.sh|bat --cache create --springXmlConfig springXmlFilePath
+----
+
+Parameters:
+
+[cols="1,3",opts="header"]
+|===
+| Parameter | Description
+| `--springXmlConfig springXmlConfigPath` | Path to the Spring XML 
configuration that contains
+'org.apache.ignite.configuration.CacheConfiguration' beans to create caches 
from.
+|===
+
+Examples:
+[source, shell]
+----
+# Create caches from the `/ignite/config/userCaches.xml` configuration.
+control.sh|bat --cache create --springXmlConfig /ignite/config/userCaches.xml`
+----
+
 == Destroying Caches
 
 You can use the control script to destroy specific caches.
diff --git a/modules/control-utility/pom.xml b/modules/control-utility/pom.xml
index 32d50b0ce0e..13729cbb94f 100644
--- a/modules/control-utility/pom.xml
+++ b/modules/control-utility/pom.xml
@@ -107,6 +107,12 @@
             <scope>test</scope>
         </dependency>
 
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>ignite-spring</artifactId>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
             <groupId>org.apache.curator</groupId>
             <artifactId>curator-test</artifactId>
diff --git 
a/modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheCreate.java
 
b/modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheCreate.java
new file mode 100644
index 00000000000..a253de81e0d
--- /dev/null
+++ 
b/modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheCreate.java
@@ -0,0 +1,124 @@
+/*
+ * 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.commandline.cache;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Set;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.internal.client.GridClient;
+import org.apache.ignite.internal.client.GridClientConfiguration;
+import org.apache.ignite.internal.commandline.AbstractCommand;
+import org.apache.ignite.internal.commandline.Command;
+import org.apache.ignite.internal.commandline.CommandArgIterator;
+import org.apache.ignite.internal.commandline.TaskExecutor;
+import org.apache.ignite.internal.util.GridStringBuilder;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.internal.visor.cache.VisorCacheCreateTask;
+
+import static org.apache.ignite.internal.IgniteComponentType.SPRING;
+
+/**
+ * Command to create caches from Spring XML configuration.
+ */
+public class CacheCreate extends AbstractCommand<String> {
+    /** */
+    public static final String SPRING_XML_CONFIG = "--springXmlConfig";
+
+    /** */
+    private String springXmlCfgPath;
+
+    /** {@inheritDoc} */
+    @Override public Object execute(GridClientConfiguration clientCfg, 
IgniteLogger log) throws Exception {
+        if (!new File(springXmlCfgPath).exists()) {
+            throw new IgniteException("Failed to create caches. Spring XML 
configuration file not found " +
+                "[file=" + springXmlCfgPath + ']');
+        }
+
+        String springXmlCfg;
+
+        try {
+            springXmlCfg = U.readFileToString(springXmlCfgPath, "UTF-8");
+        }
+        catch (IOException e) {
+            throw new IgniteException("Failed to create caches. Failed to read 
Spring XML configuration file " +
+                "[file=" + springXmlCfgPath + ']', e);
+        }
+
+        try (GridClient client = Command.startClient(clientCfg)) {
+            Set<String> caches = TaskExecutor.executeTask(client, 
VisorCacheCreateTask.class, springXmlCfg, clientCfg);
+
+            GridStringBuilder s = new GridStringBuilder("Caches created 
successfully:").nl();
+
+            for (String cache : caches)
+                s.a(cache).nl();
+
+            s.nl().a("Total count: " + caches.size());
+
+            log.info(s.toString());
+
+            return caches;
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void parseArguments(CommandArgIterator argIter) {
+        springXmlCfgPath = null;
+
+        while (argIter.hasNextSubArg()) {
+            String opt = argIter.nextArg("Failed to read command argument.");
+
+            if (SPRING_XML_CONFIG.equalsIgnoreCase(opt)) {
+                if (springXmlCfgPath != null)
+                    throw new IllegalArgumentException(SPRING_XML_CONFIG + " 
argument specified twice.");
+
+                if (!argIter.hasNextSubArg())
+                    throw new IllegalArgumentException("Expected path to the 
Spring XML configuration.");
+
+                springXmlCfgPath = argIter.nextArg("Expected path to the 
Spring XML configuration.");
+            }
+        }
+
+        if (F.isEmpty(springXmlCfgPath))
+            throw new IllegalArgumentException(SPRING_XML_CONFIG + " must be 
specified.");
+    }
+
+    /** {@inheritDoc} */
+    @Override public String arg() {
+        return springXmlCfgPath;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void printUsage(IgniteLogger log) {
+        String springXmlPathArgDesc = SPRING_XML_CONFIG + " 
springXmlConfigPath";
+
+        usageCache(log, CacheSubcommands.CREATE, "Create caches from Spring 
XML configuration. Note that the '" +
+                SPRING.module() + "' module should be enabled.",
+            F.asMap(springXmlPathArgDesc, "Path to the Spring XML 
configuration that contains '" +
+                CacheConfiguration.class.getName() + "' beans to create caches 
from."),
+            springXmlPathArgDesc);
+    }
+
+    /** {@inheritDoc} */
+    @Override public String name() {
+        return CacheSubcommands.CREATE.name().toUpperCase();
+    }
+}
diff --git 
a/modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheSubcommands.java
 
b/modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheSubcommands.java
index 33cb67613fc..0d2b29e76f4 100644
--- 
a/modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheSubcommands.java
+++ 
b/modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheSubcommands.java
@@ -49,6 +49,11 @@ public enum CacheSubcommands {
      */
     LIST("list", ListCommandArg.class, new CacheViewer()),
 
+    /**
+     * Create caches.
+     */
+    CREATE("create", null, new CacheCreate()),
+
     /**
      * Destroy caches.
      */
diff --git 
a/modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerClusterByClassTest.java
 
b/modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerClusterByClassTest.java
index 769cd10944c..a7d78c49c43 100644
--- 
a/modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerClusterByClassTest.java
+++ 
b/modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerClusterByClassTest.java
@@ -115,6 +115,7 @@ import static 
org.apache.ignite.internal.commandline.CommandList.WAL;
 import static 
org.apache.ignite.internal.commandline.CommonArgParser.CMD_VERBOSE;
 import static org.apache.ignite.internal.commandline.OutputFormat.MULTI_LINE;
 import static org.apache.ignite.internal.commandline.OutputFormat.SINGLE_LINE;
+import static 
org.apache.ignite.internal.commandline.cache.CacheCreate.SPRING_XML_CONFIG;
 import static 
org.apache.ignite.internal.commandline.cache.CacheDestroy.CACHE_NAMES_ARG;
 import static 
org.apache.ignite.internal.commandline.cache.CacheDestroy.DESTROY_ALL_ARG;
 import static 
org.apache.ignite.internal.commandline.cache.CacheSubcommands.CLEAR;
@@ -127,6 +128,7 @@ import static 
org.apache.ignite.internal.commandline.consistency.ConsistencyComm
 import static 
org.apache.ignite.internal.commandline.consistency.ConsistencyCommand.PARTITIONS;
 import static 
org.apache.ignite.internal.commandline.consistency.ConsistencyCommand.STRATEGY;
 import static org.apache.ignite.internal.util.IgniteUtils.nl;
+import static org.apache.ignite.internal.util.IgniteUtils.resolveIgnitePath;
 import static org.apache.ignite.testframework.GridTestUtils.assertContains;
 import static org.apache.ignite.testframework.GridTestUtils.assertNotContains;
 import static org.apache.ignite.testframework.GridTestUtils.readResource;
@@ -1216,6 +1218,40 @@ public class GridCommandHandlerClusterByClassTest 
extends GridCommandHandlerClus
         assertContains(log, testOut.toString(), "G100");
     }
 
+    /** */
+    @Test
+    public void testCacheCreate() {
+        injectTestSystemOut();
+
+        assertContains(log, executeCommand(EXIT_CODE_INVALID_ARGUMENTS,
+                "--cache", CacheSubcommands.CREATE.name()),
+            SPRING_XML_CONFIG + " must be specified.");
+
+        assertContains(log, executeCommand(EXIT_CODE_INVALID_ARGUMENTS,
+                "--cache", CacheSubcommands.CREATE.name(), SPRING_XML_CONFIG),
+            "Expected path to the Spring XML configuration.");
+
+        assertContains(log, executeCommand(EXIT_CODE_INVALID_ARGUMENTS,
+                "--cache", CacheSubcommands.CREATE.name(), SPRING_XML_CONFIG, 
"file1", SPRING_XML_CONFIG, "file2"),
+            SPRING_XML_CONFIG + " argument specified twice.");
+
+        String cfgPath = 
resolveIgnitePath("modules/control-utility/src/test/resources/config/cache").getAbsolutePath();
+
+        assertContains(log, executeCommand(EXIT_CODE_UNEXPECTED_ERROR, 
"--cache", CacheSubcommands.CREATE.name(),
+                SPRING_XML_CONFIG, cfgPath + "/cache-create-no-configs.xml"),
+            "Failed to create caches. Make sure that Spring XML contains '" +
+                CacheConfiguration.class.getName() + "' beans.");
+
+        assertContains(log, executeCommand(EXIT_CODE_UNEXPECTED_ERROR, 
"--cache", CacheSubcommands.CREATE.name(),
+                SPRING_XML_CONFIG, cfgPath + "/unknown.xml"),
+            "Failed to create caches. Spring XML configuration file not 
found");
+
+        assertEquals(CommandHandler.EXIT_CODE_OK, execute("--cache", 
CacheSubcommands.CREATE.name(), SPRING_XML_CONFIG,
+            cfgPath + "/cache-create-correct.xml"));
+
+        assertTrue(crd.cacheNames().containsAll(F.asList("cache1", "cache2")));
+    }
+
     /** */
     @Test
     public void testCacheDestroy() throws IgniteCheckedException {
diff --git 
a/modules/control-utility/src/test/resources/config/cache/cache-create-correct.xml
 
b/modules/control-utility/src/test/resources/config/cache/cache-create-correct.xml
new file mode 100644
index 00000000000..7bf4bab6573
--- /dev/null
+++ 
b/modules/control-utility/src/test/resources/config/cache/cache-create-correct.xml
@@ -0,0 +1,30 @@
+<?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.
+-->
+
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+        http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd";>
+    <bean class="org.apache.ignite.configuration.CacheConfiguration">
+        <property name="name" value="cache1"/>
+    </bean>
+    <bean class="org.apache.ignite.configuration.CacheConfiguration">
+        <property name="name" value="cache2"/>
+    </bean>
+</beans>
diff --git 
a/modules/control-utility/src/test/resources/config/cache/cache-create-no-configs.xml
 
b/modules/control-utility/src/test/resources/config/cache/cache-create-no-configs.xml
new file mode 100644
index 00000000000..f7ad8d540cf
--- /dev/null
+++ 
b/modules/control-utility/src/test/resources/config/cache/cache-create-no-configs.xml
@@ -0,0 +1,26 @@
+<?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.
+-->
+
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+        http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd";>
+    <bean abstract="true" id="grid.cfg" 
class="org.apache.ignite.configuration.IgniteConfiguration">
+    </bean>
+</beans>
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheCreateTask.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheCreateTask.java
new file mode 100644
index 00000000000..3461468a076
--- /dev/null
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheCreateTask.java
@@ -0,0 +1,97 @@
+/*
+ * 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.visor.cache;
+
+import java.io.ByteArrayInputStream;
+import java.util.Collection;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
+import javax.cache.Cache;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.processors.task.GridInternal;
+import org.apache.ignite.internal.util.spring.IgniteSpringHelper;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.visor.VisorJob;
+import org.apache.ignite.internal.visor.VisorOneNodeTask;
+import org.apache.ignite.resources.IgniteInstanceResource;
+
+import static org.apache.ignite.internal.IgniteComponentType.SPRING;
+
+/**
+ * Task to create caches from Spring XML configuration.
+ */
+@GridInternal
+public class VisorCacheCreateTask extends VisorOneNodeTask<String, 
Set<String>> {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** {@inheritDoc} */
+    @Override protected VisorJob<String, Set<String>> job(String 
springXmlConfig) {
+        return new VisorCacheCreateJob(springXmlConfig, false);
+    }
+
+    /** */
+    private static class VisorCacheCreateJob extends VisorJob<String, 
Set<String>> {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /** */
+        @IgniteInstanceResource
+        protected transient IgniteEx ignite;
+
+        /** */
+        protected VisorCacheCreateJob(String springXmlConfig, boolean debug) {
+            super(springXmlConfig, debug);
+        }
+
+        /** {@inheritDoc} */
+        @Override protected Set<String> run(String springXmlConfig) throws 
IgniteException {
+            if (F.isEmpty(springXmlConfig))
+                throw new IllegalArgumentException("Configurations not 
specified.");
+
+            IgniteSpringHelper spring;
+
+            try {
+                spring = SPRING.create(false);
+            }
+            catch (IgniteCheckedException e) {
+                throw new IgniteException("Failed to create caches. " + 
SPRING.module() + " module is not configured.", e);
+            }
+
+            Collection<CacheConfiguration> ccfgs;
+
+            try {
+                ccfgs = spring.loadConfigurations(new 
ByteArrayInputStream(springXmlConfig.getBytes()),
+                    CacheConfiguration.class).get1();
+            }
+            catch (IgniteCheckedException e) {
+                throw new IgniteException("Failed to create caches. Make sure 
that Spring XML contains '" +
+                    CacheConfiguration.class.getName() + "' beans.", e);
+            }
+
+            Collection<IgniteCache> caches = ignite.createCaches(ccfgs);
+
+            return 
caches.stream().map(Cache::getName).collect(Collectors.toCollection(TreeSet::new));
+        }
+    }
+}
diff --git 
a/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassTest_cache_help.output
 
b/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassTest_cache_help.output
index ffe0491c120..fcb323d6377 100644
--- 
a/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassTest_cache_help.output
+++ 
b/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassTest_cache_help.output
@@ -29,6 +29,12 @@ Arguments: --cache help --yes
       --groups                    - print information about groups.
       --seq                       - print information about sequences.
 
+  --cache create --springXmlConfig springXmlConfigPath
+    Create caches from Spring XML configuration. Note that the 'ignite-spring' 
module should be enabled.
+
+    Parameters:
+      --springXmlConfig springXmlConfigPath  - Path to the Spring XML 
configuration that contains 
'org.apache.ignite.configuration.CacheConfiguration' beans to create caches 
from.
+
   --cache destroy --caches cache1,...,cacheN|--destroy-all-caches
     Permanently destroy specified caches.
 
diff --git 
a/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassWithSSLTest_cache_help.output
 
b/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassWithSSLTest_cache_help.output
index ffe0491c120..fcb323d6377 100644
--- 
a/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassWithSSLTest_cache_help.output
+++ 
b/modules/core/src/test/resources/org.apache.ignite.util/GridCommandHandlerClusterByClassWithSSLTest_cache_help.output
@@ -29,6 +29,12 @@ Arguments: --cache help --yes
       --groups                    - print information about groups.
       --seq                       - print information about sequences.
 
+  --cache create --springXmlConfig springXmlConfigPath
+    Create caches from Spring XML configuration. Note that the 'ignite-spring' 
module should be enabled.
+
+    Parameters:
+      --springXmlConfig springXmlConfigPath  - Path to the Spring XML 
configuration that contains 
'org.apache.ignite.configuration.CacheConfiguration' beans to create caches 
from.
+
   --cache destroy --caches cache1,...,cacheN|--destroy-all-caches
     Permanently destroy specified caches.
 

Reply via email to