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);
}
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations