Java Code Examples for com.google.cloud.tools.appengine.operations.cloudsdk.CloudSdkVersionFileException

The following examples show how to use com.google.cloud.tools.appengine.operations.cloudsdk.CloudSdkVersionFileException. These examples are extracted from open source projects. 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 Project: app-gradle-plugin   Source File: CheckCloudSdkTask.java    License: Apache License 2.0 6 votes vote down vote up
/** Task entrypoint : Verify Cloud SDK installation. */
@TaskAction
public void checkCloudSdkAction()
    throws CloudSdkNotFoundException, CloudSdkVersionFileException, CloudSdkOutOfDateException,
        AppEngineJavaComponentsNotInstalledException {
  // These properties are only set by AppEngineCorePluginConfiguration if the correct config
  // params are set in the tools extension.
  if (Strings.isNullOrEmpty(version) || cloudSdk == null) {
    throw new GradleException(
        "Cloud SDK home path and version must be configured in order to run this task.");
  }

  if (!version.equals(cloudSdk.getVersion().toString())) {
    throw new GradleException(
        "Specified Cloud SDK version ("
            + version
            + ") does not match installed version ("
            + cloudSdk.getVersion()
            + ").");
  }

  cloudSdk.validateCloudSdk();
  if (requiresAppEngineJava) {
    cloudSdk.validateAppEngineJavaComponents();
  }
}
 
Example 2
Source Project: app-gradle-plugin   Source File: DeployTargetResolver.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Process user configuration of "projectId". If set to GCLOUD_CONFIG then read from gcloud's
 * global state. If set but not a keyword then just return the set value.
 */
public String getProject(String configString) {
  if (configString == null
      || configString.trim().isEmpty()
      || configString.equals(APPENGINE_CONFIG)) {
    throw new GradleException(PROJECT_ERROR);
  }
  if (configString.equals(GCLOUD_CONFIG)) {
    try {
      String gcloudProject = cloudSdkOperations.getGcloud().getConfig().getProject();
      if (gcloudProject == null || gcloudProject.trim().isEmpty()) {
        throw new GradleException("Project was not found in gcloud config");
      }
      return gcloudProject;
    } catch (IOException
        | CloudSdkOutOfDateException
        | ProcessHandlerException
        | CloudSdkNotFoundException
        | CloudSdkVersionFileException ex) {
      throw new GradleException("Failed to read project from gcloud config", ex);
    }
  }
  return configString;
}
 
Example 3
Source Project: appengine-plugins-core   Source File: Gcloud.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the list of Cloud SDK Components and their settings, reported by the current gcloud
 * installation. Unlike other methods in this class that call gcloud, this method always uses a
 * synchronous ProcessRunner and will block until the gcloud process returns.
 *
 * @throws ProcessHandlerException when process runner encounters an error
 * @throws JsonSyntaxException when the cloud SDK output cannot be parsed
 * @throws CloudSdkNotFoundException when the Cloud SDK is not installed where expected
 * @throws CloudSdkOutOfDateException when the installed Cloud SDK is too old
 */
public List<CloudSdkComponent> getComponents()
    throws ProcessHandlerException, JsonSyntaxException, CloudSdkNotFoundException,
        CloudSdkOutOfDateException, CloudSdkVersionFileException, IOException {
  sdk.validateCloudSdk();

  // gcloud components list --show-versions --format=json
  List<String> command =
      new ImmutableList.Builder<String>()
          .add("components", "list")
          .addAll(GcloudArgs.get("show-versions", true))
          .addAll(GcloudArgs.get("format", "json"))
          .build();

  String componentsJson = runCommand(command);
  return CloudSdkComponent.fromJsonList(componentsJson);
}
 
Example 4
Source Project: app-maven-plugin   Source File: ConfigReader.java    License: Apache License 2.0 6 votes vote down vote up
/** Return gcloud config property for project, or error out if not found. */
public String getProjectId() {
  try {
    String gcloudProject = gcloud.getConfig().getProject();
    if (gcloudProject == null || gcloudProject.trim().isEmpty()) {
      throw new RuntimeException("Project was not found in gcloud config");
    }
    return gcloudProject;
  } catch (CloudSdkNotFoundException
      | CloudSdkOutOfDateException
      | CloudSdkVersionFileException
      | IOException
      | ProcessHandlerException ex) {
    throw new RuntimeException("Failed to read project from gcloud config", ex);
  }
}
 
