Tony CHEMIT pushed to branch develop at ultreiaio / ird-t3 Commits: bdbead0f by Tony CHEMIT at 2018-04-15T14:55:51Z [INSTALLEUR] Améliorer le lancement de l'installeur (ne plus éditer un fichier avant le démarrage) (Closes #315) - - - - - 10 changed files: - pom.xml - src/site/markdown/installer.md.vm - t3-installer/pom.xml - − t3-installer/src/main/assembly/dist/db.properties - + t3-installer/src/main/assembly/dist/help.bat - + t3-installer/src/main/assembly/dist/help.sh - t3-installer/src/main/config/T3Installer.ini - t3-installer/src/main/java/fr/ird/t3/console/T3Console.java - t3-domain/src/main/java/fr/ird/t3/console/T3DatabaseTool.java → t3-installer/src/main/java/fr/ird/t3/console/T3DatabaseTool.java - t3-installer/src/test/resources/log4j.properties Changes: ===================================== pom.xml ===================================== --- a/pom.xml +++ b/pom.xml @@ -102,6 +102,7 @@ <applicationName>t3</applicationName> <t3-data.version>1.9</t3-data.version> <lib.version.java4all.i18n>4.0-alpha-11</lib.version.java4all.i18n> + <lib.version.java4all.config>1.0.2-SNAPSHOT</lib.version.java4all.config> <env>dev</env> <skipITs>true</skipITs> ===================================== src/site/markdown/installer.md.vm ===================================== --- a/src/site/markdown/installer.md.vm +++ b/src/site/markdown/installer.md.vm @@ -27,13 +27,14 @@ Après téléchargement, on dézippe l'archive et on obtient l'arborescence suiv t3-installer-${project.version} ├── create-db.bat (1) ├── create-db.sh (1) + ├── help.bat (1) + ├── help.sh (1) ├── fill-db.bat (1) ├── fill-db.sh (1) - ├── db.properties (2) - ├── translate.sh - ├── translate.bat + ├── translate.sh (1) + ├── translate.bat (1) ├── README.md - ├── scripts (3) + ├── scripts (2) │ ├── ddl │ ├── h2 │ ├── referential @@ -41,25 +42,8 @@ Après téléchargement, on dézippe l'archive et on obtient l'arborescence suiv └── t3-installer-${project.version}.jar ``` -1. les scripts de lancements -2. le fichier où enregister la configuration de la base postgres -3. Répertoire des scripts offert par l'installeur - -### Configuration de la base à remplir - -Le fichier *db.properties* contient la configuration de connection aux bases. - -L'utilisateur doit avoir les droits de création de bases et être super-utilisateur, il sera le propriétaire de toutes les bases créées. - -Par défaut, il contient la configuration suivante: -``` -# Super user login that can create and should be also owner of all t3 databases -login=t3-admin -# Super user password that can create and should be also owner of all t3 databases -password=a -# Url pattern used to connect to any base -url=jdbc:postgresql:%s -``` +1. les scripts +2. Répertoire des scripts offert par l'installeur # Créer une nouvelle base @@ -73,6 +57,14 @@ Sous windows create-db.bat dbName ``` +Trois questions vous seront posées (login, password, url). + +Si les réponses vont conviennent, vous n'avez qu'à presser la touche **Entrée**. + +Les réponses sont sauvegardée dans un fichier nommé **t3-installer.conf** dans le répertoire où vous lancer la commande. + +Son contenu est relu à chaque nouveau lancement. + # Remplir une nouvelle base **Il faut que la base soit créée (mais sans schéma) et que l'extension postgis soit installée**. @@ -87,6 +79,8 @@ Sous windows fill-db.bat dbName ``` +Comme pour la commande Créer une nouvelle base, trois questions vous seront posées... + # Traduire l'application Sous Linux, Mac @@ -98,3 +92,15 @@ Sous windows ``` translate.bat ``` + +# Aide + +Sous Linux, Mac +``` + sh help.sh +``` + +Sous windows +``` + help.bat +``` ===================================== t3-installer/pom.xml ===================================== --- a/t3-installer/pom.xml +++ b/t3-installer/pom.xml @@ -64,10 +64,29 @@ <dependency> <groupId>io.ultreia.java4all.i18n</groupId> + <artifactId>i18n-api</artifactId> + </dependency> + <dependency> + <groupId>io.ultreia.java4all.i18n</groupId> <artifactId>i18n-editor</artifactId> </dependency> <dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-utils</artifactId> + </dependency> + + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </dependency> + + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + + <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> ===================================== t3-installer/src/main/assembly/dist/db.properties deleted ===================================== --- a/t3-installer/src/main/assembly/dist/db.properties +++ /dev/null @@ -1,27 +0,0 @@ -### -# #%L -# T3 :: Installer -# %% -# Copyright (C) 2010 - 2018 IRD, Code Lutin, Ultreia.io -# %% -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. -# #L% -### - -# Super user login that can create and should be also owner of all t3 databases -login=t3-admin -# Super user password that can create and should be also owner of all t3 databases -password=a -# Url pattern used to connect to any base (if you need to change it, please keep the %s, it will be replaced by db name) -url=jdbc:postgresql:%s \ No newline at end of file ===================================== t3-installer/src/main/assembly/dist/help.bat ===================================== --- /dev/null +++ b/t3-installer/src/main/assembly/dist/help.bat @@ -0,0 +1 @@ +java -Xmx2048M -Xms512M -jar ${project.build.finalName}.jar --help \ No newline at end of file ===================================== t3-installer/src/main/assembly/dist/help.sh ===================================== --- /dev/null +++ b/t3-installer/src/main/assembly/dist/help.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +java -Xmx2048M -Xms512M -jar ${project.build.finalName}.jar --help ===================================== t3-installer/src/main/config/T3Installer.ini ===================================== --- a/t3-installer/src/main/config/T3Installer.ini +++ b/t3-installer/src/main/config/T3Installer.ini @@ -1,11 +1,49 @@ +description = T3 Installer Config + +[option filename] +description = Config filename +key = config.file +type = string +defaultValue = t3-installer.conf + + +[option login] +description = Login +key = login +type = string + +[option password] +description = Password +key = password +type = string + +[option dbName] +description = Database's name +key = dbName +type = string + +[option url] +description = Jdbc connection url +key = url +type = string +defaultValue = jdbc:postgresql:${dbName} + [action createDb] +description = To create a new database (requires one argument: dbName) action = fr.ird.t3.console.T3DatabaseTool#create aliases = --create-db [action fillDb] +description = To fill a database (requires one argument: dbName) action = fr.ird.t3.console.T3DatabaseTool#fill aliases = --fill-db [action translate] +description = Launch T3 Translate tool action = fr.ird.t3.console.T3I18nTool#start -aliases = --translate +aliases = --translate, -t + +[action help] +description = Display help +action = fr.ird.t3.console.T3Console#help +aliases = --help, -h ===================================== t3-installer/src/main/java/fr/ird/t3/console/T3Console.java ===================================== --- a/t3-installer/src/main/java/fr/ird/t3/console/T3Console.java +++ b/t3-installer/src/main/java/fr/ird/t3/console/T3Console.java @@ -21,13 +21,15 @@ package fr.ird.t3.console; * #L% */ -import fr.ird.t3.T3Config; import fr.ird.t3.installer.T3InstallerConfig; -import io.ultreia.java4all.config.ApplicationConfig; +import io.ultreia.java4all.config.ApplicationConfigInit; +import io.ultreia.java4all.config.ApplicationConfigScope; +import io.ultreia.java4all.config.ArgumentsParserException; -import java.io.File; +import java.io.Console; import java.util.Arrays; import java.util.Date; +import java.util.Objects; /** * Created by tchemit on 13/03/2018. @@ -39,13 +41,70 @@ public class T3Console { public static void main(String... args) throws Exception { System.out.printf("Starting T3 Console with arguments: %s at %s %n", Arrays.toString(args), new Date()); - T3Config config = new T3Config(); - config.parse(); - config.setScriptsDirectory(new File("scripts")); - T3Config.set(config); + if (args.length == 0) { + throw new IllegalArgumentException("Need at least one argument. Try with --help to get details."); + } - ApplicationConfig installerConfig = new T3InstallerConfig().get(); - installerConfig.parse(args); - installerConfig.doAllAction(); + T3InstallerConfig installerConfig; + + String action = args[0]; + + if (action.contains("db")) { + + if (args.length != 2) { + throw new IllegalArgumentException(String.format("Need one arguments for command %s. Try with --help to get details.", action)); + } + + installerConfig = new T3InstallerConfig(ApplicationConfigInit.forAllScopesWithout(ApplicationConfigScope.HOME, ApplicationConfigScope.SYSTEM).setConfigFileName(args[1])); + installerConfig.get().parse(args); + + Console console = System.console(); + + String login = readValue(console, "Login", installerConfig.getLogin()); + if (login != null) { + installerConfig.setLogin(login); + } + String password = readValue(console, "Password", installerConfig.getPassword()); + if (password != null) { + installerConfig.setPassword(password); + } + String url = readValue(console, "Url", installerConfig.getUrl()); + if (url != null) { + installerConfig.setUrl(url); + } + installerConfig.get().saveForCurrent(); + } else { + + installerConfig = new T3InstallerConfig(); + installerConfig.get().parse(args); + + } + installerConfig.get().doAllAction(); + } + + private static String readValue(Console console, String fieldName, String fieldValue) { + String result = null; + while (result == null) { + console.printf("%s [%s]: ", fieldName, fieldValue == null ? "Not defined" : fieldValue); + result = console.readLine().trim(); + if (fieldValue == null) { + if (result.isEmpty()) { + result = null; + } + } else { + if (result.isEmpty()) { + result = fieldValue; + } + } + } + + return Objects.equals(fieldValue, result) ? null : result; + } + + public void help() throws ArgumentsParserException { + T3InstallerConfig config = new T3InstallerConfig(); + + config.get().parse(); + System.out.println(config.getConfigurationDescription()); } } ===================================== t3-domain/src/main/java/fr/ird/t3/console/T3DatabaseTool.java → t3-installer/src/main/java/fr/ird/t3/console/T3DatabaseTool.java ===================================== --- a/t3-domain/src/main/java/fr/ird/t3/console/T3DatabaseTool.java +++ b/t3-installer/src/main/java/fr/ird/t3/console/T3DatabaseTool.java @@ -2,7 +2,7 @@ package fr.ird.t3.console; /*- * #%L - * T3 :: Domain + * T3 :: Installer * %% * Copyright (C) 2010 - 2018 IRD, Code Lutin, Ultreia.io * %% @@ -10,25 +10,26 @@ package fr.ird.t3.console; * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import com.google.common.base.Preconditions; -import fr.ird.t3.T3Config; import fr.ird.t3.T3SqlScriptsImporter; import fr.ird.t3.entities.T3EntityHelper; import fr.ird.t3.entities.T3JdbcHelper; import fr.ird.t3.entities.T3TopiaApplicationContext; import fr.ird.t3.entities.T3TopiaApplicationContextBuilder; import fr.ird.t3.entities.user.JdbcConfiguration; +import fr.ird.t3.installer.T3InstallerConfigOption; +import io.ultreia.java4all.config.ApplicationConfig; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.i18n.I18n; @@ -37,7 +38,6 @@ import org.nuiton.util.FileUtil; import org.nuiton.util.ZipUtil; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; @@ -47,7 +47,6 @@ import java.sql.SQLException; import java.util.List; import java.util.Locale; import java.util.Objects; -import java.util.Properties; /** * Tool around T3 database. @@ -60,25 +59,27 @@ public class T3DatabaseTool { private static final Log log = LogFactory.getLog(T3DatabaseTool.class); + private final ApplicationConfig config; + + public T3DatabaseTool(ApplicationConfig config) { + this.config = config; + } + @SuppressWarnings("unused") - public void create(File connectionFile, String databaseName) throws IOException, SQLException { - createOrFill(connectionFile, databaseName, true); + public void create(String databaseName) throws IOException, SQLException { + createOrFill(databaseName, true); } @SuppressWarnings("unused") - public void fill(File connectionFile, String databaseName) throws IOException, SQLException { - createOrFill(connectionFile, databaseName, false); + public void fill(String databaseName) throws IOException, SQLException { + createOrFill(databaseName, false); } - private void createOrFill(File connectionFile, String databaseName, boolean create) throws IOException, SQLException { + private void createOrFill(String databaseName, boolean create) throws IOException, SQLException { - Objects.requireNonNull(connectionFile); - Preconditions.checkArgument(connectionFile.exists(), String.format("%s was not found!", connectionFile)); Objects.requireNonNull(databaseName); - T3Config config = Objects.requireNonNull(T3Config.instance()); - - File scriptsDirectory = config.getScriptsDirectory(); + File scriptsDirectory = new File(new File("scripts").getAbsolutePath()); Preconditions.checkArgument(scriptsDirectory.exists(), String.format("%s was not found!", scriptsDirectory)); File ddlDirectory = new File(scriptsDirectory, "ddl"); @@ -93,7 +94,7 @@ public class T3DatabaseTool { File[] postgisDataFiles = Objects.requireNonNull(postgisDataScript.listFiles((dir, name) -> name.endsWith(".zip"))); log.info("1/5 - Setup and connect to database..."); - T3TopiaApplicationContext applicationContext = setup(connectionFile, databaseName, create); + T3TopiaApplicationContext applicationContext = setup(databaseName, create); try { log.info("2/5 - Create database schema..."); @@ -121,19 +122,14 @@ public class T3DatabaseTool { } } - private T3TopiaApplicationContext setup(File connectionFile, String databaseName, boolean create) throws IOException, SQLException { + private T3TopiaApplicationContext setup(String databaseName, boolean create) throws SQLException { DefaultI18nInitializer i18nInitializer = new DefaultI18nInitializer("t3-i18n"); i18nInitializer.setMissingKeyReturnNull(true); I18n.init(i18nInitializer, Locale.getDefault()); - Properties p = new Properties(); - try (FileInputStream inputStream = new FileInputStream(connectionFile)) { - p.load(inputStream); - } - - String login = Objects.requireNonNull(p.getProperty("login"), String.format("login property not found in file %s", connectionFile)); - String password = Objects.requireNonNull(p.getProperty("password"), String.format("password property not found in file %s", connectionFile)); - String urlPattern = Objects.requireNonNull(p.getProperty("url"), String.format("url property not found in file %s", connectionFile)); + String login = Objects.requireNonNull(config.getOption(T3InstallerConfigOption.LOGIN.getKey())); + String password = Objects.requireNonNull(config.getOption(T3InstallerConfigOption.PASSWORD.getKey())); + String urlPattern = Objects.requireNonNull(config.getOption(T3InstallerConfigOption.URL.getKey())); String adminUrl = String.format(urlPattern, "postgres"); try (Connection connection = DriverManager.getConnection(adminUrl, login, password)) { ===================================== t3-installer/src/test/resources/log4j.properties ===================================== --- a/t3-installer/src/test/resources/log4j.properties +++ b/t3-installer/src/test/resources/log4j.properties @@ -1,6 +1,6 @@ ### # #%L -# T3 :: Test +# T3 :: Installer # %% # Copyright (C) 2010 - 2018 IRD, Code Lutin, Ultreia.io # %% View it on GitLab: https://gitlab.com/ultreiaio/ird-t3/commit/bdbead0f7f51591c6093c6df6eb252a57... --- View it on GitLab: https://gitlab.com/ultreiaio/ird-t3/commit/bdbead0f7f51591c6093c6df6eb252a57... You're receiving this email because of your account on gitlab.com.