Search in sources :

Example 6 with ExecutionError

use of de.cinovo.cloudconductor.agent.exceptions.ExecutionError in project cloudconductor-agent-redhat by cinovo.

the class FilesJob method run.

@Override
public void run() {
    FilesJob.LOGGER.debug("Started FilesJob");
    // only run if no other blocking job is currently running
    if (AgentState.filesExecutionLock.tryLock()) {
        try {
            new ConfigFileHandler().run();
        } catch (ExecutionError e) {
            if (e.getCause() instanceof CloudConductorException) {
                FilesJob.LOGGER.debug(e.getMessage(), e);
            }
        } finally {
            AgentState.filesExecutionLock.unlock();
        }
    }
    FilesJob.LOGGER.debug("Finished FilesJob");
}
Also used : CloudConductorException(de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException) ExecutionError(de.cinovo.cloudconductor.agent.exceptions.ExecutionError) ConfigFileHandler(de.cinovo.cloudconductor.agent.jobs.handler.ConfigFileHandler)

Example 7 with ExecutionError

use of de.cinovo.cloudconductor.agent.exceptions.ExecutionError in project cloudconductor-agent-redhat by cinovo.

the class NodeAgent method doAfterSpringStart.

@Override
public void doAfterSpringStart() {
    super.doAfterSpringStart();
    this.waitForServer();
    // initialize yum repos
    try {
        if (AgentState.repoExecutionLock.tryLock()) {
            new RepoHandler().run();
        }
    } catch (ExecutionError e) {
        NodeAgent.LOGGER.error("Error initializing yum repos: ", e);
        return;
    } finally {
        AgentState.repoExecutionLock.unlock();
    }
    // start timed jobs
    for (Class<AgentJob> jobClazz : OptionHandler.jobRegistry) {
        AgentJob job;
        try {
            job = jobClazz.newInstance();
            if (job.isDefaultStart()) {
                SchedulerService.instance.register(job.getJobIdentifier(), job, job.defaultStartTimer(), job.defaultStartTimerUnit());
                NodeAgent.LOGGER.info("Registered " + job.getJobIdentifier() + " as defaultstart with " + job.defaultStartTimer() + ":" + job.defaultStartTimerUnit());
            } else {
                SchedulerService.instance.register(job.getJobIdentifier(), job);
                NodeAgent.LOGGER.info("Registered " + job.getJobIdentifier());
            }
        } catch (InstantiationException | IllegalAccessException e) {
            NodeAgent.LOGGER.error("Couldn't start job: " + jobClazz.getName(), e);
        }
    }
}
Also used : ExecutionError(de.cinovo.cloudconductor.agent.exceptions.ExecutionError) AgentJob(de.cinovo.cloudconductor.agent.jobs.AgentJob) RepoHandler(de.cinovo.cloudconductor.agent.jobs.handler.RepoHandler)

Example 8 with ExecutionError

use of de.cinovo.cloudconductor.agent.exceptions.ExecutionError in project cloudconductor-agent-redhat by cinovo.

the class FileExecutor method execute.

@Override
public IExecutor<Set<String>> execute() throws ExecutionError {
    this.errors = new StringBuilder();
    for (ConfigFile configFile : this.files) {
        boolean changeOccured = false;
        if (configFile.isDirectory()) {
            File dirs = new File(configFile.getTargetPath());
            String dirMode = configFile.getFileMode();
            if (!dirs.exists()) {
                // create missing directories
                if (dirs.mkdirs()) {
                    changeOccured = true;
                    this.checkDirPermOwner(dirs, dirMode, configFile.getOwner(), configFile.getGroup());
                }
            } else {
                changeOccured = this.checkDirPermOwner(dirs, dirMode, configFile.getOwner(), configFile.getGroup());
            }
        } else {
            // in case config file is a file
            File localFile = new File(configFile.getTargetPath());
            HashCode localFileHash = FileHelper.getChecksum(localFile);
            String serverFile;
            try {
                serverFile = ServerCom.getFileData(configFile);
            } catch (TransformationErrorException | CloudConductorException e) {
                FileExecutor.LOGGER.error("Error getting file data for config file '" + configFile.getName() + "': ", e);
                continue;
            }
            HashCode serverFileHash = FileHelper.getChecksum(serverFile);
            if (!serverFileHash.equals(localFileHash)) {
                try {
                    FileExecutor.LOGGER.debug("Update config file '" + configFile.getName() + "'...");
                    Files.createParentDirs(localFile);
                    Files.write(serverFile, localFile, Charset.forName("UTF-8"));
                    changeOccured = true;
                } catch (IOException e) {
                    // add error to exception list
                    this.errors.append("Failed to write file: " + localFile.getAbsolutePath());
                    this.errors.append(System.lineSeparator());
                    // just skip this file
                    continue;
                }
            }
            // set file owner and group
            try {
                if (!FileHelper.isFileOwner(localFile, configFile.getOwner(), configFile.getGroup())) {
                    FileHelper.chown(localFile, configFile.getOwner(), configFile.getGroup());
                    changeOccured = true;
                }
            } catch (IOException e) {
                this.errors.append("Failed to set user and/or group for file: " + localFile.getAbsolutePath());
                this.errors.append(System.lineSeparator());
            }
            // set file mode
            try {
                String fileMode = FileHelper.fileModeIntToString(ServerCom.getFileMode(configFile.getName()));
                if (!FileHelper.isFileMode(localFile, fileMode)) {
                    FileHelper.chmod(localFile, fileMode);
                    changeOccured = true;
                }
            } catch (IOException e) {
                this.errors.append("Failed to set chmod for file: " + localFile.getAbsolutePath());
                this.errors.append(System.lineSeparator());
            } catch (CloudConductorException e) {
                this.errors.append(e.getMessage());
                this.errors.append(System.lineSeparator());
            }
        }
        // set services to restart
        if (configFile.isReloadable() && changeOccured) {
            Set<String> servicesToRestart = configFile.getDependentServices();
            this.restart.addAll(servicesToRestart);
            FileExecutor.LOGGER.debug("Config file changed, " + servicesToRestart.size() + " services have to be restarted!");
        }
    }
    if (!this.errors.toString().trim().isEmpty()) {
        throw new ExecutionError(this.errors.toString().trim());
    }
    return this;
}
Also used : CloudConductorException(de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException) ExecutionError(de.cinovo.cloudconductor.agent.exceptions.ExecutionError) HashCode(com.google.common.hash.HashCode) ConfigFile(de.cinovo.cloudconductor.api.model.ConfigFile) TransformationErrorException(de.cinovo.cloudconductor.agent.exceptions.TransformationErrorException) IOException(java.io.IOException) ConfigFile(de.cinovo.cloudconductor.api.model.ConfigFile) File(java.io.File)

