com.google.cloud.tools.appengine.operations.cloudsdk.process.LegacyProcessHandler Java Examples

The following examples show how to use com.google.cloud.tools.appengine.operations.cloudsdk.process.LegacyProcessHandler. 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: DevAppServerStartTask.java    From app-gradle-plugin with Apache License 2.0 6 votes vote down vote up
/** Task entrypoint : start the dev appserver (non-blocking). */
@TaskAction
public void startAction() throws AppEngineException, IOException {

  // Add a listener to write to a file for non-blocking starts, this really only works
  // when the gradle daemon is running (which is default for newer versions of gradle)
  File logFile = new File(devAppServerLoggingDir, "dev_appserver.out");
  FileOutputLineListener logFileWriter = new FileOutputLineListener(logFile);

  Logger taskLogger = getLogger();
  ProcessHandler processHandler =
      LegacyProcessHandler.builder()
          .addStdOutLineListener(taskLogger::lifecycle)
          .addStdOutLineListener(logFileWriter)
          .addStdErrLineListener(taskLogger::lifecycle)
          .addStdErrLineListener(logFileWriter)
          .setExitListener(new NonZeroExceptionExitListener())
          .buildDevAppServerAsync(runConfig.getStartSuccessTimeout());

  devServers.newDevAppServer(processHandler).run(runConfig.toRunConfiguration());

  getLogger().lifecycle("Dev App Server output written to : " + logFile.getAbsolutePath());
}
 
Example #2
Source File: CloudSdkProcessWrapper.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up a {@link CloudSdk} to be used for App Engine standard staging.
 *
 * @param javaHome JDK/JRE to 1) run {@code com.google.appengine.tools.admin.AppCfg} from
 *     {@code appengine-tools-api.jar}; and 2) compile JSPs during staging
 */
public AppEngineWebXmlProjectStaging getAppEngineStandardStaging(Path javaHome,
    MessageConsoleStream stdoutOutputStream, MessageConsoleStream stderrOutputStream) 
        throws CloudSdkNotFoundException {
  Preconditions.checkState(!initialized, "process wrapper already set up");
  initialized = true;

  CloudSdk cloudSdk = javaHome == null
      ? new CloudSdk.Builder().build()
      : new CloudSdk.Builder().javaHome(javaHome).build();

  ProcessHandler processHandler = LegacyProcessHandler.builder()
      .setStartListener(this::storeProcessObject)
      .setExitListener(this::recordProcessExitCode)
      .addStdOutLineListener(new MessageConsoleWriterListener(stdoutOutputStream))
      .addStdErrLineListener(new MessageConsoleWriterListener(stderrOutputStream))
      .build();

  return AppCfg.builder(cloudSdk).build().newStaging(processHandler);
}
 
Example #3
Source File: LocalAppEngineServerBehaviour.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
private void initializeDevServer(MessageConsoleStream stdout, MessageConsoleStream stderr,
    Path javaHomePath) throws CloudSdkNotFoundException {
  MessageConsoleWriterListener stdoutListener = new MessageConsoleWriterListener(stdout);
  MessageConsoleWriterListener stderrListener = new MessageConsoleWriterListener(stderr);

  // dev_appserver output goes to stderr
  cloudSdk = new CloudSdk.Builder()
      .javaHome(javaHomePath)
      .build();

  ProcessHandler processHandler = LegacyProcessHandler.builder()
      .addStdOutLineListener(stdoutListener).addStdErrLineListener(stderrListener)
      .addStdErrLineListener(serverOutputListener)
      .setStartListener(localAppEngineStartListener)
      .setExitListener(localAppEngineExitListener)
      .async(true)
      .build();

  DevServers localRun = DevServers.builder(cloudSdk).build();
  devServer = localRun.newDevAppServer(processHandler);
  moduleToUrlMap.clear();
}
 
Example #4
Source File: StandardStagingDelegateTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
private void setUpProject(IProjectFacetVersion... facetVersions)
    throws CloudSdkNotFoundException {
  project = projectCreator.withFacets(facetVersions).getProject();
  safeWorkDirectory = project.getFolder("safe-work-directory").getLocation();
  stagingDirectory = project.getFolder("staging-result").getLocation();

  CloudSdk cloudSdk = new CloudSdk.Builder().build();
  LegacyProcessHandler processHandler = LegacyProcessHandler.builder()
      .addStdOutLineListener(line -> { System.out.println("    [Cloud SDK] " + line); })
      .addStdErrLineListener(line -> { System.out.println("    [Cloud SDK] " + line); })
      .setExitListener(exitCode -> { cloudSdkExitCode = exitCode; })
      .build();
  AppEngineWebXmlProjectStaging staging =
      AppCfg.builder(cloudSdk).build().newStaging(processHandler);

  when(cloudSdkWrapper.getAppEngineStandardStaging(
      any(Path.class), any(MessageConsoleStream.class), any(MessageConsoleStream.class)))
      .thenReturn(staging);
}
 
Example #5
Source File: CloudSdkAppEngineFactory.java    From app-maven-plugin with Apache License 2.0 6 votes vote down vote up
private ProcessHandler newDevAppServerAsyncHandler(int timeout) {
  Path logDir =
      Paths.get(mojo.getMavenProject().getBuild().getDirectory()).resolve("dev-appserver-out");
  if (!Files.exists(logDir)) {
    try {
      logDir = Files.createDirectories(logDir);
    } catch (IOException e) {
      throw new RuntimeException("Failed to create dev-appserver logging directory.");
    }
  }
  File logFile = logDir.resolve("dev_appserver.out").toFile();
  FileOutputLineListener fileListener = new FileOutputLineListener(logFile);
  mojo.getLog().info("Dev App Server output written to : " + logFile);

  ProcessOutputLineListener lineListener = new DefaultProcessOutputLineListener(mojo.getLog());

  return LegacyProcessHandler.builder()
      .addStdOutLineListener(lineListener)
      .addStdOutLineListener(fileListener)
      .addStdErrLineListener(lineListener)
      .addStdErrLineListener(fileListener)
      .setExitListener(new NonZeroExceptionExitListener())
      .buildDevAppServerAsync(timeout);
}
 
