com.google.cloud.tools.managedcloudsdk.ProgressListener Java Examples

The following examples show how to use com.google.cloud.tools.managedcloudsdk.ProgressListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloudSdkUpdateJobTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testRun_sdkInstalled_updateFailed()
    throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException,
        InterruptedException, CommandExitException, CommandExecutionException {
  when(managedCloudSdk.isInstalled()).thenReturn(true);
  when(managedCloudSdk.isUpToDate()).thenReturn(false);
  when(managedCloudSdk.newUpdater()).thenReturn(sdkUpdater);
  CommandExecutionException exception = new CommandExecutionException(new RuntimeException());
  doThrow(exception).when(sdkUpdater)
      .update(any(ProgressListener.class), any(ConsoleListener.class));

  CloudSdkUpdateJob job = newCloudSdkUpdateJob();
  job.schedule();
  job.join();

  assertEquals(IStatus.ERROR, job.getResult().getSeverity());
  assertEquals(exception, job.getResult().getException());
  verify(managedCloudSdk).isInstalled();
  verify(managedCloudSdk).isUpToDate();
  verify(managedCloudSdk).newUpdater();
  verify(sdkUpdater).update(any(ProgressListener.class), any(ConsoleListener.class));
  verify(managedCloudSdk).getSdkHome(); // used to obtain old version
  verifyNoMoreInteractions(managedCloudSdk);
}
 
Example #2
Source File: CloudSdkUpdateJobTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testRun_sdkInstalled_updateSuccess()
    throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException,
        InterruptedException, CommandExecutionException, CommandExitException {
  when(managedCloudSdk.isInstalled()).thenReturn(true);
  when(managedCloudSdk.isUpToDate()).thenReturn(false);
  when(managedCloudSdk.newUpdater()).thenReturn(sdkUpdater);

  CloudSdkUpdateJob job = newCloudSdkUpdateJob();
  job.schedule();
  job.join();

  assertTrue(job.getResult().isOK());
  verify(managedCloudSdk).isInstalled();
  verify(managedCloudSdk).isUpToDate();
  verify(managedCloudSdk).newUpdater();
  verify(sdkUpdater).update(any(ProgressListener.class), any(ConsoleListener.class));
  verify(managedCloudSdk, times(2)).getSdkHome(); // used to log old and new versions
  verifyNoMoreInteractions(managedCloudSdk);
}
 
Example #3
Source File: CloudSdkInstallJobTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testRun_sdkInstalled_componentNotInstalled()
    throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException,
        InterruptedException, CommandExecutionException, CommandExitException {
  when(managedCloudSdk.isInstalled()).thenReturn(true);
  when(managedCloudSdk.hasComponent(any(SdkComponent.class))).thenReturn(false);

  CloudSdkInstallJob job = newCloudSdkInstallJob();
  job.schedule();
  job.join();

  assertTrue(job.getResult().isOK());
  verify(managedCloudSdk, never()).newInstaller();
  verify(managedCloudSdk).newComponentInstaller();
  verify(componentInstaller)
      .installComponent(
          any(SdkComponent.class), any(ProgressListener.class), any(ConsoleListener.class));
}
 
Example #4
Source File: CloudSdkInstallJobTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testRun_notInstalled()
    throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException,
        InterruptedException, IOException, SdkInstallerException, CommandExecutionException,
        CommandExitException {
  when(managedCloudSdk.isInstalled()).thenReturn(false);
  when(managedCloudSdk.hasComponent(any(SdkComponent.class))).thenReturn(false);

  CloudSdkInstallJob job = newCloudSdkInstallJob();
  job.schedule();
  job.join();

  assertTrue(job.getResult().isOK());
  verify(managedCloudSdk).newInstaller();
  verify(managedCloudSdk).newComponentInstaller();
  verify(sdkInstaller).install(any(ProgressListener.class), any(ConsoleListener.class));
  verify(componentInstaller)
      .installComponent(
          any(SdkComponent.class), any(ProgressListener.class), any(ConsoleListener.class));
}
 
Example #5
Source File: SdkComponentInstaller.java    From appengine-plugins-core with Apache License 2.0 6 votes vote down vote up
/**
 * Install a component.
 *
 * @param component component to install
 * @param progressListener listener to action progress feedback
 * @param consoleListener listener to process console feedback
 */
