Search in sources :

Example 1 with ConfigFile

use of de.cinovo.cloudconductor.api.model.ConfigFile in project cloudconductor-agent-redhat by cinovo.

the class ConfigFileHandler method run.

/**
 * @throws ExecutionError an error occurred during execution
 */
public void run() throws ExecutionError {
    ConfigFileHandler.LOGGER.debug("Start Config File Handler");
    Set<ConfigFile> configFiles;
    try {
        configFiles = ServerCom.getFiles();
        ConfigFileHandler.LOGGER.debug("Received " + configFiles.size() + " configuration files.");
    } catch (CloudConductorException e) {
        ConfigFileHandler.LOGGER.error("Error getting configuration files from server: ", e);
        throw new ExecutionError(e);
    }
    // handle files
    ConfigFileHandler.LOGGER.debug("Handle files");
    IExecutor<Set<String>> files = new FileExecutor(configFiles);
    try {
        files.execute();
    } catch (ExecutionError e) {
        // just log the error but go on with execution
        ConfigFileHandler.LOGGER.error("Error handling files: " + e.getMessage(), e);
    }
    Set<String> servicesToRestart = files.getResult();
    if ((servicesToRestart != null) && !servicesToRestart.isEmpty()) {
        // handle restart of services
        ConfigFileHandler.LOGGER.debug("Restart services");
        ScriptExecutor serviceHandler = ScriptExecutor.generateServiceStateHandler(servicesToRestart, null, null);
        try {
            serviceHandler.execute();
        } catch (ExecutionError e) {
            // just log the error but go on with execution
            ConfigFileHandler.LOGGER.error(e.getMessage());
        }
    }
    ConfigFileHandler.LOGGER.debug("Finished Config File Handler");
}
Also used : CloudConductorException(de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException) ExecutionError(de.cinovo.cloudconductor.agent.exceptions.ExecutionError) Set(java.util.Set) FileExecutor(de.cinovo.cloudconductor.agent.executors.FileExecutor) ConfigFile(de.cinovo.cloudconductor.api.model.ConfigFile) ScriptExecutor(de.cinovo.cloudconductor.agent.executors.ScriptExecutor)

Example 2 with ConfigFile

use of de.cinovo.cloudconductor.api.model.ConfigFile 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 3 with ConfigFile

use of de.cinovo.cloudconductor.api.model.ConfigFile in project cloudconductor-agent-redhat by cinovo.

the class AgentHandler method getFileFileMode.

/**
 * @param fileName the name of the file or directory
 * @return file file mode the file mode of the given file or directory
 * @throws CloudConductorException Error indicating connection or data problems
 */
public String getFileFileMode(String fileName) throws CloudConductorException {
    String path = this.pathGenerator("/file/{name}", fileName);
    ConfigFile file = this._get(path, ConfigFile.class);
    return file.getFileMode();
}
Also used : ConfigFile(de.cinovo.cloudconductor.api.model.ConfigFile)

Aggregations

ConfigFile (de.cinovo.cloudconductor.api.model.ConfigFile)3 ExecutionError (de.cinovo.cloudconductor.agent.exceptions.ExecutionError)2 CloudConductorException (de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException)2 HashCode (com.google.common.hash.HashCode)1 TransformationErrorException (de.cinovo.cloudconductor.agent.exceptions.TransformationErrorException)1 FileExecutor (de.cinovo.cloudconductor.agent.executors.FileExecutor)1 ScriptExecutor (de.cinovo.cloudconductor.agent.executors.ScriptExecutor)1 File (java.io.File)1 IOException (java.io.IOException)1 Set (java.util.Set)1