Search in sources :

Example 6 with HalException

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);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 7 with HalException

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;
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 8 with HalException

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);
    }
}
Also used : Arrays(java.util.Arrays) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) BufferedOutputStream(java.io.BufferedOutputStream) Halconfig(com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) HalconfigParser(com.netflix.spinnaker.halyard.config.config.v1.HalconfigParser) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) Path(java.nio.file.Path) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HalconfigDirectoryStructure(com.netflix.spinnaker.halyard.config.config.v1.HalconfigDirectoryStructure) File(java.io.File) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Paths(java.nio.file.Paths) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) Objects(java.util.Objects) IOException(java.io.IOException) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

Example 9 with HalException

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);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) Path(java.nio.file.Path) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 10 with HalException

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);
    }
}
Also used : JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) FileOutputStream(java.io.FileOutputStream) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted) File(java.io.File)

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