Search in sources :

Example 1 with MalformedYamlFileException

use of com.google.cloud.tools.intellij.appengine.project.MalformedYamlFileException in project google-cloud-intellij by GoogleCloudPlatform.

the class AppEngineFlexibleStage method stage.

/**
 * Stages the application in the given staging directory, in preparation for deployment to the App
 * Engine flexible environment.
 *
 * @param stagingDirectory the directory to stage the application to
 * @return {@code true} if the application was staged successfully, {@code false} otherwise
 * @throws IOException if there was a filesystem error during copying
 */
public boolean stage(@NotNull Path stagingDirectory) throws IOException {
    try {
        String moduleName = deploymentConfiguration.getModuleName();
        if (StringUtils.isEmpty(moduleName)) {
            loggingHandler.print(getMessage("appengine.deployment.error.staging.yaml.notspecified"));
            return false;
        }
        AppEngineFlexibleFacet flexibleFacet = AppEngineFlexibleFacet.getFacetByModuleName(moduleName, project);
        if (flexibleFacet == null || StringUtils.isEmpty(flexibleFacet.getConfiguration().getAppYamlPath())) {
            loggingHandler.print(getMessage("appengine.deployment.error.staging.yaml.notspecified"));
            return false;
        }
        AppEngineFlexibleFacetConfiguration facetConfiguration = flexibleFacet.getConfiguration();
        String appYaml = facetConfiguration.getAppYamlPath();
        // Checks if the app.yaml exists before staging.
        if (!Files.exists(Paths.get(appYaml))) {
            loggingHandler.print(getMessage("appengine.deployment.error.staging.yaml.notfound"));
            return false;
        }
        boolean isCustomRuntime = AppEngineProjectService.getInstance().getFlexibleRuntimeFromAppYaml(appYaml).filter(FlexibleRuntime::isCustom).isPresent();
        // Checks if the Dockerfile exists before staging.
        String dockerDirectory = facetConfiguration.getDockerDirectory();
        if (isCustomRuntime) {
            if (Strings.isNullOrEmpty(dockerDirectory)) {
                loggingHandler.print(getMessage("appengine.deployment.error.staging.dockerfile.notspecified"));
                return false;
            }
            if (!Files.isRegularFile(Paths.get(dockerDirectory, DOCKERFILE_NAME))) {
                loggingHandler.print(getMessage("appengine.deployment.error.staging.dockerfile.notfound"));
                return false;
            }
        }
        String stagedArtifactName = deploymentConfiguration.getStagedArtifactName();
        if (Strings.isNullOrEmpty(stagedArtifactName)) {
            if (deploymentConfiguration.isStagedArtifactNameLegacy()) {
                // Uses "target.jar" or "target.war" for legacy configs.
                AppEngineFlexibleDeploymentArtifactType artifactType = AppEngineFlexibleDeploymentArtifactType.typeForPath(deploymentArtifactPath);
                stagedArtifactName = StagedArtifactNameLegacySupport.getTargetName(artifactType);
            } else {
                // Defaults to the name of the artifact.
                stagedArtifactName = deploymentArtifactPath.getFileName().toString();
            }
        }
        loggingHandler.print(getMessage("appengine.deployment.staging.artifact.as", stagedArtifactName));
        Path stagedArtifactPath = stagingDirectory.resolve(stagedArtifactName);
        // Creates parent directories first, if necessary.
        Files.createDirectories(stagedArtifactPath.getParent());
        Files.copy(deploymentArtifactPath, stagedArtifactPath);
        Path appYamlPath = Paths.get(appYaml);
        Files.copy(appYamlPath, stagingDirectory.resolve(appYamlPath.getFileName()));
        if (isCustomRuntime) {
            FileUtils.copyDirectory(Paths.get(dockerDirectory).toFile(), stagingDirectory.toFile());
        }
    } catch (InvalidPathException | MalformedYamlFileException e) {
        loggingHandler.print(e.getMessage() + "\n");
    }
    return true;
}
Also used : Path(java.nio.file.Path) MalformedYamlFileException(com.google.cloud.tools.intellij.appengine.project.MalformedYamlFileException) AppEngineFlexibleFacet(com.google.cloud.tools.intellij.appengine.facet.flexible.AppEngineFlexibleFacet) AppEngineFlexibleFacetConfiguration(com.google.cloud.tools.intellij.appengine.facet.flexible.AppEngineFlexibleFacetConfiguration) InvalidPathException(java.nio.file.InvalidPathException)

Example 2 with MalformedYamlFileException

use of com.google.cloud.tools.intellij.appengine.project.MalformedYamlFileException in project google-cloud-intellij by GoogleCloudPlatform.

the class AppEngineDeploymentConfigurationTest method checkConfiguration_withMalformedAppYaml_throwsException.

@Test
public void checkConfiguration_withMalformedAppYaml_throwsException() throws Exception {
    setUpValidFlexConfiguration();
    when(mockAppEngineProjectService.getFlexibleRuntimeFromAppYaml(javaYaml.getPath())).thenThrow(new MalformedYamlFileException(null));
    RuntimeConfigurationError error = expectThrows(RuntimeConfigurationError.class, () -> configuration.checkConfiguration(mockRemoteServer, mockAppEngineDeployable, project));
    assertThat(error).hasMessage("The selected app.yaml file is malformed.");
}
Also used : MalformedYamlFileException(com.google.cloud.tools.intellij.appengine.project.MalformedYamlFileException) RuntimeConfigurationError(com.intellij.execution.configurations.RuntimeConfigurationError) Test(org.junit.Test)

