PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/amps-maven-plugin/src/main/java/com/atlassian/maven/plugins/amps/UpdateMojo.java

https://bitbucket.org/atlassian/amps
Java | 159 lines | 123 code | 23 blank | 13 comment | 21 complexity | 4624bf65a1ae6494c81fa3ad313ace79 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. package com.atlassian.maven.plugins.amps;
  2. import com.atlassian.maven.plugins.amps.util.ZipUtils;
  3. import com.atlassian.maven.plugins.updater.LocalSdk;
  4. import com.atlassian.maven.plugins.updater.SdkPackageType;
  5. import com.atlassian.maven.plugins.updater.SdkResource;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.apache.maven.plugin.MojoExecutionException;
  8. import org.apache.maven.plugin.MojoFailureException;
  9. import org.apache.maven.plugins.annotations.Component;
  10. import org.apache.maven.plugins.annotations.Mojo;
  11. import org.apache.maven.plugins.annotations.Parameter;
  12. import java.io.BufferedReader;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import static com.atlassian.maven.plugins.amps.util.FileUtils.makeExecutable;
  19. import static java.lang.Thread.currentThread;
  20. import static java.util.Collections.addAll;
  21. /**
  22. * Downloads the latest version of the SDK and installs it into ATLAS_HOME.
  23. */
  24. @Mojo(name = "update", requiresProject = false)
  25. public class UpdateMojo extends AbstractAmpsMojo {
  26. @Component
  27. private SdkResource sdkResource;
  28. @Component
  29. private LocalSdk localSdk;
  30. /**
  31. * The version to update the SDK to (defaults to latest)
  32. */
  33. @Parameter(property = "update.version")
  34. private String updateVersion;
  35. /**
  36. * If present, use this file as the SDK archive instead of trying to download it from MPAC.
  37. */
  38. @Parameter(property = "sdk.archive.path")
  39. private String sdkArchivePath;
  40. @Override
  41. public void execute() throws MojoExecutionException, MojoFailureException {
  42. SdkPackageType packageType = localSdk.sdkPackageType();
  43. checkUpdatePreconditions(packageType);
  44. File sdkArchive;
  45. if (StringUtils.isNotBlank(sdkArchivePath)) {
  46. // use local file for SDK update
  47. sdkArchive = new File(sdkArchivePath);
  48. if (!sdkArchive.isFile() || !sdkArchive.canRead()) {
  49. throw new MojoExecutionException("Can't read archive file at " + sdkArchivePath);
  50. }
  51. getLog().info("Using local file " + sdkArchive.getAbsolutePath() + " for SDK install.");
  52. } else {
  53. // determine which version to download from PAC
  54. String downloadVersion = StringUtils.isNotBlank(updateVersion) ?
  55. updateVersion : sdkResource.getLatestSdkVersion(packageType);
  56. String ourVersion = getAmpsPluginVersion();
  57. if (ourVersion.equals(downloadVersion)) {
  58. getLog().info("SDK is already at the latest version: " + ourVersion);
  59. return;
  60. }
  61. getLog().info("Downloading SDK version " + downloadVersion + " from marketplace.atlassian.com...");
  62. sdkArchive = sdkResource.downloadSdk(packageType, downloadVersion);
  63. getLog().info("Download complete.");
  64. getLog().debug("SDK download artifact at " + sdkArchive.getAbsolutePath());
  65. }
  66. getLog().info("Beginning upgrade of SDK.");
  67. if (packageType == SdkPackageType.TGZ) {
  68. installSdkFromTarGz(sdkArchive);
  69. } else {
  70. installSdkFromExecutable(sdkArchive, packageType);
  71. }
  72. getLog().info("SDK upgrade successful.");
  73. }
  74. private void installSdkFromTarGz(File sdkZip) throws MojoExecutionException {
  75. String sdkHome = localSdk.sdkHomeDir();
  76. try {
  77. ZipUtils.untargz(sdkZip, sdkHome, 1); // skip first directory path of artifact name/version
  78. } catch (IOException e) {
  79. throw new MojoExecutionException("Error extracting new SDK", e);
  80. }
  81. }
  82. private void installSdkFromExecutable(final File sdkInstaller, final SdkPackageType packageType)
  83. throws MojoExecutionException {
  84. makeExecutable(sdkInstaller);
  85. final List<String> commands = new ArrayList<>();
  86. addAll(commands, packageType.installCommands());
  87. commands.add(sdkInstaller.getAbsolutePath());
  88. final ProcessBuilder installerBuilder = new ProcessBuilder(commands);
  89. try {
  90. Process installer = installerBuilder.start();
  91. try (BufferedReader in = new BufferedReader(new InputStreamReader(installer.getInputStream()));
  92. BufferedReader err = new BufferedReader(new InputStreamReader(installer.getErrorStream()))) {
  93. String line;
  94. getLog().info("Output from installer process follow:");
  95. while ((line = in.readLine()) != null) {
  96. getLog().info(line);
  97. }
  98. if (err.ready()) {
  99. getLog().error("Errors from installer process follow:");
  100. while ((line = err.readLine()) != null) {
  101. getLog().error(line);
  102. }
  103. }
  104. }
  105. waitFor(installer);
  106. } catch (IOException e) {
  107. throw new MojoExecutionException("error from installer subprocess", e);
  108. }
  109. }
  110. private void waitFor(final Process installer) throws MojoExecutionException {
  111. try {
  112. if (installer.waitFor() != 0) {
  113. throw new MojoExecutionException("Installer failed; see above for errors.");
  114. }
  115. } catch (InterruptedException e) {
  116. currentThread().interrupt();
  117. throw new MojoExecutionException("Subprocess installer interrupted", e);
  118. }
  119. }
  120. private void checkUpdatePreconditions(SdkPackageType packageType) throws MojoExecutionException {
  121. if (packageType == SdkPackageType.TGZ) {
  122. // we're about to overwrite an existing tar.gz. Make sure the directory
  123. // is defined by the atlas-update script and is writable.
  124. String sdkHome = localSdk.sdkHomeDir();
  125. if (sdkHome == null) {
  126. throw new MojoExecutionException("SDK update must be run from the atlas-update script.");
  127. }
  128. File sdkHomeDir = new File(sdkHome);
  129. if (!sdkHomeDir.exists() || !sdkHomeDir.canWrite()) {
  130. throw new MojoExecutionException("To update successfully, SDK home directory " + sdkHome +
  131. " must be writable by the current user. The current user does not have appropriate permissions.");
  132. }
  133. getLog().debug("Detected current SDK install from ATLAS_HOME in " + sdkHomeDir.getAbsolutePath());
  134. }
  135. }
  136. }