Search in sources :

Example 31 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class RegistryBackedArchiveProfileBuilder method build.

public List<Profile> build(DeploymentConfiguration deploymentConfiguration, String baseOutputPath, SpinnakerArtifact artifact, String archiveName) {
    String version = artifactService.getArtifactVersion(deploymentConfiguration.getName(), artifact);
    InputStream is;
    try {
        is = profileRegistry.readArchiveProfile(artifact.getName(), version, archiveName);
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Error retrieving contents of archive " + archiveName + ".tar.gz", e);
    }
    TarArchiveInputStream tis;
    try {
        tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    } catch (ArchiveException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to unpack tar archive", e);
    }
    try {
        List<Profile> result = new ArrayList<>();
        ArchiveEntry profileEntry = tis.getNextEntry();
        while (profileEntry != null) {
            if (profileEntry.isDirectory()) {
                profileEntry = tis.getNextEntry();
                continue;
            }
            String entryName = profileEntry.getName();
            String profileName = String.join("/", artifact.getName(), archiveName, entryName);
            String outputPath = Paths.get(baseOutputPath, archiveName, entryName).toString();
            String contents = IOUtils.toString(tis);
            result.add((new ProfileFactory() {

                @Override
                protected void setProfile(Profile profile, DeploymentConfiguration deploymentConfiguration, SpinnakerRuntimeSettings endpoints) {
                    profile.setContents(profile.getBaseContents());
                }

                @Override
                protected Profile getBaseProfile(String name, String version, String outputFile) {
                    return new Profile(name, version, outputFile, contents);
                }

                @Override
                protected boolean showEditWarning() {
                    return false;
                }

                @Override
                protected ArtifactService getArtifactService() {
                    return artifactService;
                }

                @Override
                public SpinnakerArtifact getArtifact() {
                    return artifact;
                }

                @Override
                protected String commentPrefix() {
                    return null;
                }
            }).getProfile(profileName, outputPath, deploymentConfiguration, null));
            profileEntry = tis.getNextEntry();
        }
        return result;
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to read profile entry", e);
    }
}
Also used : SpinnakerArtifact(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerArtifact) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) ArrayList(java.util.ArrayList) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) IOException(java.io.IOException) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)

Example 32 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class VaultServerService method getToken.

public String getToken(String deploymentName, Vault vault) {
    String result = "";
    InitStatus initStatus;
    try {
        initStatus = vault.initStatus();
    } catch (RetrofitError e) {
        throw handleVaultError(e, "check init status");
    }
    if (!initStatus.isInitialized()) {
        try {
            InitResponse init = vault.init(new InitRequest().setSecretShares(3).setSecretThreshold(3));
            result = init.getRootToken();
            for (String key : init.getKeys()) {
                vault.unseal(new UnsealRequest().setKey(key));
            }
        } catch (RetrofitError e) {
            throw handleVaultError(e, "init vault");
        }
    }
    SealStatus sealStatus;
    try {
        sealStatus = vault.sealStatus();
    } catch (RetrofitError e) {
        throw handleVaultError(e, "check seal status");
    }
    if (sealStatus.isSealed()) {
        throw new HalException(Problem.Severity.FATAL, "Your vault is in a sealed state, no config can be written.");
    }
    File vaultTokenFile = halconfigDirectoryStructure.getVaultTokenPath(deploymentName).toFile();
    if (result.isEmpty()) {
        try {
            result = IOUtils.toString(new FileInputStream(vaultTokenFile));
        } catch (IOException e) {
            throw new HalException(new ProblemBuilder(Problem.Severity.FATAL, "Unable to read vault token: " + e.getMessage()).setRemediation("This file is needed for storing credentials to your vault server. " + "If you have deployed vault by hand, make sure Halyard can authenticate using the token in that file.").build());
        }
    } else {
        try {
            IOUtils.write(result.getBytes(), new FileOutputStream(vaultTokenFile));
        } catch (IOException e) {
            throw new HalException(Problem.Severity.FATAL, "Unable to write vault token: " + e.getMessage());
        }
    }
    return result;
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ProblemBuilder(com.netflix.spinnaker.halyard.core.problem.v1.ProblemBuilder) FileOutputStream(java.io.FileOutputStream) File(java.io.File) RetrofitError(retrofit.RetrofitError)

Example 33 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class SpinnakerServiceProvider method getSpinnakerService.

SpinnakerService getSpinnakerService(SpinnakerService.Type type) {
    Field serviceField = getField(type.getCanonicalName() + "service");
    if (serviceField == null) {
        return null;
    }
    serviceField.setAccessible(true);
    try {
        return (SpinnakerService) serviceField.get(this);
    } catch (IllegalAccessException e) {
        throw new HalException(Problem.Severity.FATAL, "Can't access service field for " + type + ": " + e.getMessage());
    } finally {
        serviceField.setAccessible(false);
    }
}
Also used : Field(java.lang.reflect.Field) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException)

