use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class GoogleStorage method writeBytes.
void writeBytes(String name, byte[] contents) {
name = String.join("/", rootFolder, name);
try {
StorageObject object = new StorageObject().setBucket(bucketId).setName(name);
ByteArrayContent content = new ByteArrayContent("application/text", contents);
storage.objects().insert(bucketId, object, content).execute();
} catch (IOException e) {
throw new HalException(Problem.Severity.FATAL, "Failed to write to " + name + ": " + e.getMessage(), e);
}
}
use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class BackupService method tarHalconfig.
private void tarHalconfig(String halconfigDir, String halconfigTar) throws IOException {
FileOutputStream tarOutput = null;
BufferedOutputStream bufferedTarOutput = null;
TarArchiveOutputStream tarArchiveOutputStream = null;
IOException fatalCleanup = null;
try {
tarOutput = new FileOutputStream(new File(halconfigTar));
bufferedTarOutput = new BufferedOutputStream(tarOutput);
tarArchiveOutputStream = new TarArchiveOutputStream(bufferedTarOutput);
TarArchiveOutputStream finalTarArchiveOutputStream = tarArchiveOutputStream;
Arrays.stream(new File(halconfigDir).listFiles()).filter(Objects::nonNull).forEach(f -> addFileToTar(finalTarArchiveOutputStream, f.getAbsolutePath(), ""));
} catch (HalException e) {
log.info("HalException caught during tar operation", e);
throw e;
} catch (IOException e) {
log.info("IOException caught during tar operation", e);
throw new HalException(Problem.Severity.FATAL, "Failed to backup halconfig: " + e.getMessage(), e);
} finally {
if (tarArchiveOutputStream != null) {
try {
tarArchiveOutputStream.finish();
tarArchiveOutputStream.close();
} catch (IOException e) {
fatalCleanup = e;
}
}
if (bufferedTarOutput != null) {
bufferedTarOutput.close();
}
if (tarOutput != null) {
tarOutput.close();
}
}
if (fatalCleanup != null) {
throw fatalCleanup;
}
}
use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class BackupService method addFileToTar.
private void addFileToTar(TarArchiveOutputStream tarArchiveOutputStream, String path, String base) {
File file = new File(path);
String fileName = file.getName();
if (Arrays.stream(omitPaths).anyMatch(s -> s.equals(fileName))) {
return;
}
String tarEntryName = String.join("/", base, fileName);
try {
if (file.isFile()) {
TarArchiveEntry tarEntry = new TarArchiveEntry(file, tarEntryName);
tarArchiveOutputStream.putArchiveEntry(tarEntry);
IOUtils.copy(new FileInputStream(file), tarArchiveOutputStream);
tarArchiveOutputStream.closeArchiveEntry();
} else if (file.isDirectory()) {
Arrays.stream(file.listFiles()).filter(Objects::nonNull).forEach(f -> addFileToTar(tarArchiveOutputStream, f.getAbsolutePath(), tarEntryName));
} else {
log.warn("Unknown file type: " + file + " - skipping addition to tar archive");
}
} catch (IOException e) {
throw new HalException(Problem.Severity.FATAL, "Unable to file " + file.getName() + " to archive entry: " + tarEntryName + " " + e.getMessage(), e);
}
}
use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class BackupService method untarHalconfig.
private void untarHalconfig(String halconfigDir, String halconfigTar) {
FileInputStream tarInput = null;
TarArchiveInputStream tarArchiveInputStream = null;
try {
tarInput = new FileInputStream(new File(halconfigTar));
tarArchiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", tarInput);
} catch (IOException | ArchiveException e) {
throw new HalException(Problem.Severity.FATAL, "Failed to open backup: " + e.getMessage(), e);
}
try {
ArchiveEntry archiveEntry = tarArchiveInputStream.getNextEntry();
while (archiveEntry != null) {
String entryName = archiveEntry.getName();
Path outputPath = Paths.get(halconfigDir, entryName);
File outputFile = outputPath.toFile();
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
if (archiveEntry.isDirectory()) {
outputFile.mkdir();
} else {
Files.copy(tarArchiveInputStream, outputPath, REPLACE_EXISTING);
}
archiveEntry = tarArchiveInputStream.getNextEntry();
}
} catch (IOException e) {
throw new HalException(Problem.Severity.FATAL, "Failed to read archive entry: " + e.getMessage(), e);
}
}
use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.
the class KubernetesV1ProviderUtils method storeInstanceLogs.
static void storeInstanceLogs(JobExecutor jobExecutor, AccountDeploymentDetails<KubernetesAccount> details, String namespace, String instanceName, String containerName, File outputFile) {
List<String> command = kubectlAccountCommand(details);
command.add("--namespace");
command.add(namespace);
command.add("logs");
command.add(instanceName);
command.add(containerName);
JobRequest request = new JobRequest().setTokenizedCommand(command);
JobStatus status;
try {
status = jobExecutor.backoffWait(jobExecutor.startJob(request));
} catch (InterruptedException e) {
throw new DaemonTaskInterrupted(e);
}
try {
IOUtils.write(status.getStdOut().getBytes(), new FileOutputStream(new File(outputFile, containerName)));
} catch (IOException e) {
throw new HalException(Severity.FATAL, "Unable to store logs: " + e.getMessage(), e);
}
}
Aggregations