[ 
https://issues.apache.org/jira/browse/ARTEMIS-4020?focusedWorklogId=822267&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-822267
 ]

ASF GitHub Bot logged work on ARTEMIS-4020:
-------------------------------------------

                Author: ASF GitHub Bot
            Created on: 01/Nov/22 11:26
            Start Date: 01/Nov/22 11:26
    Worklog Time Spent: 10m 
      Work Description: clebertsuconic commented on code in PR #4246:
URL: https://github.com/apache/activemq-artemis/pull/4246#discussion_r1010329924


##########
artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Upgrade.java:
##########
@@ -0,0 +1,318 @@
+/*
+ * 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.activemq.artemis.cli.commands;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.util.Iterator;
+import java.util.stream.Stream;
+
+import io.airlift.airline.Command;
+
+@Command(name = "upgrade", description = "Update an artemis instance to the 
current artemis.home, keeping all the data and broker.xml. Warning: backup your 
instance before using this command and compare the files.")
+public class Upgrade extends InstallAbstract {
+
+   /**
+    * Checks that the directory provided either exists and is writable or 
doesn't exist but can be created.
+    */
+   protected void checkDirectory() {
+      if (!directory.exists()) {
+         throw new RuntimeException(String.format("Could not find path '%s' to 
upgrade.", directory));
+      } else if (!directory.canWrite()) {
+         throw new RuntimeException(String.format("The path '%s' is not 
writable.", directory));
+      }
+   }
+
+
+   @Override
+   public Object execute(ActionContext context) throws Exception {
+      this.checkDirectory();
+      super.execute(context);
+
+      return run(context);
+   }
+
+
+   public Object run(ActionContext context) throws Exception {
+      File bkpFolder = findBackup();
+      File bkpBin = new File(bkpFolder, "bin");
+      File etcBkp = new File(bkpFolder, "etc");
+      bkpBin.mkdirs();
+      etcBkp.mkdirs();
+
+      File bin = new File(directory, "bin");
+      File etcFolder = new File(directory, etc);
+
+      if (!bin.exists()) {
+         throw new IOException(bin.getAbsolutePath() + " does not exist");
+      }
+
+      
System.out.println("*******************************************************************************************************************************");
+      System.out.println("Upgrading broker instance " + directory + " to use 
artemis.home=" + getBrokerHome());
+
+      upgradeArtemis(bkpBin, bin, etcFolder);
+      upgradeArtemisCmd(bkpBin, bin, etcFolder);
+      upgradeArtemisProfile(etcBkp, etcFolder, directory);
+      upgradeBootstratp(etcBkp, etcFolder, directory);
+      upgradeLogging(etcBkp, etcFolder, directory);
+      System.out.println();
+      System.out.println("Warning: ./artemis upgrade does not bring new 
functionality on the script itself from newer artemis versions. Please check 
for any differences on artemis script in the current release!");
+      
System.out.println("*******************************************************************************************************************************");
+
+      return null;
+   }
+
+   private void upgradeLogging(File bkpFolder, File etc, File instance) throws 
Exception {
+      File oldLogging = new File(etc, "logging.properties");
+
+      if (oldLogging.exists()) {
+         System.out.println("Moving " + oldLogging + " under " + bkpFolder);
+         File oldLoggingCopy = new File(bkpFolder, "logging.properties");
+         Files.copy(oldLogging.toPath(), oldLoggingCopy.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
+         oldLogging.delete();
+         File newLogging = new File(etc, Create.ETC_LOG4J2_PROPERTIES);
+
+
+         if (!newLogging.exists()) {
+            System.out.println("Creating " + newLogging);
+            InputStream inputStream = getClass().getResourceAsStream("etc/" + 
Create.ETC_LOG4J2_PROPERTIES);
+            OutputStream outputStream = new FileOutputStream(newLogging);
+            outputStream.write(inputStream.readAllBytes());
+            inputStream.close();
+         }
+      }
+
+
+
+      File bootstrap = new File(etc, "bootstrap.xml");
+      if (bootstrap.exists()) {
+         File bootstrapCopy = new File(bkpFolder, "bootstrap.xml");
+         Files.copy(bootstrap.toPath(), bootstrapCopy.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
+         System.out.println("Converting bootstrap.xml. Original file backed up 
at " + bkpFolder.getAbsolutePath());
+
+         upgrade(bootstrapCopy, bootstrap, (input, iterator, out) -> {
+            if (input.trim().startsWith("<server configuration=")) {
+               return "   <server configuration=\"" + etc.toURI() + 
"/broker.xml" + "\"/>";
+            }
+
+            return input;
+         });
+      } else {
+         System.out.println("Could not find bootstrap.xml");
+      }
+   }
+
+
+
+   private void upgradeBootstratp(File bkpFolder, File etc, File instance) 
throws Exception {
+      File bootstrap = new File(etc, "bootstrap.xml");
+      if (bootstrap.exists()) {
+         File bootstrapCopy = new File(bkpFolder, "bootstrap.xml");
+         Files.copy(bootstrap.toPath(), bootstrapCopy.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
+         System.out.println("Converting bootstrap.xml. Original file backed up 
at " + bkpFolder.getAbsolutePath());
+
+         upgrade(bootstrapCopy, bootstrap, (input, iterator, out) -> {
+            if (input.trim().startsWith("<server configuration=")) {
+               return "   <server configuration=\"" + etc.toURI() + 
"/broker.xml" + "\"/>";
+            }
+
+            return input;
+         });
+      } else {
+         System.out.println("Could not find bootstrap.xml");
+      }
+   }
+
+
+   private void upgradeArtemisProfile(File bkpFolder, File etc, File instance) 
throws Exception {
+      File artemisProfile = new File(etc, "artemis.profile");
+      if (artemisProfile.exists()) {
+         File artemisProfileCopy = new File(bkpFolder, "artemis.profile");
+         Files.copy(artemisProfile.toPath(), artemisProfileCopy.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
+         System.out.println("Converting artemis profile. Original file backed 
up at " + bkpFolder.getAbsolutePath());
+
+         upgrade(artemisProfileCopy, artemisProfile, (input, iterator, out) -> 
{
+
+            if (input.startsWith("ARTEMIS_HOME=")) {
+               return "ARTEMIS_HOME='" + getHome().getAbsolutePath() + "'";
+            }
+
+            if (input.startsWith("ARTEMIS_INSTANCE=")) {
+               return "ARTEMIS_INSTANCE='" + instance.getAbsolutePath() + "'";
+            }
+
+            if (input.startsWith("ARTEMIS_DATA_DIR=")) {
+               // TODO: this variable is not really used, we should get rid of 
it
+               return "ARTEMIS_DATA_DIR='" + instance.getAbsolutePath() + 
"/data'";
+            }
+
+            if (input.startsWith("ARTEMIS_ETC_DIR=")) {
+               return "ARTEMIS_ETC_DIR='" + etc.getAbsolutePath() + "'";
+            }
+
+            if (input.startsWith("ARTEMIS_OOME_DUMP=")) {
+               return "ARTEMIS_OOME_DUMP='" + instance.getAbsolutePath() + 
"/log/oom_dump.hprof'";
+            }
+
+            if (input.startsWith("ARTEMIS_INSTANCE_URI=")) {
+               return "ARTEMIS_INSTANCE_URI='" + instance.toURI() + "'";
+            }
+
+            if (input.startsWith("ARTEMIS_INSTANCE_ETC_URI=")) {
+               return "ARTEMIS_INSTANCE_ETC_URI='" + etc.toURI() + "'";
+            }
+
+            return input;
+         });
+      }
+   }
+
+   private void upgradeArtemis(File bkpFolder, File bin, File etc) throws 
Exception {
+      File artemis = new File(bin, "artemis");
+      if (artemis.exists()) {
+         File artemisCopy = new File(bkpFolder, "artemis");
+         Files.copy(artemis.toPath(), artemisCopy.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
+         System.out.println("Converting artemis script. Original file backed 
up at " + bkpFolder.getAbsolutePath());
+
+         upgrade(artemisCopy, artemis, (input, iterator, out) -> {
+
+            if (input.startsWith("ARTEMIS_INSTANCE_ETC=")) {
+               return "ARTEMIS_INSTANCE_ETC='" + etc.getAbsolutePath() + "'";
+            }
+
+            if (input.trim().startsWith("-Xbootclasspath/a:\"$LOG_MANAGER:")) {
+               return null;
+            }
+
+            if (input.trim().startsWith("-Djava.util.logging.manager")) {
+               return null;
+            }
+
+            if (input.trim().startsWith("-Dlogging.configuration=")) {
+               return null;
+            }
+
+            switch (input.trim()) {
+               case "if [ -z \"$WILDFLY_COMMON\" ] ; then": {
+                  String nextLine = iterator.next().trim();
+                  if (!nextLine.startsWith("# this is the one")) {
+                     throw new RuntimeException("Script artemis was manually 
updated. Can't proceed with upgrade");
+                  }
+                  nextLine = iterator.next().trim();
+                  if (!nextLine.startsWith("WILDFLY_COMMON=")) {
+                     throw new RuntimeException("Script artemis was manually 
updated. Can't proceed with upgrade");
+                  }
+
+                  nextLine = iterator.next().trim();
+                  if (!nextLine.startsWith("fi")) {
+                     throw new RuntimeException("Script artemis was manually 
updated. Can't proceed with upgrade");
+                  }
+                  return null;
+               }
+
+               case "if [ -z \"$LOG_MANAGER\" ] ; then":
+                  return null;
+               case "# this is the one found when the server was created": {
+                  // looking for the following snippet:
+                  // 
https://github.com/apache/activemq-artemis/blob/fb1b362b473cad51ae5d05a897be02b1fa8461d4/artemis-cli/src/main/resources/org/apache/activemq/artemis/cli/commands/bin/artemis#L103-L109
+                  String nextLine = iterator.next().trim();
+                  if (!nextLine.startsWith("LOG_MANAGER=")) {
+                     throw new RuntimeException("Script artemis was manually 
updated. Can't proceed with upgrade");
+                  }
+                  nextLine = iterator.next().trim();
+                  if (!nextLine.startsWith("fi")) {
+                     throw new RuntimeException("Script artemis was manually 
updated. Can't proceed with upgrade");
+                  }
+                  return null;
+               }
+
+               case "# Set Defaults Properties":
+               case 
"ARTEMIS_LOGGING_CONF=\"$ARTEMIS_INSTANCE_ETC_URI/logging.properties\"":
+               case "ARTEMIS_LOG_MANAGER=org.jboss.logmanager.LogManager":
+               case "# finding the Log Manager":
+               case "LOG_MANAGER=`ls $ARTEMIS_HOME/lib/jboss-logmanager*jar 
2>/dev/null`":
+               case "WILDFLY_COMMON=`ls $ARTEMIS_HOME/lib/wildfly-common*jar 
2>/dev/null`":
+                  return null;
+            }
+            return input;
+         });
+      }
+   }
+
+   private void upgradeArtemisCmd(File bkpFolder, File bin, File etc) throws 
Exception {
+      File artemis = new File(bin, "artemis.cmd");
+      if (artemis.exists()) {
+         File artemisCopy = new File(bkpFolder, "artemis.cmd");
+         Files.copy(artemis.toPath(), artemisCopy.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
+         System.out.println("Converting artemis script. Original file backed 
up at " + bkpFolder.getAbsolutePath());
+
+         upgrade(artemisCopy, artemis, (input, iterator, out) -> {
+            switch (input.trim()) {
+               case "rem \"Set Defaults.\"":
+               case "set 
ARTEMIS_LOGGING_CONF=%ARTEMIS_INSTANCE_ETC_URI%/logging.properties":
+               case "set ARTEMIS_LOG_MANAGER=org.jboss.logmanager.LogManager":
+               case "set JVM_ARGS=%JVM_ARGS% 
-Djava.util.logging.manager=%ARTEMIS_LOG_MANAGER%":
+               case " set JVM_ARGS=%JVM_ARGS% 
-Dlogging.configuration=%ARTEMIS_LOGGING_CONF%":

Review Comment:
   thanks!





Issue Time Tracking
-------------------

    Worklog Id:     (was: 822267)
    Time Spent: 11.5h  (was: 11h 20m)

> switch to using SLF4J for logging API and use Log4j 2 for broker distribution
> -----------------------------------------------------------------------------
>
>                 Key: ARTEMIS-4020
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-4020
>             Project: ActiveMQ Artemis
>          Issue Type: Improvement
>            Reporter: Robbie Gemmell
>            Assignee: Robbie Gemmell
>            Priority: Major
>             Fix For: 2.27.0
>
>          Time Spent: 11.5h
>  Remaining Estimate: 0h
>
> Switch to using [SLF4J|https://www.slf4j.org/] as the logging API for the 
> code base, with end-uses supplying and configuring an SLF4J-supporting 
> logging implementation of their choice based on their needs.
> For the client, applications will need to supply an SLF4J binding to a 
> logging implementation of their choice to enable logging. An example of doing 
> so using [Log4J 2|https://logging.apache.org/log4j/2.x/manual/index.html] is 
> given in (/will be, once the release is out) the [client logging 
> documentation|https://activemq.apache.org/components/artemis/documentation/latest/logging.html#logging-in-a-client-application].
> For the broker, the assembly distribution will include [Log4J 
> 2|https://logging.apache.org/log4j/2.x/manual/index.html] as its logging 
> implentation, with the "artemis create" CLI command used to create broker 
> instances now creating a log4j2.properties configuration within the 
> <broker-instance>/etc/ directory to configure Log4J. Details for upgrading an 
> existing broker-instance is given in (/will be, once the release is out) the 
> [version upgrade 
> documentation|https://activemq.apache.org/components/artemis/documentation/latest/versions.html].



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to