Example 9 with ExecutionError

use of de.cinovo.cloudconductor.agent.exceptions.ExecutionError in project cloudconductor-agent-redhat by cinovo.

the class DefaultJob method handleServices.

private void handleServices() {
    try {
        ServiceHandler serviceHandler = new ServiceHandler();
        serviceHandler.run();
    } catch (ExecutionError e) {
        DefaultJob.LOGGER.error("Error handling services: " + e.getMessage(), e);
    }
}
Also used : ExecutionError(de.cinovo.cloudconductor.agent.exceptions.ExecutionError) ServiceHandler(de.cinovo.cloudconductor.agent.jobs.handler.ServiceHandler)

Example 10 with ExecutionError

use of de.cinovo.cloudconductor.agent.exceptions.ExecutionError in project cloudconductor-agent-redhat by cinovo.

the class RepoHandler method run.

/**
 * @throws ExecutionError an error occurred during the execution
 */
public void run() throws ExecutionError {
    RepoHandler.LOGGER.debug("Start RepoHandler");
    Set<Repo> repos;
    try {
        repos = ServerCom.getRepos();
    } catch (CloudConductorException e) {
        throw new ExecutionError("Error getting repositories for template: ", e);
    }
    RepoHandler.LOGGER.debug("Update " + repos.size() + " yum repos...");
    Set<String> repoNames = new HashSet<>();
    for (Repo repo : repos) {
        try {
            repoNames.add(repo.getName());
            FileHelper.writeYumRepo(repo);
        } catch (IOException e) {
            throw new ExecutionError("Error writing yum repo for '" + repo.getName() + "': ", e);
        }
    }
    AgentState.info().setRepos(repoNames);
    RepoHandler.LOGGER.debug("Finished RepoHandler");
}
Also used : CloudConductorException(de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException) ExecutionError(de.cinovo.cloudconductor.agent.exceptions.ExecutionError) Repo(de.cinovo.cloudconductor.api.model.Repo) IOException(java.io.IOException) HashSet(java.util.HashSet)

Aggregations

ExecutionError (de.cinovo.cloudconductor.agent.exceptions.ExecutionError)13 CloudConductorException (de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException)9 ScriptExecutor (de.cinovo.cloudconductor.agent.executors.ScriptExecutor)3 IOException (java.io.IOException)3 RepoHandler (de.cinovo.cloudconductor.agent.jobs.handler.RepoHandler)2 ConfigFile (de.cinovo.cloudconductor.api.model.ConfigFile)2 HashSet (java.util.HashSet)2 HashCode (com.google.common.hash.HashCode)1 TransformationErrorException (de.cinovo.cloudconductor.agent.exceptions.TransformationErrorException)1 FileExecutor (de.cinovo.cloudconductor.agent.executors.FileExecutor)1 InstalledPackages (de.cinovo.cloudconductor.agent.executors.InstalledPackages)1 AgentJob (de.cinovo.cloudconductor.agent.jobs.AgentJob)1 ConfigFileHandler (de.cinovo.cloudconductor.agent.jobs.handler.ConfigFileHandler)1 OptionHandler (de.cinovo.cloudconductor.agent.jobs.handler.OptionHandler)1 PackageHandler (de.cinovo.cloudconductor.agent.jobs.handler.PackageHandler)1 ServiceHandler (de.cinovo.cloudconductor.agent.jobs.handler.ServiceHandler)1 AgentOption (de.cinovo.cloudconductor.api.model.AgentOption)1 PackageState (de.cinovo.cloudconductor.api.model.PackageState)1 PackageVersion (de.cinovo.cloudconductor.api.model.PackageVersion)1 Repo (de.cinovo.cloudconductor.api.model.Repo)1