[PATCH] Add namespaces support to rlFile{Backup,Restore}
by Jiri Jaburek
This change introduces a new --namespace option for rlFileBackup
and rlFileRestore, allowing the user to perform several backup/restore
operations independently.
Getopt might seem like an overkill, but there's no primitive and safe
way of using multiple long options with arguments without it.
Also while rlFileRestore could get away without it (no --clean),
I believe it's important to keep a certain level of consistency.
Signed-off-by: Jiri Jaburek <jjaburek(a)redhat.com>
---
src/infrastructure.sh | 70 ++++++++++++++++++++++++++++++++++--------
src/test/infrastructureTest.sh | 32 +++++++++++++++++++
2 files changed, 89 insertions(+), 13 deletions(-)
diff --git a/src/infrastructure.sh b/src/infrastructure.sh
index a5edd76..73096fa 100644
--- a/src/infrastructure.sh
+++ b/src/infrastructure.sh
@@ -295,7 +295,7 @@ Create a backup of files or directories (recursive). Can be used
multiple times to add more files to backup. Backing up an already
backed up file overwrites the original backup.
- rlFileBackup [--clean] file [file...]
+ rlFileBackup [--clean] [--namespace name] file [file...]
=over
@@ -305,6 +305,11 @@ If this option is provided (have to be first option of the command),
then file/dir backuped using this command (provided in next
options) will be (resursively) removed before we will restore it.
+=item --namespace name
+
+Specifies the namespace to use.
+Namespaces can be used to separate backups and their restoration.
+
=item file
Files and/or directories to be backed up.
@@ -329,9 +334,29 @@ Returns 0 if the backup was successful.
rlFileBackup() {
local backup status file path dir failed selinux acl
+ local OPTS clean="" namespace=""
+
+ # getopt will cut off first long opt when no short are defined
+ OPTS=$(getopt -o "cn:" -l "clean,namespace:" -- "$@")
+ [ $? -ne 0 ] && return 1
+
+ eval set -- "$OPTS"
+ while true; do
+ case "$1" in
+ '--clean') shift; clean=1 ;;
+ '--namespace') shift; namespace="$1"; shift ;;
+ --) shift; break ;;
+ esac
+ done;
+
+ # check parameter sanity
+ if [ -z "$1" ]; then
+ rlLogError "rlFileBackup: Nothing to backup... supply a file or dir"
+ return 2
+ fi
+
# check if we have '--clean' option and save items if we have
- if [ "$1" = '--clean' ]; then
- shift
+ if [ "$clean" ]; then
rlLogDebug "rlFileBackup: Adding '$@' to the clean list"
for file in "$@"; do
###rlLogDebug "rlFileBackup: ... '$@'"
@@ -343,18 +368,14 @@ rlFileBackup() {
done
fi
- # check parameter sanity
- if [ -z "$1" ]; then
- rlLogError "rlFileBackup: Nothing to backup... supply a file or dir"
- return 2
- fi
-
# set the backup dir
if [ -z "$BEAKERLIB_DIR" ]; then
rlLogError "rlFileBackup: BEAKERLIB_DIR not set, run rlJournalStart first"
return 3
fi
- backup="$BEAKERLIB_DIR/backup"
+
+ # backup dir to use, append namespace if defined
+ backup="$BEAKERLIB_DIR/backup${namespace:+-$namespace}"
# create backup dir (unless it already exists)
if [ -d "$backup" ]; then
@@ -442,15 +463,38 @@ has been made. If you don't want to leave anything behind just
remove the whole original tree before running C<rlFileRestore>,
or see C<--clean> option of C<rlFileBackup>.
- rlFileRestore
+ rlFileRestore [--namespace name]
+
+=over
+
+=item --namespace name
+
+Specifies the namespace to use.
+Namespaces can be used to separate backups and their restoration.
+
+=back
Returns 0 if backup dir is found and files are restored successfully.
=cut
rlFileRestore() {
- # check for backup dir first
- backup="$BEAKERLIB_DIR/backup"
+ local OPTS namespace=""
+
+ # getopt will cut off first long opt when no short are defined
+ OPTS=$(getopt -o "n:" -l "namespace:" -- "$@")
+ [ $? -ne 0 ] && return 1
+
+ eval set -- "$OPTS"
+ while true; do
+ case "$1" in
+ '--namespace') shift; namespace="$1"; shift ;;
+ --) shift; break ;;
+ esac
+ done;
+
+ # check for backup dir first, append namespace if defined
+ backup="$BEAKERLIB_DIR/backup${namespace:+-$namespace}"
if [ -d "$backup" ]; then
rlLogDebug "rlFileRestore: Backup dir ready: $backup"
else
diff --git a/src/test/infrastructureTest.sh b/src/test/infrastructureTest.sh
index 7d9fadf..8542728 100644
--- a/src/test/infrastructureTest.sh
+++ b/src/test/infrastructureTest.sh
@@ -198,6 +198,38 @@ test_rlFileBackupCleanAndRestoreWhitespace() {
chmod -R 777 "$BEAKERLIB_DIR/backup" && rm -rf "$BEAKERLIB_DIR/backup"
}
+test_rlFileBackupAndRestoreNamespaces() {
+ test_file=$(mktemp '/tmp/beakerlib-test-XXXXXX')
+ test_file2=$(mktemp '/tmp/beakerlib-test-XXXXXX')
+
+ # namespaced backup should restore independently
+ echo "abcde" > "$test_file"
+ rlFileBackup "$test_file"
+ echo "fghij" > "$test_file2"
+ rlFileBackup --namespace myspace1 "$test_file2"
+ echo "12345" > "$test_file"
+ echo "67890" > "$test_file2"
+ rlFileRestore
+ assertRun "grep 'abcde' '$test_file' && grep '67890' '$test_file2'" 0 \
+ "Normal restore shouldn't restore namespaced backup"
+ echo "12345" > "$test_file"
+ echo "67890" > "$test_file2"
+ rlFileRestore --namespace myspace1
+ assertRun "grep '12345' '$test_file' && grep 'fghij' '$test_file2'" 0 \
+ "Namespaced restore shouldn't restore normal backup"
+
+ # namespaced backup doesn't overwrite normal one
+ echo "abcde" > "$test_file"
+ rlFileBackup "$test_file"
+ echo "12345" > "$test_file"
+ rlFileBackup --namespace myspace2 "$test_file"
+ rlFileRestore
+ assertRun "grep 'abcde' '$test_file'" 0 \
+ "Namespaced backup shouldn't overwrite normal one"
+
+ rm -f "$test_file" "$test_file2"
+ chmod -R 777 "$BEAKERLIB_DIR"/backup* && rm -rf "$BEAKERLIB_DIR"/backup*
+}
test_rlFileBackup_MissingFiles() {
local dir
--
1.7.11.4
11 years, 5 months
[PATCH 0/6] Journaling cleanups, custom TMP dir
by Jiri Jaburek
This is a series of cleanups/rewrites of the journaling subsystem,
ultimately leading to custom BEAKERLIB_DIR specification (its usage
from the external environment), taking several side benefits along
the way.
Historically, "journalling.py" had --id option for Beaker test ID
specification, but was not really environment-independent (PKGNVR,
BEAKERLIB variables). Each invocation had to pass --id.
This made sense for beaker jobs, /tmp/beakerlib-$id/journal.xml
(hardcoded) was used as temp dir.
During manual runs, however, a randomly generated and created (mktemp)
directory /tmp/beakerlib-XXXXXXX was created and the XXXXXXX used
as "run_id" (instead of Beaker-provided test run number).
There was whole lot of an infrastructure in journal.sh to support this
kind of hack, like BEAKERLIB_RUN, which was then contained in
BEAKERLIB_DIR's path. This would make custom specification of
BEAKERLIB_DIR quite complex.
This series therefore removes BEAKERLIB_RUN, makes "journalling.py"
get the test run number from TESTID env variable directly, which
eliminates the need for --id passing infrastructure inside
journalling.py and also solves the hardcoded /tmp/beakerlib-%s/journal.xml
in it, by using BEAKERLIB_JOURNAL directly.
That simplifies temporary directory management a lot, making custom
BEAKERLIB_DIR specification possible, along with certain side advantages.
BEAKERLIB_RUN has been removed, BEAKERLIB_DIR management is done only
by journal.sh, which exports BEAKERLIB_JOURNAL for "journalling.py".
Furthermore, test_id is now present in final journal.xml only during
Beaker runs (where TESTID is available). This might lead to a potential
API change, assuming journal.xml is considered to be an API.
Specifics for each commit/change are in respective commit messages,
the series has been tested both in Beaker and during manual test runs,
test coverage for custom BEAKERLIB_DIR specification has been made.
Thanks for review.
--jjk
Jiri Jaburek (6):
Use BEAKERLIB_JOURNAL instead of hardcoded path with testid
Use TESTID env var, remove --id, restructuralize code
Remove BEAKERLIB_RUN from journal.sh completely
Do no use testid at all if TESTID env var is unset
Document Test run ID as a number rather than a string
Allow custom specification of BEAKERLIB_DIR
src/beakerlib.sh | 2 +-
src/journal.sh | 38 +++++++-------
src/python/journalling.py | 127 ++++++++++++++++++++++++----------------------
src/test/journalTest.sh | 43 ++++++++++++----
4 files changed, 122 insertions(+), 88 deletions(-)
--
1.7.11.2
11 years, 6 months