Commit 7d33e5cb authored by Daniel Eggert's avatar Daniel Eggert
Browse files

added new gms-db-utils cli tool

parent 9ce3ede1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>gms-db-utils</artifactId>
<name>gms-db-utils</name>
<packaging>jar</packaging>
<parent>
<groupId>de.potsdam.gfz</groupId>
<artifactId>gms-utils</artifactId>
<version>0.0.1</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>de.potsdam.gfz.gms.DbUtils</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
<resource>.svg</resource>
<resource>.properties</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- <dependency> -->
<!-- <groupId>de.potsdam.gfz</groupId> -->
<!-- <artifactId>gms-utils-core</artifactId> -->
<!-- <version>0.0.1</version> -->
<!-- </dependency> -->
<dependency>
<groupId>de.potsdam.gfz</groupId>
<artifactId>gms-database</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
package de.potsdam.gfz.gms;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import de.potsdam.gfz.gms.database.SceneDatabase;
public class DbUtils {
private static final String OP_BACKUP_CONFIG = "b";
private static final String OP_RESTORE_CONFIG = "r";
private static final String OP_UPDATE_SCENE_STATUS = "u";
public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(Option.builder(OP_BACKUP_CONFIG).longOpt("backup-cfg").argName("file")
.desc("stores a copy of the gms config table in the given file").numberOfArgs(1).build());
options.addOption(Option.builder(OP_RESTORE_CONFIG).longOpt("restore-cfg").argName("file")
.desc("restores the db config table from the given file").numberOfArgs(1).build());
options.addOption(Option.builder(OP_UPDATE_SCENE_STATUS).longOpt("update-scene-status").hasArg(false)
.desc("updates the proc_level of the scenes to match the downloaded scenes on the harddrive").build());
DefaultParser parser = new DefaultParser();
try {
CommandLine cli = parser.parse(options, args);
if (cli.hasOption(OP_BACKUP_CONFIG)) {
backupConfig(cli.getOptionValue(OP_BACKUP_CONFIG));
} else if (cli.hasOption(OP_RESTORE_CONFIG)) {
System.err.println("not yet implemented");
restoreConfig(cli.getOptionValue(OP_RESTORE_CONFIG));
} else if (cli.hasOption(OP_UPDATE_SCENE_STATUS)) {
System.err.println("not yet implemented");
} else {
printHelp(options);
}
} catch (ParseException p) {
System.err.println(p.getMessage());
System.out.println();
printHelp(options);
}
}
private static void printHelp(Options options) {
HelpFormatter hf = new HelpFormatter();
hf.printHelp("java -jar gms-db-utils.jar", options, true);
}
private static void backupConfig(String filepath) throws IOException {
System.out.println("creating backup of gms config table");
SceneDatabase db = SceneDatabase.getInstance();
// get current config
Properties props = new Properties();
props.putAll(db.getConfig());
props.store(new FileOutputStream(filepath), "backup of gms db config table");
}
private static void restoreConfig(String filepath) throws IOException {
Map<String, String> dbCfg = SceneDatabase.getInstance().getConfig();
Properties localCfg = new Properties();
localCfg.load(new FileInputStream(filepath));
for (Entry<String, String> entry : dbCfg.entrySet()) {
if (!entry.getValue().equals(localCfg.getProperty(entry.getKey()))) {
System.err.println("not matching cfg values found for key '" + entry.getKey() + "'");
System.err.println("dbCfg value: " + entry.getValue());
System.err.println("localCfg value: " + localCfg.getProperty(entry.getKey()));
}
}
}
private static void updateSceneStatus() throws SQLException {
// reset all scenes to METADATA proc_level
resetScenesToMetadataLevel();
SceneDatabase db = SceneDatabase.getInstance();
Map<String, String> cfg = db.getConfig();
final String downloadFolder = cfg.get("path_data_root") + cfg.get("foldername_download");
// iterate over datasets
for (String dataset : getDatasetNames()) {
// TODO: continue here
}
}
private static void resetScenesToMetadataLevel() throws SQLException {
System.out.println("resetting all scenes back to proc_level METADATA");
SceneDatabase.getInstance().prepareCustomStatement("update scenes set proc_level='METADATA'::proc_level;")
.executeUpdate();
}
private static List<String> getDatasetNames() throws SQLException {
System.out.println("getting dataset names");
ResultSet rs = SceneDatabase.getInstance().placeCustomQuery("select name from datasets;");
List<String> datasets = new ArrayList<>();
while (rs.next()) {
datasets.add(rs.getString(1));
}
return datasets;
}
}
# host the gms database is running, default: geoms.gfz-potsdam.de
db.host = geoms.gfz-potsdam.de
# port the gms database is listening to, default: 5432
db.port = 5432
# name of the database, default: geomultisens
db.name = geomultisens
# database user, default: gmsdb
db.user = gmsdb
# database password for the above user, default: gmsdb
db.pass = gmsdb
# host the index mediator server is running on, default: value of db.host
index.host = geoms.gfz-potsdam.de
# port the index mediator server is listening to, default: 8654
index.port = 8654
\ No newline at end of file
......@@ -16,6 +16,7 @@
<modules>
<module>gms-utils-core</module>
<module>gms-mosaic-linker</module>
<module>gms-db-utils</module>
<!--<module>gms-geoserver-publisher</module>-->
</modules>
</project>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment