-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
459 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
.../src/main/java/bio/terra/workspace/azureDatabaseUtils/process/LaunchProcessException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package bio.terra.workspace.azureDatabaseUtils.process; | ||
|
||
/** | ||
* Custom exception class for system or internal exceptions. These represent errors that the user | ||
* cannot fix. | ||
*/ | ||
public class LaunchProcessException extends RuntimeException { | ||
/** | ||
* Constructs an exception with the given message. The cause is set to null. | ||
* | ||
* @param message description of error that may help with debugging | ||
*/ | ||
public LaunchProcessException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Constructs an exception with the given message and cause. | ||
* | ||
* @param message description of error that may help with debugging | ||
* @param cause underlying exception that can be logged for debugging purposes | ||
*/ | ||
public LaunchProcessException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...ls/src/main/java/bio/terra/workspace/azureDatabaseUtils/process/LocalProcessLauncher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package bio.terra.workspace.azureDatabaseUtils.process; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** This class provides utility methods for launching local child processes. */ | ||
public class LocalProcessLauncher { | ||
private Process process; | ||
|
||
public enum Output { | ||
OUT, | ||
ERROR | ||
} | ||
|
||
/** | ||
* Executes a command in a separate process from the current working directory (i.e. the same | ||
* place as this Java process is running). | ||
* | ||
* @param command the command and arguments to execute | ||
* @param envVars the environment variables to set or overwrite if already defined | ||
*/ | ||
public void launchProcess(List<String> command, Map<String, String> envVars) { | ||
launchProcess(command, envVars, null); | ||
} | ||
|
||
/** | ||
* Executes a command in a separate process from the given working directory, with the given | ||
* environment variables set beforehand. | ||
* | ||
* @param command the command and arguments to execute | ||
* @param envVars the environment variables to set or overwrite if already defined | ||
* @param workingDirectory the working directory to launch the process from | ||
*/ | ||
public void launchProcess( | ||
List<String> command, Map<String, String> envVars, Path workingDirectory) { | ||
// build and run process from the specified working directory | ||
ProcessBuilder procBuilder = new ProcessBuilder(command); | ||
if (workingDirectory != null) { | ||
procBuilder.directory(workingDirectory.toFile()); | ||
} | ||
if (envVars != null) { | ||
Map<String, String> procEnvVars = procBuilder.environment(); | ||
procEnvVars.putAll(envVars); | ||
} | ||
|
||
try { | ||
process = procBuilder.start(); | ||
} catch (IOException ioEx) { | ||
throw new LaunchProcessException("Error launching local process", ioEx); | ||
} | ||
} | ||
|
||
/** | ||
* Stream standard out/err from the child process to the CLI console. | ||
* | ||
* @param type specifies which process stream to get data from | ||
*/ | ||
public InputStream getOutputForProcess(Output type) { | ||
if (type == Output.ERROR) { | ||
return process.getErrorStream(); | ||
} | ||
|
||
return process.getInputStream(); | ||
} | ||
|
||
/** Block until the child process terminates, then return its exit code. */ | ||
public int waitForTerminate() { | ||
try { | ||
return process.waitFor(); | ||
} catch (InterruptedException intEx) { | ||
Thread.currentThread().interrupt(); | ||
throw new LaunchProcessException("Error waiting for child process to terminate", intEx); | ||
} | ||
} | ||
|
||
/** Get stdout input stream from the child process. */ | ||
public InputStream getInputStream() { | ||
return process.getInputStream(); | ||
} | ||
|
||
/** Get stdin output stream from the child process. */ | ||
public OutputStream getOutputStream() { | ||
return process.getOutputStream(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ls/src/main/java/bio/terra/workspace/azureDatabaseUtils/runners/PgDumpDatabaseRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package bio.terra.workspace.azureDatabaseUtils.runners; | ||
|
||
import bio.terra.workspace.azureDatabaseUtils.database.DatabaseService; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Profile("PgDumpDatabase") | ||
@Component | ||
public class PgDumpDatabaseRunner implements ApplicationRunner { | ||
|
||
@Value("${env.db.connectToDatabase}") | ||
private String sourceDbName; | ||
|
||
@Value("${env.db.url}") | ||
private String sourceDbHost; | ||
|
||
@Value("${env.db.port}") | ||
private String sourceDbPort; | ||
|
||
@Value("${env.db.user}") | ||
private String sourceDbUser; | ||
|
||
@Value("${env.params.dumpfileName}") | ||
private String dumpfileName; | ||
|
||
@Value("${env.params.destinationWorkspaceId}") | ||
private String destinationWorkspaceId; | ||
|
||
@Value("${env.params.blobstorageDetails}") | ||
private String blobstorageDetails; | ||
|
||
private final DatabaseService databaseService; | ||
|
||
public PgDumpDatabaseRunner(DatabaseService databaseService) { | ||
this.databaseService = databaseService; | ||
} | ||
|
||
@Override | ||
public void run(ApplicationArguments args) { | ||
// should I reuse `newDbName`, or create a new param `cloneDbName`? | ||
databaseService.pgDump( | ||
sourceDbName, | ||
sourceDbHost, | ||
sourceDbPort, | ||
sourceDbUser, | ||
dumpfileName, | ||
destinationWorkspaceId, | ||
blobstorageDetails); | ||
} | ||
} |
Oops, something went wrong.