Example 3 with MalformedYamlFileException

use of com.google.cloud.tools.intellij.appengine.project.MalformedYamlFileException in project google-cloud-intellij by GoogleCloudPlatform.

the class AppEngineDeploymentConfiguration method checkFlexConfig.

/**
 * Checks that this configuration is valid for a flex deployment, otherwise throws a {@link
 * RuntimeConfigurationError}.
 *
 * @param deployable the {@link AppEngineDeployable deployment source} that was selected by the
 *     user to deploy
 * @param project the {@link Project} that this configuration belongs to
 * @throws RuntimeConfigurationError if this configuration is not valid for a flex deployment
 */
private void checkFlexConfig(AppEngineDeployable deployable, Project project) throws RuntimeConfigurationError {
    check(!(deployable instanceof UserSpecifiedPathDeploymentSource) || (!StringUtil.isEmpty(userSpecifiedArtifactPath) && isJarOrWar(userSpecifiedArtifactPath)), "appengine.flex.config.user.specified.artifact.error");
    check(StringUtils.isNotBlank(moduleName), "appengine.flex.config.select.module");
    AppEngineFlexibleFacet facet = AppEngineFlexibleFacet.getFacetByModuleName(moduleName, project);
    check(facet != null, "appengine.flex.config.select.module");
    String appYamlPath = facet.getConfiguration().getAppYamlPath();
    check(StringUtils.isNotBlank(appYamlPath), "appengine.flex.config.browse.app.yaml");
    check(Files.exists(Paths.get(appYamlPath)), "appengine.deployment.config.appyaml.error");
    try {
        Optional<FlexibleRuntime> runtime = AppEngineProjectService.getInstance().getFlexibleRuntimeFromAppYaml(appYamlPath);
        if (runtime.isPresent() && runtime.get().isCustom()) {
            String dockerDirectory = facet.getConfiguration().getDockerDirectory();
            check(StringUtils.isNotBlank(dockerDirectory), "appengine.flex.config.browse.docker.directory");
            check(Files.exists(Paths.get(dockerDirectory, DOCKERFILE_NAME)), "appengine.deployment.config.dockerfile.error");
        }
    } catch (MalformedYamlFileException e) {
        throw new RuntimeConfigurationError(GctBundle.message("appengine.appyaml.malformed"));
    }
}
Also used : UserSpecifiedPathDeploymentSource(com.google.cloud.tools.intellij.appengine.cloud.flexible.UserSpecifiedPathDeploymentSource) MalformedYamlFileException(com.google.cloud.tools.intellij.appengine.project.MalformedYamlFileException) AppEngineFlexibleFacet(com.google.cloud.tools.intellij.appengine.facet.flexible.AppEngineFlexibleFacet) FlexibleRuntime(com.google.cloud.tools.intellij.appengine.project.AppEngineProjectService.FlexibleRuntime) RuntimeConfigurationError(com.intellij.execution.configurations.RuntimeConfigurationError)

Example 4 with MalformedYamlFileException

use of com.google.cloud.tools.intellij.appengine.project.MalformedYamlFileException in project google-cloud-intellij by GoogleCloudPlatform.

the class AppEngineDeploymentConfigurationTest method setUpValidCustomFlexConfiguration.

/**
 * Sets up the {@code configuration} to be a valid custom deployment to a flex environment.
 */
private void setUpValidCustomFlexConfiguration() {
    when(mockSdkValidator.validateCloudSdk()).thenReturn(ImmutableSet.of());
    when(mockAppEngineDeployable.isValid()).thenReturn(true);
    when(mockAppEngineDeployable.getEnvironment()).thenReturn(AppEngineEnvironment.APP_ENGINE_FLEX);
    AppEngineFlexibleFacetConfiguration facetConfig = FacetManager.getInstance(module).getFacetByType(AppEngineFlexibleFacet.getFacetType().getId()).getConfiguration();
    facetConfig.setAppYamlPath(customYaml.getPath());
    facetConfig.setDockerDirectory(dockerfile.getParent());
    configuration.setCloudProjectName("some-project-name");
    configuration.setModuleName(module.getName());
    configuration.setStagedArtifactName("target.war");
    try {
        when(mockAppEngineProjectService.getFlexibleRuntimeFromAppYaml(customYaml.getPath())).thenReturn(Optional.of(FlexibleRuntime.CUSTOM));
    } catch (MalformedYamlFileException e) {
        throw new AssertionError("This should not happen; Mockito must be broken.", e);
    }
}
Also used : MalformedYamlFileException(com.google.cloud.tools.intellij.appengine.project.MalformedYamlFileException) AppEngineFlexibleFacetConfiguration(com.google.cloud.tools.intellij.appengine.facet.flexible.AppEngineFlexibleFacetConfiguration)

Aggregations

MalformedYamlFileException (com.google.cloud.tools.intellij.appengine.project.MalformedYamlFileException)4 AppEngineFlexibleFacet (com.google.cloud.tools.intellij.appengine.facet.flexible.AppEngineFlexibleFacet)2 AppEngineFlexibleFacetConfiguration (com.google.cloud.tools.intellij.appengine.facet.flexible.AppEngineFlexibleFacetConfiguration)2 RuntimeConfigurationError (com.intellij.execution.configurations.RuntimeConfigurationError)2 UserSpecifiedPathDeploymentSource (com.google.cloud.tools.intellij.appengine.cloud.flexible.UserSpecifiedPathDeploymentSource)1 FlexibleRuntime (com.google.cloud.tools.intellij.appengine.project.AppEngineProjectService.FlexibleRuntime)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 Test (org.junit.Test)1