-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MID-8842 upgrade, more ninja code regarding upgrade steps
- Loading branch information
Showing
15 changed files
with
268 additions
and
107 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
101 changes: 101 additions & 0 deletions
101
.../src/main/java/com/evolveum/midpoint/ninja/action/upgrade/DownloadDistributionAction.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,101 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.fusesource.jansi.Ansi; | ||
|
||
import com.evolveum.midpoint.ninja.action.Action; | ||
import com.evolveum.midpoint.ninja.util.Log; | ||
|
||
public class DownloadDistributionAction extends Action<DownloadDistributionOptions> { | ||
|
||
@Override | ||
public void execute() throws Exception { | ||
final Log log = context.getLog(); | ||
|
||
final File tempDirectory = options.getTempDirectory(); | ||
|
||
File distributionZipFile = options.getDistributionArchive(); | ||
if (distributionZipFile == null || !distributionZipFile.exists()) { | ||
DistributionManager manager = new DistributionManager(tempDirectory); | ||
ProgressListener listener = new ConsoleProgressListener(); | ||
|
||
distributionZipFile = manager.downloadDistribution(UpgradeConstants.SUPPORTED_VERSION_TARGET, listener); | ||
} else { | ||
log.info(Ansi.ansi().fgGreen().a("Distribution zip already downloaded here: " + distributionZipFile.getAbsolutePath()).reset().toString()); | ||
} | ||
|
||
File distributionDirectory = unzipDistribution(distributionZipFile); | ||
|
||
log.info(Ansi.ansi().a("Distribution zip: ").a(distributionZipFile.getAbsolutePath()).reset().toString()); | ||
log.info(Ansi.ansi().a("Distribution directory: ").fgGreen().a(distributionDirectory.getAbsolutePath()).reset().toString()); | ||
|
||
// return new DownloadDistributionResult(distributionZipFile, distributionDirectory); // todo pass result somewhere | ||
} | ||
|
||
private File unzipDistribution(File distributionZip) throws IOException { | ||
final File tempDirectory = options.getTempDirectory(); | ||
|
||
String name = distributionZip.getName(); | ||
File distribution = new File(tempDirectory, StringUtils.left(name, name.length() - 4)); | ||
|
||
byte[] buffer = new byte[1024]; | ||
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(distributionZip))) { | ||
ZipEntry zipEntry = zis.getNextEntry(); | ||
while (zipEntry != null) { | ||
File newFile = newFile(distribution, zipEntry); | ||
if (zipEntry.isDirectory()) { | ||
if (!newFile.isDirectory() && !newFile.mkdirs()) { | ||
throw new IOException("Failed to create directory " + newFile); | ||
} | ||
} else { | ||
// fix for Windows-created archives | ||
File parent = newFile.getParentFile(); | ||
if (!parent.isDirectory() && !parent.mkdirs()) { | ||
throw new IOException("Failed to create directory " + parent); | ||
} | ||
|
||
// write file content | ||
FileOutputStream fos = new FileOutputStream(newFile); | ||
int len; | ||
while ((len = zis.read(buffer)) > 0) { | ||
fos.write(buffer, 0, len); | ||
} | ||
fos.close(); | ||
} | ||
zipEntry = zis.getNextEntry(); | ||
} | ||
} | ||
|
||
File[] files = distribution.listFiles(); | ||
if (files != null && files.length == 1) { | ||
File zipRootDirectory = files[0]; | ||
for (File file : zipRootDirectory.listFiles()) { | ||
FileUtils.moveToDirectory(file, distribution, false); | ||
} | ||
zipRootDirectory.delete(); | ||
} | ||
|
||
return distribution; | ||
} | ||
|
||
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException { | ||
File destFile = new File(destinationDir, zipEntry.getName()); | ||
|
||
String destDirPath = destinationDir.getCanonicalPath(); | ||
String destFilePath = destFile.getCanonicalPath(); | ||
|
||
if (!destFilePath.startsWith(destDirPath + File.separator)) { | ||
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName()); | ||
} | ||
|
||
return destFile; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...src/main/java/com/evolveum/midpoint/ninja/action/upgrade/DownloadDistributionOptions.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,21 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
import java.io.File; | ||
|
||
import com.beust.jcommander.Parameter; | ||
|
||
public class DownloadDistributionOptions extends UpgradeCommonOptions { | ||
|
||
public static final String P_DISTRIBUTION_ARCHIVE = "--distribution-archive"; | ||
|
||
@Parameter(names = { P_DISTRIBUTION_ARCHIVE }, descriptionKey = "upgrade.distributionArchive") | ||
private File distributionArchive; | ||
|
||
public File getDistributionArchive() { | ||
return distributionArchive; | ||
} | ||
|
||
public void setDistributionArchive(File distributionArchive) { | ||
this.distributionArchive = distributionArchive; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../ninja/src/main/java/com/evolveum/midpoint/ninja/action/upgrade/UpgradeCommonOptions.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 com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
import java.io.File; | ||
|
||
import com.beust.jcommander.Parameter; | ||
import org.apache.commons.io.FileUtils; | ||
|
||
public class UpgradeCommonOptions { | ||
|
||
public static final String P_TEMP_DIR_LONG = "--temp-dir"; | ||
|
||
@Parameter(names = { P_TEMP_DIR_LONG }, descriptionKey = "upgrade.tempDir") // todo fix key | ||
private File tempDirectory; | ||
|
||
public File getTempDirectory() { | ||
if (this.tempDirectory == null) { | ||
this.tempDirectory = new File(FileUtils.getTempDirectory(), UpgradeConstants.UPGRADE_TEMP_DIRECTORY); | ||
} | ||
|
||
return tempDirectory; | ||
} | ||
|
||
public void setTempDirectory(File tempDirectory) { | ||
this.tempDirectory = tempDirectory; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ninja/src/main/java/com/evolveum/midpoint/ninja/action/upgrade/UpgradeDatabaseAction.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,11 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
import com.evolveum.midpoint.ninja.action.Action; | ||
|
||
public class UpgradeDatabaseAction extends Action<UpgradeDatabaseOptions> { | ||
|
||
@Override | ||
public void execute() throws Exception { | ||
|
||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...inja/src/main/java/com/evolveum/midpoint/ninja/action/upgrade/UpgradeDatabaseOptions.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,4 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
public class UpgradeDatabaseOptions { | ||
} |
30 changes: 30 additions & 0 deletions
30
...a/src/main/java/com/evolveum/midpoint/ninja/action/upgrade/UpgradeDistributionAction.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,30 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
import com.evolveum.midpoint.ninja.action.Action; | ||
|
||
public class UpgradeDistributionAction extends Action<UpgradeDistributionOptions> { | ||
|
||
@Override | ||
public void execute() throws Exception { | ||
DownloadDistributionOptions downloadOptions = new DownloadDistributionOptions(); | ||
// todo options | ||
|
||
DownloadDistributionAction downloadAction = new DownloadDistributionAction(); | ||
downloadAction.init(context, downloadOptions); | ||
downloadAction.execute(); | ||
|
||
UpgradeInstallationOptions installationOptions = new UpgradeInstallationOptions(); | ||
// todo options | ||
|
||
UpgradeInstallationAction installationAction = new UpgradeInstallationAction(); | ||
installationAction.init(context, installationOptions); | ||
installationAction.execute(); | ||
|
||
UpgradeDatabaseOptions databaseOptions = new UpgradeDatabaseOptions(); | ||
// todo options | ||
|
||
UpgradeDatabaseAction databaseAction = new UpgradeDatabaseAction(); | ||
databaseAction.init(context, databaseOptions); | ||
databaseAction.execute(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
.../src/main/java/com/evolveum/midpoint/ninja/action/upgrade/UpgradeDistributionOptions.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,4 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
public class UpgradeDistributionOptions { | ||
} |
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
36 changes: 36 additions & 0 deletions
36
.../src/main/java/com/evolveum/midpoint/ninja/action/upgrade/UpgradeInstallationOptions.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,36 @@ | ||
package com.evolveum.midpoint.ninja.action.upgrade; | ||
|
||
import java.io.File; | ||
|
||
public class UpgradeInstallationOptions { | ||
|
||
private File distributionDirectory; | ||
|
||
private boolean backup; | ||
|
||
private File installationDirectory; | ||
|
||
public File getDistributionDirectory() { | ||
return distributionDirectory; | ||
} | ||
|
||
public void setDistributionDirectory(File distributionDirectory) { | ||
this.distributionDirectory = distributionDirectory; | ||
} | ||
|
||
public boolean isBackup() { | ||
return backup; | ||
} | ||
|
||
public void setBackup(boolean backup) { | ||
this.backup = backup; | ||
} | ||
|
||
public File getInstallationDirectory() { | ||
return installationDirectory; | ||
} | ||
|
||
public void setInstallationDirectory(File installationDirectory) { | ||
this.installationDirectory = installationDirectory; | ||
} | ||
} |
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
Oops, something went wrong.