From 58bf491d83a757bdbf0d5ebfd66780b4458e02e3 Mon Sep 17 00:00:00 2001 From: Thomas Vachuska Date: Tue, 31 Oct 2017 12:00:32 -0700 Subject: [PATCH] Adding utilities for offline backup/restore. Relocating local ECM caches and partitions under data/db directory. Change-Id: I4790c7488765d8195496a1830abfca897129784c --- .../persistence/impl/PersistenceManager.java | 21 +++++++------ .../primitives/impl/CoordinationManager.java | 3 +- .../primitives/impl/PartitionManager.java | 12 +++---- tools/package/bin/onos-backup | 31 +++++++++++++++++++ tools/package/bin/onos-restore | 26 ++++++++++++++++ tools/package/bin/onos-secure-ssh | 3 -- 6 files changed, 76 insertions(+), 20 deletions(-) create mode 100755 tools/package/bin/onos-backup create mode 100755 tools/package/bin/onos-restore delete mode 100755 tools/package/bin/onos-secure-ssh diff --git a/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceManager.java b/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceManager.java index 3f7a49b096..498f5d0aed 100644 --- a/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceManager.java +++ b/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceManager.java @@ -25,7 +25,6 @@ import org.mapdb.DBMaker; import org.onosproject.persistence.PersistenceService; import org.onosproject.persistence.PersistentMapBuilder; import org.onosproject.persistence.PersistentSetBuilder; -import org.osgi.service.component.ComponentContext; import org.slf4j.Logger; import java.io.File; @@ -42,17 +41,19 @@ import static org.onosproject.security.AppPermission.Type.PERSISTENCE_WRITE; import static org.slf4j.LoggerFactory.getLogger; /** - * Service that maintains local disk backed maps and sets. This implementation automatically deletes empty structures - * on shutdown. + * Service that maintains local disk backed maps and sets. + * This implementation automatically deletes empty structures on shutdown. */ @Component(immediate = true) @Service public class PersistenceManager implements PersistenceService { - private static final String DATABASE_PATH = "localDB"; + private static final String DATABASE_ROOT = + System.getProperty("karaf.data") + "/db/local/"; + + private static final String DATABASE_PATH = "cache"; static final String MAP_PREFIX = "map:"; - static final String SET_PREFIX = "set:"; private final Logger log = getLogger(getClass()); @@ -66,10 +67,10 @@ public class PersistenceManager implements PersistenceService { private final CommitTask commitTask = new CommitTask(); @Activate - public void activate(ComponentContext context) { + public void activate() { timer = new Timer(); - // bundle's persistent storage area directory - File dbFolderPath = context.getBundleContext().getDataFile(""); + + File dbFolderPath = new File(DATABASE_ROOT); Path dbPath = dbFolderPath.toPath().resolve(DATABASE_PATH); log.debug("dbPath: {}", dbPath); @@ -105,15 +106,15 @@ public class PersistenceManager implements PersistenceService { for (Map.Entry entry : localDB.getAll().entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); - //This is a map implementation to be handled as such if (value instanceof Map) { + // This is a map implementation to be handled as such Map asMap = (Map) value; if (asMap.isEmpty()) { //the map is empty and may be deleted localDB.delete(key); } - //This is a set implementation and can be handled as such } else if (value instanceof Set) { + // This is a set implementation and can be handled as such Set asSet = (Set) value; if (asSet.isEmpty()) { //the set is empty and may be deleted diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/CoordinationManager.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/CoordinationManager.java index 80753f366e..f92e3f5da2 100644 --- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/CoordinationManager.java +++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/CoordinationManager.java @@ -57,6 +57,7 @@ import org.slf4j.Logger; import static org.onosproject.security.AppGuard.checkPermission; import static org.onosproject.security.AppPermission.Type.STORAGE_WRITE; +import static org.onosproject.store.primitives.impl.PartitionManager.PARTITIONS_DIR; import static org.slf4j.LoggerFactory.getLogger; /** @@ -94,7 +95,7 @@ public class CoordinationManager implements CoordinationService { null, clusterCommunicator, clusterService, - new File(System.getProperty("karaf.data") + "/partitions/coordination")); + new File(PARTITIONS_DIR + "/coordination")); partition.open().join(); primitiveCreator = partition.client(); log.info("Started"); diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/PartitionManager.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/PartitionManager.java index 5bbd681cae..8d5440d9cf 100644 --- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/PartitionManager.java +++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/PartitionManager.java @@ -68,6 +68,9 @@ import static org.slf4j.LoggerFactory.getLogger; public class PartitionManager extends AbstractListenerManager implements PartitionService, PartitionAdminService { + static final String PARTITIONS_DIR = + System.getProperty("karaf.data") + "/db/partitions/"; + private final Logger log = getLogger(getClass()); @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) @@ -110,16 +113,14 @@ public class PartitionManager extends AbstractListenerManager/dev/null 2>&1 && pwd)} +ONOS_DEFAULT_ARCHIVE=$(dirname $ONOS_HOME)/onos-data.tar.gz +ONOS_KARAF=$ONOS_HOME/apache-karaf-3.0.8 + +# Fetch the name of the archive or use a default if none is specified +archive=${1:-$ONOS_DEFAULT_ARCHIVE} +archive=$(cd "$(dirname "$archive")"; pwd)/$(basename "$archive") + +# Make sure that the apache karaf data directory exists +[ ! -d $ONOS_KARAF/data ] && echo "ONOS data directory not found" && exit 1 + +# Make sure that the archive lies outside of $ONOS_HOME for safety +[[ "$archive" =~ ^$ONOS_HOME/.* ]] && \ + echo "Archive cannot be in $ONOS_HOME directory tree" && exit 1 + +# Make sure that the archive file does not already exist +[ -f $archive ] && echo "Archive $archive already exists" && exit 1 + +# Make sure we can write the archive +! touch $archive 2>/dev/null && \ + echo "No permission to write archive $archive" && exit 1 +rm -f $archive + +# Now produce the archive using the subset of the data directory tree +cd $ONOS_KARAF +tar zcf $archive data/db data/log diff --git a/tools/package/bin/onos-restore b/tools/package/bin/onos-restore new file mode 100755 index 0000000000..b7204e553c --- /dev/null +++ b/tools/package/bin/onos-restore @@ -0,0 +1,26 @@ +#!/bin/bash +# ----------------------------------------------------------------------------- +# ONOS utility to restore persistent data from a backup archive. +# ----------------------------------------------------------------------------- + +ONOS_HOME=${ONOS_HOME:-$(cd $(dirname $0)/.. >/dev/null 2>&1 && pwd)} +ONOS_DEFAULT_ARCHIVE=$(dirname $ONOS_HOME)/onos-data.tar.gz +ONOS_KARAF=$ONOS_HOME/apache-karaf-3.0.8 + +# Fetch the name of the archive or use a default if none is specified +archive=${1:-$ONOS_DEFAULT_ARCHIVE} +archive=$(cd "$(dirname "$1")"; pwd)/$(basename "$1") + +# Make sure that the apache karaf data directory exists +[ ! -d $ONOS_KARAF/data ] && echo "ONOS data directory not found" && exit 1 + +# Make sure that the archive lies outside of $ONOS_HOME for safety +[[ "$archive" =~ ^$ONOS_HOME/.* ]] && \ + echo "Archive cannot be in $ONOS_HOME directory tree" && exit 1 + +# Make sure that the archive file exists +[ ! -f $archive ] && echo "Archive $archive not found" && exit 1 + +# Now unroll the archive into the data directory tree +cd $ONOS_KARAF +tar zxf $archive diff --git a/tools/package/bin/onos-secure-ssh b/tools/package/bin/onos-secure-ssh deleted file mode 100755 index fba3a03359..0000000000 --- a/tools/package/bin/onos-secure-ssh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -echo "This command has been deprecated!" -echo "Please use 'onos-user-key' and 'onos-user-password' commands instead." \ No newline at end of file