Search in sources :

Example 16 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class JmxRrdMigratorOffline method rollback.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#rollback()
     */
@Override
public void rollback() throws OnmsUpgradeException {
    try {
        for (File jmxResourceDir : getJmxResourceDirectories()) {
            File zip = new File(jmxResourceDir.getAbsolutePath() + ZIP_EXT);
            FileUtils.deleteDirectory(jmxResourceDir);
            if (!jmxResourceDir.mkdirs()) {
                LOG.warn("Could not make directory: {}", jmxResourceDir.getPath());
            }
            unzipFile(zip, jmxResourceDir);
            if (!zip.delete()) {
                LOG.warn("Could not delete file: {}", zip.getPath());
            }
        }
        File configDir = new File(ConfigFileConstants.getFilePathString());
        for (File backupFile : backupFiles) {
            unzipFile(backupFile, configDir);
        }
    } catch (IOException e) {
        throw new OnmsUpgradeException("Can't restore the backup files because " + e.getMessage());
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 17 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class KscReportsMigrator method preExecute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#preExecute()
     */
public void preExecute() throws OnmsUpgradeException {
    try {
        log("Backing up %s\n", configFile);
        zipFile(configFile);
        initializeDatasource();
        KSC_PerformanceReportFactory.init();
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't initialize ksc-performance-reports.xml because " + e.getMessage());
    }
}
Also used : OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 18 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class MagicUsersMigratorOffline method preExecute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#preExecute()
     */
@Override
public void preExecute() throws OnmsUpgradeException {
    if (!canRun())
        return;
    try {
        File[] files = { magicUsersFile, magicUsersFileRPM, magicUsersFileDEB, usersFile };
        for (File file : files) {
            if (file.exists()) {
                log("Backing up %s\n", file);
                zipFile(file);
            }
        }
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't backup files because " + e.getMessage());
    }
}
Also used : File(java.io.File) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 19 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class DataCollectionConfigMigratorOffline method migrateDataCollectionConfig.

private void migrateDataCollectionConfig(File cfgFile) throws OnmsUpgradeException {
    try {
        log("  %s\n", cfgFile.getAbsolutePath());
        // Read the file into memory
        String cfgFileContents = FileUtils.readFileToString(cfgFile);
        // Perform the required substitutions
        for (final Map.Entry<String, String> substitution : substitutionMap.entrySet()) {
            cfgFileContents = cfgFileContents.replace(substitution.getKey(), substitution.getValue());
        }
        // Write the (modified) file back to disk
        FileUtils.write(cfgFile, cfgFileContents);
    } catch (IOException e) {
        throw new OnmsUpgradeException(String.format("Failed to update %s", cfgFile), e);
    }
}
Also used : IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 20 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class DiscoveryConfigurationLocationMigratorOffline method execute.

@Override
public void execute() throws OnmsUpgradeException {
    Writer out = null;
    try {
        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        final Document doc = docBuilder.parse(m_configFile);
        updateLocations(doc, "discovery-configuration");
        updateLocations(doc, "include-range");
        updateLocations(doc, "include-url");
        updateLocations(doc, "specific");
        final Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        out = new StringWriter();
        tf.transform(new DOMSource(doc), new StreamResult(out));
        FileUtils.write(m_configFile, out.toString());
    } catch (final IOException | SAXException | ParserConfigurationException | TransformerException e) {
        throw new OnmsUpgradeException("Failed to upgrade discovery-configuration.xml", e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) Document(org.w3c.dom.Document) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Aggregations

OnmsUpgradeException (org.opennms.upgrade.api.OnmsUpgradeException)31 IOException (java.io.IOException)23 File (java.io.File)14 FileWriter (java.io.FileWriter)8 StringWriter (java.io.StringWriter)6 ArrayList (java.util.ArrayList)5 Matcher (java.util.regex.Matcher)5 Map (java.util.Map)3 Pattern (java.util.regex.Pattern)3 LineIterator (org.apache.commons.io.LineIterator)3 DefaultDataCollectionConfigDao (org.opennms.netmgt.config.DefaultDataCollectionConfigDao)3 Service (org.opennms.netmgt.config.service.Service)3 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 Writer (java.io.Writer)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2