Example 34 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class DistributedService method buildDeployServerGroupPipeline.

default Map<String, Object> buildDeployServerGroupPipeline(AccountDeploymentDetails<A> details, SpinnakerRuntimeSettings runtimeSettings, List<ConfigSource> configSources, Integer maxRemaining, boolean scaleDown) {
    String accountName = details.getAccount().getName();
    String region = runtimeSettings.getServiceSettings(getService()).getLocation();
    Map<String, Object> deployDescription = getServerGroupDescription(details, runtimeSettings, configSources);
    deployDescription.put("name", "deploy");
    Map<String, String> source = new HashMap<>();
    RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
    if (runningServiceDetails.getLatestEnabledVersion() == null) {
        throw new HalException(Problem.Severity.FATAL, "No prior server group to clone for " + getServiceName());
    }
    source.put("account", accountName);
    source.put("credentials", accountName);
    source.put("serverGroupName", getVersionedName(runningServiceDetails.getLatestEnabledVersion()));
    source.put("region", region);
    source.put("namespace", region);
    deployDescription.put("source", source);
    deployDescription.put("interestingHealthProviders", getHealthProviders());
    deployDescription.put("type", AtomicOperations.CLONE_SERVER_GROUP);
    deployDescription.put("cloudProvider", getProviderType().getId());
    deployDescription.put("refId", "deployredblack");
    deployDescription.put("region", getRegion(runtimeSettings.getServiceSettings(getService())));
    deployDescription.put("strategy", "redblack");
    if (maxRemaining != null) {
        deployDescription.put("maxRemainingAsgs", maxRemaining + "");
    }
    deployDescription.put("scaleDown", scaleDown + "");
    if (scaleDown) {
        deployDescription.put("allowShrinkDownActive", "true");
    }
    List<Map<String, Object>> stages = new ArrayList<>();
    stages.add(deployDescription);
    Map<String, Object> pipeline = new HashMap<>();
    pipeline.put("stages", stages);
    pipeline.put("application", "spin");
    pipeline.put("name", "Deploy/Upgrade " + getServiceName());
    pipeline.put("description", "Auto-generated by Halyard");
    return pipeline;
}
Also used : HashMap(java.util.HashMap) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 35 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class DistributedService method buildRollbackPipeline.