Example 5
@Test
public void testBuildCloudSdk_checkNoAppEngine()
    throws CloudSdkOutOfDateException, CloudSdkNotFoundException, CloudSdkVersionFileException {
  when(mojoMock.getCloudSdkHome()).thenReturn(CLOUD_SDK_HOME);
  when(mojoMock.getCloudSdkVersion()).thenReturn(CLOUD_SDK_VERSION);

  // invoke
  CloudSdk sdk =
      CloudSdkAppEngineFactory.buildCloudSdk(
          mojoMock, cloudSdkChecker, cloudSdkDownloader, false);

  // verify
  Assert.assertEquals(CLOUD_SDK_HOME, sdk.getPath());
  verify(cloudSdkChecker).checkCloudSdk(sdk, CLOUD_SDK_VERSION);
  verifyNoMoreInteractions(cloudSdkDownloader);
  verifyNoMoreInteractions(cloudSdkChecker);
}
 
Example 6
@Test
public void testBuildCloudSdk_checkAppEngine()
    throws CloudSdkOutOfDateException, CloudSdkNotFoundException, CloudSdkVersionFileException,
        AppEngineJavaComponentsNotInstalledException {
  when(mojoMock.getCloudSdkHome()).thenReturn(CLOUD_SDK_HOME);
  when(mojoMock.getCloudSdkVersion()).thenReturn(CLOUD_SDK_VERSION);

  // invoke
  CloudSdk sdk =
      CloudSdkAppEngineFactory.buildCloudSdk(mojoMock, cloudSdkChecker, cloudSdkDownloader, true);

  // verify
  Assert.assertEquals(CLOUD_SDK_HOME, sdk.getPath());
  verify(cloudSdkChecker).checkCloudSdk(sdk, CLOUD_SDK_VERSION);
  verify(cloudSdkChecker).checkForAppEngine(sdk);
  verifyNoMoreInteractions(cloudSdkDownloader);
  verifyNoMoreInteractions(cloudSdkChecker);
}
 
Example 7
@Before
public void setup()
    throws CloudSdkNotFoundException, ProcessHandlerException, CloudSdkOutOfDateException,
        CloudSdkVersionFileException, IOException {
  Mockito.when(cloudSdkOperations.getGcloud()).thenReturn(gcloud);
  Mockito.when(gcloud.getConfig()).thenReturn(cloudSdkConfig);
  Mockito.when(cloudSdkConfig.getProject()).thenReturn(PROJECT_GCLOUD);
}
 
Example 8
Source Project: app-gradle-plugin   Source File: CheckCloudSdkTaskTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckCloudSdkAction_nullVersion()
    throws CloudSdkNotFoundException, CloudSdkVersionFileException, CloudSdkOutOfDateException,
        AppEngineJavaComponentsNotInstalledException {
  checkCloudSdkTask.setVersion(null);
  try {
    checkCloudSdkTask.checkCloudSdkAction();
    Assert.fail();
  } catch (GradleException ex) {
    Assert.assertEquals(
        "Cloud SDK home path and version must be configured in order to run this task.",
        ex.getMessage());
  }
}
 
Example 9
Source Project: app-gradle-plugin   Source File: CheckCloudSdkTaskTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckCloudSdkAction_versionMismatch()
    throws CloudSdkVersionFileException, CloudSdkNotFoundException, CloudSdkOutOfDateException,
        AppEngineJavaComponentsNotInstalledException {
  checkCloudSdkTask.setVersion("191.0.0");
  when(sdk.getVersion()).thenReturn(new CloudSdkVersion("190.0.0"));
  try {
    checkCloudSdkTask.checkCloudSdkAction();
    Assert.fail();
  } catch (GradleException ex) {
    Assert.assertEquals(
        "Specified Cloud SDK version (191.0.0) does not match installed version (190.0.0).",
        ex.getMessage());
  }
}
 