public void installComponent(
    SdkComponent component, ProgressListener progressListener, ConsoleListener consoleListener)
    throws InterruptedException, CommandExitException, CommandExecutionException {

  progressListener.start("Installing " + component.toString(), ProgressListener.UNKNOWN);

  Map<String, String> environment = null;
  if (pythonCopier != null) {
    environment = pythonCopier.copyPython();
  }

  Path workingDirectory = gcloudPath.getRoot();
  List<String> command =
      Arrays.asList(
          gcloudPath.toString(), "components", "install", component.toString(), "--quiet");
  commandRunner.run(command, workingDirectory, environment, consoleListener);
  progressListener.done();
}
 
Example #6
Source File: Installer.java    From appengine-plugins-core with Apache License 2.0 6 votes vote down vote up
/** Install a cloud sdk (only run this on LATEST). */
public void install()
    throws CommandExitException, CommandExecutionException, InterruptedException {
  List<String> command =
      new ArrayList<>(installScriptProvider.getScriptCommandLine(installedSdkRoot));
  command.add("--path-update=false"); // don't update user's path
  command.add("--command-completion=false"); // don't add command completion
  command.add("--quiet"); // don't accept user input during install
  command.add("--usage-reporting=" + usageReporting); // usage reporting passthrough

  Path workingDirectory = installedSdkRoot.getParent();
  Map<String, String> installerEnvironment = installScriptProvider.getScriptEnvironment();

  progressListener.start("Installing Cloud SDK", ProgressListener.UNKNOWN);
  commandRunner.run(command, workingDirectory, installerEnvironment, consoleListener);
  progressListener.done();
}
 
Example #7
Source File: DownloadCloudSdkTask.java    From app-gradle-plugin with Apache License 2.0 5 votes vote down vote up
/** Task entrypoint : Download/update Cloud SDK. */
@TaskAction
public void downloadCloudSdkAction()
    throws ManagedSdkVerificationException, ManagedSdkVersionMismatchException,
        InterruptedException, CommandExecutionException, SdkInstallerException,
        CommandExitException, IOException {
  // managedCloudSdk is set by AppEngineCorePluginConfiguration if the cloud SDK home is empty
  if (managedCloudSdk == null) {
    throw new GradleException("Cloud SDK home path must not be configured to run this task.");
  }

  ProgressListener progressListener = new NoOpProgressListener();
  ConsoleListener consoleListener = new DownloadCloudSdkTaskConsoleListener(getProject());

  // Install sdk if not installed
  if (!managedCloudSdk.isInstalled()) {
    SdkInstaller installer = managedCloudSdk.newInstaller();
    installer.install(progressListener, consoleListener);
  }

  // install components
  if (components != null) {
    for (SdkComponent component : components) {
      if (!managedCloudSdk.hasComponent(component)) {
        managedCloudSdk
            .newComponentInstaller()
            .installComponent(component, progressListener, consoleListener);
      }
    }
  }

  // If version is set to LATEST, update Cloud SDK
  if (!managedCloudSdk.isUpToDate()) {
    SdkUpdater updater = managedCloudSdk.newUpdater();
    updater.update(progressListener, consoleListener);
  }
}
 
Example #8
Source File: SdkUpdater.java    From appengine-plugins-core with Apache License 2.0 5 votes vote down vote up
/**
 * Update the Cloud SDK.
 *
 * @param progressListener listener to action progress feedback
 * @param consoleListener listener to process console feedback
 */
public void update(ProgressListener progressListener, ConsoleListener consoleListener)
    throws InterruptedException, CommandExitException, CommandExecutionException {
  progressListener.start("Updating Cloud SDK", ProgressListener.UNKNOWN);

  Map<String, String> environment = null;
  if (pythonCopier != null) {
    environment = pythonCopier.copyPython();
  }

  Path workingDirectory = gcloudPath.getRoot();
  List<String> command = Arrays.asList(gcloudPath.toString(), "components", "update", "--quiet");
  commandRunner.run(command, workingDirectory, environment, consoleListener);
  progressListener.done();
}
 
Example #9
Source File: Installer.java    From appengine-plugins-core with Apache License 2.0 5 votes vote down vote up
/** Instantiated by {@link InstallerFactory}. */
Installer(
    Path installedSdkRoot,
    InstallScriptProvider installScriptProvider,
    boolean usageReporting,
    ProgressListener progressListener,
    ConsoleListener consoleListener,
    CommandRunner commandRunner) {
  this.installedSdkRoot = installedSdkRoot;
  this.installScriptProvider = installScriptProvider;
  this.usageReporting = usageReporting;
  this.progressListener = progressListener;
  this.consoleListener = consoleListener;
  this.commandRunner = commandRunner;
}
 
