From 864e20652458c1a4ae09c882ad3e29d6b0988b06 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 28 Sep 2020 09:10:45 +0300 Subject: feat: add rudimentary backup client Also, a bit of logging for server. --- subplot/obnam.py | 32 +++++++++++++++++++++++++++++++- subplot/obnam.yaml | 6 ++++++ 2 files changed, 37 insertions(+), 1 deletion(-) (limited to 'subplot') 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 -- cgit v1.2.1