Example #6
Source File: CloudSdkOperations.java    From app-gradle-plugin with Apache License 2.0 5 votes vote down vote up
/** Create a return a new default configured process handler. */
public static ProcessHandler getDefaultHandler(Logger logger) {
  return LegacyProcessHandler.builder()
      .addStdErrLineListener(logger::lifecycle)
      .addStdOutLineListener(logger::lifecycle)
      .setExitListener(new NonZeroExceptionExitListener())
      .build();
}
 
Example #7
Source File: Gcloud.java    From appengine-plugins-core with Apache License 2.0 5 votes vote down vote up
/**
 * Run short lived gcloud commands.
 *
 * @param args the arguments to gcloud command (not including 'gcloud')
 * @return standard out collected as a single string
 */
public String runCommand(List<String> args)
    throws CloudSdkNotFoundException, IOException, ProcessHandlerException {
  sdk.validateCloudSdkLocation();

  StringBuilderProcessOutputLineListener stdOutListener =
      StringBuilderProcessOutputLineListener.newListener();
  StringBuilderProcessOutputLineListener stdErrListener =
      StringBuilderProcessOutputLineListener.newListenerWithNewlines();
  ExitCodeRecorderProcessExitListener exitListener = new ExitCodeRecorderProcessExitListener();

  // build and run the command
  List<String> command =
      new ImmutableList.Builder<String>()
          .add(sdk.getGCloudPath().toAbsolutePath().toString())
          .addAll(args)
          .build();

  Process process = new ProcessBuilder(command).start();
  LegacyProcessHandler.builder()
      .addStdOutLineListener(stdOutListener)
      .addStdErrLineListener(stdErrListener)
      .setExitListener(exitListener)
      .build()
      .handleProcess(process);

  if (exitListener.getMostRecentExitCode() != null
      && !exitListener.getMostRecentExitCode().equals(0)) {
    Logger.getLogger(Gcloud.class.getName()).severe(stdErrListener.toString());
    throw new ProcessHandlerException(
        "Process exited unsuccessfully with code " + exitListener.getMostRecentExitCode());
  }

  return stdOutListener.toString();
}
 
Example #8
Source File: CloudSdkProcessWrapper.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up a {@link CloudSdk} to be used for App Engine deploy.
 */
public Deployment getAppEngineDeployment(Path credentialFile,
    MessageConsoleStream normalOutputStream) throws CloudSdkNotFoundException {
  Preconditions.checkNotNull(credentialFile, "credential required for deploying");
  Preconditions.checkArgument(Files.exists(credentialFile), "non-existing credential file");
  Preconditions.checkState(!initialized, "process wrapper already set up");
  initialized = true;

  CloudSdk cloudSdk = new CloudSdk.Builder().build();
  Gcloud gcloud = Gcloud.builder(cloudSdk)
      .setCredentialFile(credentialFile.toFile().toPath())
      .setMetricsEnvironment(CloudToolsInfo.METRICS_NAME, CloudToolsInfo.getToolsVersion())
      .setShowStructuredLogs("always")  // turns on gcloud structured log
      .setOutputFormat("json")  // Deploy result will be in JSON.
      .build();

  // Gcloud sends structured deploy result (in JSON format) to stdout, so prepare to capture that.
  stdOutCaptor = StringBuilderProcessOutputLineListener.newListenerWithNewlines();
  // Gcloud sends structured gcloud logs (in JSON format) to stderr, so prepare to capture them.
  gcloudErrorMessageCollector = new GcloudStructuredLogErrorMessageCollector();

  ProcessHandler processHandler = LegacyProcessHandler.builder()
      .setStartListener(this::storeProcessObject)
      .setExitListener(this::recordProcessExitCode)
      // Gcloud sends normal operation output to stderr.
      .addStdErrLineListener(new MessageConsoleWriterListener(normalOutputStream))
      .addStdErrLineListener(gcloudErrorMessageCollector)
      .addStdOutLineListener(stdOutCaptor)
      .build();

  return gcloud.newDeployment(processHandler);
}
 
Example #9
Source File: CloudSdkStagingHelperTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private AppEngineWebXmlProjectStaging setUpAppEngineStaging()
    throws CloudSdkNotFoundException, CoreException {
  createFile("WebContent/WEB-INF/appengine-web.xml", APPENGINE_WEB_XML);
  createFile("WebContent/WEB-INF/web.xml", WEB_XML);
  createFile("WebContent/META-INF/MANIFEST.MF", "");

  CloudSdk cloudSdk = new CloudSdk.Builder().build();
  AppCfg appCfg = AppCfg.builder(cloudSdk).build();
  ProcessHandler processHandler = LegacyProcessHandler.builder().async(false).build();
  return appCfg.newStaging(processHandler);
}
 
Example #10
Source File: CloudSdkAppEngineFactory.java    From app-maven-plugin with Apache License 2.0 5 votes vote down vote up
private ProcessHandler newDefaultProcessHandler() {
  ProcessOutputLineListener lineListener = new DefaultProcessOutputLineListener(mojo.getLog());
  return LegacyProcessHandler.builder()
      .addStdOutLineListener(lineListener)
      .addStdErrLineListener(lineListener)
      .setExitListener(new NonZeroExceptionExitListener())
      .build();
}