Example #10
Source File: InstallerFactory.java    From appengine-plugins-core with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new {@link Installer} instance.
 *
 * @param installedSdkRoot path to the Cloud SDK directory
 * @param progressListener listener on installer script output
 * @return a {@link Installer} instance.
 */
Installer newInstaller(
    Path installedSdkRoot, ProgressListener progressListener, ConsoleListener consoleListener) {

  return new Installer(
      installedSdkRoot,
      getInstallScriptProvider(),
      usageReporting,
      progressListener,
      consoleListener,
      CommandRunner.newRunner());
}
 
Example #11
Source File: Extractor.java    From appengine-plugins-core with Apache License 2.0 5 votes vote down vote up
/** Use {@link ExtractorFactory} to instantiate. */
Extractor(
    Path archive,
    Path destination,
    ExtractorProvider extractorProvider,
    ProgressListener progressListener) {
  this.archive = archive;
  this.destination = destination;
  this.extractorProvider = extractorProvider;
  this.progressListener = progressListener;
}
 
Example #12
Source File: Downloader.java    From appengine-plugins-core with Apache License 2.0 5 votes vote down vote up
/** Use {@link DownloaderFactory} to instantiate. */
Downloader(
    URL source, Path destinationFile, String userAgentString, ProgressListener progressListener) {
  this.address = source;
  this.destinationFile = destinationFile;
  this.userAgentString = userAgentString;
  this.progressListener = progressListener;
}
 
Example #13
Source File: ZipExtractorProvider.java    From appengine-plugins-core with Apache License 2.0 4 votes vote down vote up
@Override
public void extract(Path archive, Path destination, ProgressListener progressListener)
    throws IOException {

  progressListener.start(
      "Extracting archive: " + archive.getFileName(), ProgressListener.UNKNOWN);

  String canonicalDestination = destination.toFile().getCanonicalPath();

  // Use ZipFile instead of ZipArchiveInputStream so that we can obtain file permissions
  // on unix-like systems via getUnixMode(). ZipArchiveInputStream doesn't have access to
  // all the zip file data and will return "0" for any call to getUnixMode().
  try (ZipFile zipFile = new ZipFile(archive.toFile())) {
    // TextProgressBar progressBar = textBarFactory.newProgressBar(messageListener, count);
    Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
    while (zipEntries.hasMoreElements()) {
      ZipArchiveEntry entry = zipEntries.nextElement();
      Path entryTarget = destination.resolve(entry.getName());

      String canonicalTarget = entryTarget.toFile().getCanonicalPath();
      if (!canonicalTarget.startsWith(canonicalDestination + File.separator)) {
        throw new IOException("Blocked unzipping files outside destination: " + entry.getName());
      }

      progressListener.update(1);
      logger.fine(entryTarget.toString());

      if (entry.isDirectory()) {
        if (!Files.exists(entryTarget)) {
          Files.createDirectories(entryTarget);
        }
      } else {
        if (!Files.exists(entryTarget.getParent())) {
          Files.createDirectories(entryTarget.getParent());
        }
        try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) {
          try (InputStream in = zipFile.getInputStream(entry)) {
            IOUtils.copy(in, out);
            PosixFileAttributeView attributeView =
                Files.getFileAttributeView(entryTarget, PosixFileAttributeView.class);
            if (attributeView != null) {
              attributeView.setPermissions(
                  PosixUtil.getPosixFilePermissions(entry.getUnixMode()));
            }
          }
        }
      }
    }
  }
  progressListener.done();
}
 
Example #14
Source File: ProgressVerifier.java    From appengine-plugins-core with Apache License 2.0 4 votes vote down vote up
public static void verifyUnknownProgress(ProgressListener mockProgressListener, String message) {
  Mockito.verify(mockProgressListener).start(message, -1);
  // any update calls are irrelevant, they may or may not be called
  Mockito.verify(mockProgressListener).done();
}
 
