Created
March 28, 2018 20:37
-
-
Save idsecurity/1092ade8b84b1ba1d750132e74cd30ea to your computer and use it in GitHub Desktop.
Quick and dirty script for adding views
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
/** | |
* USE AT YOUR OWN RISK - BACKUP THE DATABASE FIRST! | |
* 1) Download the additional report ZIP files from the Reporting Module | |
* 2) Place them in a separate directory | |
* 3) Place this file in the same directory | |
* 4) Copy the directory to the Postgres server | |
* 5) Change the information under the START CHANGE THIS heading | |
* 6) Run jjs from Java 8 or later from same directory: | |
* jjs -scripting installreports.js | |
*/ | |
//Declare Java stuff | |
var FilesSystem = Java.type("java.nio.file.FileSystem"); | |
var FileSystems = Java.type("java.nio.file.FileSystems") | |
var Files = Java.type("java.nio.file.Files") | |
var Path = Java.type("java.nio.file.Path") | |
var Paths = Java.type("java.nio.file.Paths") | |
var URI = Java.type("java.net.URI") | |
var HashMap = Java.type("java.util.HashMap") | |
var FileVisitOption = Java.type("java.nio.file.FileVisitOption") | |
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption") | |
var emptyMap = new HashMap() | |
var JavaStringArray = Java.type("java.lang.String[]") | |
var emptyStringArray = new JavaStringArray(0) | |
//End declare Java stuff | |
/** | |
* START CHANGE THIS | |
*/ | |
var pgHostname = "localhost" | |
var pgPort = "5432" | |
var pgDbName = "igops" | |
var pgUsername = "postgres" | |
var pgPassword = "12345" | |
var psqlPath = "C:\\netiq\\idm\\apps\\postgres\\bin\\psql.exe" | |
var workingDir = Paths.get("C:\\Users\\svciga\\Downloads\\ReportingAdditional") | |
/** | |
* END CHANGE THIS | |
*/ | |
//Run psql without asking for password | |
$ENV.PGPASSWORD = pgPassword | |
/** | |
* 1) Get all files whose filename ends with .zip | |
* 2) From each ZIP file find the file whose filename starts with "pg-" and ends with ".sql" | |
* 3) Extract the file | |
* 4) Call psql to apply the changes to the Postgres database | |
*/ | |
var findZipFiles = Files.walk(workingDir) | |
try { | |
findZipFiles.filter(function(zip) { | |
return zip.getFileName().toString().endsWith(".zip") } ).forEach(function(file) { | |
var toUri = file.toUri() | |
var uri = URI.create("jar:" + toUri.toString()) | |
var zipFileSystem = FileSystems.newFileSystem(uri, emptyMap) | |
print("Processing file: " + file.toString()) | |
var zipStream = Files.walk(zipFileSystem.getPath("/", emptyStringArray)) | |
zipStream.filter(function(file) { | |
return file.getFileName() != null && | |
file.getFileName().toString().startsWith("pg-") && | |
file.getFileName().toString().endsWith(".sql") | |
}).forEach(function(sqlFile) { | |
print(sqlFile) | |
var destination = Paths.get(workingDir.toString(), sqlFile.getFileName()) | |
Files.copy(sqlFile, destination, StandardCopyOption.REPLACE_EXISTING) | |
runpsql(destination.toString()) | |
}) | |
}) | |
} catch(e) { | |
print(e) | |
} | |
function runpsql(file) { | |
$EXEC("${psqlPath} -U ${pgUsername} -d ${pgDbName} -f ${file} -h ${pgHostname} -p ${pgPort}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment