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

Commit

Permalink
Start wiring up schedule at configure
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Oct 19, 2023
1 parent ff2a771 commit c897e32
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
23 changes: 14 additions & 9 deletions src/privateer2/configure.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import docker
from privateer2.keys import keys_data
from privateer2.util import string_to_volume
from privateer2.yacron import generate_yacron_yaml


def configure(cfg, name):
cl = docker.from_env()
data = keys_data(cfg, name)
keys = keys_data(cfg, name)
schedule = generate_yacron_yaml(cfg, name)
vol = cfg.machine_config(name).key_volume
cl.volumes.create(vol)
print(f"Copying keypair for '{name}' to volume '{vol}'")
string_to_volume(
data["public"], vol, "id_rsa.pub", uid=0, gid=0, mode=0o644
keys["public"], vol, "id_rsa.pub", uid=0, gid=0, mode=0o644
)
string_to_volume(data["private"], vol, "id_rsa", uid=0, gid=0, mode=0o600)
if data["authorized_keys"]:
string_to_volume(keys["private"], vol, "id_rsa", uid=0, gid=0, mode=0o600)
if keys["authorized_keys"]:
print("Authorising public keys")
string_to_volume(
data["authorized_keys"],
keys["authorized_keys"],
vol,
"authorized_keys",
uid=0,
gid=0,
mode=0o600,
)
if data["known_hosts"]:
if keys["known_hosts"]:
print("Recognising servers")
string_to_volume(
data["known_hosts"], vol, "known_hosts", uid=0, gid=0, mode=0o600
keys["known_hosts"], vol, "known_hosts", uid=0, gid=0, mode=0o600
)
if data["config"]:
if keys["config"]:
print("Adding ssh config")
string_to_volume(
data["config"], vol, "config", uid=0, gid=0, mode=0o600
keys["config"], vol, "config", uid=0, gid=0, mode=0o600
)
if schedule:
print("Adding yacron schedule")
string_to_volume(schedule, vol, "yacron.yml", uid=0, gid=0)
string_to_volume(name, vol, "name", uid=0, gid=0)
2 changes: 1 addition & 1 deletion src/privateer2/schedule.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import docker
from privateer2.keys import check
from privateer2.check import check
from privateer2.service import service_start, service_status, service_stop
from privateer2.util import unique

Expand Down
4 changes: 4 additions & 0 deletions src/privateer2/yacron.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import yacron.config

from privateer2.backup import backup_command
from privateer2.config import Client
from privateer2.util import current_timezone_name


def generate_yacron_yaml(cfg, name):
machine = cfg.machine_config(name)
if not isinstance(cfg, Client) or not machine.schedule:
return None

ret = ["defaults:", f' timezone: "{current_timezone_name()}"']

if machine.schedule.port:
Expand Down
33 changes: 33 additions & 0 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,36 @@ def test_can_unpack_keys_for_client(managed_docker):
cfg.servers[0].key_volume = vol
with pytest.raises(Exception, match=msg):
check(cfg, "alice")


def test_can_write_schedule_for_client(managed_docker):
with vault_dev.Server(export_token=True) as server:
cfg = read_config("example/schedule.json")
cfg.vault.url = server.url()
vol = managed_docker("volume")
cfg.clients[0].key_volume = vol
keygen_all(cfg)
configure(cfg, "bob")
client = docker.from_env()
mounts = [docker.types.Mount("/keys", vol, type="volume")]
name = managed_docker("container")
res = client.containers.run(
"alpine",
mounts=mounts,
command=["ls", "/keys"],
name=name,
)
assert set(res.decode("UTF-8").strip().split("\n")) == {
"known_hosts",
"id_rsa",
"id_rsa.pub",
"name",
"config",
"yacron.yml"
}
# assert string_from_volume(vol, "name") == "bob"
# assert check(cfg, "bob").key_volume == vol
# msg = "Configuration is for 'bob', not 'alice'"
# cfg.servers[0].key_volume = vol
# with pytest.raises(Exception, match=msg):
# check(cfg, "alice")
3 changes: 2 additions & 1 deletion tests/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import privateer2.schedule
from privateer2.config import read_config
from privateer2.keys import configure, keygen_all
from privateer2.configure import configure
from privateer2.keys import keygen_all
from privateer2.schedule import schedule_start, schedule_status, schedule_stop


Expand Down
5 changes: 5 additions & 0 deletions tests/test_yacron.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def test_can_generate_yacron_yaml():
assert res == expected


def test_can_generate_empty_yacron_yaml():
cfg = read_config("example/simple.json")
assert generate_yacron_yaml(cfg, "alice") is None


def test_can_add_web_interface():
cfg = read_config("example/schedule.json")
cfg.clients[0].schedule.jobs.pop()
Expand Down

0 comments on commit c897e32

Please sign in to comment.