Example #15
Source File: TarGzExtractorProvider.java    From appengine-plugins-core with Apache License 2.0 4 votes vote down vote up
@Override
public void extract(Path archive, Path destination, ProgressListener progressListener)
    throws IOException {

  progressListener.start(
      "Extracting archive: " + archive.getFileName(), ProgressListener.UNKNOWN);

  String canonicalDestination = destination.toFile().getCanonicalPath();

  GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(Files.newInputStream(archive));
  try (TarArchiveInputStream in = new TarArchiveInputStream(gzipIn)) {
    TarArchiveEntry entry;
    while ((entry = in.getNextTarEntry()) != null) {
      Path entryTarget = destination.resolve(entry.getName());

      String canonicalTarget = entryTarget.toFile().getCanonicalPath();
      if (!canonicalTarget.startsWith(canonicalDestination + File.separator)) {
        throw new IOException("Blocked unzipping files outside destination: " + entry.getName());
      }

      progressListener.update(1);
      logger.fine(entryTarget.toString());

      if (entry.isDirectory()) {
        if (!Files.exists(entryTarget)) {
          Files.createDirectories(entryTarget);
        }
      } else if (entry.isFile()) {
        if (!Files.exists(entryTarget.getParent())) {
          Files.createDirectories(entryTarget.getParent());
        }
        try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) {
          IOUtils.copy(in, out);
          PosixFileAttributeView attributeView =
              Files.getFileAttributeView(entryTarget, PosixFileAttributeView.class);
          if (attributeView != null) {
            attributeView.setPermissions(PosixUtil.getPosixFilePermissions(entry.getMode()));
          }
        }
      } else {
        // we don't know what kind of entry this is (we only process directories and files).
        logger.warning("Skipping entry (unknown type): " + entry.getName());
      }
    }
    progressListener.done();
  }
}
 
Example #16
Source File: ProgressWrapperTest.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
@Test
public void testNewChild() {
  wrapper.start("testNewChild", 100);
  assertThat(wrapper.newChild(10), instanceOf(ProgressListener.class));
}
 
Example #17
Source File: SdkInstaller.java    From appengine-plugins-core with Apache License 2.0 4 votes vote down vote up
/** Download and install a new Cloud SDK. */
public Path install(
    final ProgressListener progressListener, final ConsoleListener consoleListener)
    throws IOException, InterruptedException, SdkInstallerException, CommandExecutionException,
        CommandExitException {

  FileResourceProvider fileResourceProvider =
      fileResourceProviderFactory.newFileResourceProvider();

  // Cleanup, remove old downloaded archive if exists
  if (Files.isRegularFile(fileResourceProvider.getArchiveDestination())) {
    logger.info("Removing stale archive: " + fileResourceProvider.getArchiveDestination());
    Files.delete(fileResourceProvider.getArchiveDestination());
  }

  // Cleanup, remove old SDK directory if exists
  if (Files.exists(fileResourceProvider.getArchiveExtractionDestination())) {
    logger.info(
        "Removing stale install: " + fileResourceProvider.getArchiveExtractionDestination());

    MoreFiles.deleteRecursively(
        fileResourceProvider.getArchiveExtractionDestination(),
        RecursiveDeleteOption.ALLOW_INSECURE);
  }

  progressListener.start("Installing Cloud SDK", installerFactory != null ? 300 : 200);

  // download and verify
  Downloader downloader =
      downloaderFactory.newDownloader(
          fileResourceProvider.getArchiveSource(),
          fileResourceProvider.getArchiveDestination(),
          progressListener.newChild(100));
  downloader.download();
  if (!Files.isRegularFile(fileResourceProvider.getArchiveDestination())) {
    throw new SdkInstallerException(
        "Download succeeded but valid archive not found at "
            + fileResourceProvider.getArchiveDestination());
  }

  try {
    // extract and verify
    extractorFactory
        .newExtractor(
            fileResourceProvider.getArchiveDestination(),
            fileResourceProvider.getArchiveExtractionDestination(),
            progressListener.newChild(100))
        .extract();
    if (!Files.isDirectory(fileResourceProvider.getExtractedSdkHome())) {
      throw new SdkInstallerException(
          "Extraction succeeded but valid sdk home not found at "
              + fileResourceProvider.getExtractedSdkHome());
    }
  } catch (UnknownArchiveTypeException e) {
    // fileResourceProviderFactory.newFileResourceProvider() creates a fileResourceProvider that
    // returns either .tar.gz or .zip for getArchiveDestination().
    throw new RuntimeException(e);
  }

  // install if necessary
  if (installerFactory != null) {
    installerFactory
        .newInstaller(
            fileResourceProvider.getExtractedSdkHome(),
            progressListener.newChild(100),
            consoleListener)
        .install();
  }

  // verify final state
  if (!Files.isRegularFile(fileResourceProvider.getExtractedGcloud())) {
    throw new SdkInstallerException(
        "Installation succeeded but gcloud executable not found at "
            + fileResourceProvider.getExtractedGcloud());
  }

  progressListener.done();
  return fileResourceProvider.getExtractedSdkHome();
}
 
