From 47ed3ca7752eef708085f5184c9541bf78791057 Mon Sep 17 00:00:00 2001 From: roo00 Date: Fri, 22 Feb 2019 23:22:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9update=E6=96=B9=E5=BC=8F,?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0update=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/x/server/console/CommandFactory.java | 89 +------ .../x/server/console/action/ActionUpdate.java | 50 +--- .../console/action/ActionUpdateFast.java | 241 ------------------ .../console/action/ActionUpdateFile.java | 84 ++++++ 4 files changed, 106 insertions(+), 358 deletions(-) delete mode 100644 o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdateFast.java create mode 100644 o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdateFile.java diff --git a/o2server/x_console/src/main/java/com/x/server/console/CommandFactory.java b/o2server/x_console/src/main/java/com/x/server/console/CommandFactory.java index 956d386fda..8ef221946c 100644 --- a/o2server/x_console/src/main/java/com/x/server/console/CommandFactory.java +++ b/o2server/x_console/src/main/java/com/x/server/console/CommandFactory.java @@ -37,9 +37,10 @@ public class CommandFactory { public static final Pattern exit_pattern = Pattern.compile("^ {0,}exit {0,}$", Pattern.CASE_INSENSITIVE); - public static final Pattern update_pattern = Pattern.compile("^ {0,}update (.+)$", Pattern.CASE_INSENSITIVE); + public static final Pattern update_pattern = Pattern.compile("^ {0,}update (true|false) (true|false) (.+)$", + Pattern.CASE_INSENSITIVE); - public static final Pattern fastUpdate_pattern = Pattern.compile("^ {0,}fast update (.+)$", + public static final Pattern updateFile_pattern = Pattern.compile("^ {0,}update file (.+) (true|false) (.+)$", Pattern.CASE_INSENSITIVE); public static final Pattern version_pattern = Pattern.compile("^ {0,}version {0,}$", Pattern.CASE_INSENSITIVE); @@ -48,7 +49,7 @@ public class CommandFactory { Pattern.CASE_INSENSITIVE); public static final Pattern erase_content_pattern = Pattern - .compile("^ {0,}erase content (pp|cms|log|report|bbs) (.+)$", Pattern.CASE_INSENSITIVE); + .compile("^ {0,}erase content (cms|pp|bbs|log) (.+)$", Pattern.CASE_INSENSITIVE); public static final Pattern compact_data_pattern = Pattern.compile("^ {0,}compact data (.+)$", Pattern.CASE_INSENSITIVE); @@ -114,18 +115,20 @@ public class CommandFactory { help += StringUtils.LF; help += " setPassword (oldpasswd) (newpasswd)" + "\t\t\t" + "change initial manager password."; help += StringUtils.LF; - help += " version " + "\t\t\t\t\t\t" + "show available update version."; + help += " update (backup) (latest) (passwd)" + "\t\t\t" + "upgrade to new version."; help += StringUtils.LF; - help += " update (passwd)" + "\t\t\t\t\t" + "upgrade to next version."; + help += " update file (path) (backup) (passwd)" + "\t\t\t" + "upgrade to new version from local zip file."; help += StringUtils.LF; help += " compact data (passwd)" + "\t\t\t\t\t" + "compact local h2 repository database."; help += StringUtils.LF; - help += " erase content (cms|pp|bbs|log|report) (passwd)" + "\t\t" + "remove all data except design."; + help += " erase content (cms|pp|bbs|log) (passwd)" + "\t\t" + "remove all data except design."; help += StringUtils.LF; help += " create encrypt key" + "\t\t\t\t\t" + "create random RSA key."; help += StringUtils.LF; help += " show (os|cpu|memory|thread) interval repeat" + "\t\t" + "show operating system infomation."; help += StringUtils.LF; + help += " version " + "\t\t\t\t\t\t" + "show available update version."; + help += StringUtils.LF; help += " exit" + "\t\t\t\t\t\t\t" + "exit after stop."; help += StringUtils.LF; System.out.println(help); @@ -134,81 +137,7 @@ public class CommandFactory { } } -// public static void printHelpTable() { -// try { -// AsciiTable at = new AsciiTable(); -// at.addRule(); -// AT_Row row = at.addRow("command", "description"); -// row.setTextAlignment(TextAlignment.CENTER); -// at.addRule(); -// row = at.addRow("help", " show useage message"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("start|stop [all]", "start stop all enable server"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("start|stop data", "start stop data server"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("start|stop storage", "start stop storage server"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("start|stop center", "start stop center server"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("start|stop application", "start stop application server"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("start|stop web", "start stop web server"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("dump data (passwd)", "dump data from database"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("dump storage (passwd)", "dump storage from database,file"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("restore data yyyyMMddHHmmss (passwd)", "restore data to database"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("restore storage yyyyMMddHHmmss (passwd)", "restore storage to database,file"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("setPassword (oldpasswd) (newpasswd)", "change initial manager password"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("version", "show available update version"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("update (passwd)", "upgrade to next version"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("compact data (passwod)", "compact local h2 repository database"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("erase content (cms|pp|bss|log|report) (passwd)", "remove all data except design"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("create encrypt key", "create random RSA key"); -// row.setPaddingLeft(1); -// at.addRule(); -// row = at.addRow("exit", "exit after stop"); -// row.setPaddingLeft(1); -// at.addRule(); -// at.getRenderer().setCWC(new CWC_FixedWidth().add(48).add(38)); -// System.out.println(at.render()); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } - private static void printStartImage() { -// try { -// String ascii = FigletFont.convertOneLine(Config.currentNode().getBanner()); -// System.out.println(ascii); -// } catch (Exception e) { -// e.printStackTrace(); -// } try { File file = Config.startImage(); BufferedImage image = null; diff --git a/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdate.java b/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdate.java index 887144fef7..44755eacda 100644 --- a/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdate.java +++ b/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdate.java @@ -38,14 +38,14 @@ public class ActionUpdate extends ActionBase { private static final String LATEST = "latest"; - public boolean execute(String password, boolean backup, boolean toLatest) { + public boolean execute(String password, boolean backup, boolean latest) { try { this.init(); if (!StringUtils.equals(Config.token().getPassword(), password)) { logger.print("password not mactch."); return false; } - WrapUpdateVersion wrapUpdateVersion = this.get(toLatest); + WrapUpdateVersion wrapUpdateVersion = this.get(latest); if (StringUtils.equals(LATEST, wrapUpdateVersion.getVersion())) { logger.print("already the latest version."); return false; @@ -67,21 +67,24 @@ public class ActionUpdate extends ActionBase { } private void backup() throws Exception { - File dir = new File(Config.base(), "local/backup"); - FileUtils.forceMkdir(dir); + File dir = Config.dir_local_backup(true); String tag = DateTools.compact(new Date()); File dest = new File(dir, tag + ".zip"); logger.print("backup current version to {}.", dest.getAbsolutePath()); List files = new ArrayList<>(); - files.add(new File(Config.base(), "commons")); - files.add(new File(Config.base(), "config")); - files.add(new File(Config.base(), "jvm")); - files.add(new File(Config.base(), "servers")); - files.add(new File(Config.base(), "store")); + files.add(Config.dir_commons()); + files.add(Config.dir_config()); + files.add(Config.dir_configSample()); + files.add(Config.dir_localSample()); + files.add(Config.dir_jvm()); + files.add(Config.dir_servers()); + files.add(Config.dir_store()); + files.add(Config.dir_dynamic()); + files.add(Config.dir_custom()); files.add(new File(Config.base(), "console.jar")); files.add(new File(Config.base(), "index.html")); files.add(new File(Config.base(), "version.o2")); - FileFilter fileFilter = new RegexFileFilter("^(start_|stop_)(aix|windows|linux|macos).(sh|bat)$"); + FileFilter fileFilter = new RegexFileFilter("^(start_|stop_|console_)(aix|windows|linux|macos).(sh|bat)$"); for (File _f : new File(Config.base()).listFiles(fileFilter)) { files.add(_f); } @@ -172,31 +175,4 @@ public class ActionUpdate extends ActionBase { JarTools.unjar(file, "", dir, true); } - // private byte[] getPack(String address) throws Exception { - // logger.print("download update pack form url: {}.", address); - // URL url = new URL(address); - // - // HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - // connection.setUseCaches(false); - // connection.setRequestProperty("Content-Type", - // HttpMediaType.APPLICATION_JSON_UTF_8); - // connection.setRequestMethod("GET"); - // connection.setDoOutput(false); - // connection.setDoInput(true); - // connection.connect(); - // byte[] bytes; - // try (InputStream input = connection.getInputStream()) { - // bytes = IOUtils.toByteArray(input); - // } - // logger.print("download update pack completed."); - // return bytes; - // } - - // private void unzip(byte[] bytes) throws Exception { - // File dir = new File(Config.base(), "local/update"); - // FileUtils.forceMkdir(dir); - // FileUtils.cleanDirectory(dir); - // JarTools.unjar(bytes, "", dir, true); - // } - } \ No newline at end of file diff --git a/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdateFast.java b/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdateFast.java deleted file mode 100644 index dbbaf859ba..0000000000 --- a/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdateFast.java +++ /dev/null @@ -1,241 +0,0 @@ -package com.x.server.console.action; - -import java.io.File; -import java.io.FileFilter; -import java.io.InputStream; -import java.lang.reflect.Method; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLClassLoader; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import java.util.stream.Collectors; - -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.filefilter.RegexFileFilter; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.reflect.MethodUtils; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.x.base.core.project.config.Config; -import com.x.base.core.project.gson.XGsonBuilder; -import com.x.base.core.project.http.HttpMediaType; -import com.x.base.core.project.logger.Logger; -import com.x.base.core.project.logger.LoggerFactory; -import com.x.base.core.project.tools.DateTools; -import com.x.base.core.project.tools.JarTools; -import com.x.server.console.NodeAgent; - -public class ActionUpdateFast extends ActionBase { - - private static Logger logger = LoggerFactory.getLogger(ActionUpdateFast.class); - - private Date start; - - private void init() throws Exception { - this.start = new Date(); - } - - private static final String LATEST = "latest"; - - public boolean execute(String password) throws Exception { - this.init(); - if (!StringUtils.equals(Config.token().getPassword(), password)) { - System.out.println("password not match."); - return false; - } - WrapUpdateVersion wrapUpdateVersion = this.get(); - if (StringUtils.equals(LATEST, wrapUpdateVersion.getVersion())) { - System.out.println("already the latest version!"); - return false; - } else { - this.backup(); - File file = this.getPack(wrapUpdateVersion.getUrl()); - this.unzip(file); - FileUtils.forceDelete(file); - System.out.println("update completed in " + (((new Date()).getTime() - start.getTime()) / 1000) - + " seconds, restart server to continue update!"); - return true; - } - } - - private void backup() throws Exception { - File dir = new File(Config.base(), "local/backup"); - FileUtils.forceMkdir(dir); - String tag = DateTools.compact(new Date()); - File dest = new File(dir, tag + ".zip"); - logger.print("backup current version to {}.", dest.getAbsolutePath()); - List files = new ArrayList<>(); - files.add(new File(Config.base(), "commons")); - files.add(new File(Config.base(), "config")); - files.add(new File(Config.base(), "jvm")); - files.add(new File(Config.base(), "servers")); - files.add(new File(Config.base(), "store")); - files.add(new File(Config.base(), "console.jar")); - files.add(new File(Config.base(), "index.html")); - files.add(new File(Config.base(), "version.o2")); - FileFilter fileFilter = new RegexFileFilter("^(start_|stop_)(aix|windows|linux|macos).(sh|bat)$"); - for (File _f : new File(Config.base()).listFiles(fileFilter)) { - files.add(_f); - } - JarTools.jar(files, dest); - logger.print("backup current version completed."); - } - - private WrapUpdateVersion get() throws Exception { - String address = Config.collect().url("/o2_collect_assemble/jaxrs/update/next/" + Config.version()); - URL url = new URL(address); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setUseCaches(false); - connection.setRequestProperty("Content-Type", HttpMediaType.APPLICATION_JSON_UTF_8); - connection.setRequestMethod("GET"); - connection.setDoOutput(false); - connection.setDoInput(true); - connection.connect(); - String json = ""; - try (InputStream input = connection.getInputStream()) { - json = IOUtils.toString(input, StandardCharsets.UTF_8); - } - connection.disconnect(); - Gson gson = XGsonBuilder.instance(); - JsonElement jsonElement = gson.fromJson(json, JsonElement.class); - if (jsonElement.isJsonObject()) { - JsonObject jsonObject = jsonElement.getAsJsonObject(); - if (jsonObject.has("data")) { - return XGsonBuilder.instance().fromJson(jsonObject.get("data"), WrapUpdateVersion.class); - } - } - return null; - } - - public static class WrapUpdateVersion { - - private String version; - - private Long size; - - private String url; - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public Long getSize() { - return size; - } - - public void setSize(Long size) { - this.size = size; - } - } - - // private byte[] getPack(String address) throws Exception { - // logger.print("download upate pack form url: {}.", address); - // URL url = new URL(address); - // - // HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - // connection.setUseCaches(false); - // connection.setRequestProperty("Content-Type", - // HttpMediaType.APPLICATION_JSON_UTF_8); - // connection.setRequestMethod("GET"); - // connection.setDoOutput(false); - // connection.setDoInput(true); - // connection.connect(); - // byte[] bytes; - // try (InputStream input = connection.getInputStream()) { - // bytes = IOUtils.toByteArray(input); - // } - // System.out.println("download upate pack completed."); - // return bytes; - // } - // - // private void unzip(byte[] bytes) throws Exception { - // File dir = new File(Config.base(), "local/update"); - // FileUtils.forceMkdir(dir); - // FileUtils.cleanDirectory(dir); - // JarTools.unjar(bytes, "", dir, true); - // } - - private File getPack(String address) throws Exception { - logger.print("download update pack form url: {}.", address); - URL url = new URL(address); - File file = new File(Config.base(), "local/update.zip"); - if (file.exists() && file.isFile()) { - FileUtils.forceDelete(file); - } - FileUtils.copyURLToFile(url, file); - logger.print("download update pack completed."); - return file; - } - - private void unzip(File file) throws Exception { - File dir = new File(Config.base(), "local/update"); - FileUtils.forceMkdir(dir); - FileUtils.cleanDirectory(dir); - JarTools.unjar(file, "", dir, true); - } - - private void updateFiles(String base, String updateVersion) throws Exception { - File file = new File(base, "local/updates/" + updateVersion + "/commons"); - if (file.exists() && file.isDirectory()) { - FileUtils.copyDirectory(file, new File(base, "commons")); - } - file = new File(base, "local/updates/" + updateVersion + "/servers"); - if (file.exists() && file.isDirectory()) { - FileUtils.copyDirectory(file, new File(base, "servers")); - } - file = new File(base, "local/updates/" + updateVersion + "/store"); - if (file.exists() && file.isDirectory()) { - FileUtils.copyDirectory(file, new File(base, "store")); - } - file = new File(base, "local/updates/" + updateVersion + "/console.jar"); - if (file.exists() && file.isFile()) { - FileUtils.copyFile(file, new File(base, "console.jar")); - } - } - - private void executeBeforeScript(String base, String updateVersion) throws Exception { - File file = new File(base, "local/updates/" + updateVersion + "/script/update" + updateVersion + "before.jar"); - if (file.exists() && file.isFile()) { - System.out.println("executing before update script."); - File tempFile = new File(base, "local/temp/" + file.getName()); - FileUtils.copyFile(file, tempFile); - addJar(tempFile); - Class clz = Class.forName("update" + updateVersion + "before.Main"); - MethodUtils.invokeStaticMethod(clz, "main", new Object[] { new String[] {} }); - } - } - - private void changeVersion(String base, String updateVersion) throws Exception { - File file = new File(base, "version.o2"); - FileUtils.writeStringToFile(file, updateVersion, "UTF-8"); - } - - private void addJar(File file) throws Exception { - URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); - Class urlClass = URLClassLoader.class; - Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class }); - method.setAccessible(true); - method.invoke(urlClassLoader, new Object[] { file.toURI().toURL() }); - } - -} \ No newline at end of file diff --git a/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdateFile.java b/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdateFile.java new file mode 100644 index 0000000000..bf1249437a --- /dev/null +++ b/o2server/x_console/src/main/java/com/x/server/console/action/ActionUpdateFile.java @@ -0,0 +1,84 @@ +package com.x.server.console.action; + +import java.io.File; +import java.io.FileFilter; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.filefilter.RegexFileFilter; +import org.apache.commons.lang3.StringUtils; + +import com.x.base.core.project.config.Config; +import com.x.base.core.project.logger.Logger; +import com.x.base.core.project.logger.LoggerFactory; +import com.x.base.core.project.tools.DateTools; +import com.x.base.core.project.tools.JarTools; + +public class ActionUpdateFile extends ActionUpdate { + + private static Logger logger = LoggerFactory.getLogger(ActionUpdateFile.class); + + private Date start; + + private void init() throws Exception { + this.start = new Date(); + } + + public boolean execute(String path, boolean backup, String password) { + try { + this.init(); + if (!StringUtils.equals(Config.token().getPassword(), password)) { + logger.print("password not mactch."); + return false; + } + File file = new File(path); + if (!file.exists() || file.isDirectory()) { + logger.print("zip file not exist path:{}.", path); + } + if (backup) { + this.backup(); + } + this.unzip(file); + logger.print("update completed in {} seconds, restart server to continue update.", + ((new Date()).getTime() - start.getTime()) / 1000); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + private void backup() throws Exception { + File dir = Config.dir_local_backup(true); + String tag = DateTools.compact(new Date()); + File dest = new File(dir, tag + ".zip"); + logger.print("backup current version to {}.", dest.getAbsolutePath()); + List files = new ArrayList<>(); + files.add(Config.dir_commons()); + files.add(Config.dir_config()); + files.add(Config.dir_configSample()); + files.add(Config.dir_localSample()); + files.add(Config.dir_jvm()); + files.add(Config.dir_servers()); + files.add(Config.dir_store()); + files.add(Config.dir_dynamic()); + files.add(Config.dir_custom()); + files.add(new File(Config.base(), "console.jar")); + files.add(new File(Config.base(), "index.html")); + files.add(new File(Config.base(), "version.o2")); + FileFilter fileFilter = new RegexFileFilter("^(start_|stop_|console_)(aix|windows|linux|macos).(sh|bat)$"); + for (File _f : new File(Config.base()).listFiles(fileFilter)) { + files.add(_f); + } + JarTools.jar(files, dest); + logger.print("backup current version completed."); + } + + private void unzip(File file) throws Exception { + File dir = Config.dir_local_update(true); + FileUtils.cleanDirectory(dir); + JarTools.unjar(file, "", dir, true); + } +} \ No newline at end of file -- GitLab