default Map<String, Object> buildRollbackPipeline(AccountDeploymentDetails<A> details, SpinnakerRuntimeSettings runtimeSettings) {
    RunningServiceDetails serviceDetails = getRunningServiceDetails(details, runtimeSettings);
    Integer version = serviceDetails.getLatestEnabledVersion();
    if (version == null) {
        throw new HalException(Problem.Severity.FATAL, "There are no enabled server groups for service " + getServiceName() + " nothing to rollback to.");
    }
    int targetSize = serviceDetails.getInstances().get(version).size();
    targetSize = targetSize == 0 ? 1 : targetSize;
    ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
    Map<String, Object> baseDescription = new HashMap<>();
    baseDescription.put("cloudProvider", getProviderType().getId());
    baseDescription.put("cloudProviderType", getProviderType().getId());
    baseDescription.put("region", getRegion(settings));
    baseDescription.put("credentials", details.getAccount().getName());
    baseDescription.put("cluster", getServiceName());
    baseDescription.put("name", "rollback");
    Map<String, Object> capacity = new HashMap<>();
    capacity.put("desired", targetSize);
    Map<String, Object> resizeDescription = new HashMap<>();
    resizeDescription.putAll(baseDescription);
    String resizeId = "resize";
    resizeDescription.put("name", "Resize old " + getServiceName() + " to prior size");
    resizeDescription.put("capacity", capacity);
    resizeDescription.put("type", "resizeServerGroup");
    resizeDescription.put("refId", resizeId);
    resizeDescription.put("target", "ancestor_asg_dynamic");
    resizeDescription.put("action", "scale_exact");
    resizeDescription.put("requisiteStageRefIds", Collections.emptyList());
    Map<String, Object> enableDescription = new HashMap<>();
    enableDescription.putAll(baseDescription);
    String enableId = "enable";
    enableDescription.put("name", "Enable old " + getServiceName());
    enableDescription.put("type", "enableServerGroup");
    enableDescription.put("refId", enableId);
    enableDescription.put("target", "ancestor_asg_dynamic");
    enableDescription.put("requisiteStageRefIds", Collections.singletonList(resizeId));
    // This is a destroy, rather than a disable because the typical flow will look like this:
    // 
    // 1. You deploy a new version/config
    // 2. Something is wrong, so you rollback.
    // 3. Fixing the bad server group requires redeploying.
    // 
    // Since you can't fix the newest destroyed server group in place, and you won't (at least I can't imagine why)
    // want to reenable that server group, there is no point it keeping it around. There's an argument
    // to be made for keeping it around to debug, but that's far from what the average halyard user will want
    // to do.
    Map<String, Object> destroyDescription = new HashMap<>();
    String destroyId = "destroy";
    destroyDescription.putAll(baseDescription);
    destroyDescription.put("name", "Destroy current " + getServiceName());
    destroyDescription.put("type", "destroyServerGroup");
    destroyDescription.put("refId", destroyId);
    destroyDescription.put("requisiteStageRefIds", Collections.singletonList(enableId));
    destroyDescription.put("target", "current_asg_dynamic");
    List<Map<String, Object>> stages = new ArrayList<>();
    stages.add(resizeDescription);
    stages.add(enableDescription);
    stages.add(destroyDescription);
    Map<String, Object> pipeline = new HashMap<>();
    pipeline.put("stages", stages);
    pipeline.put("application", "spin");
    pipeline.put("name", "Rollback " + getServiceName());
    pipeline.put("description", "Auto-generated by Halyard");
    return pipeline;
}
Also used : HashMap(java.util.HashMap) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) HasServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.HasServiceSettings) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)88 IOException (java.io.IOException)37 ConfigProblemBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder)17 ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)16 ArrayList (java.util.ArrayList)15 FileInputStream (java.io.FileInputStream)14 File (java.io.File)12 HashMap (java.util.HashMap)12 JobStatus (com.netflix.spinnaker.halyard.core.job.v1.JobStatus)11 RunningServiceDetails (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails)11 Map (java.util.Map)11 JobRequest (com.netflix.spinnaker.halyard.core.job.v1.JobRequest)10 Field (java.lang.reflect.Field)9 SpinnakerRuntimeSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings)8 Path (java.nio.file.Path)8 List (java.util.List)7 Compute (com.google.api.services.compute.Compute)6 Problem (com.netflix.spinnaker.halyard.core.problem.v1.Problem)6 Paths (java.nio.file.Paths)6 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)5