Example 10
Source Project: app-gradle-plugin   Source File: CheckCloudSdkTaskTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckCloudSdkAction_callPluginsCoreChecks()
    throws CloudSdkVersionFileException, CloudSdkNotFoundException, CloudSdkOutOfDateException,
        AppEngineJavaComponentsNotInstalledException {
  checkCloudSdkTask.setVersion("192.0.0");
  checkCloudSdkTask.requiresAppEngineJava(true);
  when(sdk.getVersion()).thenReturn(new CloudSdkVersion("192.0.0"));

  checkCloudSdkTask.checkCloudSdkAction();

  Mockito.verify(sdk).getVersion();
  Mockito.verify(sdk).validateCloudSdk();
  Mockito.verify(sdk).validateAppEngineJavaComponents();
  Mockito.verifyNoMoreInteractions(sdk);
}
 
Example 11
Source Project: app-gradle-plugin   Source File: CheckCloudSdkTaskTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckCloudSdkAction_callPluginsCoreChecksSkipJava()
    throws CloudSdkVersionFileException, CloudSdkNotFoundException, CloudSdkOutOfDateException,
        AppEngineJavaComponentsNotInstalledException {
  checkCloudSdkTask.setVersion("192.0.0");
  when(sdk.getVersion()).thenReturn(new CloudSdkVersion("192.0.0"));

  checkCloudSdkTask.checkCloudSdkAction();

  Mockito.verify(sdk).getVersion();
  Mockito.verify(sdk).validateCloudSdk();
  Mockito.verify(sdk, Mockito.never()).validateAppEngineJavaComponents();
  Mockito.verifyNoMoreInteractions(sdk);
}
 
Example 12
Source Project: appengine-plugins-core   Source File: Gcloud.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a representation of gcloud config, it makes a synchronous call to gcloud config list to
 * do so.
 */
public CloudSdkConfig getConfig()
    throws CloudSdkNotFoundException, CloudSdkOutOfDateException, CloudSdkVersionFileException,
        IOException, ProcessHandlerException {
  sdk.validateCloudSdk();

  List<String> command =
      new ImmutableList.Builder<String>()
          .add("config", "list")
          .addAll(GcloudArgs.get("format", "json"))
          .build();

  String configJson = runCommand(command);
  return CloudSdkConfig.fromJson(configJson);
}
 
Example 13
Source Project: appengine-plugins-core   Source File: GcloudRunner.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Launch an external process that runs gcloud.
 *
 * @param workingDirectory if null then the working directory of current Java process
 */
void run(List<String> arguments, @Nullable Path workingDirectory)
    throws ProcessHandlerException, CloudSdkNotFoundException, CloudSdkOutOfDateException,
        CloudSdkVersionFileException, IOException {

  sdk.validateCloudSdk();

  List<String> command = new ArrayList<>();
  command.add(sdk.getGCloudPath().toAbsolutePath().toString());

  command.addAll(arguments);
  if (outputFormat != null) {
    command.addAll(GcloudArgs.get("format", outputFormat));
  }

  if (credentialFile != null) {
    command.addAll(GcloudArgs.get("credential-file-override", credentialFile));
  }

  if (flagsFiles != null) {
    for (Path flagFile : flagsFiles) {
      command.addAll(GcloudArgs.get("flags-file", flagFile));
    }
  }

  logger.info("submitting command: " + Joiner.on(" ").join(command));

  ProcessBuilder processBuilder = processBuilderFactory.newProcessBuilder();
  processBuilder.command(command);
  if (workingDirectory != null) {
    processBuilder.directory(workingDirectory.toFile());
  }
  processBuilder.environment().putAll(getGcloudCommandEnvironment());
  Process process = processBuilder.start();
  processHandler.handleProcess(process);
}
 
Example 14
Source Project: appengine-plugins-core   Source File: CloudSdkTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetVersion_fileNotExists() {
  try {
    sdk.getVersion();
    fail();
  } catch (CloudSdkVersionFileException e) {
    assertEquals(
        "Cloud SDK version file not found at " + root.resolve("VERSION"), e.getMessage());
  }
}
 
Example 15
Source Project: appengine-plugins-core   Source File: CloudSdkTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetVersion_fileContentInvalid() throws IOException {
  String fileContents = "this is not a valid version string";
  writeVersionFile(fileContents);
  try {
    sdk.getVersion();
    fail();
  } catch (CloudSdkVersionFileException ex) {
    assertEquals(
        "Pattern found in the Cloud SDK version file could not be parsed: " + fileContents,
        ex.getMessage());
  }
}
 