Example #18
Source File: NoOpProgressListener.java    From app-gradle-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public ProgressListener newChild(long allocation) {
  return new NoOpProgressListener();
}
 
Example #19
Source File: ProgressWrapper.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
@Override
public ProgressListener newChild(long allocation) {
  return new ProgressWrapper(progress.split((int) allocation));
}
 
Example #20
Source File: CloudSdkDownloader.java    From app-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Downloads/installs/updates the Cloud SDK.
 *
 * @return The cloud SDK installation directory
 */
public Path downloadIfNecessary(
    String version, Log log, List<SdkComponent> components, boolean offline) {
  ManagedCloudSdk managedCloudSdk = managedCloudSdkFactory.apply(version);
  if (offline) { // in offline mode, don't download anything
    return managedCloudSdk.getSdkHome();
  }
  try {
    ProgressListener progressListener = new NoOpProgressListener();
    ConsoleListener consoleListener = new CloudSdkDownloaderConsoleListener(log);

    if (!managedCloudSdk.isInstalled()) {
      managedCloudSdk.newInstaller().install(progressListener, consoleListener);
    }

    // install requested components
    if (components != null) {
      for (SdkComponent component : components) {
        if (!managedCloudSdk.hasComponent(component)) {
          managedCloudSdk
              .newComponentInstaller()
              .installComponent(component, progressListener, consoleListener);
        }
      }
    }

    if (!managedCloudSdk.isUpToDate()) {
      managedCloudSdk.newUpdater().update(progressListener, consoleListener);
    }

    return managedCloudSdk.getSdkHome();
  } catch (IOException
      | SdkInstallerException
      | ManagedSdkVersionMismatchException
      | InterruptedException
      | CommandExecutionException
      | CommandExitException
      | ManagedSdkVerificationException ex) {
    throw new RuntimeException(ex);
  }
}
 
Example #21
Source File: NoOpProgressListener.java    From app-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public ProgressListener newChild(long l) {
  return new NoOpProgressListener();
}
 
Example #22
Source File: ExtractorFactory.java    From appengine-plugins-core with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new extractor based on filetype. Filetype determination is based on the filename
 * string, this method makes no attempt to validate the file contents to verify they are the type
 * defined by the file extension.
 *
 * @param archive the archive to extract
 * @param destination the destination folder for extracted files
 * @param progressListener a listener for progress
 * @return {@link Extractor} with {@link TarGzExtractorProvider} for ".tar.gz", {@link
 *     ZipExtractorProvider} for ".zip"
 * @throws UnknownArchiveTypeException if not ".tar.gz" or ".zip"
 */
public Extractor newExtractor(Path archive, Path destination, ProgressListener progressListener)
    throws UnknownArchiveTypeException {

  if (archive.toString().toLowerCase().endsWith(".tar.gz")) {
    return new Extractor(archive, destination, new TarGzExtractorProvider(), progressListener);
  }
  if (archive.toString().toLowerCase().endsWith(".zip")) {
    return new Extractor(archive, destination, new ZipExtractorProvider(), progressListener);
  }
  throw new UnknownArchiveTypeException(archive);
}
 
Example #23
Source File: ExtractorProvider.java    From appengine-plugins-core with Apache License 2.0 2 votes vote down vote up
/**
 * Extracts a single file archive into target destination folder.
 *
 * @param archive the archive to extract
 * @param destination the destination folder for extracted files
 * @param progressListener the progress listener passthrough from the extractor
 * @throws IOException if extractor fails
 */
void extract(Path archive, Path destination, ProgressListener progressListener)
    throws IOException;
 
Example #24
Source File: DownloaderFactory.java    From appengine-plugins-core with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a new {@link Downloader} implementation.
 *
 * @param source URL of file to download (remote)
 * @param destination Path on local file system to save the file
 * @param progressListener Progress feedback handler
 * @return a {@link Downloader} instance
 */
public Downloader newDownloader(URL source, Path destination, ProgressListener progressListener) {
  return new Downloader(source, destination, userAgentString, progressListener);
}