|
@@ -1,3 +1,4 @@
|
|
|
|
|
+import argparse
|
|
|
import os
|
|
import os
|
|
|
import sys
|
|
import sys
|
|
|
import subprocess
|
|
import subprocess
|
|
@@ -190,6 +191,71 @@ class BorgmaticContainer:
|
|
|
return cls(hostname, login, f"borgmatic_{login}")
|
|
return cls(hostname, login, f"borgmatic_{login}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+class CliArguments:
|
|
|
|
|
+ def __init__(self, namespace) -> None:
|
|
|
|
|
+ for k, v in vars(namespace).items():
|
|
|
|
|
+ if k != "type_":
|
|
|
|
|
+ setattr(self, k, v)
|
|
|
|
|
+
|
|
|
|
|
+ @classmethod
|
|
|
|
|
+ def init_subparser(cls, p):
|
|
|
|
|
+ ...
|
|
|
|
|
+
|
|
|
|
|
+ @classmethod
|
|
|
|
|
+ def new(cls) -> None:
|
|
|
|
|
+ parser = argparse.ArgumentParser(prog=sys.argv[0])
|
|
|
|
|
+ subparsers = parser.add_subparsers()
|
|
|
|
|
+
|
|
|
|
|
+ for sub in [
|
|
|
|
|
+ CliArgumentsStart,
|
|
|
|
|
+ CliArgumentsRm,
|
|
|
|
|
+ CliArgumentsBash,
|
|
|
|
|
+ CliArgumentsCreateRepo,
|
|
|
|
|
+ CliArgumentsExportKey,
|
|
|
|
|
+ CliArgumentsCreateSecrets
|
|
|
|
|
+ ]:
|
|
|
|
|
+ p = subparsers.add_parser(sub.command, help=sub.help)
|
|
|
|
|
+ sub.init_subparser(p)
|
|
|
|
|
+ p.set_defaults(type_=sub)
|
|
|
|
|
+
|
|
|
|
|
+ args = parser.parse_args()
|
|
|
|
|
+ return args.type_(args)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class CliArgumentsStart(CliArguments):
|
|
|
|
|
+ command = "start"
|
|
|
|
|
+ help = "start container"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class CliArgumentsRm(CliArguments):
|
|
|
|
|
+ command = "rm"
|
|
|
|
|
+ help = "remove container"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class CliArgumentsBash(CliArguments):
|
|
|
|
|
+ command = "bash"
|
|
|
|
|
+ help = "run shell in container"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class CliArgumentsCreateRepo(CliArguments):
|
|
|
|
|
+ command = "create_repo"
|
|
|
|
|
+ help = "create repository"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class CliArgumentsExportKey(CliArguments):
|
|
|
|
|
+ command = "export_key"
|
|
|
|
|
+ help = "export the repository key"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class CliArgumentsCreateSecrets(CliArguments):
|
|
|
|
|
+ command="create_secrets"
|
|
|
|
|
+ help="create podman secrets"
|
|
|
|
|
+
|
|
|
|
|
+ @classmethod
|
|
|
|
|
+ def init_subparser(cls, p):
|
|
|
|
|
+ p.add_argument("keepass_path", type=Path, help="Path to the keepass")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def main():
|
|
def main():
|
|
|
login = os.getlogin()
|
|
login = os.getlogin()
|
|
|
hostname = socket.gethostname()
|
|
hostname = socket.gethostname()
|
|
@@ -197,37 +263,33 @@ def main():
|
|
|
secret_sources = Secret.read_sources(hostname, login)
|
|
secret_sources = Secret.read_sources(hostname, login)
|
|
|
data_sources = read_data_sources(hostname, login)
|
|
data_sources = read_data_sources(hostname, login)
|
|
|
|
|
|
|
|
|
|
+ args = CliArguments.new()
|
|
|
|
|
+
|
|
|
if not secret_sources:
|
|
if not secret_sources:
|
|
|
print("no secret required ?")
|
|
print("no secret required ?")
|
|
|
|
|
|
|
|
container = BorgmaticContainer.new(hostname, login)
|
|
container = BorgmaticContainer.new(hostname, login)
|
|
|
env_vars = ["BORG_PASSPHRASE_NAME", "STORAGE_BOX_USER", "SSH_KEY_NAME"]
|
|
env_vars = ["BORG_PASSPHRASE_NAME", "STORAGE_BOX_USER", "SSH_KEY_NAME"]
|
|
|
|
|
|
|
|
- try:
|
|
|
|
|
- if sys.argv[1] == "create_secrets":
|
|
|
|
|
- keepass_path = Path(sys.argv[2])
|
|
|
|
|
- keepass = KeePass.new(keepass_path)
|
|
|
|
|
- for s in secret_sources:
|
|
|
|
|
- s.create(keepass)
|
|
|
|
|
-
|
|
|
|
|
- elif sys.argv[1] == "start":
|
|
|
|
|
- container.run(data_sources, secret_sources)
|
|
|
|
|
|
|
+ if isinstance(args, CliArgumentsCreateSecrets):
|
|
|
|
|
+ keepass = KeePass.new(args.keepass_path)
|
|
|
|
|
+ for s in secret_sources:
|
|
|
|
|
+ s.create(keepass)
|
|
|
|
|
|
|
|
- elif sys.argv[1] == "rm":
|
|
|
|
|
- container.rm()
|
|
|
|
|
|
|
+ elif isinstance(args, CliArgumentsStart):
|
|
|
|
|
+ container.run(data_sources, secret_sources)
|
|
|
|
|
|
|
|
- elif sys.argv[1] == "bash":
|
|
|
|
|
- container.exec(["bash"])
|
|
|
|
|
|
|
+ elif isinstance(args, CliArgumentsRm):
|
|
|
|
|
+ container.rm()
|
|
|
|
|
|
|
|
- elif sys.argv[1] == "create_repo":
|
|
|
|
|
- container.exec(["borgmatic", "repo-create", "--encryption", "repokey"], env_vars)
|
|
|
|
|
|
|
+ elif isinstance(args, CliArgumentsBash):
|
|
|
|
|
+ container.exec(["bash"])
|
|
|
|
|
|
|
|
- elif sys.argv[1] == "export_key":
|
|
|
|
|
- container.exec(["borgmatic", "export", "key"], env_vars)
|
|
|
|
|
|
|
+ elif isinstance(args, CliArgumentsCreateRepo):
|
|
|
|
|
+ container.exec(["borgmatic", "repo-create", "--encryption", "repokey"], env_vars)
|
|
|
|
|
|
|
|
- except IndexError:
|
|
|
|
|
- print("You should provide an argument")
|
|
|
|
|
- exit(1)
|
|
|
|
|
|
|
+ elif isinstance(args, CliArgumentsExportKey):
|
|
|
|
|
+ container.exec(["borgmatic", "export", "key"], env_vars)
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|