Example 16
Source Project: appengine-plugins-core   Source File: CloudSdkTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testValidateCloudSdk_versionFileContentInvalid()
    throws IOException, CloudSdkNotFoundException, CloudSdkOutOfDateException,
        CloudSdkVersionFileException {
  String fileContents = "this is not a valid version string";
  writeVersionFile(fileContents);
  root.resolve("bin").toFile().mkdir();
  // fake SDK contents
  root.resolve("bin/gcloud").toFile().createNewFile();
  root.resolve("bin/gcloud.cmd").toFile().createNewFile(); // for Windows
  root.resolve("bin/dev_appserver.py").toFile().createNewFile();
  sdk.validateCloudSdk();
}
 
Example 17
private static String getSdkVersion(Path path) {
  if (!Files.exists(path) || !Files.isDirectory(path)) {
    return Messages.getString("NoSdkFound"); //$NON-NLS-1$
  }

  try {
    return new CloudSdk.Builder().sdkPath(path).build().getVersion().toString();
  } catch (CloudSdkVersionFileException | CloudSdkNotFoundException ex) {
    return Messages.getString("NoSdkFound"); //$NON-NLS-1$
  }
}
 
Example 18
/**
 * Return the version of the Google Cloud SDK at the given location, or {@code null} if it cannot
 * be determined.
 */
private static CloudSdkVersion getVersion(Path sdkHome) {
  try {
    CloudSdk sdk = new CloudSdk.Builder().sdkPath(sdkHome).build();
    return sdk.getVersion();
  } catch (CloudSdkVersionFileException | CloudSdkNotFoundException ex) {
    return null;
  }
}
 
Example 19
Source Project: app-maven-plugin   Source File: CloudSdkChecker.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Validates the cloud SDK installation.
 *
 * @param cloudSdk CloudSdk with a configured sdk home directory
 */
public void checkCloudSdk(CloudSdk cloudSdk, String version)
    throws CloudSdkVersionFileException, CloudSdkNotFoundException, CloudSdkOutOfDateException {
  if (!version.equals(cloudSdk.getVersion().toString())) {
    throw new RuntimeException(
        "Specified Cloud SDK version ("
            + version
            + ") does not match installed version ("
            + cloudSdk.getVersion()
            + ").");
  }

  cloudSdk.validateCloudSdk();
}
 
Example 20
Source Project: app-maven-plugin   Source File: CloudSdkAppEngineFactory.java    License: Apache License 2.0 5 votes vote down vote up
static CloudSdk buildCloudSdk(
    CloudSdkMojo mojo,
    CloudSdkChecker cloudSdkChecker,
    CloudSdkDownloader cloudSdkDownloader,
    boolean requiresAppEngineComponents) {

  try {
    if (mojo.getCloudSdkHome() != null) {
      // if user defined
      CloudSdk cloudSdk = new CloudSdk.Builder().sdkPath(mojo.getCloudSdkHome()).build();

      if (mojo.getCloudSdkVersion() != null) {
        cloudSdkChecker.checkCloudSdk(cloudSdk, mojo.getCloudSdkVersion());
      }
      if (requiresAppEngineComponents) {
        cloudSdkChecker.checkForAppEngine(cloudSdk);
      }
      return cloudSdk;
    } else {
      // we need to use a managed cloud sdk
      List<SdkComponent> requiredComponents = new ArrayList<>();
      if (requiresAppEngineComponents) {
        requiredComponents.add(SdkComponent.APP_ENGINE_JAVA);
      }
      return new CloudSdk.Builder()
          .sdkPath(
              cloudSdkDownloader.downloadIfNecessary(
                  mojo.getCloudSdkVersion(),
                  mojo.getLog(),
                  requiredComponents,
                  mojo.getMavenSession().isOffline()))
          .build();
    }
  } catch (CloudSdkNotFoundException
      | CloudSdkVersionFileException
      | AppEngineJavaComponentsNotInstalledException
      | CloudSdkOutOfDateException ex) {
    throw new RuntimeException(ex);
  }
}
 
Example 21
Source Project: app-maven-plugin   Source File: CloudSdkCheckerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckCloudSdk_versionMismatch()
    throws CloudSdkVersionFileException, CloudSdkOutOfDateException, CloudSdkNotFoundException {
  when(sdk.getVersion()).thenReturn(new CloudSdkVersion("190.0.0"));
  try {
    cloudSdkChecker.checkCloudSdk(sdk, "191.0.0");
    Assert.fail();
  } catch (RuntimeException ex) {
    Assert.assertEquals(
        "Specified Cloud SDK version (191.0.0) does not match installed version (190.0.0).",
        ex.getMessage());
  }
}
 
