summaryrefslogtreecommitdiff
path: root/subplot
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-09-28 09:10:45 +0300
committerLars Wirzenius <liw@liw.fi>2020-10-03 10:34:56 +0300
commit864e20652458c1a4ae09c882ad3e29d6b0988b06 (patch)
tree2e08a43a4deb3759153105a8a3a4495faa3f9121 /subplot
parent164c5bfc707e21ebc1f1bf6bfb89e4adc2332ef0 (diff)
downloadobnam2-864e20652458c1a4ae09c882ad3e29d6b0988b06.tar.gz
feat: add rudimentary backup client
Also, a bit of logging for server.
Diffstat (limited to 'subplot')
-rw-r--r--subplot/obnam.py32
-rw-r--r--subplot/obnam.yaml6
2 files changed, 37 insertions, 1 deletions
diff --git a/subplot/obnam.py b/subplot/obnam.py
index ccfdc67..5947f1f 100644
--- a/subplot/obnam.py
+++ b/subplot/obnam.py
@@ -6,6 +6,7 @@ import re
import requests
import shutil
import socket
+import tarfile
import time
import urllib3
import yaml
@@ -33,7 +34,7 @@ def start_chunk_server(ctx):
logging.debug(f"Picked randomly port for obnam-server: {config['port']}")
ctx["config"] = config
- ctx["url"] = f"https://localhost:{port}"
+ ctx["url"] = f"http://localhost:{port}"
start_daemon(ctx, "obnam-server", [_binary("obnam-server"), filename])
@@ -51,6 +52,9 @@ def stop_chunk_server(ctx):
def create_file_with_random_data(ctx, filename=None):
N = 128
data = "".join(chr(random.randint(0, 255)) for i in range(N)).encode("UTF-8")
+ dirname = os.path.dirname(filename) or "."
+ logging.debug(f"create_file_with_random_data: dirname={dirname}")
+ os.makedirs(dirname, exist_ok=True)
with open(filename, "wb") as f:
f.write(data)
@@ -125,6 +129,32 @@ def json_body_matches(ctx, wanted=None):
assert_eq(body.get(key, "not.there"), wanted[key])
+def back_up_directory(ctx, dirname=None):
+ runcmd = globals()["runcmd"]
+
+ runcmd(ctx, ["pgrep", "-laf", "obnam"])
+
+ config = {"server_name": "localhost", "server_port": ctx["config"]["port"]}
+ config = yaml.safe_dump(config)
+ logging.debug(f"back_up_directory: {config}")
+ filename = "client.yaml"
+ with open(filename, "w") as f:
+ f.write(config)
+
+ tarball = f"{dirname}.tar"
+ t = tarfile.open(name=tarball, mode="w")
+ t.add(dirname, arcname=".")
+ t.close()
+
+ with open(tarball, "rb") as f:
+ runcmd(ctx, [_binary("obnam-backup"), filename], stdin=f)
+
+
+def command_is_successful(ctx):
+ exit_code_zero = globals()["exit_code_zero"]
+ exit_code_zero(ctx)
+
+
# Name of Rust binary, debug-build.
def _binary(name):
srcdir = globals()["srcdir"]
diff --git a/subplot/obnam.yaml b/subplot/obnam.yaml
index 065cb01..c2a3608 100644
--- a/subplot/obnam.yaml
+++ b/subplot/obnam.yaml
@@ -30,6 +30,9 @@
- when: "I try to DELETE /chunks/{chunk_id}"
function: delete_chunk_by_id
+- when: "I back up {dirname} with obnam-backup"
+ function: back_up_directory
+
- then: "HTTP status code is {status}"
function: status_code_is
@@ -45,3 +48,6 @@
- then: "the body matches file {filename}"
function: body_matches_file
+
+- then: "backup command is successful"
+ function: command_is_successful