Skip to content

Commit

Permalink
added --reserve to repo-create command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Dec 31, 2024
1 parent 254a811 commit 68592d9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion assimilate/assimilate.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,8 @@ def __enter__(self):
now = arrow.now()
pid = os.getpid()
contents = dict(
started = str(now),
cmdline = ' '.join(sys.argv),
started = str(now),
pid = pid
)
nt.dump(contents, lockfile)
Expand Down
39 changes: 21 additions & 18 deletions assimilate/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def get_available_archives(settings):
try:
data = json.loads(borg.stdout)
return data["archives"]
except json.decoder.JSONDecodeError as e:
except json.decoder.JSONDecodeError as e: # pragma: no cover
raise Error("Could not decode output of Borg list command.", codicil=e)


Expand All @@ -104,7 +104,7 @@ def get_latest_archive(settings):
data = json.loads(borg.stdout)
if data["archives"]:
return data["archives"][-1]
except json.decoder.JSONDecodeError as e:
except json.decoder.JSONDecodeError as e: # pragma: no cover
raise Error("Could not decode output of Borg list command.", codicil=e)

# find_archive() {{{2
Expand Down Expand Up @@ -343,11 +343,7 @@ class Command:
@classmethod
def commands(cls):
for cmd in cls.__subclasses__():
if hasattr(cmd, "NAMES"):
yield cmd
for sub in cmd.commands():
if hasattr(sub, "NAMES"):
yield sub
yield cmd

@classmethod
def commands_sorted(cls):
Expand Down Expand Up @@ -2218,26 +2214,33 @@ class RepoCreateCommand(Command):
def run(cls, command, args, settings, options):
# read command line
cmdline = process_cmdline(cls.USAGE, argv=[command] + args)
borg_opts = []

# run borg repo-create
borg = settings.run_borg(
cmd="repo-create",
assimilate_opts = cmdline
)
if borg.status:
return borg.status

# run borg repo-space
if cmdline["--reserve"]:
try:
space = Quantity(cmdline["--reserve"], binary=True, ignore_sf=False)
if space.units and space.units not in ["B", "bytes", "byte"]:
raise Error('expected bytes.', culprit="--reserve")
space = space.render(prec="full", show_units=False)
borg_opts.extend(["--reserve", space])
except QuantiPhyError as e:
raise Error(e, culprit=cmdline["--reserve"])

# run borg
borg = settings.run_borg(
cmd="repo-create",
borg_opts = borg_opts,
assimilate_opts = cmdline
)
out = borg.stderr or borg.stdout
if out:
output(out.rstrip())
borg = settings.run_borg(
cmd="repo-space",
borg_opts = [f"--reserve={space}"],
assimilate_opts = cmdline
)
out = borg.stderr or borg.stdout
if out:
output(out.rstrip())

settings.date_file.touch()
return borg.status
Expand Down
44 changes: 24 additions & 20 deletions tests/assimilate.nt
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,6 @@ assimilate:
binding — repo-create {{{2:
run: assimilate repo-create
checks:
stdout:
contains text: Reserve some repository storage space now for emergencies

~/.local/share/assimilate/test.log:
contains text:
> running:
Expand All @@ -237,6 +234,7 @@ assimilate:
> --repo=/{run_dir}/REPO/test \
> --make-parent-dirs \
> --encryption=none
excludes text: borg-space

~/.local/share/assimilate/test.log.nt:
contains lines in order:
Expand Down Expand Up @@ -863,9 +861,6 @@ assimilate:
albino — repo-create {{{2:
run: assimilate repo-create
checks:
stdout:
contains text: Reserve some repository storage space now for emergencies

~/.local/share/assimilate/test.log:
contains text:
> running:
Expand All @@ -874,6 +869,7 @@ assimilate:
> --repo=/{run_dir}/REPO/test \
> --make-parent-dirs \
> --encryption=none
excludes text: borg-space

~/.local/share/assimilate/test.log.nt:
contains lines in order:
Expand Down Expand Up @@ -1602,15 +1598,13 @@ assimilate:
epicenter — repo-create {{{2:
run: assimilate repo-create
checks:
stdout:
contains text: Reserve some repository storage space now for emergencies

~/.local/share/assimilate/test.log:
contains text:
> Borg-related environment variables: {{
> 'BORG_PASSPHRASE': '❬redacted❭',
> 'BORG_DISPLAY_PASSPHRASE': 'no',
> }}
excludes text: borg-space
excludes text: roadhouse pause govern parry
contains text:
> running:
Expand Down Expand Up @@ -2294,9 +2288,6 @@ assimilate:
tundra — repo-create {{{2:
run: assimilate repo-create
checks:
stdout:
contains text: Reserve some repository storage space now for emergencies

stderr:
matches text:
> === test1 ===
Expand All @@ -2311,6 +2302,7 @@ assimilate:
> --repo=/{run_dir}/REPO/test1 \
> --make-parent-dirs \
> --encryption=repokey-blake2-chacha20-poly1305
excludes text: borg-space

~/.local/share/assimilate/test1.log.nt:
contains lines in order:
Expand Down Expand Up @@ -3178,9 +3170,6 @@ assimilate:
pantry — repo-create {{{2:
run: assimilate repo-create
checks:
stdout:
contains text: Reserve some repository storage space now for emergencies

~/.local/share/assimilate/test.log:
contains text:
> running:
Expand All @@ -3189,6 +3178,7 @@ assimilate:
> --repo=/{run_dir}/REPO/test \
> --make-parent-dirs \
> --encryption=none
excludes text: borg-space

~/.local/share/assimilate/test.log.nt:
contains lines in order:
Expand Down Expand Up @@ -3750,9 +3740,6 @@ assimilate:
annual — repo-create {{{2:
run: assimilate repo-create
checks:
stdout:
contains text: Reserve some repository storage space now for emergencies

~/.local/share/assimilate/test.log:
contains text:
> running:
Expand All @@ -3761,6 +3748,7 @@ assimilate:
> --repo=/{run_dir}/REPO/test \
> --make-parent-dirs \
> --encryption=none
excludes text: borg-space


~/.local/share/assimilate/test.log.nt:
Expand Down Expand Up @@ -4236,10 +4224,26 @@ assimilate:

tests:
crumb — repo-create {{{2:
run: assimilate repo-create
run: assimilate repo-create --reserve=1MiB
checks:
stdout:
contains text: Reserve some repository storage space now for emergencies
contains text: There is 67.11 MB reserved space in this repository now.

~/.local/share/assimilate/test.log:
contains text:
> running:
> borg \
> repo-create \
> --repo=/{run_dir}/REPO/test \
> --make-parent-dirs \
> --encryption=none
contains text:
> running:
> borg \
> repo-space \
> --reserve=1048576 \
> --repo=/{run_dir}/REPO/test


decimate — create {{{2:
initialization:
Expand Down

0 comments on commit 68592d9

Please sign in to comment.