Search in sources :

Example 11 with CloudConductorException

use of de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException in project cloudconductor-agent-redhat by cinovo.

the class ServerCom method getFileData.

/**
 * @param cf the file
 * @return the data
 * @throws CloudConductorException thrown if communication with cloudconductor failed
 * @throws TransformationErrorException error on generating the localized config file
 */
public static String getFileData(ConfigFile cf) throws CloudConductorException, TransformationErrorException {
    try {
        String content = ServerCom.agent.getConfigFileData(cf.getName());
        content = content.replaceAll("\\r\\n", "\n");
        content = content.replaceAll("\\r", "\n");
        if (!cf.isTemplate()) {
            return content;
        }
        StringWriter w = new StringWriter();
        try {
            Velocity.evaluate(AgentState.vContext(), w, "configfileGen", content);
        } catch (ParseErrorException | MethodInvocationException | ResourceNotFoundException | IOException e) {
            throw new TransformationErrorException("Failed to generate template", e);
        }
        return w.toString();
    } catch (RuntimeException e) {
        throw new CloudConductorException(e.getMessage());
    }
}
Also used : CloudConductorException(de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException) StringWriter(java.io.StringWriter) ParseErrorException(org.apache.velocity.exception.ParseErrorException) TransformationErrorException(de.cinovo.cloudconductor.agent.exceptions.TransformationErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 12 with CloudConductorException

use of de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException in project cloudconductor-agent-redhat by cinovo.

the class RefreshJWTJob method run.

@Override
public void run() {
    RefreshJWTJob.LOGGER.debug("Start RefreshJWTJob");
    long period = this.defaultPeriod;
    AgentState.info().setJWT(null);
    try {
        String newJWT = ServerCom.getJWT();
        if (newJWT != null) {
            JWTClaimsSet claimsSet = SignedJWT.parse(newJWT).getJWTClaimsSet();
            AgentState.info().setJWT(newJWT);
            period = JWTHelper.calcNextRefreshInMillis(claimsSet);
            RefreshJWTJob.LOGGER.debug("Authentication successful!");
        } else {
            RefreshJWTJob.LOGGER.error("Authentication failed: Missing JWT!");
            throw new CloudConductorException("Missing JWT!");
        }
    } catch (CloudConductorException e) {
        RefreshJWTJob.LOGGER.error("Error refreshing JWT: ", e);
    } catch (ParseException e) {
        RefreshJWTJob.LOGGER.error("Error parsing new JWT: ", e);
    } finally {
        SchedulerService.instance.executeOnce(new RefreshJWTJob(), period, TimeUnit.MILLISECONDS);
        if (period == this.defaultPeriod) {
            RefreshJWTJob.LOGGER.warn("Scheduled next refresh of JWT in default period time! Something is wrong. ");
        }
        RefreshJWTJob.LOGGER.debug("Scheduled next refresh of JWT in " + period + " ms");
        RefreshJWTJob.LOGGER.debug("Finished RefreshJWTJob");
    }
}
Also used : CloudConductorException(de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) ParseException(java.text.ParseException)

Example 13 with CloudConductorException

use of de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException 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)

Example 14 with CloudConductorException

use of de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException in project cloudconductor-agent-redhat by cinovo.

the class PackageHandler method reportInstalledPackages.

private PackageStateChanges reportInstalledPackages() throws ExecutionError {
    PackageState installedPackages;
    PackageHandler.LOGGER.debug("Starting to report packages");
    IExecutor<List<PackageVersion>> execute = new InstalledPackages().execute();
    PackageHandler.LOGGER.debug("Found packages to report");
    installedPackages = new PackageState(execute.getResult());
    try {
        return ServerCom.notifyInstalledPackages(installedPackages);
    } catch (CloudConductorException e) {
        PackageHandler.LOGGER.error("Error reporting installed packages: ", e);
        throw new ExecutionError(e);
    }
}
Also used : CloudConductorException(de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException) ExecutionError(de.cinovo.cloudconductor.agent.exceptions.ExecutionError) InstalledPackages(de.cinovo.cloudconductor.agent.executors.InstalledPackages) PackageState(de.cinovo.cloudconductor.api.model.PackageState) List(java.util.List)

