Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Allow ommitting the path
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Oct 18, 2023
1 parent 569b308 commit 43e3648
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
22 changes: 11 additions & 11 deletions development.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,32 @@ sed "s/alice.example.com/$(hostname)/" example/local.json > tmp/privateer.json
Create a set of keys

```
privateer2 --path tmp/privateer.json keygen --all
privateer2 --path tmp keygen --all
```

You could also do this individually like

```
privateer2 --path tmp/privateer.json keygen alice
privateer2 --path tmp keygen alice
```

Set up the key volumes

```
privateer2 --path tmp/privateer.json configure alice
privateer2 --path tmp/privateer.json configure bob
privateer2 --path tmp configure alice
privateer2 --path tmp configure bob
```

Start the server, as a background process

```
privateer2 --path tmp/privateer.json --as=alice server start
privateer2 --path tmp --as=alice server start
```

Once `alice` is running, we can test this connection from `bob`:

```
privateer2 --path tmp/privateer.json --as=bob check --connection
privateer2 --path tmp --as=bob check --connection
```

This command would be simpler to run if in the `tmp` directory, which would be the usual situation in a multi-machine setup
Expand All @@ -77,13 +77,13 @@ docker run -it --rm -v data:/data ubuntu bash -c "base64 /dev/urandom | head -c
We can now backup from `bob` to `alice` as:

```
privateer2 --path tmp/privateer.json --as=bob backup data
privateer2 --path tmp --as=bob backup data
```

or see what commands you would need in order to try this yourself:

```
privateer2 --path tmp/privateer.json --as=bob backup data --dry-run
privateer2 --path tmp --as=bob backup data --dry-run
```

Delete the volume
Expand All @@ -95,19 +95,19 @@ docker volume rm data
We can now restore it:

```
privateer2 --path tmp/privateer.json --as=bob restore data
privateer2 --path tmp --as=bob restore data
```

or see the commands to do this outselves:

```
privateer2 --path tmp/privateer.json --as=bob restore data --dry-run
privateer2 --path tmp --as=bob restore data --dry-run
```

Tear down the server with

```
privateer2 --path tmp/privateer.json --as=alice server stop
privateer2 --path tmp --as=alice server stop
```

## Writing tests
Expand Down
15 changes: 13 additions & 2 deletions src/privateer2/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
privateer2 [options] restore <volume> [--server=NAME] [--source=NAME]
Options:
--path=PATH The path to the configuration (rather than privateer.json)
--path=PATH The path to the configuration, or directory with privateer.json
--as=NAME The machine to run the command as
--dry-run Do nothing, but print docker commands
Expand Down Expand Up @@ -90,13 +90,24 @@ def _parse_argv(argv):
return _parse_opts(opts)


def _path_config(path):
if not path:
path = "privateer.json"
elif os.path.isdir(path):
path = os.path.join(path, "privateer.json")
if not os.path.exists(path):
msg = f"Did not find privateer configuration at '{path}'"
raise Exception(msg)
return path


def _parse_opts(opts):
if opts["--version"]:
return Call(_show_version)

dry_run = opts["--dry-run"]
name = opts["--as"]
path_config = opts["--path"] or "privateer.json"
path_config = _path_config(opts["--path"])
root_config = os.path.dirname(path_config)
cfg = read_config(path_config)
if opts["keygen"]:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
_find_identity,
_parse_argv,
_parse_opts,
_path_config,
_show_version,
main,
pull,
Expand Down Expand Up @@ -275,3 +276,20 @@ def test_run_pull(monkeypatch):
assert client.images.pull.call_count == 2
assert client.images.pull.call_args_list[0] == call(image_client)
assert client.images.pull.call_args_list[1] == call(image_server)


def test_clean_path(tmp_path):
with pytest.raises(Exception, match="Did not find privateer configuration"):
with transient_working_directory(str(tmp_path)):
_path_config(None)
with pytest.raises(Exception, match="Did not find privateer configuration"):
_path_config(tmp_path)
with pytest.raises(Exception, match="Did not find privateer configuration"):
_path_config(tmp_path / "foo.json")
with pytest.raises(Exception, match="Did not find privateer configuration"):
_path_config("foo.json")
shutil.copy("example/simple.json", tmp_path / "privateer.json")
assert _path_config(str(tmp_path)) == str(tmp_path / "privateer.json")
assert _path_config("example/simple.json") == "example/simple.json"
with transient_working_directory(str(tmp_path)):
assert _path_config(None) == "privateer.json"

0 comments on commit 43e3648

Please sign in to comment.