Example 22
Source Project: app-maven-plugin   Source File: CloudSdkCheckerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckCloudSdk_callPluginsCoreChecks()
    throws CloudSdkVersionFileException, CloudSdkNotFoundException, CloudSdkOutOfDateException {
  when(sdk.getVersion()).thenReturn(new CloudSdkVersion("192.0.0"));

  cloudSdkChecker.checkCloudSdk(sdk, "192.0.0");

  verify(sdk).getVersion();
  verify(sdk).validateCloudSdk();
  verifyNoMoreInteractions(sdk);
}
 
Example 23
Source Project: appengine-plugins-core   Source File: CloudSdkTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateCloudSdk()
    throws CloudSdkNotFoundException, CloudSdkOutOfDateException, CloudSdkVersionFileException {
  new CloudSdk.Builder().build().validateCloudSdk();
}
 
Example 24
Source Project: appengine-plugins-core   Source File: CloudSdkTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateCloudSdk_doesNotThrowInvalidJdkException()
    throws CloudSdkNotFoundException, CloudSdkOutOfDateException, CloudSdkVersionFileException {
  new CloudSdk.Builder().javaHome(Paths.get("/fake/path")).build().validateCloudSdk();
}
 
Example 25
Source Project: appengine-plugins-core   Source File: CloudSdkTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testGetVersion_fileContentValid() throws IOException, CloudSdkVersionFileException {
  String version = "136.0.0";
  writeVersionFile(version);
  assertEquals(version, sdk.getVersion().toString());
}
 
Example 26
Source Project: appengine-plugins-core   Source File: GcloudRunnerTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testRun_builtFromFactory()
    throws CloudSdkOutOfDateException, CloudSdkNotFoundException, ProcessHandlerException,
        CloudSdkVersionFileException, IOException {
  GcloudRunner gcloudRunner =
      new GcloudRunner.Factory(processBuilderFactory)
          .newRunner(
              sdk,
              "intellij", // metrics env
              "99", // metrics env version
              credentialFile, // credential file
              flagsFiles, // gcloud flags file(s)
              "some-format", // output format
              "always", // show structured logs
              processHandler);

  gcloudRunner.run(ImmutableList.of("some", "command"), workingDirectory);

  Mockito.verify(processBuilder).environment();
  Mockito.verify(processEnv).putAll(gcloudRunner.getGcloudCommandEnvironment());
  Mockito.verify(processBuilder).directory(workingDirectory.toFile());

  Mockito.verify(processHandler).handleProcess(process);
  Mockito.verify(processBuilder)
      .command(
          ImmutableList.of(
              gcloudPath.toString(),
              "some",
              "command",
              "--format",
              "some-format",
              "--credential-file-override",
              credentialFile.toAbsolutePath().toString(),
              "--flags-file",
              flagsFiles.get(0).toAbsolutePath().toString(),
              "--flags-file",
              flagsFiles.get(1).toAbsolutePath().toString()));
  Mockito.verify(processBuilder).start();
  Mockito.verifyNoMoreInteractions(processBuilder);

  Mockito.verify(processHandler).handleProcess(process);
}
 
Example 27
Source Project: google-cloud-eclipse   Source File: CloudSdkModifyJob.java    License: Apache License 2.0 4 votes vote down vote up
/** Retrieve the version of the Cloud SDK at the provided location. */
protected static String getVersion(Path sdkPath) 
    throws CloudSdkVersionFileException, CloudSdkNotFoundException {
  CloudSdk sdk = new CloudSdk.Builder().sdkPath(sdkPath).build();
  return sdk.getVersion().toString();
}
 
Example 28
Source Project: appengine-plugins-core   Source File: CloudSdk.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether the Cloud SDK path and version are valid.
 *
 * @throws CloudSdkNotFoundException when Cloud SDK is not installed where expected
 * @throws CloudSdkOutOfDateException when Cloud SDK is out of date
 * @throws CloudSdkVersionFileException VERSION file could not be read
 */
public void validateCloudSdk()
    throws CloudSdkNotFoundException, CloudSdkOutOfDateException, CloudSdkVersionFileException {
  validateCloudSdkLocation();
  validateCloudSdkVersion();
}