Example 15 with CloudConductorException

use of de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException in project cloudconductor-agent-redhat by cinovo.

the class ServiceHandler method run.

/**
 * @throws ExecutionError an error occurred during execution
 */
public void run() throws ExecutionError {
    ServiceHandler.LOGGER.debug("Start ServiceHandler");
    ServiceHandler.LOGGER.debug("ServiceHandler : Report running services");
    List<String> runningServices = this.collectRunningServices();
    ServiceStates req = new ServiceStates(runningServices);
    ServiceStatesChanges serviceChanges;
    try {
        serviceChanges = ServerCom.notifyRunningServices(req);
    } catch (CloudConductorException e) {
        throw new ExecutionError(e);
    }
    int nStart = serviceChanges.getToStart().size();
    int nStop = serviceChanges.getToStop().size();
    int nRestart = serviceChanges.getToRestart().size();
    ServiceHandler.LOGGER.debug("Service changes: " + nStart + " to be started, " + nStop + " to be stopped, " + nRestart + " to be restarted");
    // handle service changes
    ServiceHandler.LOGGER.debug("ServiceHandler: Handle service changes");
    ScriptExecutor serviceHandler = ScriptExecutor.generateServiceStateHandler(serviceChanges.getToRestart(), serviceChanges.getToStart(), serviceChanges.getToStop());
    try {
        serviceHandler.execute();
    } catch (ExecutionError e) {
        // just log the error but go on with execution
        ServiceHandler.LOGGER.error("Error executing service handler: ", e);
    }
    // notify server on current state
    ServiceHandler.LOGGER.debug("ServiceHandler : Report running services again");
    runningServices = this.collectRunningServices();
    req = new ServiceStates(runningServices);
    try {
        ServerCom.notifyRunningServices(req);
    } catch (CloudConductorException e) {
        throw new ExecutionError(e);
    }
    ServiceHandler.LOGGER.debug("Finished ServiceHandler");
}
Also used : CloudConductorException(de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException) ExecutionError(de.cinovo.cloudconductor.agent.exceptions.ExecutionError) ServiceStates(de.cinovo.cloudconductor.api.model.ServiceStates) ServiceStatesChanges(de.cinovo.cloudconductor.api.model.ServiceStatesChanges) ScriptExecutor(de.cinovo.cloudconductor.agent.executors.ScriptExecutor)

Aggregations

CloudConductorException (de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException)16 ExecutionError (de.cinovo.cloudconductor.agent.exceptions.ExecutionError)9 ScriptExecutor (de.cinovo.cloudconductor.agent.executors.ScriptExecutor)3 IOException (java.io.IOException)3 TransformationErrorException (de.cinovo.cloudconductor.agent.exceptions.TransformationErrorException)2 ConfigFile (de.cinovo.cloudconductor.api.model.ConfigFile)2 HashCode (com.google.common.hash.HashCode)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 ServerConnectionException (de.cinovo.cloudconductor.agent.exceptions.ServerConnectionException)1 FileExecutor (de.cinovo.cloudconductor.agent.executors.FileExecutor)1 InstalledPackages (de.cinovo.cloudconductor.agent.executors.InstalledPackages)1 RefreshJWTJob (de.cinovo.cloudconductor.agent.jobs.RefreshJWTJob)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 RepoHandler (de.cinovo.cloudconductor.agent.jobs.handler.RepoHandler)1 AgentOption (de.cinovo.cloudconductor.api.model.AgentOption)1 ConfigValue (de.cinovo.cloudconductor.api.model.ConfigValue)1 PackageState (de.cinovo.cloudconductor.api.model.PackageState)1 Repo (de.cinovo.cloudconductor.api.model.Repo)1