Java Code Examples for com.google.cloud.tools.managedcloudsdk.ProgressListener#start()

The following examples show how to use com.google.cloud.tools.managedcloudsdk.ProgressListener#start() . 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: 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 2
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 3
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 4
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 5
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();
}